From 11eb9557a35e17450c0aefb758471f1ff3148336 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Fri, 29 Jun 2012 16:20:11 +0200 Subject: Pull remaining graph GUI components into cristal-gui --- .../java/com/c2kernel/graph/model/GraphModel.java | 367 ++------------------- 1 file changed, 27 insertions(+), 340 deletions(-) (limited to 'src/main/java/com/c2kernel/graph/model/GraphModel.java') diff --git a/src/main/java/com/c2kernel/graph/model/GraphModel.java b/src/main/java/com/c2kernel/graph/model/GraphModel.java index 6f5ced5..c428e79 100644 --- a/src/main/java/com/c2kernel/graph/model/GraphModel.java +++ b/src/main/java/com/c2kernel/graph/model/GraphModel.java @@ -1,7 +1,5 @@ package com.c2kernel.graph.model; -import java.awt.Point; -import java.awt.Polygon; import java.io.Serializable; import java.util.Hashtable; import java.util.Vector; @@ -9,14 +7,10 @@ import java.util.Vector; import com.c2kernel.graph.event.ClearedEvent; import com.c2kernel.graph.event.EdgeRemovedEvent; import com.c2kernel.graph.event.EdgesChangedEvent; -import com.c2kernel.graph.event.ElasticBandResizedEvent; -import com.c2kernel.graph.event.ElasticBandSetEvent; import com.c2kernel.graph.event.ForcedNotifyEvent; import com.c2kernel.graph.event.GraphModelEvent; import com.c2kernel.graph.event.GraphModelResizedEvent; import com.c2kernel.graph.event.NewEdgeEndPointChangedEvent; -import com.c2kernel.graph.event.SelectionChangedEvent; -import com.c2kernel.graph.event.SelectionMovedEvent; import com.c2kernel.graph.event.StartVertexIdChangedEvent; import com.c2kernel.graph.event.VertexAddedEvent; import com.c2kernel.graph.event.VertexCreatedEvent; @@ -25,8 +19,8 @@ import com.c2kernel.graph.event.VertexRemovedEvent; import com.c2kernel.graph.event.VerticesChangedEvent; import com.c2kernel.utils.Logger; -public class GraphModel implements Serializable{ - /* Persistant data */ +public class GraphModel implements Serializable { + /* Persistent data */ private int mWidth = 0; private int mHeight = 0; @@ -38,11 +32,9 @@ public class GraphModel implements Serializable{ /* Transient data */ - // There should always be a Selection object - protected Selection mSelection = new Selection(null, null, 0, 0, 0, 0); protected Vertex mNewEdgeOriginVertex = null; - protected Point mNewEdgeEndPoint = null; - private ElasticBand mElasticBand = null; + protected GraphPoint mNewEdgeEndPoint = null; + private GraphModelManager mManager = null; /* External factories */ @@ -61,16 +53,12 @@ public class GraphModel implements Serializable{ private final EdgesChangedEvent mEdgesChangedEvent = new EdgesChangedEvent(); private final ForcedNotifyEvent mForcedNotifyEvent = new ForcedNotifyEvent(); private final NewEdgeEndPointChangedEvent mNewEdgeEndPointChangedEvent = new NewEdgeEndPointChangedEvent(); - private final SelectionChangedEvent mSelectionChangedEvent = new SelectionChangedEvent(); private final StartVertexIdChangedEvent mStartVertexIdChangedEvent = new StartVertexIdChangedEvent(); private final VertexAddedEvent mVertexAddedEvent = new VertexAddedEvent(); private final VertexCreatedEvent mVertexCreatedEvent = new VertexCreatedEvent(); private final VertexMovedEvent mVertexMovedEvent = new VertexMovedEvent(); - private final SelectionMovedEvent mSelectionMovedEvent = new SelectionMovedEvent(); private final VertexRemovedEvent mVertexRemovedEvent = new VertexRemovedEvent(); private final VerticesChangedEvent mVerticesChangedEvent = new VerticesChangedEvent(); - private final ElasticBandSetEvent mElasticBandSetEvent = new ElasticBandSetEvent(); - private final ElasticBandResizedEvent mElasticBandResizedEvent = new ElasticBandResizedEvent(); private final GraphModelResizedEvent mGraphModelResizedEvent = new GraphModelResizedEvent(); // Calling this constructor does not create a vertex outline creator @@ -90,16 +78,6 @@ public class GraphModel implements Serializable{ return number; } - private void setChanged() { - if (mManager != null) - mManager.setChanged(); - } - - private void notifyObservers(GraphModelEvent ev) { - if (mManager != null) - mManager.notifyObservers(ev); - } - public void setNextId(int id) { mNextId = id; } @@ -152,14 +130,14 @@ public class GraphModel implements Serializable{ } if (resized) { - setChanged(); - notifyObservers(mGraphModelResizedEvent); + publishEvent(mGraphModelResizedEvent); } } public void setStartVertexId(int id) { mStartVertexId = id; + publishEvent(mStartVertexIdChangedEvent); } public int getStartVertexId() { @@ -190,8 +168,7 @@ public class GraphModel implements Serializable{ checkSize(vertice); } - setChanged(); - notifyObservers(mVerticesChangedEvent); + publishEvent(mVerticesChangedEvent); } public Vertex[] getVertices() { @@ -209,8 +186,7 @@ public class GraphModel implements Serializable{ for (DirectedEdge edge : edges) { mEdgeHashtable.put(String.valueOf(edge.getID()), edge); } - setChanged(); - notifyObservers(mEdgesChangedEvent); + publishEvent(mEdgesChangedEvent); } public DirectedEdge[] getEdges() { @@ -304,12 +280,7 @@ public class GraphModel implements Serializable{ terminus.removeInEdgeId(edgeId); // Remove the edge mEdgeHashtable.remove(String.valueOf(e.getID())); - setChanged(); - notifyObservers(mEdgeRemovedEvent); - } - - public int addVertexAndCreateId(Vertex v, Point location) { - return addVertexAndCreateId(v, new GraphPoint(location.x, location.y)); + publishEvent(mEdgeRemovedEvent); } public int addVertexAndCreateId(Vertex v, GraphPoint location) { @@ -332,8 +303,7 @@ public class GraphModel implements Serializable{ if (mVertexOutlineCreator != null) { mVertexOutlineCreator.setOutline(v); } - setChanged(); - notifyObservers(mVertexAddedEvent); + publishEvent(mVertexAddedEvent); checkSize(v); } @@ -365,8 +335,7 @@ public class GraphModel implements Serializable{ } // Remove the vertex mVertexHashtable.remove(String.valueOf(v.getID())); - setChanged(); - notifyObservers(mVertexRemovedEvent); + publishEvent(mVertexRemovedEvent); } public void moveAbsoluteVertex(Vertex v, GraphPoint p) { @@ -376,8 +345,7 @@ public class GraphModel implements Serializable{ if (p.x > mWidth) p.x = mWidth; if (p.y > mHeight) p.y = mHeight; moveAbsoluteVertexAndConnectingEdges(v, p); - setChanged(); - notifyObservers(mVertexMovedEvent); + publishEvent(mVertexMovedEvent); } private void moveAbsoluteVertexAndConnectingEdges(Vertex v, GraphPoint p) { @@ -397,43 +365,7 @@ public class GraphModel implements Serializable{ checkSize(v); } - public void moveAbsoluteSelection(int newTopLeftX, int newTopLeftY) { - int selectionHeight = mSelection.mBottomRightY - mSelection.mTopLeftY; - int selectionWidth = mSelection.mBottomRightX - mSelection.mTopLeftX; - int bottomRightX = newTopLeftX + selectionWidth; - int bottomRightY = newTopLeftY + selectionHeight; - GraphPoint oldCentrePoint = null; - GraphPoint newCentrePoint = null; - int distXFromTopLeft = 0; - int distYFromTopLeft = 0; - int i = 0; - // Make sure the selection does not move - // outside the boundaries of the graph - if (newTopLeftX < 0) newTopLeftX = 0; - if (newTopLeftY < 0) newTopLeftY = 0; - if (bottomRightX > mWidth) newTopLeftX = mWidth - selectionWidth; - if (bottomRightY > mHeight) newTopLeftY = mHeight - selectionHeight; - // For each selected vertex - for (i = 0; i < mSelection.mVertices.length; i++) { - // Calculate the new centre point of the vertex. - // First calculate the distance of the centre point - // from the old top left hand corner of the selection, - // then move the point to the new top left hand - // corner plus the distance. - oldCentrePoint = mSelection.mVertices[i].getCentrePoint(); - distXFromTopLeft = oldCentrePoint.x - mSelection.mTopLeftX; - distYFromTopLeft = oldCentrePoint.y - mSelection.mTopLeftY; - newCentrePoint = new GraphPoint(newTopLeftX + distXFromTopLeft, newTopLeftY + distYFromTopLeft); - moveAbsoluteVertexAndConnectingEdges(mSelection.mVertices[i], newCentrePoint); - } - // Update the top left and bottom right corners - mSelection.mTopLeftX = newTopLeftX; - mSelection.mTopLeftY = newTopLeftY; - mSelection.mBottomRightX = newTopLeftX + selectionWidth; - mSelection.mBottomRightY = newTopLeftY + selectionHeight; - setChanged(); - notifyObservers(mSelectionMovedEvent); - } + public Vertex resolveVertex(int id) { return mVertexHashtable.get(String.valueOf(id)); @@ -525,62 +457,17 @@ public class GraphModel implements Serializable{ } public void forceNotify() { - setChanged(); - notifyObservers(mForcedNotifyEvent); + publishEvent(mForcedNotifyEvent); } public void clear() { mVertexHashtable = new Hashtable(); mEdgeHashtable = new Hashtable(); mStartVertexId = -1; - setChanged(); - notifyObservers(mClearedEvent); + publishEvent(mClearedEvent); } - public void setSelection(Selection s) { - // If the there is a change - if (selectionChanged(s)) { - mSelection = s; - mSelectionChangedEvent.mSelection = s; - setChanged(); - notifyObservers(mSelectionChangedEvent); - } - } - private boolean selectionChanged(Selection newValue) { - int i = 0; - if (mSelection.mEdge != newValue.mEdge) { - return true; - } - if (mSelection.mVertices == null) { - if (newValue.mVertices == null) { - return false; - } - else { - return true; - } - } - else { - if (newValue.mVertices == null) { - return true; - } - else { - if (mSelection.mVertices.length != newValue.mVertices.length) { - return true; - } - for (i = 0; i < mSelection.mVertices.length; i++) { - if (mSelection.mVertices[i] != newValue.mVertices[i]) { - return true; - } - } - return false; - } - } - } - - public Selection getSelection() { - return mSelection; - } public void setNewEdgeOriginVertex(Vertex v) { mNewEdgeOriginVertex = v; @@ -590,13 +477,12 @@ public class GraphModel implements Serializable{ return mNewEdgeOriginVertex; } - public void setNewEdgeEndPoint(Point p) { + public void setNewEdgeEndPoint(GraphPoint p) { mNewEdgeEndPoint = p; - setChanged(); - notifyObservers(mNewEdgeEndPointChangedEvent); + publishEvent(mNewEdgeEndPointChangedEvent); } - public Point getNewEdgeEndPoint() { + public GraphPoint getNewEdgeEndPoint() { return mNewEdgeEndPoint; } @@ -604,15 +490,20 @@ public class GraphModel implements Serializable{ mExternalVertexFactory = factory; } - public void createVertex(Point location, TypeNameAndConstructionInfo typeNameAndConstructionInfo) throws Exception { + public void createVertex(GraphPoint location, TypeNameAndConstructionInfo typeNameAndConstructionInfo) throws Exception { if (mExternalVertexFactory != null) { mExternalVertexFactory.create(mManager, location, typeNameAndConstructionInfo); - setChanged(); - notifyObservers(mVertexCreatedEvent); + publishEvent(mVertexCreatedEvent); } } - public void setExternalEdgeFactory(EdgeFactory factory) { + private void publishEvent(GraphModelEvent event) { + if (mManager!=null) + mManager.notifyObservers(event); + + } + + public void setExternalEdgeFactory(EdgeFactory factory) { mExternalEdgeFactory = factory; } @@ -626,211 +517,7 @@ public class GraphModel implements Serializable{ } } - public void selectAll() { - Vertex[] allVertices = getVertices(); - if (allVertices.length > 0) { - mSelection.mEdge = null; - mSelection.mVertices = allVertices; - updateSelectionCorners(); - mSelectionChangedEvent.mSelection = mSelection; - setChanged(); - notifyObservers(mSelectionChangedEvent); - } - } - - public void selectContentsOfElasticBand() { - if (mElasticBand == null) return; - Polygon bandPolygon = new Polygon(); - Vertex[] allVertices = getVertices(); - GraphPoint centrePoint = null; - Vector verticesInside = new Vector(10, 10); - int i = 0; - // Create a polygon representing the elastic band - bandPolygon.addPoint(mElasticBand.mFixedCorner.x, mElasticBand.mFixedCorner.y); - bandPolygon.addPoint(mElasticBand.mMovingCorner.x, mElasticBand.mFixedCorner.y); - bandPolygon.addPoint(mElasticBand.mMovingCorner.x, mElasticBand.mMovingCorner.y); - bandPolygon.addPoint(mElasticBand.mFixedCorner.x, mElasticBand.mMovingCorner.y); - // Create a vector of all of the vertices within the elastic band polygon - for (i = 0; i < allVertices.length; i++) { - centrePoint = allVertices[i].getCentrePoint(); - if (bandPolygon.contains(centrePoint.x, centrePoint.y)) { - verticesInside.add(allVertices[i]); - } - } - - // Select the vertices found within the elastic band polygon - if (verticesInside.size() == 0) { - mSelection.mTopLeftX = 0; - mSelection.mTopLeftY = 0; - mSelection.mBottomRightX = 0; - mSelection.mBottomRightY = 0; - mSelection.mEdge = null; - if (mContainingVertex != null) - verticesInside.add(mContainingVertex); - else - mSelection.mVertices = null; - } - - if (verticesInside.size() > 0) { - mSelection.mEdge = null; - mSelection.mVertices = new Vertex[verticesInside.size()]; - for (i = 0; i < verticesInside.size(); i++) { - mSelection.mVertices[i] = verticesInside.elementAt(i); - } - updateSelectionCorners(); - } - // Remove the elastic band - mElasticBand = null; - mSelectionChangedEvent.mSelection = mSelection; - setChanged(); - notifyObservers(mSelectionChangedEvent); - } - - // Updates the top left and bottom right corners of the selection - private void updateSelectionCorners() { - Vertex vertex = mSelection.mVertices[0]; - GraphPoint centrePoint = vertex.getCentrePoint(); - if (centrePoint == null) return; - mSelection.mTopLeftX = centrePoint.x; - mSelection.mTopLeftY = centrePoint.y; - mSelection.mBottomRightX = centrePoint.x; - mSelection.mBottomRightY = centrePoint.y; - for (Vertex mVertice : mSelection.mVertices) { - vertex = mVertice; - centrePoint = vertex.getCentrePoint(); - if (centrePoint.x < mSelection.mTopLeftX) { - mSelection.mTopLeftX = centrePoint.x; - } - if (centrePoint.y < mSelection.mTopLeftY) { - mSelection.mTopLeftY = centrePoint.y; - } - if (centrePoint.x > mSelection.mBottomRightX) { - mSelection.mBottomRightX = centrePoint.x; - } - if (centrePoint.y > mSelection.mBottomRightY) { - mSelection.mBottomRightY = centrePoint.y; - } - } - } - - public void deleteSelection() { - int i = 0; - if (mSelection.mVertices != null) { - for (i = 0; i < mSelection.mVertices.length; i++) { - removeVertex(mSelection.mVertices[i]); - } - } - else if (mSelection.mEdge != null) { - removeEdge(mSelection.mEdge); - } - // Make sure nothing is selected - if ((mSelection.mEdge != null) || (mSelection.mVertices != null)) { - mSelection.mEdge = null; - mSelection.mVertices = null; - mSelectionChangedEvent.mSelection = mSelection; - setChanged(); - notifyObservers(mSelectionChangedEvent); - } - } - public void setSelectedVertexToBeStart() { - if (mSelection.mVertices != null) { - if (mSelection.mVertices.length == 1) { - setStartVertexId(mSelection.mVertices[0].getID()); - setChanged(); - notifyObservers(mStartVertexIdChangedEvent); - } - } - } - - public void setElasticBand(ElasticBand elasticBand) { - mElasticBand = elasticBand; - setChanged(); - notifyObservers(mElasticBandSetEvent); - } - - public ElasticBand getElasticBand() { - return mElasticBand; - } - - public void resizeElasticBand(Point movingCorner) { - mElasticBand.mMovingCorner = movingCorner; - setChanged(); - notifyObservers(mElasticBandResizedEvent); - } - - public boolean inSelection(Vertex v) { - int i = 0; - if (mSelection.mVertices == null) { - return false; - } - else { - for (i = 0; i < mSelection.mVertices.length; i++) { - if (mSelection.mVertices[i] == v) { - return true; - } - } - return false; - } - } - - // Only use this method to remove one vertex. - // If you wish to remove more, it would - // propably be more efficient to create a - // new Selection object. - public void removeFromSelection(Vertex v) { - Vertex[] vertices = null; - int i = 0; - int j = 0; - if (mSelection.mVertices.length == 1) { - mSelection.mVertices = null; - mSelection.mTopLeftX = 0; - mSelection.mTopLeftY = 0; - mSelection.mBottomRightX = 0; - mSelection.mBottomRightY = 0; - } - else { - vertices = new Vertex[mSelection.mVertices.length - 1]; - for (i = 0; i < mSelection.mVertices.length; i++) { - if (mSelection.mVertices[i] != v) { - vertices[j] = mSelection.mVertices[i]; - j++; - } - } - mSelection.mVertices = vertices; - updateSelectionCorners(); - } - setChanged(); - notifyObservers(mSelectionChangedEvent); - } - - // Only use this method to add one vertex. - // If you wish to add more, it would - // propably be more efficient to create a - // new Selection object. - public void addToSelection(Vertex v) { - Vertex[] vertices = new Vertex[mSelection.mVertices.length + 1]; - GraphPoint centrePoint = null; - int i = 0; - if (mSelection.mVertices == null) { - centrePoint = v.getCentrePoint(); - mSelection.mVertices = new Vertex[] { v }; - mSelection.mTopLeftX = centrePoint.x; - mSelection.mTopLeftY = centrePoint.y; - mSelection.mBottomRightX = centrePoint.x; - mSelection.mBottomRightY = centrePoint.y; - } - else { - for (i = 0; i < mSelection.mVertices.length; i++) { - vertices[i] = mSelection.mVertices[i]; - } - vertices[mSelection.mVertices.length] = v; - mSelection.mVertices = vertices; - updateSelectionCorners(); - } - setChanged(); - notifyObservers(mSelectionChangedEvent); - } public void resetVertexOutlines() { Vertex[] vertices = getVertices(); -- cgit v1.2.3