nl.cloudfarming.client.geoviewer.jxmap.map.RootMapPanel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of geoviewer-jxmap Show documentation
Show all versions of geoviewer-jxmap Show documentation
AgroSense geoviewer JXMap implementation. Contains a map/geoviewer TopComponent based on the JXMap classes from swingx.
The newest version!
/**
* Copyright (C) 2008-2012 AgroSense Foundation.
*
* AgroSense is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* There are special exceptions to the terms and conditions of the GPLv3 as it is applied to
* this software, see the FLOSS License Exception
* .
*
* AgroSense is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AgroSense. If not, see .
*/
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_LEGEND(Integer.valueOf(49)),
LAYER_TOOLS(Integer.valueOf(52)),
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) {
}
});
}
layerPanelContainer.add(new LegendPanel(), LayerGroup.LAYER_LEGEND.getValue());
DrawingController drawingController = new DrawingController(mapPanel);
DrawingPanel drawingPanel = drawingController.getDrawingPanel();
layerPanelContainer.add(drawingPanel, LayerGroup.LAYER_DRAWING.getValue());
JXPanel toolsPanel = drawingController.getToolsPanel();
layerPanelContainer.add(toolsPanel, LayerGroup.LAYER_TOOLS.getValue());
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
*/
JXMapViewer getMapViewer() {
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) {
}
}