001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.uml.ui.products.ad.viewfactory;
043:
044: import java.awt.Graphics;
045: import java.util.List;
046:
047: import org.netbeans.modules.uml.common.ETException;
048: import org.netbeans.modules.uml.core.metamodel.core.foundation.IElement;
049: import org.netbeans.modules.uml.core.metamodel.core.foundation.INamespace;
050: import org.netbeans.modules.uml.core.metamodel.core.foundation.IPresentationElement;
051: import org.netbeans.modules.uml.core.support.umlsupport.IETPoint;
052: import org.netbeans.modules.uml.core.support.umlsupport.IETRect;
053: import org.netbeans.modules.uml.core.support.umlsupport.IStrings;
054: import org.netbeans.modules.uml.ui.products.ad.drawengines.ETDrawEngineFactory;
055: import org.netbeans.modules.uml.ui.products.ad.graphobjects.ETEdgeLabel;
056: import org.netbeans.modules.uml.ui.support.ElementReloader;
057: import org.netbeans.modules.uml.ui.support.applicationmanager.DrawingFactory;
058: import org.netbeans.modules.uml.ui.support.applicationmanager.IGraphPresentation;
059: import org.netbeans.modules.uml.ui.support.applicationmanager.ILabelPresentation;
060: import org.netbeans.modules.uml.ui.support.archivesupport.IProductArchive;
061: import org.netbeans.modules.uml.ui.support.archivesupport.IProductArchiveDefinitions;
062: import org.netbeans.modules.uml.ui.support.archivesupport.IProductArchiveElement;
063: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IDrawEngine;
064: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IDrawInfo;
065: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IETGraphObject;
066: import org.netbeans.modules.uml.ui.support.viewfactorysupport.ITSGraphObject;
067: import org.netbeans.modules.uml.ui.support.viewfactorysupport.TypeConversions;
068: import org.netbeans.modules.uml.ui.swing.drawingarea.IDrawingAreaControl;
069: import com.tomsawyer.editor.TSEEdgeLabel;
070: import com.tomsawyer.editor.TSEGraphWindow;
071: import com.tomsawyer.editor.TSEObjectUI;
072: import com.tomsawyer.editor.graphics.TSEGraphics;
073: import com.tomsawyer.editor.ui.TSELabelUI;
074: import com.tomsawyer.graph.TSGraphObject;
075: import com.tomsawyer.util.TSProperty;
076:
077: public class ETGenericEdgeLabelUI extends TSELabelUI implements
078: IETGraphObjectUI {
079:
080: private final String ZERO_VALUE = "";
081:
082: private String peidValue = this .ZERO_VALUE;
083: private String meidValue = this .ZERO_VALUE;
084: private String topLevelMEIDValue = this .ZERO_VALUE;
085: private String initStringValue = this .ZERO_VALUE;
086: private String m_ReloadedOwnerPresentationXMIID = this .ZERO_VALUE;
087:
088: private IElement modelElement = null;
089: private IDrawEngine drawEngine = null;
090: private String drawEngineClass = null;
091: private IStrings m_PresentationReferenceReferredElements = null;
092: private boolean m_WasModelElementDeleted = false;
093: private boolean m_FailedToCreateDrawEngine = false;
094:
095: private int m_Placement = 0;
096: private int m_LabelKind = 0;
097: /// Logical offset from the top left of the draw engine for the location of the label
098: private IETPoint m_ptSpecifiedXY;
099:
100: public ETGenericEdgeLabelUI() {
101: super ();
102: }
103:
104: private boolean initDrawEngine() throws ETException {
105: if (this .getInitStringValue() != null
106: && getInitStringValue().length() > 0) {
107: this .setDrawEngine(ETDrawEngineFactory
108: .createDrawEngine(this ));
109: return this .drawEngine != null;
110: } else
111: return false;
112: }
113:
114: public IDrawEngine getDrawEngine() {
115: if (drawEngine == null) {
116: try {
117: this .initDrawEngine();
118: } catch (Exception e) {
119: return null;
120: }
121: }
122:
123: return drawEngine;
124: }
125:
126: public void setDrawEngine(IDrawEngine newDrawEngine) {
127: try {
128: if (newDrawEngine != drawEngine) {
129: drawEngine = newDrawEngine;
130: // if (drawEngine != null)
131: // drawEngine.init();
132: }
133: } catch (Exception e) {
134: e.printStackTrace();
135: }
136: }
137:
138: public void draw(TSEGraphics graphics) {
139: if (getDrawEngine() != null) {
140: IDrawInfo drawInfo = getDrawInfo(graphics);
141: if (getDrawEngine().getParent() == null)
142: setDrawEngine(null);
143: else if (drawInfo != null)
144: getDrawEngine().doDraw(drawInfo);
145: }
146: }
147:
148: /**
149: * Retrieves the graphics context for the node ui.
150: *
151: * @return The graphics context.
152: */
153: public IDrawInfo getDrawInfo() {
154: IDrawInfo retVal = null;
155:
156: TSEGraphWindow window = getGraphWindow();
157: if (window != null) {
158: Graphics g = window.getGraphics();
159: if (g instanceof TSEGraphics) {
160: TSEGraphics tsGraphics = (TSEGraphics) g;
161: retVal = getDrawInfo(tsGraphics);
162:
163: }
164: }
165:
166: return retVal;
167: }
168:
169: /*
170: * Returns the GraphWindow.
171: */
172: public TSEGraphWindow getGraphWindow() {
173: return ETBaseUI.getGraphWindow(this );
174: }
175:
176: /*
177: * Returns World points, (Logical)
178: */
179: public IETRect getLogicalBounds() {
180: return ETBaseUI.getLogicalBounds(this );
181: }
182:
183: /*
184: * Returns the device bounding rect.
185: */
186: public IETRect getDeviceBounds() {
187: return ETBaseUI.getDeviceBounds(this );
188: }
189:
190: /**
191: * Retrieves the graphics context for the node ui.
192: *
193: * @param graphics The TS graphics class.
194: * @return The graphics context.
195: */
196: public IDrawInfo getDrawInfo(TSEGraphics graphics) {
197: IDrawInfo retVal = ETBaseUI.getDrawInfo(graphics, this );
198:
199: if (retVal != null) {
200: retVal.setIsTransparent(isTransparent());
201: retVal.setIsBorderDrawn(isBorderDrawn());
202: }
203:
204: // TODO: Determine what the DrawinToMainDrawingArea and AlwaysSetFont
205: // Should be set to.
206:
207: return retVal;
208: }
209:
210: /*
211: * (non-Javadoc)
212: * @see org.netbeans.modules.uml.ui.products.ad.viewfactory.IETGraphObjectUI#getDrawingArea()
213: */
214: public IDrawingAreaControl getDrawingArea() {
215: return ETBaseUI.getDrawingArea(this );
216: }
217:
218: /*
219: * (non-Javadoc)
220: * @see org.netbeans.modules.uml.ui.products.ad.viewfactory.IETGraphObjectUI#setDrawingArea(org.netbeans.modules.uml.ui.swing.drawingarea.IDrawingAreaControl)
221: */
222: public void setDrawingArea(IDrawingAreaControl control) {
223: //drawingArea = control;
224: }
225:
226: /*
227: * (non-Javadoc)
228: * @see org.netbeans.modules.uml.ui.products.ad.viewfactory.IETGraphObjectUI#getDrawEngineClass()
229: */
230: public String getDrawEngineClass() {
231: return drawEngineClass;
232: }
233:
234: /*
235: * (non-Javadoc)
236: * @see org.netbeans.modules.uml.ui.products.ad.viewfactory.IETGraphObjectUI#setDrawEngineClass(java.lang.String)
237: */
238: public void setDrawEngineClass(String string) {
239: drawEngineClass = string;
240: }
241:
242: /*
243: * (non-Javadoc)
244: * @see org.netbeans.modules.uml.ui.products.ad.viewfactory.IETGraphObjectUI#getModelElement()
245: */
246: public IElement getModelElement() {
247: return modelElement;
248: }
249:
250: /*
251: * (non-Javadoc)
252: * @see org.netbeans.modules.uml.ui.products.ad.viewfactory.IETGraphObjectUI#setModelElement(org.netbeans.modules.uml.core.metamodel.core.foundation.IElement)
253: */
254: public void setModelElement(IElement element) {
255: modelElement = element;
256: }
257:
258: /*
259: * (non-Javadoc)
260: * @see org.netbeans.modules.uml.ui.products.ad.viewfactory.IETGraphObjectUI#getInitStringValue()
261: */
262: public String getInitStringValue() {
263: return this .initStringValue;
264: }
265:
266: /*
267: * (non-Javadoc)
268: * @see org.netbeans.modules.uml.ui.products.ad.viewfactory.IETGraphObjectUI#setInitStringValue(java.lang.String)
269: */
270: public void setInitStringValue(String string) {
271: this .initStringValue = string;
272: }
273:
274: /*
275: * (non-Javadoc)
276: * @see org.netbeans.modules.uml.ui.products.ad.viewfactory.IETGraphObjectUI#getTSObject()
277: */
278: public ITSGraphObject getTSObject() {
279: return getOwner() instanceof ITSGraphObject ? (ITSGraphObject) getOwner()
280: : null;
281: }
282:
283: /// Returns the Model Element XMIID that was loaded from the file.
284: public String getReloadedModelElementXMIID() {
285: return meidValue;
286: }
287:
288: /// Sets the Model Element XMIID that will be persisted to the file.
289: public void setReloadedModelElementXMIID(String newVal) {
290: meidValue = newVal;
291: }
292:
293: /// Returns the Toplevel Element XMIID that was loaded from the file.
294: public String getReloadedTopLevelXMIID() {
295: return topLevelMEIDValue;
296: }
297:
298: /// Sets the Toplevel Element XMIID that will be persisted to the file.
299: public void setReloadedTopLevelXMIID(String newVal) {
300: topLevelMEIDValue = newVal;
301: }
302:
303: /// Returns the Toplevel Element XMIID that was loaded from the file.
304: public String getReloadedPresentationXMIID() {
305: return peidValue;
306: }
307:
308: /// Sets the Toplevel Element XMIID that will be persisted to the file.
309: public void setReloadedPresentationXMIID(String newVal) {
310: peidValue = newVal;
311: }
312:
313: /// Returns the Toplevel Element XMIID that was loaded from the file.
314: public String getReloadedOwnerPresentationXMIID() {
315: return m_ReloadedOwnerPresentationXMIID;
316: }
317:
318: /// Sets the Toplevel Element XMIID that will be persisted to the file.
319: public void setReloadedOwnerPresentationXMIID(String newVal) {
320: m_ReloadedOwnerPresentationXMIID = newVal;
321: }
322:
323: public IStrings getReferredElements() {
324: return m_PresentationReferenceReferredElements;
325: }
326:
327: public void setReferredElements(IStrings newVal) {
328: m_PresentationReferenceReferredElements = newVal;
329: }
330:
331: public void readFromArchive(IProductArchive prodArch,
332: IProductArchiveElement archEle) {
333: m_LabelKind = (int) archEle
334: .getAttributeLong(IProductArchiveDefinitions.LABELVIEW_TSLABELKIND);
335: m_Placement = (int) archEle
336: .getAttributeLong(IProductArchiveDefinitions.LABELVIEW_TSLABELPLACEMENTKIND);
337:
338: //read from archive using the common static method
339: ETBaseUI.readFromArchive(prodArch, archEle, this );
340:
341: // Create the presentation element and hook things up
342: IETGraphObject etObj = (IETGraphObject) getTSObject();
343: if (etObj != null) {
344: IPresentationElement pPresEle = ((IETGraphObject) getTSObject())
345: .getPresentationElement();
346: if (pPresEle != null) {
347: if (meidValue.length() > 0
348: && topLevelMEIDValue.length() > 0) {
349: // Reattach to this presentation element
350: ElementReloader reloader = new ElementReloader();
351: IElement modEle = reloader.getElement(
352: topLevelMEIDValue, meidValue);
353: if (modEle != null
354: && pPresEle instanceof IGraphPresentation) {
355: ((IGraphPresentation) pPresEle)
356: .setModelElement(modEle);
357: }
358:
359: ETBaseUI.createDrawEngineOneTime(this );
360:
361: // Allow the engine to read in any specific engine stuff
362: if (drawEngine != null) {
363: IProductArchiveElement foundEngEle = archEle
364: .getElement(IProductArchiveDefinitions.ENGINENAMEELEMENT_STRING);
365: if (foundEngEle != null) {
366: drawEngine.readFromArchive(prodArch,
367: foundEngEle);
368: }
369: }
370: }
371: }
372: }
373: }
374:
375: /**
376: * Saves the basic label stuff to the product archive
377: *
378: * @param pProductArchive [in] The archive we're saving to
379: * @param pElement [in] The current element, or parent for any new attributes or elements.
380: */
381: public void writeToArchive(IProductArchive prodArch,
382: IProductArchiveElement archEle) {
383: // Write the label kind
384: archEle.addAttributeLong(
385: IProductArchiveDefinitions.LABELVIEW_TSLABELKIND,
386: (int) m_LabelKind);
387:
388: // Write the placement
389: archEle
390: .addAttributeLong(
391: IProductArchiveDefinitions.LABELVIEW_TSLABELPLACEMENTKIND,
392: (int) m_Placement);
393:
394: ETBaseUI.writeToArchive(prodArch, archEle, this );
395: }
396:
397: /**
398: * Gets the IPresentationElement
399: */
400: public IPresentationElement createPresentationElement(IElement pElem) {
401: IPresentationElement retObj = null;
402: ILabelPresentation labelPres = DrawingFactory
403: .retrieveLabelPresentationMetaType();
404: if (labelPres != null) {
405: String xmiid = getReloadedPresentationXMIID();
406: if (xmiid != null && xmiid.length() > 0) {
407: // Reset the presentation XMIID to what it was last time.
408: labelPres.setXMIID(xmiid);
409: }
410:
411: // Assign the TS element to the presentation element
412: TSEEdgeLabel label = (TSEEdgeLabel) getOwnerLabel();
413: if (label != null) {
414: labelPres.setTSLabel(label);
415: }
416:
417: // Hook up the default model element (the parent's).
418: // This can be overriden in LabelPresentationImpl::put_ModelElement)
419: IPresentationElement parentPE = getParentPresentationElement();
420: if (parentPE != null) {
421: IElement elem = parentPE.getFirstSubject();
422: if (elem != null) {
423: labelPres.addSubject(elem);
424: }
425: }
426: retObj = labelPres;
427: }
428: return retObj;
429: }
430:
431: /**
432: * Gets the parent's IPresentationElement
433: */
434: private IPresentationElement getParentPresentationElement() {
435: IPresentationElement retObj = null;
436: IETGraphObject pPE = getParentETElement();
437: if (pPE != null) {
438: retObj = pPE.getPresentationElement();
439: }
440: return retObj;
441: }
442:
443: /**
444: * Gets the parent's IETElement
445: */
446: private IETGraphObject getParentETElement() {
447: IETGraphObject retObj = null;
448: TSEEdgeLabel label = (TSEEdgeLabel) getOwnerLabel();
449: TSGraphObject obj = label.getOwner();
450: if (obj != null) {
451: retObj = TypeConversions.getETGraphObject(obj);
452: }
453: return retObj;
454: }
455:
456: public boolean getWasModelElementDeleted() {
457: return m_WasModelElementDeleted;
458: }
459:
460: public void setWasModelElementDeleted(boolean newVal) {
461: m_WasModelElementDeleted = newVal;
462: }
463:
464: public boolean getFailedToCreateDrawEngine() {
465: return m_FailedToCreateDrawEngine;
466: }
467:
468: public void setFailedToCreateDrawEngine(boolean newVal) {
469: m_FailedToCreateDrawEngine = newVal;
470: }
471:
472: public IElement createNew(INamespace space, String initStr) {
473: IElement retEle = null;
474: IPresentationElement presEle = createPresentationElement(null);
475: ((ETEdgeLabel) this .getTSObject())
476: .setPresentationElement(presEle);
477: if (presEle != null) {
478: retEle = presEle.getFirstSubject();
479: }
480: return retEle;
481: }
482:
483: /* (non-Javadoc)
484: * @see com.tomsawyer.editor.ui.TSEAnnotatedUI#isAnnotationEditable()
485: */
486: public boolean isAnnotationEditable() {
487: // TODO Auto-generated method stub
488: return false;
489: }
490:
491: /* (non-Javadoc)
492: * @see org.netbeans.modules.uml.ui.products.ad.viewfactory.IETGraphObjectUI#getTopLevelMEIDValue()
493: */
494: public String getTopLevelMEIDValue() {
495: // TODO Auto-generated method stub
496: return null;
497: }
498:
499: /*
500: * (non-Javadoc)
501: * @see org.netbeans.modules.uml.ui.products.ad.viewfactory.IETGraphObjectUI#isOnTheScreen(com.tomsawyer.editor.graphics.TSEGraphics)
502: */
503: public boolean isOnTheScreen(TSEGraphics g) {
504: return ETBaseUI.isOnTheScreen(g, this );
505: }
506:
507: public void reset() {
508: super .reset();
509: this .peidValue = this .ZERO_VALUE;
510: this .meidValue = this .ZERO_VALUE;
511: this .topLevelMEIDValue = this .ZERO_VALUE;
512: this .initStringValue = this .ZERO_VALUE;
513: }
514:
515: public void copy(TSEObjectUI sourceUI) {
516: super .copy(sourceUI);
517: ETGenericEdgeLabelUI sourceNodeUI = (ETGenericEdgeLabelUI) sourceUI;
518: this .peidValue = sourceNodeUI.peidValue;
519: this .meidValue = sourceNodeUI.meidValue;
520: this .topLevelMEIDValue = sourceNodeUI.topLevelMEIDValue;
521:
522: this .setInitStringValue(sourceNodeUI.getInitStringValue());
523: this .setDrawEngineClass(sourceNodeUI.getDrawEngineClass());
524: }
525:
526: public List getProperties() {
527: /* jyothi
528: List list = super.getProperties();
529: list.add(new TSProperty(IProductArchiveDefinitions.PRESENTATIONELEMENTID_STRING, this.peidValue));
530: return list;
531: */
532: List list = super .getProperties();
533: String value = null;
534: IPresentationElement pPE = ((IETGraphObject) getTSObject())
535: .getPresentationElement();
536: if (pPE != null) {
537: // Get the presentation el ement id
538: String presEleId = pPE.getXMIID();
539: if (presEleId != null && presEleId.length() > 0) {
540: value = presEleId;
541: }
542: }
543:
544: list
545: .add(new TSProperty(
546: IProductArchiveDefinitions.PRESENTATIONELEMENTID_STRING,
547: value));
548: return list;
549:
550: }
551:
552: public void setProperty(TSProperty property) {
553: //String attrString = (String) property.getValue(); //jyothi
554: if (IProductArchiveDefinitions.PRESENTATIONELEMENTID_STRING
555: .equals(property.getName())) {
556: String attrString = (String) property.getValue(); //jyothi
557: this .peidValue = attrString;
558: } else {
559: super .setProperty(property);
560: }
561: }
562:
563: public List getChangedProperties() {
564: List list = super .getChangedProperties();
565:
566: IPresentationElement pPE = ((IETGraphObject) getTSObject())
567: .getPresentationElement();
568: if (pPE != null) {
569: // Get the presentation element id
570: String presEleId = pPE.getXMIID();
571: if (presEleId != null && presEleId.length() > 0) {
572: list
573: .add(new TSProperty(
574: IProductArchiveDefinitions.PRESENTATIONELEMENTID_STRING,
575: presEleId));
576: }
577: }
578: return list;
579: }
580:
581: public int getLabelKind() {
582: return m_LabelKind;
583: }
584:
585: public int getPlacement() {
586: return m_Placement;
587: }
588:
589: public IETPoint getSpecifiedXY() {
590: return m_ptSpecifiedXY;
591: }
592:
593: public void setLabelKind(int l) {
594: m_LabelKind = l;
595: }
596:
597: public void setPlacement(int l) {
598: m_Placement = l;
599: }
600:
601: public void setSpecifiedXY(IETPoint point) {
602: m_ptSpecifiedXY = point;
603: }
604:
605: }
|