nl.cloudfarming.client.geoviewer.jxmap.map.RootMapPanelController Maven / Gradle / Ivy
Show all versions of geoviewer-jxmap Show documentation
/**
* 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 com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Point;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import nl.cloudfarming.client.geoviewer.Geographical;
import nl.cloudfarming.client.geoviewer.Layer;
import nl.cloudfarming.client.geoviewer.LayerInfo;
import nl.cloudfarming.client.geoviewer.event.GeoEvent;
import nl.cloudfarming.client.geoviewer.LayerDropTarget;
import nl.cloudfarming.client.geoviewer.ObjectLayer;
import nl.cloudfarming.client.geoviewer.jxmap.MapViewerTopComponent;
import nl.cloudfarming.client.geoviewer.jxmap.Module;
import nl.cloudfarming.client.geoviewer.jxmap.layerlist.LayerListController;
import nl.cloudfarming.eventbus.GuiEvent;
import nl.cloudfarming.eventbus.GuiEventBus;
import nl.cloudfarming.eventbus.GuiEventKey;
import nl.cloudfarming.eventbus.GuiEventListener;
import org.jdesktop.swingx.mapviewer.GeoPosition;
import org.openide.nodes.Node;
/**
* Controller of the rootmap panel.
*
* The RootMapPanelController is the
* MapController
implementation for the JXMap geoviewer
* implementation. It exposes methods to control this map implementation.
*
* Swing hierarchy
The top swing component for the
* RootMapController
is the {@link MapViewerTopComponent}. This
* TopComponent only contains the hook to the netbeans window management system,
* like {@link #setActive(boolean)} on componentActivated and
* componentDeactivated. The controller delivers the root panel to TopComponent
* which is a {@link RootMapPanel}. The Controller controls the state and
* content of this root panel.
The {@link RootMapPanel} is a
* JXPanel
which contains a JLayerPane. The layered pane contains
* several layers starting with the JXMap layer as background.
Layer
* containing object ({@link ObjectLayer)} implementations) are represented as
* an {@link ObjectLayerPanel} and added to the layered panel on the
* {@link RootMapPanel.LayerGroup#LAYER_ACTIVE} position.
* ObjectLayerPanel
s are ObjectScenes and contain
* LayerObjectWidget
s representing geographical components on the
* map.
*
* MapViewerTopComponent->RootMapPanel->ObjectLayerPanel->LayerObjectWidget
*
* @author Timon Veenstra
*/
public class RootMapPanelController {
private final RootMapPanel mapPanel;
private final Map layerPanelMap = new HashMap<>();
private static int lastId = 0;
private final LayerDropTargetListener dropTargetListener = new LayerDropTargetListener();
private final RequestFocusEventListener requestFocusEventListener = new RequestFocusEventListener();
private static final Logger LOGGER = Logger.getLogger(RootMapPanelController.class.getName());
public RootMapPanelController(LayerListController layerListController) {
mapPanel = new RootMapPanel(layerListController.getLayerList());
//
// register a drop target listener for the layerlist component
//
DropTarget dt = new DropTarget(layerListController.getLayerList(), dropTargetListener);
dt.setDefaultActions(DnDConstants.ACTION_COPY);
dt.setActive(true);
layerListController.getLayerList().setDropTarget(dt);
//
// register a drop target listener for the top panel of the map component
//
DropTarget dt2 = new DropTarget(mapPanel.getTopPanel(), dropTargetListener);
dt.setDefaultActions(DnDConstants.ACTION_COPY);
dt.setActive(true);
mapPanel.getTopPanel().setDropTarget(dt2);
//
// add the request focus listener
//
GuiEventBus.addListener(requestFocusEventListener);
}
public void addLayerDropTarget(LayerDropTarget dropTarget) {
dropTargetListener.addDropTarget(dropTarget);
}
public RootMapPanel getPanel() {
return mapPanel;
}
public void addLayerNode(Node layerNode, LayerInfo layerInfo) {
Layer layer = Layer.Finder.findLayer(layerNode);
//FIXME set propert ID, should depend on layer order since its used for event dispatching
LayerPanel layerPanel = new LayerPanel(layerNode, mapPanel.getMapViewer(), lastId++, layerInfo);
//
// let layerpanel listen to selection changes in layelist em.
//
layer.addPropertyChangeListener(layerPanel);
//
// save reference to the layerpanel and add it to the map panel
//
layerPanelMap.put(layer, layerPanel);
mapPanel.addLayerPanel(layerPanel);
}
public void removeLayerNode(Node layerNode) {
final Layer layer = Layer.Finder.findLayer(layerNode);
if (layer == null) {
LOGGER.warning("removeLayerNode called with a node without a layer in its hierarchy");
return;
}
LayerPanel layerPanel = layerPanelMap.get(layer);
//
// remove reference to the layerpanel and remove it from the map panel
//
mapPanel.removeLayerPanel(layerPanel);
layerPanelMap.remove(layer);
}
public void refreshLayerNode(Node layerNode) {
final Layer layer = Layer.Finder.findLayer(layerNode);
if (layer == null) {
LOGGER.warning("refreshLayerNode called with a node without a layer in its hierarchy");
return;
}
LayerPanel layerPanel = layerPanelMap.get(layer);
if (layerPanel != null) {
layerPanel.repaint();
} else {
LOGGER.log(Level.WARNING, "refresh requested for unmanaged layer {0} in node {1}", new Object[]{layer, layerNode});
}
}
private class RequestFocusEventListener implements GuiEventListener {
@Override
public void onEvent(GuiEvent event) {
Geographical geographical = event.getContent();
Point centroid = geographical.getCentroid();
if (centroid != null) {
GeoPosition geoPosition = new GeoPosition(centroid.getY(), centroid.getX());
mapPanel.getMapViewer().setCenterPosition(geoPosition);
//Default zoom level
mapPanel.getMapViewer().setZoom(3);
//Now try to zoom out to boundingbox
if (geographical.getBoundingBox() != null) {
Set geoPositions = new HashSet();
for (Coordinate coordinate :geographical.getBoundingBox().getCoordinates() ) {
GeoPosition position = new GeoPosition(coordinate.y, coordinate.x);
geoPositions.add(position);
}
mapPanel.getMapViewer().calculateZoomFrom(geoPositions);
}
}
}
@Override
public boolean listensTo(GuiEventKey guiEventKey) {
return GeoEvent.REQUEST_FOCUS.equals(guiEventKey);
}
@Override
public String getModuleName() {
return Module.MODULE_NAME;
}
}
}