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: /*
043: * Created on May 16, 2003
044: *
045: */
046: package org.netbeans.modules.uml.ui.products.ad.viewfactory;
047:
048: import java.awt.Graphics;
049: import java.util.List;
050:
051: import org.netbeans.modules.uml.common.ETException;
052: import org.netbeans.modules.uml.core.metamodel.core.foundation.IElement;
053: import org.netbeans.modules.uml.core.metamodel.core.foundation.INamespace;
054: import org.netbeans.modules.uml.core.metamodel.core.foundation.IPresentationElement;
055: import org.netbeans.modules.uml.core.support.umlsupport.IETPoint;
056: import org.netbeans.modules.uml.core.support.umlsupport.IETRect;
057: import org.netbeans.modules.uml.core.support.umlsupport.IStrings;
058: import org.netbeans.modules.uml.ui.products.ad.drawengines.ETDrawEngineFactory;
059: import org.netbeans.modules.uml.ui.products.ad.graphobjects.ETNodeLabel;
060: import org.netbeans.modules.uml.ui.support.ElementReloader;
061: import org.netbeans.modules.uml.ui.support.applicationmanager.DrawingFactory;
062: import org.netbeans.modules.uml.ui.support.applicationmanager.IGraphPresentation;
063: import org.netbeans.modules.uml.ui.support.applicationmanager.ILabelPresentation;
064: import org.netbeans.modules.uml.ui.support.archivesupport.IProductArchive;
065: import org.netbeans.modules.uml.ui.support.archivesupport.IProductArchiveDefinitions;
066: import org.netbeans.modules.uml.ui.support.archivesupport.IProductArchiveElement;
067: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IDrawEngine;
068: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IDrawInfo;
069: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IETGraphObject;
070: import org.netbeans.modules.uml.ui.support.viewfactorysupport.ITSGraphObject;
071: import org.netbeans.modules.uml.ui.support.viewfactorysupport.TypeConversions;
072: import org.netbeans.modules.uml.ui.swing.drawingarea.IDrawingAreaControl;
073: import com.tomsawyer.drawing.TSNodeLabel;
074: import com.tomsawyer.editor.TSEGraphWindow;
075: import com.tomsawyer.editor.TSENode;
076: import com.tomsawyer.editor.TSENodeLabel;
077: import com.tomsawyer.editor.TSEObjectUI;
078: import com.tomsawyer.editor.graphics.TSEGraphics;
079: import com.tomsawyer.editor.ui.TSELabelUI;
080: import com.tomsawyer.graph.TSGraphObject;
081: import com.tomsawyer.util.TSProperty;
082:
083: public class ETGenericNodeLabelUI extends TSELabelUI implements
084: IETGraphObjectUI {
085:
086: private final String ZERO_VALUE = "";
087:
088: private String peidValue = this .ZERO_VALUE;
089: private String meidValue = this .ZERO_VALUE;
090: private String topLevelMEIDValue = this .ZERO_VALUE;
091: private String initStringValue = this .ZERO_VALUE;
092: private String m_ReloadedOwnerPresentationXMIID = this .ZERO_VALUE;
093:
094: private IElement modelElement = null;
095: private IDrawEngine drawEngine = null;
096: private String drawEngineClass = null;
097: private IStrings m_PresentationReferenceReferredElements = null;
098: private boolean m_WasModelElementDeleted = false;
099: private boolean m_FailedToCreateDrawEngine = false;
100:
101: private int m_Placement = 0;
102: private int m_LabelKind = 0;
103: /// Logical offset from the top left of the draw engine for the location of the label
104: private IETPoint m_ptSpecifiedXY;
105:
106: public ETGenericNodeLabelUI() {
107: super ();
108:
109: }
110:
111: public void drawCalloutOutline(TSEGraphics graphics) {
112: // Fix J1075: TS will sometimes throw here with a null pointer exception
113: // This was seen when moving the combined fragment interaction constraint labels.
114: // This is also what causes the label to not be drawn while the user is moving it.
115: // see also ADDrawingAreaSelectState.onMouseDragged() and
116: // ADMoveSelectedState.onMouseDragged()
117: try {
118: super .drawCalloutOutline(graphics);
119: } catch (NullPointerException error) {
120: }
121: }
122:
123: private boolean initDrawEngine() throws ETException {
124: if (this .getInitStringValue() != null
125: && getInitStringValue().length() > 0) {
126: this .setDrawEngine(ETDrawEngineFactory
127: .createDrawEngine(this ));
128: return this .drawEngine != null;
129: } else
130: return false;
131: }
132:
133: public IDrawEngine getDrawEngine() {
134: if (drawEngine == null) {
135: try {
136: this .initDrawEngine();
137: } catch (Exception e) {
138: return null;
139: }
140: }
141:
142: return drawEngine;
143: }
144:
145: /* (non-Javadoc)
146: * @see org.netbeans.modules.uml.ui.products.ad.viewfactory.IETGraphObjectUI#getDrawEngineClass()
147: */
148: public String getDrawEngineClass() {
149: return drawEngineClass;
150: }
151:
152: /* (non-Javadoc)
153: * @see org.netbeans.modules.uml.ui.products.ad.viewfactory.IETGraphObjectUI#getDrawInfo()
154: */
155: public IDrawInfo getDrawInfo() {
156: IDrawInfo retVal = null;
157:
158: TSEGraphWindow window = getGraphWindow();
159: if (window != null) {
160: Graphics g = window.getGraphics();
161: if (g instanceof TSEGraphics) {
162: TSEGraphics tsGraphics = (TSEGraphics) g;
163: retVal = getDrawInfo(tsGraphics);
164:
165: }
166: }
167:
168: return retVal;
169: }
170:
171: /*
172: * Returns the GraphWindow.
173: */
174: public TSEGraphWindow getGraphWindow() {
175: return ETBaseUI.getGraphWindow(this );
176: }
177:
178: /*
179: * Returns World points, (Logical)
180: */
181: public IETRect getLogicalBounds() {
182: return ETBaseUI.getLogicalBounds(this );
183: }
184:
185: /*
186: * Returns the device bounding rect.
187: */
188: public IETRect getDeviceBounds() {
189: return ETBaseUI.getDeviceBounds(this );
190: }
191:
192: /**
193: * Retrieves the graphics context for the node ui.
194: *
195: * @param graphics The TS graphics class.
196: * @return The graphics context.
197: */
198: public IDrawInfo getDrawInfo(TSEGraphics graphics) {
199: IDrawInfo retVal = ETBaseUI.getDrawInfo(graphics, this );
200:
201: if (retVal != null) {
202: retVal.setIsTransparent(isTransparent());
203: retVal.setIsBorderDrawn(isBorderDrawn());
204: }
205:
206: // TODO: Determine what the DrawinToMainDrawingArea and AlwaysSetFont
207: // Should be set to.
208:
209: return retVal;
210: }
211:
212: /* (non-Javadoc)
213: * @see org.netbeans.modules.uml.ui.products.ad.viewfactory.IETGraphObjectUI#getDrawingArea()
214: */
215: public IDrawingAreaControl getDrawingArea() {
216: return ETBaseUI.getDrawingArea(this );
217: }
218:
219: /* (non-Javadoc)
220: * @see org.netbeans.modules.uml.ui.products.ad.viewfactory.IETGraphObjectUI#getInitStringValue()
221: */
222: public String getInitStringValue() {
223: return this .initStringValue;
224: }
225:
226: /* (non-Javadoc)
227: * @see org.netbeans.modules.uml.ui.products.ad.viewfactory.IETGraphObjectUI#getModelElement()
228: */
229: public IElement getModelElement() {
230: return modelElement;
231: }
232:
233: /* (non-Javadoc)
234: * @see org.netbeans.modules.uml.ui.products.ad.viewfactory.IETGraphObjectUI#setDrawEngine(org.netbeans.modules.uml.ui.support.viewfactorysupport.IDrawEngine)
235: */
236: public void setDrawEngine(IDrawEngine newDrawEngine) {
237: try {
238: if (newDrawEngine != drawEngine) {
239: drawEngine = newDrawEngine;
240: // if (drawEngine != null)
241: // drawEngine.init();
242: }
243: } catch (Exception e) {
244: e.printStackTrace();
245: }
246: }
247:
248: /* (non-Javadoc)
249: * @see org.netbeans.modules.uml.ui.products.ad.viewfactory.IETGraphObjectUI#setDrawEngineClass(java.lang.String)
250: */
251: public void setDrawEngineClass(String string) {
252: drawEngineClass = string;
253: }
254:
255: /* (non-Javadoc)
256: * @see org.netbeans.modules.uml.ui.products.ad.viewfactory.IETGraphObjectUI#setDrawingArea(org.netbeans.modules.uml.ui.swing.drawingarea.IDrawingAreaControl)
257: */
258: public void setDrawingArea(IDrawingAreaControl control) {
259: //drawingArea = control;
260: }
261:
262: /* (non-Javadoc)
263: * @see org.netbeans.modules.uml.ui.products.ad.viewfactory.IETGraphObjectUI#setInitStringValue(java.lang.String)
264: */
265: public void setInitStringValue(String string) {
266: this .initStringValue = string;
267: }
268:
269: /* (non-Javadoc)
270: * @see org.netbeans.modules.uml.ui.products.ad.viewfactory.IETGraphObjectUI#setModelElement(org.netbeans.modules.uml.core.metamodel.core.foundation.IElement)
271: */
272: public void setModelElement(IElement element) {
273: modelElement = element;
274:
275: }
276:
277: public ITSGraphObject getTSObject() {
278: return getOwner() instanceof ITSGraphObject ? (ITSGraphObject) getOwner()
279: : null;
280: }
281:
282: /// Returns the Model Element XMIID that was loaded from the file.
283: public String getReloadedModelElementXMIID() {
284: return meidValue;
285: }
286:
287: /// Sets the Model Element XMIID that will be persisted to the file.
288: public void setReloadedModelElementXMIID(String newVal) {
289: meidValue = newVal;
290: }
291:
292: /// Returns the Toplevel Element XMIID that was loaded from the file.
293: public String getReloadedTopLevelXMIID() {
294: return topLevelMEIDValue;
295: }
296:
297: /// Sets the Toplevel Element XMIID that will be persisted to the file.
298: public void setReloadedTopLevelXMIID(String newVal) {
299: topLevelMEIDValue = newVal;
300: }
301:
302: /// Returns the Toplevel Element XMIID that was loaded from the file.
303: public String getReloadedPresentationXMIID() {
304: return peidValue;
305: }
306:
307: /// Sets the Toplevel Element XMIID that will be persisted to the file.
308: public void setReloadedPresentationXMIID(String newVal) {
309: peidValue = newVal;
310: }
311:
312: /// Returns the Toplevel Element XMIID that was loaded from the file.
313: public String getReloadedOwnerPresentationXMIID() {
314: return m_ReloadedOwnerPresentationXMIID;
315: }
316:
317: /// Sets the Toplevel Element XMIID that will be persisted to the file.
318: public void setReloadedOwnerPresentationXMIID(String newVal) {
319: m_ReloadedOwnerPresentationXMIID = newVal;
320: }
321:
322: public IStrings getReferredElements() {
323: return m_PresentationReferenceReferredElements;
324: }
325:
326: public void setReferredElements(IStrings newVal) {
327: m_PresentationReferenceReferredElements = newVal;
328: }
329:
330: public void readFromArchive(IProductArchive prodArch,
331: IProductArchiveElement archEle) {
332: m_LabelKind = (int) archEle
333: .getAttributeLong(IProductArchiveDefinitions.LABELVIEW_TSLABELKIND);
334: m_Placement = (int) archEle
335: .getAttributeLong(IProductArchiveDefinitions.LABELVIEW_TSLABELPLACEMENTKIND);
336:
337: //read from archive using the common static method
338: ETBaseUI.readFromArchive(prodArch, archEle, this );
339:
340: // Create the presentation element and hook things up
341: IETGraphObject etObj = (IETGraphObject) getTSObject();
342: if (etObj != null) {
343: IPresentationElement pPresEle = ((IETGraphObject) getTSObject())
344: .getPresentationElement();
345: if (pPresEle != null) {
346: if (meidValue.length() > 0
347: && topLevelMEIDValue.length() > 0) {
348: // Reattach to this presentation element
349: ElementReloader reloader = new ElementReloader();
350: IElement modEle = reloader.getElement(
351: topLevelMEIDValue, meidValue);
352: if (modEle != null
353: && pPresEle instanceof IGraphPresentation) {
354: ((IGraphPresentation) pPresEle)
355: .setModelElement(modEle);
356: }
357:
358: ETBaseUI.createDrawEngineOneTime(this );
359:
360: // Allow the engine to read in any specific engine stuff
361: if (drawEngine != null) {
362: IProductArchiveElement foundEngEle = archEle
363: .getElement(IProductArchiveDefinitions.ENGINENAMEELEMENT_STRING);
364: if (foundEngEle != null) {
365: drawEngine.readFromArchive(prodArch,
366: foundEngEle);
367: }
368: }
369: }
370: }
371: }
372: }
373:
374: /**
375: * Saves the basic label stuff to the product archive
376: *
377: * @param pProductArchive [in] The archive we're saving to
378: * @param pElement [in] The current element, or parent for any new attributes or elements.
379: */
380: public void writeToArchive(IProductArchive prodArch,
381: IProductArchiveElement archEle) {
382: // Write the label kind
383: archEle.addAttributeLong(
384: IProductArchiveDefinitions.LABELVIEW_TSLABELKIND,
385: (int) m_LabelKind);
386:
387: // Write the placement
388: archEle
389: .addAttributeLong(
390: IProductArchiveDefinitions.LABELVIEW_TSLABELPLACEMENTKIND,
391: (int) m_Placement);
392:
393: ETBaseUI.writeToArchive(prodArch, archEle, this );
394: }
395:
396: /**
397: * Gets the IPresentationElement
398: */
399: public IPresentationElement createPresentationElement(IElement pElem) {
400: IPresentationElement retObj = null;
401: ILabelPresentation labelPres = DrawingFactory
402: .retrieveLabelPresentationMetaType();
403: if (labelPres != null) {
404: String xmiid = getReloadedPresentationXMIID();
405: if (xmiid != null && xmiid.length() > 0) {
406: // Reset the presentation XMIID to what it was last time.
407: labelPres.setXMIID(xmiid);
408: }
409:
410: // Assign the TS element to the presentation element
411: TSENodeLabel label = (TSENodeLabel) getOwnerLabel();
412: if (label != null) {
413: labelPres.setTSLabel(label);
414: }
415:
416: // Hook up the default model element (the parent's).
417: // This can be overriden in LabelPresentationImpl::put_ModelElement)
418: IPresentationElement parentPE = getParentPresentationElement();
419: if (parentPE != null) {
420: IElement elem = parentPE.getFirstSubject();
421: if (elem != null) {
422: labelPres.addSubject(elem);
423: }
424: }
425: retObj = labelPres;
426: }
427: return retObj;
428: }
429:
430: /**
431: * Gets the parent's IPresentationElement
432: */
433: private IPresentationElement getParentPresentationElement() {
434: IPresentationElement retObj = null;
435: IETGraphObject pPE = getParentETElement();
436: if (pPE != null) {
437: retObj = pPE.getPresentationElement();
438: }
439: return retObj;
440: }
441:
442: /**
443: * Gets the parent's IETElement
444: */
445: private IETGraphObject getParentETElement() {
446: IETGraphObject retObj = null;
447: TSENodeLabel label = (TSENodeLabel) getOwnerLabel();
448: TSGraphObject obj = label.getOwner();
449: if (obj != null) {
450: retObj = TypeConversions.getETGraphObject(obj);
451: }
452: return retObj;
453: }
454:
455: public boolean getWasModelElementDeleted() {
456: return m_WasModelElementDeleted;
457: }
458:
459: public void setWasModelElementDeleted(boolean newVal) {
460: m_WasModelElementDeleted = newVal;
461: }
462:
463: public boolean getFailedToCreateDrawEngine() {
464: return m_FailedToCreateDrawEngine;
465: }
466:
467: public void setFailedToCreateDrawEngine(boolean newVal) {
468: m_FailedToCreateDrawEngine = newVal;
469: }
470:
471: public IElement createNew(INamespace space, String initStr) {
472: IElement retEle = null;
473: IPresentationElement presEle = createPresentationElement(null);
474: ((ETNodeLabel) this .getTSObject())
475: .setPresentationElement(presEle);
476: if (presEle != null) {
477: retEle = presEle.getFirstSubject();
478: }
479: return retEle;
480: }
481:
482: /* (non-Javadoc)
483: * @see com.tomsawyer.editor.TSEObjectUI#draw(com.tomsawyer.editor.graphics.TSEGraphics)
484: */
485: public void draw(TSEGraphics graphics) {
486: if (getDrawEngine() != null) {
487: IDrawInfo drawInfo = getDrawInfo(graphics);
488: if (drawInfo != null)
489: getDrawEngine().doDraw(drawInfo);
490: }
491: }
492:
493: /* (non-Javadoc)
494: * @see com.tomsawyer.editor.ui.TSEAnnotatedUI#isAnnotationEditable()
495: */
496: public boolean isAnnotationEditable() {
497: // TODO Auto-generated method stub
498: return false;
499: }
500:
501: /* (non-Javadoc)
502: * @see org.netbeans.modules.uml.ui.products.ad.viewfactory.IETGraphObjectUI#getTopLevelMEIDValue()
503: */
504: public String getTopLevelMEIDValue() {
505: // TODO Auto-generated method stub
506: return null;
507: }
508:
509: /*
510: * (non-Javadoc)
511: * @see org.netbeans.modules.uml.ui.products.ad.viewfactory.IETGraphObjectUI#isOnTheScreen(com.tomsawyer.editor.graphics.TSEGraphics)
512: */
513: public boolean isOnTheScreen(TSEGraphics g) {
514: return ETBaseUI.isOnTheScreen(g, this );
515: }
516:
517: protected TSENode getOwnerNode() {
518: TSNodeLabel nodeLabel = (TSENodeLabel) this .getOwnerLabel();
519: return nodeLabel != null
520: && nodeLabel.getOwner() instanceof TSENode ? (TSENode) nodeLabel
521: .getOwner()
522: : null;
523: }
524:
525: /* (non-Javadoc)
526: * @see com.tomsawyer.editor.ui.TSELabelUI#drawCallout(com.tomsawyer.editor.graphics.TSEGraphics)
527: */
528: public void drawCallout(TSEGraphics graphics) {
529: if (this .getTSObject() != null && getTSObject().isSelected()) {
530: TSENode ownerNode = this .getOwnerNode();
531: if (ownerNode != null) {
532: // Don't draw the line if we are inside the nodes bounds
533: if (this .getBounds().intersects(ownerNode.getBounds())) {
534: return;
535: }
536: }
537:
538: }
539: super .drawCallout(graphics);
540: }
541:
542: public void reset() {
543: super .reset();
544: this .peidValue = this .ZERO_VALUE;
545: this .meidValue = this .ZERO_VALUE;
546: this .topLevelMEIDValue = this .ZERO_VALUE;
547: this .initStringValue = this .ZERO_VALUE;
548: }
549:
550: public void copy(TSEObjectUI sourceUI) {
551: super .copy(sourceUI);
552: ETGenericNodeLabelUI sourceNodeUI = (ETGenericNodeLabelUI) sourceUI;
553: this .peidValue = sourceNodeUI.peidValue;
554: this .meidValue = sourceNodeUI.meidValue;
555: this .topLevelMEIDValue = sourceNodeUI.topLevelMEIDValue;
556:
557: this .setInitStringValue(sourceNodeUI.getInitStringValue());
558: this .setDrawEngineClass(sourceNodeUI.getDrawEngineClass());
559: }
560:
561: public List getProperties() {
562: /* Jyothi
563: List list = super.getProperties();
564: list.add(new TSProperty(IProductArchiveDefinitions.PRESENTATIONELEMENTID_STRING, this.peidValue));
565: return list;
566: */
567: List list = super .getProperties();
568: String value = null;
569: IPresentationElement pPE = ((IETGraphObject) getTSObject())
570: .getPresentationElement();
571: if (pPE != null) {
572: // Get the presentation el ement id
573: String presEleId = pPE.getXMIID();
574: if (presEleId != null && presEleId.length() > 0) {
575: value = presEleId;
576: }
577: }
578:
579: list
580: .add(new TSProperty(
581: IProductArchiveDefinitions.PRESENTATIONELEMENTID_STRING,
582: value));
583: return list;
584: }
585:
586: public void setProperty(TSProperty property) {
587: //String attrString = (String) property.getValue(); //Jyothi
588: if (IProductArchiveDefinitions.PRESENTATIONELEMENTID_STRING
589: .equals(property.getName())) {
590: String attrString = (String) property.getValue(); //Jyothi
591: this .peidValue = attrString;
592: } else {
593: super .setProperty(property);
594: }
595: }
596:
597: public List getChangedProperties() {
598: List list = super .getChangedProperties();
599:
600: IPresentationElement pPE = ((IETGraphObject) getTSObject())
601: .getPresentationElement();
602: if (pPE != null) {
603: // Get the presentation element id
604: String presEleId = pPE.getXMIID();
605: if (presEleId != null && presEleId.length() > 0) {
606: list
607: .add(new TSProperty(
608: IProductArchiveDefinitions.PRESENTATIONELEMENTID_STRING,
609: presEleId));
610: }
611: }
612: return list;
613: }
614:
615: public int getLabelKind() {
616: return m_LabelKind;
617: }
618:
619: public int getPlacement() {
620: return m_Placement;
621: }
622:
623: public IETPoint getSpecifiedXY() {
624: return m_ptSpecifiedXY;
625: }
626:
627: public void setLabelKind(int l) {
628: m_LabelKind = l;
629: }
630:
631: public void setPlacement(int l) {
632: m_Placement = l;
633: }
634:
635: public void setSpecifiedXY(IETPoint point) {
636: m_ptSpecifiedXY = point;
637: }
638:
639: }
|