001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.applications.designstudio;
016:
017: import java.awt.Color;
018: import java.awt.Dimension;
019: import java.awt.Point;
020: import java.awt.Rectangle;
021: import java.awt.geom.Point2D;
022: import java.awt.geom.Rectangle2D;
023: import java.util.ArrayList;
024: import java.util.HashMap;
025: import java.util.Hashtable;
026: import java.util.Map;
027: import java.util.Set;
028:
029: import org.jgraph.event.GraphModelEvent;
030: import org.jgraph.event.GraphModelListener;
031: import org.jgraph.graph.ConnectionSet;
032: import org.jgraph.graph.DefaultEdge;
033: import org.jgraph.graph.DefaultGraphCell;
034: import org.jgraph.graph.DefaultGraphModel;
035: import org.jgraph.graph.DefaultPort;
036: import org.jgraph.graph.Edge;
037: import org.jgraph.graph.GraphConstants;
038:
039: import com.metaboss.applications.designstudio.components.DesignGraph;
040: import com.metaboss.applications.designstudio.components.VertexCell;
041: import com.metaboss.sdlctools.models.metabossmodel.MetaBossModelPackage;
042: import com.metaboss.sdlctools.models.metabossmodel.ModelElement;
043: import com.metaboss.sdlctools.models.metabossmodel.visualmodel.ColorUtils;
044: import com.metaboss.sdlctools.models.metabossmodel.visualmodel.Diagram;
045: import com.metaboss.sdlctools.models.metabossmodel.visualmodel.DiagramModelElement;
046:
047: /* Base class for graph models */
048:
049: public class BaseGraphModel extends DefaultGraphModel {
050: public final static int UNDEFINED = -1000;
051:
052: public static boolean isUndefinedPosition(Point2D lPosition) {
053: return (lPosition.getX() == UNDEFINED && lPosition.getY() == UNDEFINED);
054: }
055:
056: protected ModelElement mObject = null;
057: public Diagram mDiagram = null;
058: protected boolean mChanged = false;
059: protected boolean mLoading = true;
060: protected HashMap mVertexPorts = new HashMap();
061: public Point2D mOldEdgePoint = null;
062: public DesignGraph mGraph = null;
063: public DesignGraphModelListener mModelListener = new DesignGraphModelListener();
064:
065: /** Constructor. */
066: public BaseGraphModel(ModelElement pObject, Diagram pDiagram)
067: throws Exception {
068: super ();
069:
070: mObject = pObject;
071: mDiagram = pDiagram;
072: setModelListener();
073: loadModel();
074: }
075:
076: // reload graph model
077: public void loadModel() throws Exception {
078: //???loadVertexesPositions();
079: }
080:
081: // get diagram interface
082: /*public Diagram getDiaram() throws Exception
083: {
084: return null;
085: }*/
086:
087: // add vertex into the model
088: public DefaultGraphCell addVertex(BaseUserObject pUserObject,
089: Point pPosition, Dimension pSize, Map pStyle)
090: throws Exception {
091: if (pUserObject == null)
092: return null;
093: if (pPosition == null)
094: pPosition = new Point(10, 10);
095:
096: DefaultGraphCell lCell = new VertexCell(pUserObject);
097: DefaultPort lPort = new DefaultPort(pUserObject);
098: Map lAttributes = new Hashtable();
099: ArrayList lModelRoots = new ArrayList();
100: ConnectionSet lConnectionSet = new ConnectionSet();
101:
102: if (pSize != null)
103: GraphConstants.setBounds(pStyle, new Rectangle(pPosition,
104: pSize));
105: else
106: GraphConstants.setBounds(pStyle, new Rectangle(pPosition));
107:
108: Color lColor = getVertexColor(pUserObject.getBOObject());
109: if (lColor != null)
110: GraphConstants.setBackground(pStyle, lColor);
111:
112: lAttributes.put(lCell, pStyle);
113: lCell.add(lPort);
114: mVertexPorts.put(pUserObject.getID(), lPort);
115: lModelRoots.add(lCell);
116:
117: insert(lModelRoots.toArray(), lAttributes, lConnectionSet,
118: null, null);
119: if (mGraph != null)
120: mGraph.setSelectionCell(lCell);
121: return lCell;
122: }
123:
124: // add graph edge
125: public DefaultEdge addEdge(BaseUserObject pUserObject,
126: Point pLabelPosition, java.util.List pPoints,
127: String pIDFrom, String pIDTo, Map pStyle) {
128: ArrayList lModelRoots = new ArrayList();
129: ConnectionSet lConnectionSet = new ConnectionSet();
130: Map lAttributes = new Hashtable();
131: DefaultEdge lEdge = new DefaultEdge(pUserObject);
132:
133: Object lFromPort = getPort(pIDFrom);
134: Object lToPort = getPort(pIDTo);
135:
136: if (lFromPort == null) {
137: lFromPort = addTempVertex(mOldEdgePoint);
138: Point lPoint = (mOldEdgePoint != null) ? new Point(
139: (int) mOldEdgePoint.getX(), (int) mOldEdgePoint
140: .getY()) : new Point(10, 10);
141: if (pPoints == null)
142: pPoints = new ArrayList();
143: pPoints.add(lPoint);
144: }
145: if (lToPort == null) {
146: lToPort = addTempVertex(mOldEdgePoint);
147: Point lPoint = (mOldEdgePoint != null) ? new Point(
148: (int) mOldEdgePoint.getX(), (int) mOldEdgePoint
149: .getY()) : new Point(10, 10);
150: if (pPoints == null)
151: pPoints = new ArrayList();
152: pPoints.add(lPoint);
153: }
154:
155: lConnectionSet.connect(lEdge, lFromPort, lToPort);
156: lAttributes.put(lEdge, pStyle);
157: lModelRoots.add(lEdge);
158: specifyEdgePoints(lEdge, pPoints);
159: insert(lModelRoots.toArray(), lAttributes, lConnectionSet,
160: null, null);
161:
162: if (pLabelPosition != null)
163: GraphConstants.setLabelPosition(lEdge.getAttributes(),
164: pLabelPosition);
165:
166: if (mGraph != null)
167: mGraph.setSelectionCell(lEdge);
168:
169: return lEdge;
170: }
171:
172: // add temporary vertex
173: public DefaultPort addTempVertex(Point2D pPoint) {
174: Point lPoint = (pPoint != null) ? new Point(
175: (int) pPoint.getX(), (int) pPoint.getY()) : new Point(
176: 10, 10);
177: Map lPortAttributes = new Hashtable();
178: DefaultGraphCell lPortCell = new VertexCell("test");
179: DefaultPort lPort = new DefaultPort("test");
180: ArrayList lPortModelRoots = new ArrayList();
181: Map lPortStyle = GraphConstants.createMap();
182:
183: GraphConstants.setBounds(lPortStyle, new Rectangle(lPoint));
184: lPortAttributes.put(lPortCell, lPortStyle);
185: lPortModelRoots.add(lPortCell);
186: lPortCell.add(lPort);
187: insert(lPortModelRoots.toArray(), lPortAttributes, null, null,
188: null);
189:
190: return lPort;
191: }
192:
193: // remove temporary vertexes
194: public void removeTempVertexes() {
195: DefaultGraphCell lCell = findElementCell("test");
196: if (lCell != null) {
197: ArrayList lList = new ArrayList();
198: lList.add(lCell);
199: Set lEdgesSet = BaseGraphModel.getEdges(this , lList
200: .toArray());
201: if (lEdgesSet == null || lEdgesSet.size() == 0)
202: remove(lList.toArray());
203: }
204: }
205:
206: // save graph objects positions
207: public void savePositions() {
208: if (!getChanged() || mDiagram == null)
209: return;
210:
211: try {
212: removeTempVertexes();
213: Application.beginTransaction();
214: try {
215: for (int i = 0; i < getRootCount(); i++) {
216: DefaultGraphCell lObject = (DefaultGraphCell) getRootAt(i);
217: saveCellPosition(lObject);
218: saveCellColor(lObject);
219: }
220: Application.commit();
221: } finally {
222: if (Application.isInTransaction())
223: Application.rollback();
224: }
225: } catch (Exception e) {
226: e.printStackTrace();
227: }
228: setChanged(false);
229: }
230:
231: public void setLoading(boolean pValue) {
232: mLoading = pValue;
233: }
234:
235: // get UserObject Cells
236: public Object[] getUserObjectCells(BaseUserObject pObject) {
237: ArrayList pList = new ArrayList();
238: if (pObject != null) {
239: try {
240: for (int i = 0; i < getRootCount(); i++) {
241: DefaultGraphCell lObject = (DefaultGraphCell) getRootAt(i);
242: Object lCellUserObject = lObject.getUserObject();
243:
244: if (lCellUserObject instanceof BaseUserObject)
245: if (pObject.getClass().equals(
246: lCellUserObject.getClass())
247: && ((BaseUserObject) lCellUserObject)
248: .getID()
249: .equals(pObject.getID()))
250: pList.add(lObject);
251: }
252: } catch (Exception e) {
253: e.printStackTrace();
254: }
255: }
256: return pList.toArray();
257: }
258:
259: public Object[] getUserObjectCells(String pID) {
260: ArrayList pList = new ArrayList();
261: try {
262: for (int i = 0; i < getRootCount(); i++) {
263: DefaultGraphCell lObject = (DefaultGraphCell) getRootAt(i);
264: Object lCellUserObject = lObject.getUserObject();
265:
266: if (lCellUserObject instanceof BaseUserObject)
267: if (((BaseUserObject) lCellUserObject).getID()
268: .equals(pID))
269: pList.add(lObject);
270: }
271: } catch (Exception e) {
272: e.printStackTrace();
273: }
274: return pList.toArray();
275: }
276:
277: // find user object by id
278: public BaseUserObject findUserObjectByID(String pID) {
279: for (int i = 0; i < getRootCount(); i++) {
280: DefaultGraphCell lObject = (DefaultGraphCell) getRootAt(i);
281: Object lCellUserObject = lObject.getUserObject();
282:
283: if (lCellUserObject instanceof BaseUserObject)
284: if (((BaseUserObject) lCellUserObject).getID().equals(
285: pID))
286: return (BaseUserObject) lCellUserObject;
287: }
288: return null;
289: }
290:
291: // get element package
292: public MetaBossModelPackage getPackage() {
293: return BaseUserObject.getObjectPackage(mObject);
294: }
295:
296: // clear edge points
297: protected void clearPoints(ModelElement pEdge) {
298: DiagramModelElement lDiagramElement = getModelElement(pEdge,
299: false);
300: if (lDiagramElement != null) {
301: Object[] lPoints = lDiagramElement.getGraphicFeatures()
302: .toArray();
303: for (int i = 0; i < lPoints.length; i++) {
304: ModelElement lElement = (ModelElement) lPoints[i];
305: if (lElement.getDescription() != null
306: && lElement.getDescription()
307: .equals("EdgePoint")) {
308: lDiagramElement.getGraphicFeatures().remove(
309: lElement);
310: lElement.refDelete();
311: }
312: }
313: }
314: }
315:
316: protected void clearPoints(ModelElement pEdge, String pName) {
317: DiagramModelElement lDiagramElement = getModelElement(pEdge,
318: pName, false);
319: if (lDiagramElement != null) {
320: Object[] lPoints = lDiagramElement.getGraphicFeatures()
321: .toArray();
322: for (int i = 0; i < lPoints.length; i++) {
323: ModelElement lElement = (ModelElement) lPoints[i];
324: if (lElement.getDescription() != null
325: && lElement.getDescription()
326: .equals("EdgePoint")) {
327: lDiagramElement.getGraphicFeatures().remove(
328: lElement);
329: lElement.refDelete();
330: }
331: }
332: }
333: }
334:
335: // save edge points
336: protected void saveEdgePoints(ModelElement pEdge,
337: java.util.List pPoints) {
338: clearPoints(pEdge);
339: DiagramModelElement lDiagramElement = getModelElement(pEdge,
340: true);
341: if (lDiagramElement != null && pPoints != null
342: && pPoints.size() > 2) {
343: for (int z = 1; z < pPoints.size() - 1; z++) {
344: Point2D lPoint = (Point2D) pPoints.get(z);
345: com.metaboss.sdlctools.models.metabossmodel.visualmodel.Point lPosition = getPackage()
346: .getVisualModel().getPoint().createPoint();
347: lPosition.setDescription("EdgePoint");
348: lPosition.setX((int) lPoint.getX());
349: lPosition.setY((int) lPoint.getY());
350: lDiagramElement.getGraphicFeatures().add(lPosition);
351: }
352: }
353: }
354:
355: protected void saveEdgePoints(ModelElement pEdge, String pName,
356: java.util.List pPoints) {
357: clearPoints(pEdge, pName);
358: DiagramModelElement lDiagramElement = getModelElement(pEdge,
359: pName, true);
360: if (lDiagramElement != null && pPoints != null
361: && pPoints.size() > 2) {
362: for (int z = 1; z < pPoints.size() - 1; z++) {
363: Point2D lPoint = (Point2D) pPoints.get(z);
364: com.metaboss.sdlctools.models.metabossmodel.visualmodel.Point lPosition = getPackage()
365: .getVisualModel().getPoint().createPoint();
366: lPosition.setDescription("EdgePoint");
367: lPosition.setX((int) lPoint.getX());
368: lPosition.setY((int) lPoint.getY());
369: lDiagramElement.getGraphicFeatures().add(lPosition);
370: }
371: }
372: }
373:
374: // specify edge points
375: protected void specifyEdgePoints(DefaultEdge pEdge,
376: java.util.List pPoints) {
377: if (pPoints != null && pPoints.size() > 0) {
378: java.util.List lList = GraphConstants.getPoints(pEdge
379: .getAttributes());
380: for (int i = 0; i < pPoints.size(); i++) {
381: Point2D lPosition = (Point2D) pPoints.get(i);
382: if (lPosition != null
383: && !isUndefinedPosition(lPosition))
384: lList.add(lList.size() - 1, pPoints.get(i));
385: }
386: GraphConstants.setPoints(pEdge.getAttributes(), lList);
387: }
388: }
389:
390: // load points for edge
391: protected java.util.List loadEdgePoints(ModelElement pEdge) {
392: java.util.List lResult = new ArrayList();
393: DiagramModelElement lDiagramElement = getModelElement(pEdge,
394: false);
395: if (lDiagramElement != null) {
396: Object[] lPoints = lDiagramElement.getGraphicFeatures()
397: .toArray();
398: for (int i = 0; i < lPoints.length; i++) {
399: ModelElement lElement = (ModelElement) lPoints[i];
400: if (lElement.getDescription() != null
401: && lElement.getDescription()
402: .equals("EdgePoint")) {
403: com.metaboss.sdlctools.models.metabossmodel.visualmodel.Point lPosition = (com.metaboss.sdlctools.models.metabossmodel.visualmodel.Point) lElement;
404: Point lPoint = new Point(lPosition.getX(),
405: lPosition.getY());
406: lResult.add(lPoint);
407: }
408: }
409: }
410: return lResult;
411: }
412:
413: protected java.util.List loadEdgePoints(ModelElement pEdge,
414: String pName) {
415: java.util.List lResult = new ArrayList();
416: DiagramModelElement lDiagramElement = getModelElement(pEdge,
417: pName, false);
418: if (lDiagramElement != null) {
419: Object[] lPoints = lDiagramElement.getGraphicFeatures()
420: .toArray();
421: for (int i = 0; i < lPoints.length; i++) {
422: ModelElement lElement = (ModelElement) lPoints[i];
423: if (lElement.getDescription() != null
424: && lElement.getDescription()
425: .equals("EdgePoint")) {
426: com.metaboss.sdlctools.models.metabossmodel.visualmodel.Point lPosition = (com.metaboss.sdlctools.models.metabossmodel.visualmodel.Point) lElement;
427: Point lPoint = new Point(lPosition.getX(),
428: lPosition.getY());
429: lResult.add(lPoint);
430: }
431: }
432: }
433: return lResult;
434: }
435:
436: // create circled points
437: protected void createCircledPoints(Point pStartPoint,
438: java.util.List pPoints) {
439: if (pStartPoint == null || pPoints == null)
440: return;
441:
442: Point lPoint00 = new Point(pStartPoint);
443: Point lPoint01 = new Point(pStartPoint);
444: Point lPoint11 = new Point(pStartPoint);
445:
446: int lStep = 20;
447:
448: lPoint00.x -= lStep;
449: lPoint00.y -= lStep * 2;
450: lPoint01.x -= lStep * 2;
451: lPoint01.y -= lStep * 2;
452: lPoint11.x -= lStep * 2; //lPoint11.y += 40;
453:
454: pPoints.add(lPoint00);
455: pPoints.add(lPoint01);
456: pPoints.add(lPoint11);
457: }
458:
459: public Map getVertexStyle() {
460: Map lVertexStyle = GraphConstants.createMap();
461:
462: GraphConstants.setSizeable(lVertexStyle, false);
463: GraphConstants.setForeground(lVertexStyle, Color.black);
464: GraphConstants.setFont(lVertexStyle, Application.VERTEX_FONT);
465: GraphConstants.setOpaque(lVertexStyle, false);
466: GraphConstants.setBounds(lVertexStyle, new Rectangle(1, 1));
467: GraphConstants.setAutoSize(lVertexStyle, true);
468:
469: return lVertexStyle;
470: }
471:
472: public Map getAssociationStyle() {
473: Map lAssociationStyle = GraphConstants.createMap();
474:
475: GraphConstants.setBendable(lAssociationStyle, true);
476: GraphConstants.setOpaque(lAssociationStyle, false);
477: GraphConstants.setFont(lAssociationStyle,
478: Application.DEFAULT_FONT);
479: GraphConstants.setLineEnd(lAssociationStyle,
480: GraphConstants.ARROW_SIMPLE);
481: GraphConstants.setLineBegin(lAssociationStyle,
482: GraphConstants.ARROW_NONE);
483:
484: return lAssociationStyle;
485: }
486:
487: public Map getAggregationStyle() {
488: Map lAggregationStyle = GraphConstants.createMap();
489:
490: GraphConstants.setBendable(lAggregationStyle, true);
491: GraphConstants.setLineBegin(lAggregationStyle,
492: GraphConstants.ARROW_DIAMOND);
493: GraphConstants.setBeginFill(lAggregationStyle, false);
494: GraphConstants.setBeginSize(lAggregationStyle, 15);
495: GraphConstants.setLabelPosition(lAggregationStyle, new Point(
496: 500, 1200));
497: GraphConstants.setFont(lAggregationStyle,
498: Application.DEFAULT_FONT);
499: GraphConstants.setOpaque(lAggregationStyle, false);
500: GraphConstants.setLineEnd(lAggregationStyle,
501: GraphConstants.ARROW_NONE);
502:
503: //???GraphConstants.setFont(mAggregationStyle, GraphConstants.defaultFont.deriveFont(10));
504:
505: return lAggregationStyle;
506: }
507:
508: public Map getCompositionStyle() {
509: Map lCompositionStyle = GraphConstants.createMap();
510:
511: GraphConstants.setBendable(lCompositionStyle, true);
512: GraphConstants.setLineBegin(lCompositionStyle,
513: GraphConstants.ARROW_DIAMOND);
514: GraphConstants.setBeginFill(lCompositionStyle, true);
515: GraphConstants.setBeginSize(lCompositionStyle, 15);
516: GraphConstants.setLabelPosition(lCompositionStyle, new Point(
517: 500, 1200));
518: GraphConstants.setFont(lCompositionStyle,
519: Application.DEFAULT_FONT);
520: GraphConstants.setOpaque(lCompositionStyle, false);
521: GraphConstants.setLineEnd(lCompositionStyle,
522: GraphConstants.ARROW_NONE);
523:
524: //???GraphConstants.setFont(mCompositionStyle, GraphConstants.defaultFont.deriveFont(10));
525: //???GraphConstants.setBorderColor(lCompositionStyle, Color.black);
526:
527: return lCompositionStyle;
528: }
529:
530: public Map getGeneralizationStyle() {
531: Map lGeneralizationStyle = GraphConstants.createMap();
532:
533: GraphConstants.setBendable(lGeneralizationStyle, true);
534: //GraphConstants.setLineBegin(mGeneralizationStyle, GraphConstants.ARROW_TECHNICAL);
535: //GraphConstants.setBeginFill(mGeneralizationStyle, false);
536: //GraphConstants.setBeginSize(mGeneralizationStyle, 15);
537: GraphConstants.setLineEnd(lGeneralizationStyle,
538: GraphConstants.ARROW_TECHNICAL);
539: GraphConstants.setEndFill(lGeneralizationStyle, false);
540: GraphConstants.setEndSize(lGeneralizationStyle, 15);
541: GraphConstants.setLabelPosition(lGeneralizationStyle,
542: new Point(500, 1200));
543: GraphConstants.setOpaque(lGeneralizationStyle, false);
544:
545: return lGeneralizationStyle;
546: }
547:
548: public Map getDependencyStyle() {
549: Map lAssociationStyle = GraphConstants.createMap();
550:
551: GraphConstants.setBendable(lAssociationStyle, true);
552: GraphConstants.setOpaque(lAssociationStyle, false);
553: GraphConstants.setFont(lAssociationStyle,
554: Application.DEFAULT_FONT);
555: GraphConstants.setLineEnd(lAssociationStyle,
556: GraphConstants.ARROW_SIMPLE);
557: GraphConstants.setLineBegin(lAssociationStyle,
558: GraphConstants.ARROW_NONE);
559: //GraphConstants.setLineStyle(lAssociationStyle, GraphConstants.STYLE_QUADRATIC);
560: GraphConstants.setDashPattern(lAssociationStyle, new float[] {
561: 6, 2 });
562:
563: return lAssociationStyle;
564: }
565:
566: // find ModelElement graph cell
567: public DefaultGraphCell findElementCell(ModelElement pElement) {
568: try {
569: for (int i = 0; i < getRootCount(); i++) {
570: DefaultGraphCell lObject = (DefaultGraphCell) getRootAt(i);
571: Object lCellUserObject = lObject.getUserObject();
572:
573: if (lCellUserObject instanceof BaseUserObject) {
574: String lID = ((BaseUserObject) lCellUserObject)
575: .getID();
576: if (lID != null && lID.equals(pElement.refMofId()))
577: return lObject;
578: }
579: }
580: } catch (Exception e) {
581: e.printStackTrace();
582: }
583: return null;
584: }
585:
586: // find ModelElement graph cell by User Object
587: public DefaultGraphCell findElementCell(Object pObject) {
588: try {
589: for (int i = 0; i < getRootCount(); i++) {
590: DefaultGraphCell lObject = (DefaultGraphCell) getRootAt(i);
591: Object lCellUserObject = lObject.getUserObject();
592:
593: if (lCellUserObject != null
594: && lCellUserObject.equals(pObject))
595: return lObject;
596: }
597: } catch (Exception e) {
598: e.printStackTrace();
599: }
600: return null;
601: }
602:
603: // process cell change
604: protected void processCellChange(DefaultGraphCell pCell) {
605: //???
606: }
607:
608: // save cell position
609: protected void saveCellPosition(DefaultGraphCell pCell)
610: throws Exception {
611: if (pCell == null)
612: return;
613: Object lCellUserObject = pCell.getUserObject();
614: if (lCellUserObject instanceof BaseUserObject) {
615: ModelElement lElement = ((BaseUserObject) lCellUserObject)
616: .getBOObject();
617: if (pCell instanceof VertexCell) {
618: Rectangle2D lBounds = (Rectangle2D) GraphConstants
619: .getBounds(pCell.getAttributes());
620: setPosition(lElement, new Point((int) lBounds.getX(),
621: (int) lBounds.getY()));
622: } else if (pCell instanceof Edge) {
623: Point lPosition = GraphConstants.getLabelPosition(pCell
624: .getAttributes());
625: setPosition(lElement, lPosition);
626: saveEdgePoints(lElement, GraphConstants.getPoints(pCell
627: .getAttributes()));
628: }
629: }
630: }
631:
632: // save vertex color
633: protected void saveCellColor(DefaultGraphCell pCell)
634: throws Exception {
635: if (pCell != null && pCell instanceof VertexCell
636: && pCell.getUserObject() instanceof BaseUserObject) {
637: BaseUserObject lUserObject = (BaseUserObject) pCell
638: .getUserObject();
639: if (lUserObject != null)
640: setVertexColor(lUserObject.getBOObject(),
641: GraphConstants.getBackground(pCell
642: .getAttributes()));
643: }
644: }
645:
646: // get model element
647: protected DiagramModelElement getModelElement(
648: ModelElement pElement, boolean pCreate) {
649: // find
650: if (mDiagram != null) {
651: Object[] lElements = mDiagram.getElements().toArray();
652: for (int i = 0; i < lElements.length; i++) {
653: DiagramModelElement lElement = (DiagramModelElement) lElements[i];
654: if (lElement.getModelElement() != null
655: && lElement.getModelElement().equals(pElement))
656: return lElement;
657: }
658: }
659: // create
660: if (pCreate) {
661: DiagramModelElement lDiagramElement = getPackage()
662: .getVisualModel().getDiagramModelElement()
663: .createDiagramModelElement();
664: lDiagramElement.setModelElement(pElement);
665: mDiagram.getElements().add(lDiagramElement);
666: return lDiagramElement;
667: }
668: return null;
669: }
670:
671: protected DiagramModelElement getModelElement(
672: ModelElement pElement, String pName, boolean pCreate) {
673: // find
674: if (mDiagram != null) {
675: Object[] lElements = mDiagram.getElements().toArray();
676: for (int i = 0; i < lElements.length; i++) {
677: DiagramModelElement lElement = (DiagramModelElement) lElements[i];
678: if (lElement.getDescription() != null
679: && lElement.getDescription().equals(pName))
680: return lElement;
681: }
682: }
683: // create
684: if (pCreate) {
685: DiagramModelElement lDiagramElement = getPackage()
686: .getVisualModel().getDiagramModelElement()
687: .createDiagramModelElement();
688: lDiagramElement.setDescription(pName);
689: lDiagramElement.setModelElement(pElement);
690: mDiagram.getElements().add(lDiagramElement);
691: return lDiagramElement;
692: }
693: return null;
694: }
695:
696: // get Model Element position
697: protected com.metaboss.sdlctools.models.metabossmodel.visualmodel.Point getElementPosition(
698: DiagramModelElement pElement, boolean pCreate) {
699: Object[] lPoints = pElement.getGraphicFeatures().toArray();
700: for (int i = 0; i < lPoints.length; i++) {
701: ModelElement lElement = (ModelElement) lPoints[i];
702: if (lElement.getName().equals("Anchor"))
703: return (com.metaboss.sdlctools.models.metabossmodel.visualmodel.Point) lElement;
704: }
705: if (pCreate) {
706: com.metaboss.sdlctools.models.metabossmodel.visualmodel.Point lPosition = getPackage()
707: .getVisualModel().getPoint().createPoint();
708: lPosition.setName("Anchor");
709: pElement.getGraphicFeatures().add(lPosition);
710: return lPosition;
711: }
712: return null;
713: }
714:
715: // get Color
716: protected com.metaboss.sdlctools.models.metabossmodel.visualmodel.Color getElementColor(
717: DiagramModelElement pElement, boolean pCreate) {
718: Object[] lPoints = pElement.getGraphicFeatures().toArray();
719: for (int i = 0; i < lPoints.length; i++) {
720: ModelElement lElement = (ModelElement) lPoints[i];
721: if (lElement.getName().equals("Color"))
722: return (com.metaboss.sdlctools.models.metabossmodel.visualmodel.Color) lElement;
723: }
724: if (pCreate) {
725: com.metaboss.sdlctools.models.metabossmodel.visualmodel.Color lColor = getPackage()
726: .getVisualModel().getColor().createColor();
727: lColor.setName("Color");
728: pElement.getGraphicFeatures().add(lColor);
729: return lColor;
730: }
731: return null;
732: }
733:
734: // set position
735: protected void setPosition(ModelElement pElement, Point pPosition) {
736: DiagramModelElement lDiagramElement = getModelElement(pElement,
737: true);
738: if (lDiagramElement != null) {
739: com.metaboss.sdlctools.models.metabossmodel.visualmodel.Point lPosition = getElementPosition(
740: lDiagramElement, true);
741: lPosition.setX(pPosition.x);
742: lPosition.setY(pPosition.y);
743: }
744: }
745:
746: // get position
747: protected Point getPosition(ModelElement pElement) {
748: DiagramModelElement lDiagramElement = getModelElement(pElement,
749: false);
750: if (lDiagramElement != null) {
751: com.metaboss.sdlctools.models.metabossmodel.visualmodel.Point lPosition = getElementPosition(
752: lDiagramElement, false);
753: if (lPosition != null)
754: return new Point(lPosition.getX(), lPosition.getY());
755: }
756: return null;
757: }
758:
759: // set vertex color
760: protected void setVertexColor(ModelElement pElement, Color pColor) {
761: if (pElement != null && pColor != null) {
762: DiagramModelElement lDiagramElement = getModelElement(
763: pElement, true);
764: if (lDiagramElement != null) {
765: com.metaboss.sdlctools.models.metabossmodel.visualmodel.Color lColor = getElementColor(
766: lDiagramElement, true);
767: ColorUtils.fromAwtColor(lColor, pColor);
768: }
769: }
770: }
771:
772: // get vertex color
773: protected Color getVertexColor(ModelElement pElement) {
774: DiagramModelElement lDiagramElement = getModelElement(pElement,
775: false);
776: if (lDiagramElement != null) {
777: com.metaboss.sdlctools.models.metabossmodel.visualmodel.Color lColor = getElementColor(
778: lDiagramElement, false);
779: if (lColor != null)
780: return ColorUtils.toAwtColor(lColor);
781: }
782: return null;
783: }
784:
785: // get port
786: protected Object getPort(String pID) {
787: if (mVertexPorts != null && pID != null)
788: return mVertexPorts.get(pID);
789: else
790: return null;
791: }
792:
793: // set graph model listener
794: public void setModelListener() {
795: addGraphModelListener(mModelListener);
796: }
797:
798: // remove graph model listener
799: public void removeModelListener() {
800: removeGraphModelListener(mModelListener);
801: }
802:
803: // load vertexeses positins
804: /*protected void loadVertexesPositions() throws Exception
805: {
806: if (mDiagram==null) return;
807:
808: Object[] lObjects = getVertexesObjects();
809: if (lObjects!=null && lObjects.length>0)
810: {
811: int lMaxY = 0;
812: boolean lHasUnplacedEntities = false;
813:
814: for (int i=0; i<lObjects.length; i++)
815: {
816: Point lPosition = getPosition((ModelElement)lObjects[i]);
817: if (lPosition!=null)
818: {
819: if (lMaxY<((int)lPosition.getY()))
820: lMaxY = (int)lPosition.getY();
821: }
822: else
823: lHasUnplacedEntities = true;
824: }
825: // Calculate missing cell positions if necessary
826: if (lHasUnplacedEntities == true)
827: {
828: int lNextY = lMaxY + 100;
829: int lNextX = 10;
830: Application.beginTransaction();
831: try
832: {
833: for (int i=0; i<lObjects.length; i++)
834: {
835: Point lPosition = getPosition((ModelElement)lObjects[i]);
836: if (lPosition == null)
837: {
838: lPosition = new Point(lNextX, lNextY);
839: lNextX += 70;
840: setPosition((ModelElement)lObjects[i], lPosition);
841: }
842: }
843: Application.commit();
844: }
845: finally
846: {
847: if (Application.isInTransaction()) Application.rollback();
848: }
849: }
850: }
851: }*/
852:
853: /*protected Object[] getVertexesObjects()
854: {
855: return null;
856: }*/
857:
858: /* properties */
859:
860: public boolean getChanged() {
861: return mChanged;
862: }
863:
864: public void setChanged(boolean pValue) {
865: mChanged = pValue;
866: }
867:
868: public ModelElement getBOObject() {
869: return mObject;
870: }
871:
872: /* Auxilary classes */
873:
874: public class DesignGraphModelListener implements GraphModelListener {
875: public void graphChanged(GraphModelEvent pEvent) {
876: Object[] pChanged = pEvent.getChange().getChanged();
877: if (!mLoading) {
878: setChanged(true);
879: for (int i = 0; i < pChanged.length; i++) {
880: DefaultGraphCell lChangedObject = (DefaultGraphCell) pChanged[i];
881: processCellChange(lChangedObject);
882: }
883: }
884:
885: /*try
886: {
887: javax.naming.Context lContext = new javax.naming.InitialContext();
888: BOTransaction lTx = null;
889: try
890: {
891: lTx = (BOTransaction)lContext.lookup(BOTransaction.COMPONENT_URL);
892: mLayout.beginEdit(lTx);
893:
894: Object[] pChanged = pEvent.getChange().getChanged();
895: for (int i = 0; i < pChanged.length; i++)
896: {
897: DefaultGraphCell lChangedObject = (DefaultGraphCell)pChanged[i];
898: Object lChangedCellUserObject = lChangedObject.getUserObject();
899: // Funny thing is that the new attributes stored in what is called previous ones
900: Map lNewAttributes = (Map)pEvent.getChange().getPreviousAttributes().get(lChangedObject);
901: Map lOldAttributes = (Map)pEvent.getChange().getAttributes().get(lChangedObject);
902: //if (lChangedCellUserObject instanceof DomainCellUserObject)
903: //{
904: // Domain object can only change position
905: // Rectangle lNewBounds = GraphConstants.getBounds(lNewAttributes);
906: // Rectangle lOldBounds = GraphConstants.getBounds(lOldAttributes);
907: // if (lNewBounds != null && (lOldBounds == null || (!lNewBounds.getLocation().equals(lOldBounds.getLocation()))))
908: // mLayout.setPosition("Home", ((DomainCellUserObject)lChangedCellUserObject).getDomain().getRef(),lNewBounds.getLocation());
909: //}
910: //else
911: if (lChangedCellUserObject instanceof EntityNodeUserObject)
912: {
913: // Entity object can change position
914: Rectangle lNewBounds = GraphConstants.getBounds(lNewAttributes);
915: Rectangle lOldBounds = GraphConstants.getBounds(lOldAttributes);
916: if (lNewBounds!=null && (lOldBounds==null || (!lNewBounds.getLocation().equals(lOldBounds.getLocation()))))
917: mLayout.setPosition("Entity", ((EntityNodeUserObject)lChangedCellUserObject).getEntity().getRef(),lNewBounds.getLocation());
918: } else
919: if (lChangedCellUserObject instanceof AssociationNodeUserObject)
920: {
921: Point lNewPosition = GraphConstants.getLabelPosition(lNewAttributes);
922: Point lOldPosition = GraphConstants.getLabelPosition(lOldAttributes);
923: if (lNewPosition!=null && (lOldPosition==null || (!lNewPosition.equals(lOldPosition))))
924: mLayout.setPosition("Association", ((AssociationNodeUserObject)lChangedCellUserObject).getAssociation().getRef(), lNewPosition);
925: }
926: }
927: // Commit what we have got
928: lTx.doCommit();
929: lTx = null;
930: }
931: finally
932: {
933: if (lTx != null)
934: lTx.
935: }
936: }
937: catch(Exception e)
938: {
939: e.printStackTrace();
940: }*/
941: /*{
942: //Object[] pChanged = pEvent.getChange().getChanged();
943: for (int i = 0; i < pChanged.length; i++)
944: {
945: Object lChangedObject = pChanged[i];
946: if (lChangedObject!=null)
947: {
948: System.out.println(" Object " + i + ":");
949: System.out.println(" Class : " + lChangedObject.getClass().getName());
950: System.out.println(" User : " + ((DefaultGraphCell)lChangedObject).getUserObject().getClass().getName());
951: System.out.println(" ==== Previous attributes");
952: {
953: Map lNewAttributes = null;
954: try
955: {
956: lNewAttributes = (Map)pEvent.getChange().getPreviousAttributes().get(lChangedObject);
957: }
958: catch (Exception e)
959: {
960: lNewAttributes = null;
961: }
962: if (lNewAttributes == null)
963: {
964: System.out.println(" No attributes map found for the object");
965: continue;
966: }
967: Rectangle lBounds = GraphConstants.getBounds(lNewAttributes);
968: if (lBounds == null)
969: System.out.println(" No bounds stored for the object");
970: else
971: System.out.println(" Bounds : X(" + (int)lBounds.getX() + ") Y(" + (int)lBounds.getY() + ")");
972: }
973: System.out.println(" ==== Attributes");
974: {
975: Map lNewAttributes = (Map)pEvent.getChange().getAttributes().get(lChangedObject);
976: if (lNewAttributes == null)
977: {
978: System.out.println(" No attributes map found for the object");
979: continue;
980: }
981: Rectangle lBounds = GraphConstants.getBounds(lNewAttributes);
982: if (lBounds == null)
983: System.out.println(" No bounds stored for the object");
984: else
985: System.out.println(" Bounds : X(" + (int)lBounds.getX() + ") Y(" + (int)lBounds.getY() + ")");
986: }
987: }
988: }
989: }*/
990: }
991: }
992: }
|