ru.jcorp.smartstreets.gui.map.bundle.GraphPopupMousePlugin.java Source code

Java tutorial

Introduction

Here is the source code for ru.jcorp.smartstreets.gui.map.bundle.GraphPopupMousePlugin.java

Source

/*
 * This file is part of SmartStreets.
 *
 * This program 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
 * any later version.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package ru.jcorp.smartstreets.gui.map.bundle;

import edu.uci.ics.jung.algorithms.layout.GraphElementAccessor;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.EditingPopupGraphMousePlugin;
import edu.uci.ics.jung.visualization.picking.PickedState;
import org.apache.commons.collections15.Factory;
import ru.jcorp.smartstreets.gui.SmartStreetsApp;
import ru.jcorp.smartstreets.gui.line.LineEditor;
import ru.jcorp.smartstreets.gui.map.MapEditor;
import ru.jcorp.smartstreets.gui.node.NodeEditor;
import ru.jcorp.smartstreets.map.SmartMapLine;
import ru.jcorp.smartstreets.map.SmartMapNode;

import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.Point2D;

/**
 * <p>$Id$</p>
 *
 * @author Artamonov Yuriy
 */
public class GraphPopupMousePlugin extends EditingPopupGraphMousePlugin<GraphNode, GraphLink> {

    private MapEditor editor;

    public GraphPopupMousePlugin(MapEditor editor, Factory<GraphNode> vertexFactory,
            Factory<GraphLink> edgeFactory) {
        super(vertexFactory, edgeFactory);
        this.editor = editor;
    }

    @Override
    protected void handlePopup(MouseEvent e) {

        // noinspection unchecked
        final VisualizationViewer<GraphNode, GraphLink> viewer = (VisualizationViewer<GraphNode, GraphLink>) e
                .getSource();

        final Layout<GraphNode, GraphLink> layout = viewer.getGraphLayout();
        final Graph<GraphNode, GraphLink> graph = layout.getGraph();
        final Point2D p = e.getPoint();
        final SmartStreetsApp app = SmartStreetsApp.getInstance();

        GraphElementAccessor<GraphNode, GraphLink> pickSupport = viewer.getPickSupport();
        if (pickSupport != null) {
            popup.removeAll();

            final GraphNode vertex = pickSupport.getVertex(layout, p.getX(), p.getY());
            final GraphLink edge = pickSupport.getEdge(layout, p.getX(), p.getY());
            final PickedState<GraphNode> pickedVertexState = viewer.getPickedVertexState();
            final PickedState<GraphLink> pickedEdgeState = viewer.getPickedEdgeState();

            if (vertex != null) {
                popup.add(new JMenuItem(new AbstractAction(app.getMessage("edit.setSource")) {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        SmartMapNode mapNode = vertex.getMapNode();
                        editor.setSourceNode(mapNode);
                    }
                }));
                popup.add(new JMenuItem(new AbstractAction(app.getMessage("edit.setDest")) {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        SmartMapNode mapNode = vertex.getMapNode();
                        editor.setDestNode(mapNode);
                    }
                }));
                popup.add(new JMenuItem(new AbstractAction(app.getMessage("edit.properties")) {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        SmartMapNode mapNode = vertex.getMapNode();
                        NodeEditor nodeEditor = new NodeEditor(mapNode);
                        nodeEditor.setLocationRelativeTo(editor);
                        nodeEditor.setVisible(true);

                        editor.clearPathSheduled();
                        viewer.repaint();
                    }
                }));
                popup.add(new AbstractAction(app.getMessage("graph.deleteVertex")) {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        editor.clearPathSheduled();

                        pickedVertexState.pick(vertex, false);
                        graph.removeVertex(vertex);

                        editor.getMapContext().removeNode(vertex.getMapNode());

                        viewer.repaint();
                    }
                });
            } else if (edge != null) {
                popup.add(new JMenuItem(new AbstractAction(app.getMessage("edit.properties")) {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        SmartMapLine mapLine = edge.getMapLine();
                        LineEditor lineEditor = new LineEditor(mapLine, edge.getRoadSign());
                        lineEditor.setLocationRelativeTo(editor);
                        lineEditor.setVisible(true);

                        editor.clearPathSheduled();
                        viewer.repaint();
                    }
                }));
                popup.add(new AbstractAction(app.getMessage("graph.deleteEdge")) {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        editor.clearPathSheduled();

                        pickedEdgeState.pick(edge, false);
                        graph.removeEdge(edge);
                        graph.removeEdge(edge.getNeighbor());

                        editor.getMapContext().removeLine(edge.getMapLine());

                        viewer.repaint();
                    }
                });
            } else {
                popup.add(new AbstractAction(app.getMessage("graph.createVertex")) {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        editor.clearPathSheduled();

                        GraphNode newVertex = vertexFactory.create();
                        graph.addVertex(newVertex);
                        layout.setLocation(newVertex,
                                viewer.getRenderContext().getMultiLayerTransformer().inverseTransform(p));
                        viewer.repaint();
                    }
                });
            }
            if (popup.getComponentCount() > 0) {
                popup.show(viewer, e.getX(), e.getY());
            }
        }
    }
}