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

nl.cloudfarming.client.geoviewer.jxmap.MapDataManager 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;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import nl.cloudfarming.client.geoviewer.Layer;
import nl.cloudfarming.client.geoviewer.event.GeoEvent;
import nl.cloudfarming.client.geoviewer.jxmap.map.FilterMapDataChildren;
import nl.cloudfarming.eventbus.GuiEvent;
import nl.cloudfarming.eventbus.GuiEventBus;
import nl.cloudfarming.eventbus.GuiEventKey;
import nl.cloudfarming.eventbus.GuiEventListener;
import org.openide.explorer.ExplorerManager;
import org.openide.nodes.*;

/**
 * The MapDataManager provides an optional convenience explorerManager
 * for managing data on the map.
 * It is usefull to use in combination with a MapView.
 * 
 * In order to receive and manage nodes dropped on the map, the MapDataManager
 * should register itself on the {@link MapView} as a {@link LayerDropTarget}.
 * mapView.addLayerDropTarget(mapDataManager);
 * 
 * In order to use the MapDataManager as data manager for the map, the TopComponent
 * containing the MapView should implement {@link ExplorerManager.Provider}
 * and have getExplorerManager to return {@link MapDataManager#getExplorerManager() }
 * 
 * @author Timon Veenstra
 */
public class MapDataManager implements ExplorerManager.Provider, LayerDropTarget {

    private final ExplorerManager explorerManager = new ExplorerManager();
    private final List layerNodes = new ArrayList();
    private final MapDataRootNodeChildFactory childFactory = new MapDataRootNodeChildFactory();
    private final Node rootNode = new AbstractNode(Children.create(childFactory, false));
    private final NewLayerListener newLayerListener = new NewLayerListener();
    private final DiscardedLayerListener discardedLayerListener = new DiscardedLayerListener();
    private static final Logger LOGGER = Logger.getLogger("nl.cloudfarming.client.geoviewer");

    public MapDataManager() {
        explorerManager.setRootContext(rootNode);
        explorerManager.getRootContext().setDisplayName("");
        rootNode.getChildren();
        explorerManager.setExploredContext(rootNode);

        GuiEventBus.addListener(newLayerListener);
        GuiEventBus.addListener(discardedLayerListener);

        LOGGER.log(Level.FINE, "MapDataManager::ExplorerManager: {0}", System.identityHashCode(explorerManager));

    }

    @Override
    public ExplorerManager getExplorerManager() {
        return explorerManager;
    }

    @Override
    public boolean dropNode(Node node) {
        assert node.getLookup().lookup(Layer.class) != null:"Dropped node should always have a layer in its lookup";
        addLayerNode(node);
        return true;
    }

    public void addLayerNode(Node layerNode) {
        // prevent duplicates
        if (!layerNodes.contains(layerNode)) {
            layerNodes.add(layerNode);
            
            childFactory.refresh();
            Children c = rootNode.getChildren();
            for (Node node : c.getNodes()) {
                explorerManager.setExploredContext(node);
            }
        }
    }

    public void removeLayerNode(Node layerNode) {
        if (layerNodes.contains(layerNode)) {
            layerNodes.remove(layerNode);
            childFactory.refresh();
        }
    }

    public Node[] getLayerNodes() {
        return this.layerNodes.toArray(new Node[this.layerNodes.size()]);
    }

    private class DiscardedLayerListener implements GuiEventListener {

        @Override
        public void onEvent(GuiEvent event) {
            Layer layer = event.getContent();
            for (Node node : getLayerNodes()) {
                if (layer.equals(node.getLookup().lookup(Layer.class))) {
                    removeLayerNode(node);
                }
            }
        }

        @Override
        public boolean listensTo(GuiEventKey guiEventKey) {
            return GeoEvent.DISCARD_LAYER.equals(guiEventKey);
        }

        @Override
        public String getModuleName() {
            return "geoviewer-jxmap";
        }
    }

    /**
     * Listerner registered on the {@link GuiEventBus} listening to {@link LayerEventKey.NEW_LAYER}
     * events.
     *
     */
    private class NewLayerListener implements GuiEventListener {

        @Override
        public void onEvent(GuiEvent event) {
            final Node node = event.getContent();
            if (node != null) {
                addLayerNode(node);
            }
        }

        @Override
        public boolean listensTo(GuiEventKey guiEventKey) {
            return GeoEvent.NEW_LAYER.equals(guiEventKey);
        }

        @Override
        public String getModuleName() {
            return "geoviewer-jxmap";
        }
    }

    private class MapDataRootNodeChildFactory extends ChildFactory {

        public void refresh() {
            refresh(true);
        }

        @Override
        protected boolean createKeys(List toPopulate) {
            toPopulate.addAll(Arrays.asList(getLayerNodes()));
            return true;
        }

        @Override
        protected Node createNodeForKey(final Node key) {
            return new FilterNode(key, new FilterMapDataChildren(key));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy