All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.cloudfarming.client.geoviewer.jxmap.map.RootMapPanel Maven / Gradle / Ivy

Go to download

AgroSense geoviewer JXMap implementation. Contains a map/geoviewer TopComponent based on the JXMap classes from swingx.

There is a newer version: 13.03-beta
Show newest version
/**
 * Copyright (C) 2010-2012 Agrosense [email protected]
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.geoviewer.jxmap.map;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JComponent;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import net.miginfocom.swing.MigLayout;
import nl.cloudfarming.client.geoviewer.LayerInfo;
import org.jdesktop.swingx.JXMapViewer;
import org.jdesktop.swingx.JXPanel;

/**
 * The root map panel, containing a layered pane with all the layers. A
 * JXMapPanel is used as the bottom layer.
 *
 * @author Timon Veenstra
 */
public class RootMapPanel extends JXPanel implements MapMouseListener {

    public enum LayerGroup {

        LAYER_MAP(Integer.valueOf(1)),
        LAYER_IMAGE(Integer.valueOf(10)),
        LAYER_PASSIVE(Integer.valueOf(20)),
        LAYER_ACTIVE(Integer.valueOf(29)),
        LAYER_DRAWING(Integer.valueOf(30)),
        LAYER_LAYERLIST(Integer.valueOf(51)),
        LAYER_TOP(Integer.valueOf(50));
        private final Integer value;

        public Integer getValue() {
            return value;
        }

        private LayerGroup(Integer value) {
            this.value = value;
        }
    }
    // swing container holding all layerPanels and the map panel
    private final JLayeredPane layerPanelContainer;
    // layers
    private JXMapPanel mapPanel;
    private final JXPanel topPanel = new JXPanel();
    //
    // map with layerpanels for components
    //
    private final Set layerPanels = new HashSet();
    private final MapMouseHandler mouseHandler;

//    private Map layerInfoMap = new HashMap();
    public RootMapPanel(final JComponent layerList) {
        setLayout(new BorderLayout());
        //
        // initialize the mouse handler
        //
        mouseHandler = new MapMouseHandler(this);
        //
        // the layered pane which will contain the layers of the map
        //
        layerPanelContainer = new JLayeredPane();
        layerPanelContainer.setLayout(new FillLayout());
        add(layerPanelContainer, BorderLayout.CENTER);
        //
        // the map layer
        //
        mapPanel = new JXMapPanel();
        layerPanelContainer.add(mapPanel, LayerGroup.LAYER_MAP.getValue());
        //
        // add the layerlist panel
        //
        if (layerList != null) {
            final JXPanel layerListPanel;
            layerListPanel = new JXPanel();
            layerListPanel.setOpaque(false);
            layerListPanel.setLayout(new MigLayout());
            layerListPanel.add(layerList);
            layerPanelContainer.add(layerListPanel, LayerGroup.LAYER_LAYERLIST.getValue());
            mouseHandler.addMapMouseListener(new MapMouseListener() {

                @Override
                public LayerGroup getGroup() {
                    return LayerGroup.LAYER_LAYERLIST;
                }

                @Override
                public int getId() {
                    return 1;
                }

                @Override
                public void onMapMouseEvent(MapMouseEvent mapMouseEvent) {
                    layerList.dispatchEvent(mapMouseEvent.getEvent());
                }

                @Override
                public void mapMousePressed(MapMouseEvent mapMouseEvent) {
                }

                @Override
                public void mapMouseReleased(MapMouseEvent mapMouseEvent) {
                }

                @Override
                public void mapMouseDragged(MapMouseEvent mapMouseEvent) {
                }
            });
        }

        
        topPanel.setOpaque(false);
        layerPanelContainer.add(topPanel, LayerGroup.LAYER_TOP.getValue());

        topPanel.addMouseListener(mouseHandler);
        topPanel.addMouseMotionListener(mouseHandler);
        topPanel.addMouseWheelListener(mouseHandler);
    }
    
    JPanel getTopPanel(){
        return topPanel;
    }

    /**
     * returns the mapviewer
     *
     * @return
     */
    public JXMapViewer getMapViewer() {
        //FIXME change back to package access
        return this.mapPanel;

    }

    /**
     * internal method to add a layerpanel. Only call from the AWT thread.
     *
     * @param layerPanel
     */
    private void internalAddLayerPanel(final LayerPanel layerPanel) {
        //
        // add interactive layers to the interaction layer
        // and add non interactive layer to the object layer
        //
        mouseHandler.addMapMouseListener(layerPanel);
        layerPanels.add(layerPanel);
        layerPanelContainer.add(layerPanel, layerPanel.getGroup().getValue());

        layerPanel.addPropertyChangeListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if (LayerInfo.PROPERTY_VISIBLE.equals(evt.getPropertyName())) {
                    layerPanel.setVisible((Boolean) evt.getNewValue());
                }
            }
        });
    }

    /**
     * add a layer panel
     *
     * @param layerPanel
     */
    void addLayerPanel(final LayerPanel layerPanel) {
        //
        // do actual adding in the AWT thread because the scene could be validating and cause
        // concurrent modification exception
        //
        if (EventQueue.isDispatchThread()) {
            internalAddLayerPanel(layerPanel);
        } else {
            EventQueue.invokeLater(new Runnable() {

                @Override
                public void run() {
                    internalAddLayerPanel(layerPanel);
                }
            });
        }
    }

    /**
     * remove a layer panel
     *
     * @param layerPanel
     */
    void removeLayerPanel(final LayerPanel layerPanel) {
        //
        // do actual removing in the AWT thread because the scene could be validating and cause
        // concurrent modification exception
        //
        if (EventQueue.isDispatchThread()) {
            internalRemoveLayerPanel(layerPanel);
        } else {
            EventQueue.invokeLater(new Runnable() {

                @Override
                public void run() {
                    internalRemoveLayerPanel(layerPanel);
                }
            });
        }

    }
    
    /**
     * checks if this panel contains the supplied layerPanel
     * @param layerPanel
     * @return if the supplied layerPanel is contained in this RootMapPanel
     */
    boolean contains(final LayerPanel layerPanel){
        return layerPanels.contains(layerPanel);
    }

    /**
     * internal method to remove a layerpanel. Only call from the AWT thread.
     *
     * @param layerPanel
     */
    private void internalRemoveLayerPanel(final LayerPanel layerPanel) {
        if (this.layerPanels.contains(layerPanel)) {
            this.layerPanels.remove(layerPanel);
        }
        for (Component c:layerPanelContainer.getComponents()){
            if (c.equals(layerPanel)){
                layerPanelContainer.remove(layerPanel);
            }
        }
        
        mouseHandler.removeMapMouseListener(layerPanel);
        getMapViewer().repaint();
    }

    @Override
    public LayerGroup getGroup() {
        return LayerGroup.LAYER_MAP;
    }

    @Override
    public int getId() {
        return 1;
    }

    /**
     * dispatches all mouse events to the map when they contain a point
     *
     * @param mapMouseEvent
     */
    @Override
    public void onMapMouseEvent(MapMouseEvent mapMouseEvent) {
        if (mapMouseEvent.getEvent().getPoint() != null
                && mapPanel.contains(mapMouseEvent.getEvent().getPoint())
                && !SwingUtilities.isRightMouseButton(mapMouseEvent.getEvent())) {
            mapPanel.dispatchEvent(mapMouseEvent.getEvent());
        }
    }

    @Override
    public void mapMousePressed(MapMouseEvent mapMouseEvent) {
    }

    @Override
    public void mapMouseReleased(MapMouseEvent mapMouseEvent) {
    }

    @Override
    public void mapMouseDragged(MapMouseEvent mapMouseEvent) {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy