Math2mat
|
00001 00016 package m2m.frontend.dynamicview.model; 00017 00018 import java.beans.PropertyChangeListener; 00019 import java.beans.PropertyChangeSupport; 00020 import java.util.Vector; 00021 00022 import m2m.backend.structure.Element; 00023 00024 import org.eclipse.draw2d.geometry.Rectangle; 00025 00026 00027 public abstract class Node 00028 { 00032 public static final String LAYOUT = "Layout"; 00036 public static final String VISIBLE_COMPONENT = "VisibleComponent"; 00040 public static final String CHANGE_COLOR = "ChangeColor"; 00041 00042 00046 private String name; 00050 private Rectangle layout; 00054 private Vector<Rectangle> layoutReduction = new Vector<Rectangle>(); 00058 private Vector<Node> children; 00062 private Node parent; 00066 private boolean visible = true; 00070 private PropertyChangeSupport listeners; 00074 private Vector<Connection> sourceConnections = new Vector<Connection>(); 00078 private Vector<Connection> targetConnections = new Vector<Connection>(); 00082 private boolean colorConditionSelected = false; 00086 private int level; 00090 private int depthLevel; 00094 private Element element; 00095 00096 00100 public Node() 00101 { 00102 this.name = "Unknown"; 00103 this.layout = new Rectangle(10, 10, 100, 100); 00104 this.children = new Vector<Node>(); 00105 this.parent = null; 00106 this.listeners = new PropertyChangeSupport(this); 00107 } 00108 00109 00114 public void setLevel(int level) 00115 { 00116 this.level = level; 00117 } 00118 00119 00124 public int getLevel() 00125 { 00126 return level; 00127 } 00128 00129 00134 public void setElement(Element element) 00135 { 00136 this.element = element; 00137 } 00138 00139 00144 public Element getElement() 00145 { 00146 return element; 00147 } 00148 00149 00154 public void addConnection(Connection conn) 00155 { 00156 if (conn == null || conn.getSource() == conn.getTarget()) 00157 throw new IllegalArgumentException(); 00158 if (conn.getSource() == this) 00159 sourceConnections.add(conn); 00160 else if (conn.getTarget() == this) 00161 targetConnections.add(conn); 00162 } 00163 00164 00169 public void removeConnection(Connection conn) 00170 { 00171 if (conn == null) 00172 throw new IllegalArgumentException(); 00173 if (conn.getSource() == this) 00174 sourceConnections.remove(conn); 00175 else if (conn.getTarget() == this) 00176 targetConnections.remove(conn); 00177 } 00178 00179 00184 public Vector<Connection> getSourceConnections() 00185 { 00186 return sourceConnections; 00187 } 00188 00189 00194 public Vector<Connection> getTargetConnections() 00195 { 00196 return targetConnections; 00197 } 00198 00199 00204 public void setName(String name) 00205 { 00206 this.name = name; 00207 } 00208 00209 00214 public String getName() 00215 { 00216 return this.name; 00217 } 00218 00219 00224 public boolean getVisible() 00225 { 00226 return this.visible; 00227 } 00228 00229 00234 public void setVisible(boolean visible) 00235 { 00236 boolean odlValue = this.visible; 00237 this.visible = visible; 00238 firePropertyChange(Node.VISIBLE_COMPONENT, odlValue, visible); 00239 } 00240 00241 00246 public void setLayout(Rectangle newLayout) 00247 { 00248 Rectangle oldLayout = this.layout; 00249 this.layout = newLayout; 00250 getListeners().firePropertyChange(LAYOUT, oldLayout, newLayout); 00251 } 00252 00253 00258 public void setLayoutReduction(Rectangle newLayout) 00259 { 00260 layoutReduction.add(0, new Rectangle(layout)); 00261 this.layout = newLayout; 00262 getListeners().firePropertyChange(LAYOUT, layoutReduction.firstElement(), newLayout); 00263 } 00264 00265 00270 public Rectangle getLayout() 00271 { 00272 return this.layout; 00273 } 00274 00275 00280 public Rectangle getLayoutReduction() 00281 { 00282 return layoutReduction.firstElement(); 00283 } 00284 00285 00289 public void removeLayoutReduction() 00290 { 00291 layoutReduction.remove(layoutReduction.firstElement()); 00292 } 00293 00294 00299 public void addChild(Node child) 00300 { 00301 child.setParent(this); 00302 this.children.add(child); 00303 } 00304 00305 00310 public void removeChild(Node child) 00311 { 00312 this.children.remove(child); 00313 } 00314 00315 00320 public Vector<Node> getChildrenArray() 00321 { 00322 return this.children; 00323 } 00324 00325 00330 public void setParent(Node parent) 00331 { 00332 this.parent = parent; 00333 } 00334 00335 00340 public Node getParent() 00341 { 00342 return this.parent; 00343 } 00344 00345 00350 public boolean getColorConditionSelected() 00351 { 00352 return colorConditionSelected; 00353 } 00354 00355 00360 public void setColorConditionSelected(boolean colorConditionSelected) 00361 { 00362 boolean odlValue = this.colorConditionSelected; 00363 this.colorConditionSelected = colorConditionSelected; 00364 firePropertyChange(Node.CHANGE_COLOR, odlValue, colorConditionSelected); 00365 } 00366 00367 00372 public void addPropertyChangeListener(PropertyChangeListener listener) 00373 { 00374 listeners.addPropertyChangeListener(listener); 00375 } 00376 00377 00382 public void removePropertyChangeListener(PropertyChangeListener listener) 00383 { 00384 listeners.removePropertyChangeListener(listener); 00385 } 00386 00387 00392 public PropertyChangeSupport getListeners() 00393 { 00394 return listeners; 00395 } 00396 00397 00404 protected void firePropertyChange(String property, Object oldValue, Object newValue) 00405 { 00406 if (listeners.hasListeners(property)) 00407 listeners.firePropertyChange(property, oldValue, newValue); 00408 } 00409 00410 00415 public boolean CanSetLayout(Rectangle layoutTest) 00416 { 00417 /* Check if the new layout is under a node */ 00418 for(Node node : parent.getChildrenArray()) 00419 { 00420 if(node != this) 00421 if(nodeUnderNode(layoutTest, node.getLayout())) 00422 return false; 00423 } 00424 00425 return true; 00426 } 00427 00428 00435 public boolean nodeUnderNode(Rectangle layoutTest, Rectangle layoutNode) 00436 { 00437 Rectangle tempLayoutTest = new Rectangle(layoutTest.x-5, layoutTest.y-5, layoutTest.width+10, layoutTest.height+10); 00438 Rectangle tempLayoutNode = new Rectangle(layoutNode.x-5, layoutNode.y-5, layoutNode.width+10, layoutNode.height+10); 00439 00440 return tempLayoutTest.intersects(tempLayoutNode); 00441 } 00442 00443 00449 public boolean isInParent(Rectangle layoutTest) 00450 { 00451 Rectangle parentLayout = parent.getLayout(); 00452 00453 if(layoutTest.x < 0 || layoutTest.y < 0) 00454 return false; 00455 00456 if(!(parent instanceof Schema) && (layoutTest.x+layoutTest.width > parentLayout.width || layoutTest.y+layoutTest.height > parentLayout.height)) 00457 return false; 00458 00459 return true; 00460 } 00461 00466 public void setDepthLevel() 00467 { 00468 Node node = this; 00469 int level = 0; 00470 00471 while(!(node instanceof Schema)) 00472 { 00473 if(node instanceof InternPointConnection) 00474 node = node.getParent(); 00475 00476 level++; 00477 node = node.getParent(); 00478 } 00479 00480 depthLevel = level; 00481 } 00482 00487 public int getDepthLevel() 00488 { 00489 return depthLevel; 00490 } 00491 }