From 5e4034b5cba89460a62fa958fc78c2b85acb3d5f Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Tue, 7 Oct 2014 09:18:33 +0200 Subject: Repackage to org.cristalise --- .../controller/EdgeConstructionController.java | 262 +++++++++++++++++++++ 1 file changed, 262 insertions(+) create mode 100644 src/main/java/org/cristalise/gui/graph/controller/EdgeConstructionController.java (limited to 'src/main/java/org/cristalise/gui/graph/controller/EdgeConstructionController.java') diff --git a/src/main/java/org/cristalise/gui/graph/controller/EdgeConstructionController.java b/src/main/java/org/cristalise/gui/graph/controller/EdgeConstructionController.java new file mode 100644 index 0000000..63fc0dd --- /dev/null +++ b/src/main/java/org/cristalise/gui/graph/controller/EdgeConstructionController.java @@ -0,0 +1,262 @@ +package org.cristalise.gui.graph.controller; + +import java.awt.Point; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +import org.cristalise.gui.graph.view.EditorModeListener; +import org.cristalise.gui.graph.view.EditorToolBar; +import org.cristalise.gui.graph.view.GraphPanel; +import org.cristalise.kernel.graph.model.GraphModelManager; +import org.cristalise.kernel.graph.model.GraphPoint; +import org.cristalise.kernel.graph.model.Vertex; + + + +public class EdgeConstructionController extends MouseAdapter implements EditorModeListener +{ + private GraphModelManager mGraphModelManager = null; + private GraphPanel mGraphPanel = null; + private EditorToolBar mEditorToolBar = null; + + + /**********/ + /* States */ + /**********/ + private final Integer kOtherMode = new Integer(0); + private final Integer kWaitOrigin = new Integer(1); + private final Integer kWaitTerminus = new Integer(2); + + + /**********/ + /* Events */ + /**********/ + private final int kEdgeEntered = 0; + private final int kOtherEntered = 1; + private final int kPressOnVertex = 2; + private final int kDrag = 3; + private final int kReleaseOnTerminus = 4; + private final int kReleaseOnNothing = 5; + + + /***********/ + /* Actions */ + /***********/ + + private interface Action + { + public void doIt(Object data); + } + + + private Action mSetOriginVertex = new Action() + { + @Override + public void doIt(Object data) + { + if(mGraphModelManager != null) + { + mGraphModelManager.getModel().setNewEdgeOriginVertex((Vertex)data); + } + } + }; + + + private Action mSetEndPoint = new Action() + { + @Override + public void doIt(Object data) + { + if(mGraphModelManager != null) + { + Point mouse = (Point)data; + mGraphModelManager.getModel().setNewEdgeEndPoint(new GraphPoint(mouse.x, mouse.y)); + } + } + }; + + + private Action mClearEdge = new Action() + { + @Override + public void doIt(Object data) + { + if(mGraphModelManager != null) + { + mGraphModelManager.getModel().setNewEdgeOriginVertex(null); + mGraphModelManager.getModel().setNewEdgeEndPoint(null); + } + } + }; + + + private Action mCreateEdge = new Action() + { + @Override + public void doIt(Object data) + { + if((mGraphModelManager != null) && (mEditorToolBar != null) && mGraphModelManager.isEditable()) + { + mGraphModelManager.getModel().createDirectedEdge + ( + mGraphModelManager.getModel().getNewEdgeOriginVertex(), + (Vertex)data, + mEditorToolBar.getSelectedEdgeType() + ); + mGraphModelManager.getModel().setNewEdgeOriginVertex(null); + mGraphModelManager.getModel().setNewEdgeEndPoint(null); + } + } + }; + + + /***********************************/ + /* Finite State Transition Network */ + /***********************************/ + + private Object[][][] mFSTN = + {// OtherMode WaitOrigin WaitTerminus + /* EdgeEntered */ {{null, kWaitOrigin}, null , null }, + /* OtherEntered */ { null , {null , kOtherMode} , null }, + /* PressOnVertex */ { null , {mSetOriginVertex, kWaitTerminus}, null }, + /* Drag */ { null , null , {mSetEndPoint, null} }, + /* ReleaseOnTerminus */ { null , null , {mCreateEdge , kWaitOrigin}}, + /* ReleaseOnNothing */ { null , null , {mClearEdge , kWaitOrigin}} + }; + + + /*****************/ + /* Current state */ + /*****************/ + + private Integer mState = kOtherMode; + + + /**************************/ + /* Event processing logic */ + /**************************/ + + private void processEvent(int event, Object data) + { + Object[] transition = mFSTN[event][mState.intValue()]; + Action action = null; + Integer nextState = null; + + if(transition != null) + { + action = (Action)transition[0]; + nextState = (Integer)transition[1]; + + if(action != null) + { + action.doIt(data); + } + + if(nextState != null) + { + mState = nextState; + } + } + } + + + /********************/ + /* Public interface */ + /********************/ + + public void setGraphModelManager(GraphModelManager graphModelManager) + { + mGraphModelManager = graphModelManager; + } + + public void setGraphPanel(GraphPanel graphPanel) + { + mGraphPanel = graphPanel; + } + + + public void setEditorToolBar(EditorToolBar editorToolBar) + { + mEditorToolBar = editorToolBar; + mEditorToolBar.addEditorModeListener(this); + } + + + @Override + public void editorModeChanged(String idOfNewMode) + { + if(idOfNewMode.equals("Edge")) + { + processEvent(kEdgeEntered, null); + } + else + { + processEvent(kOtherEntered, null); + } + } + + + @Override + public void mousePressed(MouseEvent me) + { + Vertex vertex = null; + Point mousePoint = null; + + if(mGraphModelManager != null && mGraphPanel != null) + { + // Determine if there is a vertex under the mouse cursor + mousePoint = me.getPoint(); + vertex = mGraphPanel.getVertex(new GraphPoint(mousePoint.x, mousePoint.y)); + + // If the mouse has been pressed on a vertex + if(vertex != null) + { + processEvent(kPressOnVertex, vertex); + } + } + } + + + @Override + public void mouseReleased(MouseEvent me) + { + Vertex vertex = null; + Point mousePoint = null; + + if(mGraphModelManager != null && mGraphPanel != null) + { + // Determine if there is a vertex under the mouse cursor + mousePoint = me.getPoint(); + vertex = mGraphPanel.getVertex(new GraphPoint(mousePoint.x, mousePoint.y)); + + // If the mouse has been released on a vertex which is not the origin vertex + if((vertex != null) && (vertex != mGraphModelManager.getModel().getNewEdgeOriginVertex())) + { + processEvent(kReleaseOnTerminus, vertex); + } + else + { + processEvent(kReleaseOnNothing, null); + } + } + } + + + @Override + public void mouseExited(MouseEvent me) + { + } + + + @Override + public void mouseDragged(MouseEvent me) + { + processEvent(kDrag, me.getPoint()); + } + + + @Override + public void mouseMoved(MouseEvent me) + { + } +} -- cgit v1.2.3