View Javadoc
1 package com.bonevich.erj.diagram; 2 3 import com.bonevich.erj.model.*; 4 5 import org.tigris.gef.graph.*; 6 7 import java.util.*; 8 9 /*** This class defines a bridge between the Relational meta-model 10 * representation of the database design and the GraphModel interface 11 * used by GEF. 12 */ 13 public class ERDiagramGraphModel 14 extends MutableGraphSupport 15 implements MutableGraphModel 16 { 17 //////////////////////////////////////////////////////////////// 18 // instance variables 19 private Schema _schema; 20 21 //////////////////////////////////////////////////////////////// 22 // constructors 23 public ERDiagramGraphModel() 24 { 25 this(new Schema()); 26 _schema.setName(Schema.NEW_SCHEMA_STR); 27 _schema.setIdentifier(Schema.NEW_SCHEMA_STR); 28 } 29 30 public ERDiagramGraphModel(Schema schema) 31 { 32 _schema = schema; 33 } 34 35 public Schema getSchema() { return _schema; } 36 37 //////////////////////////////////////////////////////////////// 38 // GraphModel implementation 39 40 /*** Return all nodes in the graph */ 41 public Vector getNodes() 42 { 43 Vector v = new Vector(); 44 Iterator relations = _schema.getRelationIterator(); 45 while (relations.hasNext()) 46 { 47 v.addElement(relations.next()); 48 } 49 return v; 50 } 51 52 /*** Return all edges in the graph */ 53 public Vector getEdges() 54 { 55 Vector v = new Vector(); 56 Iterator keys = _schema.getKeyIterator(); 57 while (keys.hasNext()) 58 { 59 v.addElement(keys.next()); 60 } 61 return v; 62 } 63 64 /*** Return all ports on node or edge */ 65 public Vector getPorts(Object nodeOrEdge) 66 { 67 Vector v = new Vector(); //wasteful! 68 if (nodeOrEdge instanceof Relation) 69 { 70 v.addElement(nodeOrEdge); 71 } 72 if (nodeOrEdge instanceof ForeignKey) 73 { 74 ForeignKey key = (ForeignKey)nodeOrEdge; 75 v.addElement(key.getRelation()); 76 v.addElement(key.getReferentRelation()); 77 } 78 return v; 79 } 80 81 /*** Return the node or edge that owns the given port */ 82 public Object getOwner(Object port) 83 { 84 return port; 85 } 86 87 /*** Return all edges going to given port */ 88 public Vector getInEdges(Object port) 89 { 90 Vector v = new Vector(); //wasteful! 91 if (port instanceof Relation) 92 { 93 Relation rel = (Relation) port; 94 Iterator keys = rel.getKeyIterator(); 95 while (keys.hasNext()) 96 { 97 KeyConstraint c = (KeyConstraint)keys.next(); 98 if (c instanceof ForeignKey) 99 { 100 v.addElement(c); 101 } 102 } 103 } 104 return v; 105 } 106 107 /*** Return all edges going from given port */ 108 public Vector getOutEdges(Object port) 109 { 110 //FIXME: we can do this, cannot we? 111 return new Vector(); 112 } 113 114 /*** Return one end of an edge */ 115 public Object getSourcePort(Object edge) 116 { 117 if (edge instanceof ForeignKey) 118 { 119 ForeignKey fk = (ForeignKey) edge; 120 return fk.getRelation(); 121 } 122 return null; 123 } 124 125 /*** Return the other end of an edge */ 126 public Object getDestPort(Object edge) 127 { 128 if (edge instanceof ForeignKey) 129 { 130 ForeignKey fk = (ForeignKey) edge; 131 return fk.getReferentRelation(); 132 } 133 return null; 134 } 135 136 137 //////////////////////////////////////////////////////////////// 138 // MutableGraphModel implementation 139 140 /*** Return true if the given object is a valid node in this graph */ 141 public boolean canAddNode(Object node) 142 { 143 if (node instanceof Relation) 144 { 145 return _schema.containsRelation( (Relation)node ); 146 } 147 return false; 148 } 149 150 /*** Return true if the given object is a valid edge in this graph */ 151 public boolean canAddEdge(Object edge) 152 { 153 if (edge instanceof ForeignKey) 154 { 155 ForeignKey fk = (ForeignKey) edge; 156 if (!_schema.containsKey(fk)) 157 { 158 Relation referrer = fk.getRelation(); 159 Relation referent = fk.getReferentRelation(); 160 if (referrer == null || referent == null) return false; 161 if (!_schema.containsRelation(referrer)) return false; 162 if (!_schema.containsRelation(referent)) return false; 163 } 164 } 165 return false; 166 } 167 168 /*** Add the given node to the graph, if valid. */ 169 public void addNode(Object node) 170 { 171 // node has already been added to schema 172 fireNodeAdded(node); 173 } 174 175 /*** Remove the given node from the graph. */ 176 public void removeNode(Object node) 177 { 178 if (!_schema.containsRelation( (Relation)node )) return; 179 _schema.deleteRelation( (Relation)node ); 180 fireNodeRemoved(node); 181 } 182 183 /*** Add the given edge to the graph, if valid. */ 184 public void addEdge(Object edge) 185 { 186 // edge has already been added to relation and schema 187 fireEdgeAdded(edge); 188 } 189 190 /*** Remove the given edge from the graph. */ 191 public void removeEdge(Object edge) 192 { 193 if (!_schema.containsKey( (ForeignKey)edge )) return; 194 Relation r = ((ForeignKey)edge).getRelation(); 195 r.deleteKey((ForeignKey)edge); 196 fireEdgeRemoved(edge); 197 } 198 199 /*** Add existing edges that are related to the node. */ 200 public void addNodeRelatedEdges(Object node) 201 { 202 if (node instanceof Relation) 203 { 204 Iterator keys = ((Relation)node).getKeyIterator(); 205 while (keys.hasNext()) 206 { 207 KeyConstraint c = (KeyConstraint) keys.next(); 208 if(canAddEdge(c)) 209 { 210 addEdge(c); 211 } 212 } 213 } 214 } 215 216 /*** Return true if the two given ports can be connected by a 217 * kind of edge to be determined by the ports. */ 218 public boolean canConnect(Object fromP, Object toP) 219 { 220 return (fromP instanceof Relation && toP instanceof Relation); 221 } 222 223 /*** Contruct and add a new edge of a kind determined by the ports */ 224 public Object connect(Object fromPort, Object toPort) 225 { 226 return connect(fromPort, toPort, ForeignKey.class); 227 } 228 229 /*** Contruct and add a new edge of the given kind */ 230 public Object connect(Object fromPort, Object toPort, Class edgeClass) 231 { 232 if (fromPort instanceof Relation && 233 toPort instanceof Relation && 234 edgeClass == ForeignKey.class) 235 { 236 Relation fromRelation = (Relation) fromPort; 237 Relation toRelation = (Relation) toPort; 238 ForeignKey key = fromRelation.createForeignKey(toRelation); 239 addEdge(key); 240 return key; 241 } 242 System.out.println("connect: Cannot make a "+ edgeClass.getName() + 243 " between a " + fromPort.getClass().getName() + 244 " and a " + toPort.getClass().getName()); 245 return null; 246 } 247 248 ////////////////////////////////////////////////////////// 249 // Event notification 250 /*** 251 * This method provides a signature taking a node or edge as an argument 252 * (unlike the method in MutableGraphSupport, which takes no arguments). 253 */ 254 public void fireGraphChanged(Object nodeOrEdge) 255 { 256 //System.err.println("(graphmodel) fireGraphChanged called!"); 257 if (_graphListeners == null) return; 258 GraphEvent ge = new GraphEvent(this, nodeOrEdge); 259 Iterator listeners = _graphListeners.iterator(); 260 while (listeners.hasNext()) 261 { 262 GraphListener listener = (GraphListener) listeners.next(); 263 listener.graphChanged(ge); 264 } 265 } 266 267 } /* end class ERDiagramGraphModel */

This page was automatically generated by Maven