Search in sources :

Example 1 with ToolEventListener

use of org.gephi.tools.spi.ToolEventListener in project gephi-plugins-bootcamp by gephi.

the class AddNodesTool method getListeners.

@Override
public ToolEventListener[] getListeners() {
    return new ToolEventListener[] { new MouseClickEventListener() {

        @Override
        public void mouseClick(int[] positionViewport, float[] position3d) {
            //Get current graph
            GraphController gc = Lookup.getDefault().lookup(GraphController.class);
            Graph graph = gc.getGraphModel().getGraph();
            GraphFactory factory = gc.getGraphModel().factory();
            //Add node
            Node node = factory.newNode();
            node.setX(position3d[0]);
            node.setY(position3d[1]);
            node.setSize(10f);
            graph.addNode(node);
        }
    }, new NodeClickEventListener() {

        @Override
        public void clickNodes(Node[] nodes) {
            //Get mouse position
            float[] position3d = VizController.getInstance().getGraphIO().getMousePosition3d();
            //Get current graph
            GraphController gc = Lookup.getDefault().lookup(GraphController.class);
            Graph graph = gc.getGraphModel().getGraph();
            GraphFactory factory = gc.getGraphModel().factory();
            //Add node
            Node node = factory.newNode();
            node.setX(position3d[0]);
            node.setY(position3d[1]);
            node.setSize(10f);
            graph.addNode(node);
            //Add edges with the clicked nodes
            for (Node n : nodes) {
                Edge edge = factory.newEdge(node, n);
                graph.addEdge(edge);
            }
        }
    } };
}
Also used : MouseClickEventListener(org.gephi.tools.spi.MouseClickEventListener) GraphFactory(org.gephi.graph.api.GraphFactory) ToolEventListener(org.gephi.tools.spi.ToolEventListener) Graph(org.gephi.graph.api.Graph) Node(org.gephi.graph.api.Node) NodeClickEventListener(org.gephi.tools.spi.NodeClickEventListener) Edge(org.gephi.graph.api.Edge) GraphController(org.gephi.graph.api.GraphController)

Example 2 with ToolEventListener

use of org.gephi.tools.spi.ToolEventListener in project gephi by gephi.

the class DesktopToolController method select.

@Override
public void select(Tool tool) {
    unselect();
    if (tool == null) {
        return;
    }
    //Connect events
    ArrayList<ToolEventHandler> handlers = new ArrayList<>();
    for (ToolEventListener toolListener : tool.getListeners()) {
        if (toolListener instanceof NodeClickEventListener) {
            NodeClickEventHandler h = new NodeClickEventHandler(toolListener);
            h.select();
            handlers.add(h);
        } else if (toolListener instanceof NodePressingEventListener) {
            NodePressingEventHandler h = new NodePressingEventHandler(toolListener);
            h.select();
            handlers.add(h);
        } else if (toolListener instanceof MouseClickEventListener) {
            MouseClickEventHandler h = new MouseClickEventHandler(toolListener);
            h.select();
            handlers.add(h);
        } else if (toolListener instanceof NodePressAndDraggingEventListener) {
            NodePressAndDraggingEventHandler h = new NodePressAndDraggingEventHandler(toolListener);
            h.select();
            handlers.add(h);
        } else {
            throw new RuntimeException("The ToolEventListener " + toolListener.getClass().getSimpleName() + " cannot be recognized");
        }
    }
    currentHandlers = handlers.toArray(new ToolEventHandler[0]);
    switch(tool.getSelectionType()) {
        case NONE:
            VizController.getInstance().getSelectionManager().disableSelection();
            break;
        case SELECTION:
            VizController.getInstance().getSelectionManager().blockSelection(true);
            VizController.getInstance().getSelectionManager().setDraggingEnable(false);
            break;
        case SELECTION_AND_DRAGGING:
            VizController.getInstance().getSelectionManager().blockSelection(true);
            VizController.getInstance().getSelectionManager().setDraggingEnable(true);
            break;
    }
    currentTool = tool;
    currentTool.select();
}
Also used : MouseClickEventListener(org.gephi.tools.spi.MouseClickEventListener) ArrayList(java.util.ArrayList) NodePressingEventListener(org.gephi.tools.spi.NodePressingEventListener) NodePressAndDraggingEventListener(org.gephi.tools.spi.NodePressAndDraggingEventListener) ToolEventListener(org.gephi.tools.spi.ToolEventListener) NodeClickEventListener(org.gephi.tools.spi.NodeClickEventListener)

Aggregations

MouseClickEventListener (org.gephi.tools.spi.MouseClickEventListener)2 NodeClickEventListener (org.gephi.tools.spi.NodeClickEventListener)2 ToolEventListener (org.gephi.tools.spi.ToolEventListener)2 ArrayList (java.util.ArrayList)1 Edge (org.gephi.graph.api.Edge)1 Graph (org.gephi.graph.api.Graph)1 GraphController (org.gephi.graph.api.GraphController)1 GraphFactory (org.gephi.graph.api.GraphFactory)1 Node (org.gephi.graph.api.Node)1 NodePressAndDraggingEventListener (org.gephi.tools.spi.NodePressAndDraggingEventListener)1 NodePressingEventListener (org.gephi.tools.spi.NodePressingEventListener)1