0001: /*
0002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0003: *
0004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
0005: *
0006: * The contents of this file are subject to the terms of either the GNU
0007: * General Public License Version 2 only ("GPL") or the Common
0008: * Development and Distribution License("CDDL") (collectively, the
0009: * "License"). You may not use this file except in compliance with the
0010: * License. You can obtain a copy of the License at
0011: * http://www.netbeans.org/cddl-gplv2.html
0012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
0013: * specific language governing permissions and limitations under the
0014: * License. When distributing the software, include this License Header
0015: * Notice in each file and include the License file at
0016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
0017: * particular file as subject to the "Classpath" exception as provided
0018: * by Sun in the GPL Version 2 section of the License file that
0019: * accompanied this code. If applicable, add the following below the
0020: * License Header, with the fields enclosed by brackets [] replaced by
0021: * your own identifying information:
0022: * "Portions Copyrighted [year] [name of copyright owner]"
0023: *
0024: * Contributor(s):
0025: *
0026: * The Original Software is NetBeans. The Initial Developer of the Original
0027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
0028: * Microsystems, Inc. All Rights Reserved.
0029: *
0030: * If you wish your version of this file to be governed by only the CDDL
0031: * or only the GPL Version 2, indicate your decision by adding
0032: * "[Contributor] elects to include this software in this distribution
0033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
0034: * single choice of license, a recipient has the option to distribute
0035: * your version of this file under either the CDDL, the GPL Version 2 or
0036: * to extend the choice of license to its licensees as provided above.
0037: * However, if you add GPL Version 2 code and therefore, elected the GPL
0038: * Version 2 license, then the option applies only if the new code is
0039: * made subject to such option by the copyright holder.
0040: */
0041:
0042: package org.netbeans.modules.uml.ui.products.ad.viewfactory;
0043:
0044: import java.util.StringTokenizer;
0045:
0046: import org.netbeans.modules.uml.common.ETException;
0047: import org.netbeans.modules.uml.common.ETSystem;
0048: import org.netbeans.modules.uml.core.metamodel.core.foundation.IElement;
0049: import org.netbeans.modules.uml.core.metamodel.core.foundation.INamespace;
0050: import org.netbeans.modules.uml.core.metamodel.core.foundation.IPresentationElement;
0051: import org.netbeans.modules.uml.core.metamodel.diagrams.IGraphEventKind;
0052: import org.netbeans.modules.uml.core.support.umlsupport.ETDeviceRect;
0053: import org.netbeans.modules.uml.core.support.umlsupport.IETRect;
0054: import org.netbeans.modules.uml.core.support.umlsupport.IStrings;
0055: import org.netbeans.modules.uml.core.support.umlsupport.StringUtilities;
0056: import org.netbeans.modules.uml.core.support.umlsupport.Strings;
0057: import org.netbeans.modules.uml.core.support.umlutils.ETList;
0058: import org.netbeans.modules.uml.ui.products.ad.application.IMenuManager;
0059: import org.netbeans.modules.uml.ui.products.ad.drawengines.ETDrawEngineFactory;
0060: import org.netbeans.modules.uml.ui.products.ad.drawengines.ETDrawInfo;
0061: import org.netbeans.modules.uml.ui.products.ad.drawengines.INodeDrawEngine;
0062: import org.netbeans.modules.uml.ui.products.ad.graphobjects.ETNode;
0063: import org.netbeans.modules.uml.ui.support.ElementReloader;
0064: import org.netbeans.modules.uml.ui.support.PresentationReferenceHelper;
0065: import org.netbeans.modules.uml.ui.support.applicationmanager.DrawingFactory;
0066: import org.netbeans.modules.uml.ui.support.applicationmanager.GraphPresentation;
0067: import org.netbeans.modules.uml.ui.support.applicationmanager.IPresentationTypesMgr;
0068: import org.netbeans.modules.uml.ui.support.applicationmanager.IProductGraphPresentation;
0069: import org.netbeans.modules.uml.ui.support.applicationmanager.PresentationTypeDetails;
0070: import org.netbeans.modules.uml.ui.support.archivesupport.IProductArchive;
0071: import org.netbeans.modules.uml.ui.support.archivesupport.IProductArchiveDefinitions;
0072: import org.netbeans.modules.uml.ui.support.archivesupport.IProductArchiveElement;
0073: import org.netbeans.modules.uml.ui.support.viewfactorysupport.ETRectEx;
0074: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IDrawEngine;
0075: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IDrawInfo;
0076: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IETEdge;
0077: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IETEdgeUI;
0078: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IETGraphObject;
0079: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IETLabel;
0080: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IETNode;
0081: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IETNodeUI;
0082: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IEventManager;
0083: import org.netbeans.modules.uml.ui.support.viewfactorysupport.ILabelManager;
0084: import org.netbeans.modules.uml.ui.support.viewfactorysupport.ITSGraphObject;
0085: import org.netbeans.modules.uml.ui.support.viewfactorysupport.TypeConversions;
0086: import org.netbeans.modules.uml.ui.swing.drawingarea.ADDrawingAreaControl;
0087: import org.netbeans.modules.uml.ui.swing.drawingarea.ADGraphWindow;
0088: import org.netbeans.modules.uml.ui.swing.drawingarea.IDrawingAreaControl;
0089: import com.tomsawyer.editor.TSEEdge;
0090: import com.tomsawyer.editor.TSEEdgeLabel;
0091: import com.tomsawyer.editor.TSEGraph;
0092: import com.tomsawyer.editor.TSEGraphManager;
0093: import com.tomsawyer.editor.TSEGraphWindow;
0094: import com.tomsawyer.editor.TSENode;
0095: import com.tomsawyer.editor.TSENodeLabel;
0096: import com.tomsawyer.editor.TSEObject;
0097: import com.tomsawyer.editor.TSEObjectUI;
0098: import com.tomsawyer.editor.graphics.TSEGraphics;
0099: import com.tomsawyer.graph.TSGraph;
0100: import com.tomsawyer.graph.TSGraphManager;
0101: import com.tomsawyer.graph.TSGraphObject; //import com.tomsawyer.util.TSConstRect;
0102: import com.tomsawyer.drawing.geometry.TSConstRect; //import com.tomsawyer.util.TSConstSize;
0103: import com.tomsawyer.drawing.geometry.TSConstSize;
0104: import com.tomsawyer.util.TSObject; //import com.tomsawyer.util.TSRect;
0105: import com.tomsawyer.drawing.geometry.TSRect; //import com.tomsawyer.util.TSTransform;
0106: import com.tomsawyer.editor.TSTransform;
0107:
0108: /**
0109: * @author sumitabhk
0110: *
0111: */
0112: public class ETBaseUI {
0113: public static void readFromArchive(IProductArchive prodArch,
0114: IProductArchiveElement archEle, IETGraphObjectUI ui) {
0115: //Debug long timeStart = System.currentTimeMillis();
0116:
0117: String tempStr = archEle
0118: .getAttributeString(IProductArchiveDefinitions.MEID_STRING);
0119: if (tempStr != null && tempStr.length() > 0) {
0120: ui.setReloadedModelElementXMIID(tempStr);
0121: }
0122:
0123: tempStr = archEle
0124: .getAttributeString(IProductArchiveDefinitions.TOPLEVELID_STRING);
0125: if (tempStr != null && tempStr.length() > 0) {
0126: ui.setReloadedTopLevelXMIID(tempStr);
0127: }
0128:
0129: tempStr = archEle
0130: .getAttributeString(IProductArchiveDefinitions.PRESENTATIONELEMENTID_STRING);
0131: if (tempStr != null && tempStr.length() > 0) {
0132: ui.setReloadedPresentationXMIID(tempStr);
0133: }
0134:
0135: tempStr = archEle
0136: .getAttributeString(IProductArchiveDefinitions.OWNER_PRESENTATIONELEMENT);
0137: if (tempStr != null && tempStr.length() > 0) {
0138: ui.setReloadedOwnerPresentationXMIID(tempStr);
0139: }
0140:
0141: tempStr = archEle
0142: .getAttributeString(IProductArchiveDefinitions.REFERREDELEMENTS_STRING);
0143: if (tempStr != null && tempStr.length() > 0) {
0144: // Split on the delimiter (" ") and add it to the m_PresentationReferenceReferredElements
0145: // list
0146: StringTokenizer tokenizer = new StringTokenizer(tempStr,
0147: " ");
0148: IStrings m_PresentationReferenceReferredElements = new Strings();
0149: while (tokenizer.hasMoreTokens()) {
0150: m_PresentationReferenceReferredElements.add(tokenizer
0151: .nextToken());
0152: }
0153: ui
0154: .setReferredElements(m_PresentationReferenceReferredElements);
0155: }
0156:
0157: tempStr = archEle
0158: .getAttributeString(IProductArchiveDefinitions.INITIALIZATIONSTRING_STRING);
0159: if (tempStr != null && tempStr.length() > 0) {
0160: ui.setInitStringValue(tempStr);
0161: }
0162:
0163: IProductArchiveElement engineElement = archEle
0164: .getElement(IProductArchiveDefinitions.ENGINENAMEELEMENT_STRING);
0165: ui
0166: .setDrawEngineClass(engineElement
0167: .getAttributeString(IProductArchiveDefinitions.ENGINENAMEATTRIBUTE_STRING));
0168:
0169: ElementReloader reloader = new ElementReloader();
0170:
0171: // See if this element has been deleted
0172: if (archEle != null) {
0173: ui
0174: .setWasModelElementDeleted(archEle
0175: .getAttributeBool(IProductArchiveDefinitions.PE_DELETED));
0176: }
0177:
0178: String reloadedXMIID = ui.getReloadedModelElementXMIID();
0179: String reloadedTopId = ui.getReloadedTopLevelXMIID();
0180:
0181: if (reloadedXMIID.length() > 0 && reloadedTopId.length() > 0) {
0182: // Need to reload the model element and reattach
0183: IElement modEle = reloader.getElement(reloadedTopId,
0184: reloadedXMIID);
0185: if (modEle != null) {
0186: attachAndCreatePresentationElement(modEle, ui
0187: .getInitStringValue(), false, ui);
0188: ui.setModelElement(modEle);
0189: }
0190: }
0191:
0192: // Create a new engine from the archive
0193: try {
0194: IDrawEngine de = DrawingFactory.createDrawEngine(ui,
0195: prodArch, engineElement);
0196:
0197: ui.setDrawEngine(de);
0198: // if (de != null)
0199: // {
0200: // de.init();
0201: // }
0202: } catch (Exception e) {
0203: e.printStackTrace();
0204: }
0205: //Debug
0206: // long timeFinish = System.currentTimeMillis();
0207: // long tsDelta = timeFinish - timeStart;
0208: // ETSystem.out.println(tempStr + " with ID: " + reloadedXMIID + " " + " initialized in ( " + StringUtilities.timeToString(tsDelta, 3) + " )");
0209: }
0210:
0211: /**
0212: * This routine is called when a node or edge needs to be attached to an existing model element.
0213: * For example, during a drag and drop operation a model element could be dragged off of the
0214: * project tree and dropped onto the drawing area. The drawing area activeX control calls
0215: * attach on the node so that the node can create a presentation element, attach the model element
0216: * presentation element and then create the appropriate draw engine.
0217: *
0218: * This version allows you to specify if the draw engine should be created. Right now you only
0219: * specify false when reloading from a file because the .etld file is read in first and then the
0220: * .etlp. We don't want the .etld file to create an engine only to destroy it when we re-created one based on
0221: * the .etlp file.
0222: *
0223: * @param pElement [in] The element to attach to
0224: * @param sInitializationString [in] A string coming from the presentation types file telling us how
0225: * to initialize
0226: * @param bCreateEngine [in] true to create the draw engine.
0227: *
0228: */
0229: public static void attachAndCreatePresentationElement(
0230: IElement pElem, String initStr, boolean bCreateEngine,
0231: IETGraphObjectUI ui) {
0232: // This routine is called when a model element is dragged from the project tree onto the
0233: // drawing area (for example). We have a model element and we need to create a presentation
0234: // element and hook things up.
0235: ui.setInitStringValue(initStr);
0236: IETGraphObject etObj = null;
0237: ITSGraphObject graphObj = ui.getTSObject();
0238: if (graphObj != null) {
0239: if (graphObj.isNode()) {
0240: etObj = (IETNode) graphObj;
0241: } else if (graphObj.isEdge()) {
0242: etObj = (IETEdge) graphObj;
0243: } else if (graphObj.isLabel()) {
0244: etObj = (IETLabel) graphObj;
0245: }
0246: }
0247:
0248: if (bCreateEngine) {
0249: createDrawEngine(initStr, ui);
0250: }
0251:
0252: if (pElem != null) {
0253: IPresentationElement pEle = null;
0254: if (etObj != null) {
0255: pEle = etObj.getPresentationElement();
0256: }
0257:
0258: if (pEle != null) {
0259: // We have a presentation element already, just initialize the engine
0260: initializeEngine(pEle, ui);
0261: } else {
0262: IPresentationElement presEle = ui
0263: .createPresentationElement(pElem);
0264: if (presEle != null) {
0265: // If we have a reloaded presentation XMIID then this node has been
0266: // previously saved to a .etld and .etlp file. We need to reuse this id
0267: // so we call a method on IVersionableElement to force the id back to
0268: // what it was when the node was originally saved.
0269: String reloadedXMIID = ui
0270: .getReloadedPresentationXMIID();
0271: if (reloadedXMIID != null
0272: && reloadedXMIID.length() > 0) {
0273: presEle.setXMIID(reloadedXMIID);
0274: }
0275:
0276: // Attach the PE up to the model element if not already present - already present
0277: // happens in the case of a transformed element
0278:
0279: if (!pElem.isPresent(presEle)) {
0280: // Make sure to remove any existing subjects
0281: IElement firstSubject = presEle
0282: .getFirstSubject();
0283: if (firstSubject != null) {
0284: presEle.removeSubject(firstSubject);
0285: }
0286: presEle.addSubject(pElem);
0287: }
0288:
0289: // Tell the IETElement (the TSNode) that it is being represented
0290: // by this presentation element
0291: if (etObj != null) {
0292: etObj.setPresentationElement(presEle);
0293: }
0294: }
0295: }
0296: }
0297: }
0298:
0299: /**
0300: * Initializes the draw engine and resource user
0301: */
0302: public static void initializeEngine(IPresentationElement pElement,
0303: IETGraphObjectUI ui) {
0304: IDrawEngine drawEngine = ui.getDrawEngine();
0305: if (drawEngine != null && pElement != null) {
0306: try {
0307: drawEngine.init();
0308: } catch (Exception e) {
0309: e.printStackTrace();
0310: }
0311: }
0312: }
0313:
0314: /**
0315: * Creates a drawing engine based on the initialization string
0316: *
0317: * @param szInitializationString [in] The initialization string used to create the engine
0318: */
0319: public static void createDrawEngine(String initStr,
0320: IETGraphObjectUI ui) {
0321: IDrawEngine drawEngine = null; //ui.getDrawEngine();
0322: if (ui != null) {
0323: if (initStr != null && initStr.length() > 0) {
0324: ui.setInitStringValue(initStr);
0325: }
0326:
0327: // First see if we have a draw engine type in the presentation file.
0328: IDrawingAreaControl control = ui.getDrawingArea();
0329: if (control != null) {
0330: IPresentationTypesMgr presMgr = control
0331: .getPresentationTypesMgr();
0332: if (presMgr != null) {
0333: String fullInitStr = ui.getInitStringValue();
0334:
0335: int diaKind = control.getDiagramKind();
0336: PresentationTypeDetails details = presMgr
0337: .getInitStringDetails(fullInitStr, diaKind);
0338: if (details != null) {
0339: String drawEngineType = details.getEngineName();
0340: if (drawEngineType != null
0341: && drawEngineType.length() > 0) {
0342: // Create a new engine from the archive
0343: try {
0344: drawEngine = ETDrawEngineFactory
0345: .createDrawEngine(drawEngineType);
0346: if (drawEngine != null) {
0347: drawEngine.setParent(ui);
0348: ui.setDrawEngine(drawEngine);
0349: drawEngine.init();
0350: } else {
0351: ETSystem.out
0352: .print("Warning failed to create Object Drawing ");
0353: ETSystem.out
0354: .println(drawEngineType);
0355: }
0356: } catch (ETException e) {
0357: e.printStackTrace();
0358: }
0359: }
0360: }
0361: }
0362: }
0363: }
0364: }
0365:
0366: /*
0367: *
0368: */
0369: public static void createDrawEngineOneTime(IETGraphObjectUI ui) {
0370: IDrawEngine drawEngine = ui.getDrawEngine();
0371: boolean failedToCreateDrawEngine = ui
0372: .getFailedToCreateDrawEngine();
0373: if (drawEngine == null && !failedToCreateDrawEngine) {
0374: createDrawEngine(ui.getInitStringValue(), ui);
0375: drawEngine = ui.getDrawEngine();
0376: if (drawEngine == null) {
0377: // We failed to create the engine. Don't try again or draw might constantly be
0378: // pumping out messages or asserting.
0379: ui.setFailedToCreateDrawEngine(true);
0380: } else {
0381: IPresentationElement pEle = ((IETGraphObject) ui
0382: .getTSObject()).getPresentationElement();
0383: if (pEle != null) {
0384: initializeEngine(pEle, ui);
0385: }
0386: }
0387: }
0388: }
0389:
0390: public static void writeToArchive(IProductArchive prodArch,
0391: IProductArchiveElement archEle, IETGraphObjectUI ui) {
0392: // 1. This may be duplicated in the .etld file, but the .etld file is not readable by us. It's a
0393: // serialized BSTR that is different under unicode vs non-unicode. ARG!! So we also put out the
0394: // model element and toplevel element here.
0395: String meid = "";
0396: String topLevelId = "";
0397: String presEleId = "";
0398: String ownerPresEleId = "";
0399: IPresentationElement pPE = ((IETGraphObject) ui.getTSObject())
0400: .getPresentationElement();
0401: IElement modEle = null;
0402: if (pPE != null) {
0403: modEle = pPE.getFirstSubject();
0404:
0405: // Get the presentation element id
0406: presEleId = pPE.getXMIID();
0407:
0408: IElement owner = pPE.getOwner();
0409: if (owner != null && owner instanceof IPresentationElement) {
0410: ownerPresEleId = ((IPresentationElement) owner)
0411: .getXMIID();
0412: }
0413: }
0414:
0415: if (modEle != null) {
0416: meid = modEle.getXMIID();
0417: topLevelId = modEle.getTopLevelId();
0418: if (topLevelId == null)
0419: topLevelId = "";
0420: }
0421:
0422: // If we're disconnected from the presentation element we don't want to loose our ids. So
0423: // preserve the ids.
0424: if (meid.length() == 0 && topLevelId.length() == 0
0425: && presEleId.length() == 0
0426: && ownerPresEleId.length() == 0) {
0427: meid = ui.getReloadedModelElementXMIID();
0428: topLevelId = ui.getReloadedTopLevelXMIID();
0429: presEleId = ui.getReloadedPresentationXMIID();
0430: ownerPresEleId = ui.getReloadedOwnerPresentationXMIID();
0431: }
0432:
0433: if (presEleId.length() > 0) {
0434: archEle
0435: .addAttributeString(
0436: IProductArchiveDefinitions.PRESENTATIONELEMENTID_STRING,
0437: presEleId);
0438: }
0439:
0440: if (meid.length() > 0) {
0441: archEle.addAttributeString(
0442: IProductArchiveDefinitions.MEID_STRING, meid);
0443: }
0444:
0445: if (topLevelId.length() > 0) {
0446: archEle.addAttributeString(
0447: IProductArchiveDefinitions.TOPLEVELID_STRING,
0448: topLevelId);
0449: }
0450:
0451: if (ownerPresEleId.length() > 0) {
0452: archEle
0453: .addAttributeString(
0454: IProductArchiveDefinitions.OWNER_PRESENTATIONELEMENT,
0455: ownerPresEleId);
0456: }
0457:
0458: // 2. PresentationReference relationships are deleted because they are owned by presentation elements
0459: // which do not get saved in the model file. So we store the information necessary to recreate them here.
0460: // The get recreated when the diagram is reloaded.
0461: if (pPE != null) {
0462: ETList<IPresentationElement> referredElems = PresentationReferenceHelper
0463: .getHasReferredElements(pPE);
0464: if (referredElems != null) {
0465: ui.setReferredElements(null);
0466: IStrings refElems = new Strings();
0467: int count = referredElems.size();
0468: for (int i = 0; i < count; i++) {
0469: IPresentationElement refEle = referredElems.get(i);
0470: String id = refEle.getXMIID();
0471: if (id != null && id.length() > 0) {
0472: refElems.add(id);
0473: }
0474: }
0475: ui.setReferredElements(refElems);
0476: }
0477: }
0478:
0479: // 3. Save what's currently in the m_PresentationReferenceReferredElements list.
0480: IStrings strs = ui.getReferredElements();
0481: if (strs != null) {
0482: String attrVal = strs.getListAsDelimitedString(" ");
0483: if (attrVal != null && attrVal.length() > 0) {
0484: // Save off this attribute
0485: archEle
0486: .addAttributeString(
0487: IProductArchiveDefinitions.REFERREDELEMENTS_STRING,
0488: attrVal);
0489: }
0490: }
0491:
0492: // Add our initialization string
0493: String initStr = ui.getInitStringValue();
0494: if (initStr != null && initStr.length() > 0) {
0495: archEle
0496: .addAttributeString(
0497: IProductArchiveDefinitions.INITIALIZATIONSTRING_STRING,
0498: initStr);
0499: }
0500:
0501: // Save the graphics engine stuff
0502: IDrawEngine engine = ui.getDrawEngine();
0503: if (engine != null && prodArch != null && archEle != null) {
0504: // This call will allow the drawengine and each compartment to save stuff to the
0505: // product archive.
0506: engine.writeToArchive(prodArch, archEle);
0507: }
0508: }
0509:
0510: public static void onContextMenu(IMenuManager manager,
0511: IETGraphObjectUI ui) {
0512: if (ui != null) {
0513: IDrawEngine engine = ui.getDrawEngine();
0514: if (engine != null) {
0515: // Forward the context menu message to the engine
0516: engine.onContextMenu(manager);
0517:
0518: // If we have a label manager then pass the message to it as well
0519: ILabelManager labelMgr = engine.getLabelManager();
0520: if (labelMgr != null) {
0521: labelMgr.onContextMenu(manager);
0522: }
0523:
0524: // If we have a edge manager then pass the message to it as well
0525: IEventManager eventMgr = engine.getEventManager();
0526: if (eventMgr != null) {
0527: eventMgr.onContextMenu(manager);
0528: }
0529: }
0530: }
0531: }
0532:
0533: /**
0534: * Returns the model element id and toplevel id for the presentation element this object represents
0535: *
0536: * @param sPresentationElementID [in] The presentation element XMIID
0537: * @param sMEID [in] The model element XMIID
0538: * @param sTopLevelID [in] The toplevel element XMIID (the IProject XMIID)
0539: * @param sOwnerPresentationElementID [in] The owner presentation element XMIID, if this object has an owner.
0540: */
0541: private static void getXMIIDs(String presEleId, String meid,
0542: String topLevelId, String ownerId, IETGraphObjectUI ui) {
0543: //This method needs to return all this, for the time being I am copying this method at one place I needed it.
0544: IPresentationElement pPE = ((IETGraphObject) ui.getTSObject())
0545: .getPresentationElement();
0546: IElement modEle = null;
0547: if (pPE != null) {
0548: modEle = pPE.getFirstSubject();
0549:
0550: // Get the presentation element id
0551: presEleId = pPE.getXMIID();
0552:
0553: IElement owner = pPE.getOwner();
0554: if (owner != null && owner instanceof IPresentationElement) {
0555: ownerId = ((IPresentationElement) owner).getXMIID();
0556: }
0557: }
0558:
0559: if (modEle != null) {
0560: meid = modEle.getXMIID();
0561: topLevelId = modEle.getTopLevelId();
0562: }
0563:
0564: }
0565:
0566: public static void save(IProductArchive prodArch,
0567: IETGraphObjectUI ui) {
0568: if (ui != null) {
0569: ITSGraphObject obj = ui.getTSObject();
0570: if (obj != null && obj instanceof IETGraphObject) {
0571: IETGraphObject etObj = (IETGraphObject) obj;
0572: IPresentationElement presEle = etObj
0573: .getPresentationElement();
0574: if (presEle != null) {
0575: String xmiid = presEle.getXMIID();
0576: if (xmiid != null && xmiid.length() > 0) {
0577: IProductArchiveElement createdEle = prodArch
0578: .createElement(xmiid);
0579: if (createdEle != null) {
0580: etObj.writeToArchive(prodArch, createdEle);
0581: }
0582: }
0583: }
0584: }
0585: }
0586: }
0587:
0588: /**
0589: * This routine is called when a TSENodeView needs to be created from scratch. The user has dropped
0590: * a TS node on the tree and we need to create the appropriate model element and presentation elements and
0591: * tie them together. After all that is done look at the initialization string and create the correct engine.
0592: */
0593: public static IElement create(INamespace pNamespace,
0594: String initStr, IETGraphObjectUI ui) {
0595: IElement createdEle = null;
0596:
0597: // Go through the base create logic which will create the correct IElement, stick it
0598: // in the namespace and attach it to this node
0599: createdEle = ui.createNew(pNamespace, initStr);
0600: // If we have a good model element then create the presentation element and
0601: // hook the two up.
0602: if (createdEle != null) {
0603: IDrawEngine drawEngine = ui.getDrawEngine();
0604: ITSGraphObject tsObj = ui.getTSObject();
0605: if (tsObj instanceof IETGraphObject) {
0606: IETGraphObject etObj = (IETGraphObject) tsObj;
0607: IPresentationElement presEle = etObj
0608: .getPresentationElement();
0609: if (presEle != null) {
0610: initializeEngine(presEle, ui);
0611: }
0612: }
0613:
0614: // Create our draw engine
0615: createDrawEngineOneTime(ui);
0616:
0617: // Create our label manager in case we have labels
0618: IDrawEngine pEng = ui.getDrawEngine();
0619: if (pEng != null) {
0620: ILabelManager labelMgr = pEng.getLabelManager();
0621:
0622: if (labelMgr != null) {
0623: labelMgr.createInitialLabels();
0624: }
0625:
0626: // Tell the draw engine to draw with a red border if it's not well.
0627: pEng.setCheckSyncStateDuringDraw(true);
0628: }
0629: }
0630:
0631: return createdEle;
0632: }
0633:
0634: /**
0635: * This routine is called when a node or edge needs to be attached to an existing model element.
0636: * For example, during a drag and drop operation a model element could be dragged off of the
0637: * project tree and dropped onto the drawing area. The drawing area activeX control calls
0638: * attach on the node so that the node can create a presentation element, attach the model element
0639: * presentation element and then create the appropriate draw engine.
0640: *
0641: * @param pElement [in] The element to attach to
0642: * @param sInitializationString [in] A string coming from the presentation types file telling us how
0643: * to initialize
0644: */
0645: public static void attach(IElement modEle, String initStr,
0646: IETGraphObjectUI ui) {
0647: attachAndCreatePresentationElement(modEle, initStr, true, ui);
0648:
0649: // Create our draw engine
0650: IDrawEngine pEng = ui.getDrawEngine();
0651: if (pEng == null) {
0652: createDrawEngine(initStr, ui);
0653:
0654: // Tell the draw engine to draw with a red border if it's not well.
0655: pEng = ui.getDrawEngine();
0656: if (pEng != null) {
0657: pEng.setCheckSyncStateDuringDraw(true);
0658: }
0659: }
0660: }
0661:
0662: /**
0663: * Called to notify the node that a link has been added.
0664: *
0665: * @param pNewLink [in] The link about to be added
0666: * @param bIsFromNode [in] true if this is the from node.
0667: */
0668: public static void onPostAddLink(IETGraphObject newLink,
0669: boolean isFromNode, IETGraphObjectUI ui) {
0670: IDrawEngine pEng = ui.getDrawEngine();
0671: if (pEng != null) {
0672: IEventManager eventMgr = pEng.getEventManager();
0673: if (eventMgr != null) {
0674: eventMgr.onPostAddLink(newLink, isFromNode);
0675: }
0676: }
0677: }
0678:
0679: /**
0680: * Returns the type of model element to create based on the initialization string
0681: *
0682: * @return The metatype that should be created when this graph object is created.
0683: */
0684: public static String getMetaType(IETGraphObjectUI ui) {
0685: IDrawingAreaControl control = ui.getDrawingArea();
0686: if (control != null) {
0687: IPresentationTypesMgr presMgr = control
0688: .getPresentationTypesMgr();
0689: if (presMgr != null) {
0690: PresentationTypeDetails details = presMgr
0691: .getInitStringDetails(ui.getInitStringValue(),
0692: control.getDiagramKind());
0693: if (details != null) {
0694: return details.getMetaType();
0695: }
0696: }
0697: }
0698: return null;
0699: }
0700:
0701: public static void onGraphEvent(int nKind, IETGraphObjectUI ui) {
0702: if (ui == null)
0703: return;
0704:
0705: // If we have a postpaste or postduplicate then we need to get rid of the presentation element
0706: // so we don't duplicate the presentation element of the guy we were copied from.
0707: if (nKind == IGraphEventKind.GEK_POST_PASTE_VIEW) {
0708: // Clear out the reloaded stuff. Not really necessary, but we haven't been
0709: // reloaded so lets just clear them out just in case.
0710: //IETGraphObject pETGraphObject = TypeConversions.getETGraphObject((TSEObjectUI)ui.);
0711:
0712: IETGraphObject pETGraphObject = (IETGraphObject) ui
0713: .getOwner();
0714:
0715: if (pETGraphObject != null) {
0716: pETGraphObject.setReloadedModelElementXMIID("");
0717: pETGraphObject.setReloadedTopLevelXMIID("");
0718: pETGraphObject.setReloadedPresentationXMIID("");
0719: pETGraphObject.setReloadedOwnerPresentationXMIID("");
0720: }
0721:
0722: // Clear the user object
0723: //ClearTSUserObject();
0724:
0725: IElement pElement = null;
0726: IElement pOwner = null;
0727:
0728: IPresentationElement cpPE = pETGraphObject
0729: .getPresentationElement();
0730:
0731: if (cpPE != null) {
0732: // Get the model element and owner for the copied presentation element
0733: pElement = cpPE.getFirstSubject();
0734:
0735: if (pElement != null) {
0736: // Create a new presentation element
0737: IPresentationElement newPE = ui
0738: .createPresentationElement(pElement);
0739:
0740: // Preserve the ownership of the original presentation element
0741: pOwner = cpPE.getOwner();
0742:
0743: IPresentationElement pOwnerPE = (pOwner instanceof IPresentationElement) ? (IPresentationElement) pOwner
0744: : null;
0745:
0746: if (pOwnerPE != null) {
0747: pOwnerPE.addElement(newPE);
0748: }
0749:
0750: // Reconnect up the new presentation element
0751: newPE.addSubject(pElement);
0752:
0753: // Tell the product element that this is the new presentation element
0754: pETGraphObject.setPresentationElement(newPE);
0755:
0756: ((TSEObject) pETGraphObject).setTag(new String(
0757: "I have been pasted"));
0758:
0759: // make sure we are not pointing to the original node
0760:
0761: ui.setDrawEngine(null);
0762:
0763: if (ui instanceof ETGenericNodeUI) {
0764: ((ETGenericNodeUI) ui)
0765: .setOwner((TSENode) pETGraphObject);
0766: } else if (ui instanceof ETGenericEdgeUI) {
0767: ((ETGenericEdgeUI) ui)
0768: .setOwner((TSEEdge) pETGraphObject);
0769: } else if (ui instanceof ETGenericEdgeLabelUI) {
0770: ((ETGenericEdgeLabelUI) ui)
0771: .setOwner((TSEEdgeLabel) pETGraphObject);
0772: } else if (ui instanceof ETGenericNodeLabelUI) {
0773: ((ETGenericNodeLabelUI) ui)
0774: .setOwner((TSENodeLabel) pETGraphObject);
0775: }
0776:
0777: pETGraphObject.setObjectView((TSEObjectUI) ui);
0778:
0779: // Tell the product element that this is the new presentation element
0780: pETGraphObject.setPresentationElement(newPE);
0781:
0782: }
0783: }
0784: }
0785:
0786: IDrawEngine engine = ui.getDrawEngine();
0787:
0788: if (engine == null) {
0789: return;
0790: }
0791:
0792: engine.onGraphEvent(nKind);
0793:
0794: IEventManager eventManager = engine.getEventManager();
0795: if (eventManager != null) {
0796: eventManager.onGraphEvent(nKind);
0797: }
0798:
0799: ILabelManager labelManager = engine.getLabelManager();
0800: if (labelManager != null) {
0801: labelManager.onGraphEvent(nKind);
0802: }
0803: }
0804:
0805: public static boolean onKeyDown(int nKeyCode, int nShift,
0806: IETGraphObjectUI ui) {
0807: boolean handled = false;
0808: if (ui != null) {
0809: IDrawEngine engine = ui.getDrawEngine();
0810: if (engine != null) {
0811: handled = engine.onKeydown(nKeyCode, nShift);
0812: }
0813: }
0814: return handled;
0815: }
0816:
0817: public static boolean onCharTyped(char ch, IETGraphObjectUI ui) {
0818: boolean handled = false;
0819: if (ui != null) {
0820: IDrawEngine engine = ui.getDrawEngine();
0821: if (engine != null) {
0822: handled = engine.onCharTyped(ch);
0823: }
0824: }
0825: return handled;
0826: }
0827:
0828: public static void resetDrawEngine(String sInitializationString,
0829: IETGraphObjectUI ui) {
0830: if (ui != null) {
0831: IETGraphObject pThis = (IETGraphObject) ui.getTSObject();
0832: IDrawEngine m_Engine = ui.getDrawEngine();
0833: ILabelManager pLabelManager = null;
0834:
0835: // Remove all the labels on this element, we will reset them again
0836: // once the draw engine has been re-created
0837: if (m_Engine != null) {
0838: pLabelManager = m_Engine.getLabelManager();
0839: if (pLabelManager != null) {
0840: pLabelManager.discardAllLabels();
0841: pLabelManager = null;
0842: }
0843:
0844: // NULL out the draw engine
0845: ui.setDrawEngine(null);
0846: }
0847:
0848: IPresentationElement presEle = ((IETGraphObject) ui
0849: .getTSObject()).getPresentationElement();
0850: IElement pElement = null;
0851: if (presEle != null) {
0852: pElement = presEle.getFirstSubject();
0853: }
0854:
0855: // By default views should be resizable
0856: if (ui instanceof IETNodeUI) {
0857: ((IETNodeUI) ui).setResizable(true);
0858: }
0859:
0860: // Go through the attach logic to reinitialize the draw engine
0861: if (pElement != null) {
0862: // Call our base attach which will attach to this IElement - unlike Create this does not
0863: // create a new IElement, it uses the argument 'pVal'
0864: ui.setModelElement(pElement);
0865: attach(pElement, sInitializationString, ui);
0866: m_Engine = ui.getDrawEngine();
0867: }
0868:
0869: // Reset the size of this node
0870: pThis.sizeToContents();
0871:
0872: // Reset the node shape
0873: if (m_Engine != null) {
0874: if (m_Engine instanceof INodeDrawEngine) {
0875: INodeDrawEngine pNodeDrawEngine = (INodeDrawEngine) m_Engine;
0876: pNodeDrawEngine.setNodeShape(null);
0877: }
0878: }
0879:
0880: // Reset the labels and edges
0881: pLabelManager = m_Engine.getLabelManager();
0882: if (pLabelManager != null) {
0883: pLabelManager.resetLabels();
0884: }
0885: IEventManager pEventManager = m_Engine.getEventManager();
0886: if (pEventManager != null) {
0887: pEventManager.resetEdges();
0888: }
0889: }
0890: }
0891:
0892: public static void reattach(IElement pElement,
0893: String sInitializationString, IETGraphObjectUI ui) {
0894: // Discard labels
0895: ILabelManager pLabelManager = null;
0896: IDrawEngine pEngine = ui.getDrawEngine();
0897: if (pEngine != null) {
0898: pLabelManager = pEngine.getLabelManager();
0899: if (pLabelManager != null) {
0900: pLabelManager.discardAllLabels();
0901: }
0902: }
0903:
0904: // Reset presentation
0905: IETGraphObject etObj = null;
0906: ITSGraphObject graphObj = ui.getTSObject();
0907: if (graphObj != null) {
0908: if (graphObj instanceof IETNode) {
0909: etObj = (IETNode) graphObj;
0910: } else if (graphObj instanceof IETEdge) {
0911: etObj = (IETEdge) graphObj;
0912: }
0913:
0914: IPresentationElement cpPE = etObj.getPresentationElement();
0915: if (cpPE != null) {
0916: cpPE.setXMIID("");
0917: etObj.setPresentationElement(null);
0918: }
0919:
0920: // Re-attach to the IElement, because we cleared out the presentation element this should re-create the presentation element
0921: attachAndCreatePresentationElement(pElement,
0922: sInitializationString, true, ui);
0923:
0924: cpPE = etObj.getPresentationElement();
0925: if (cpPE instanceof IProductGraphPresentation) {
0926: IProductGraphPresentation cpProductGraphPresentation = (IProductGraphPresentation) cpPE;
0927: cpProductGraphPresentation.invalidate();
0928: }
0929: }
0930:
0931: // Reset labels
0932: if (pEngine != null) {
0933: pLabelManager = pEngine.getLabelManager();
0934: if (pLabelManager != null) {
0935: pLabelManager.resetLabels();
0936: }
0937: }
0938: }
0939:
0940: static public IETRect getDeviceBounds(TSTransform transform,
0941: IETGraphObjectUI ui) {
0942: IETRect deviceRect = transform != null && ui.getOwner() != null ? new ETDeviceRect(
0943: transform.boundsToDevice(ui.getOwner().getBounds()))
0944: : null;
0945: if (deviceRect != null) {
0946: // We want the inside bounds of the clipping region.
0947: deviceRect.deflateRect(1, 1);
0948: }
0949: return deviceRect;
0950: }
0951:
0952: /*
0953: * Returns the device bounding rectangle.
0954: */
0955: static public IETRect getDeviceBounds(TSEGraphics graphics,
0956: IETGraphObjectUI ui) {
0957: return graphics != null ? getDeviceBounds(graphics
0958: .getTSTransform(), ui) : null;
0959: }
0960:
0961: /*
0962: * Returns the device bounding rect.
0963: */
0964: static public IETRect getDeviceBounds(IETGraphObjectUI ui) {
0965: TSEGraphWindow wnd = getGraphWindow(ui);
0966: return wnd != null ? getDeviceBounds(wnd.getTransform(), ui)
0967: : null;
0968: }
0969:
0970: /*
0971: * Returns the IDrawingArea Interface.
0972: */
0973: static public IDrawingAreaControl getDrawingArea(IETGraphObjectUI ui) {
0974: return getDrawingArea(getGraphWindow(ui));
0975: }
0976:
0977: /*
0978: * Returns the TSEGraphWindow.
0979: */
0980: static public TSEGraphWindow getGraphWindow(IETGraphObjectUI ui) {
0981: TSEGraphWindow retVal = null;
0982:
0983: TSEObject owner = ui.getOwner();
0984: if (ui != null && owner != null) {
0985: TSGraph graph = owner.getOwnerGraph();
0986: if (graph != null) {
0987: TSGraphManager manager = graph.getOwnerGraphManager();
0988: if (manager instanceof TSEGraphManager) {
0989: TSEGraphManager tseManager = (TSEGraphManager) manager;
0990: retVal = tseManager.getGraphWindow();
0991: //return ((TSEGraphManager)ui.getOwner().getOwnerGraph().getOwnerGraphManager()).getGraphWindow();
0992: }
0993: }
0994: }
0995:
0996: return retVal;
0997: }
0998:
0999: /*
1000: * Returns World points, (Logical)
1001: */
1002: static public IETRect getLogicalBounds(IETGraphObjectUI ui) {
1003: return ui != null && ui.getOwner() != null ? new ETRectEx(ui
1004: .getOwner().getBounds()) : null;
1005: }
1006:
1007: /*
1008: * This method returns the bounding box that fully encloses this object in local coordinate system.
1009: */
1010: static public IETRect getLogicalUIBounds(IETGraphObjectUI ui) {
1011: return ui != null && ui.getOwner() != null ? new ETRectEx(ui
1012: .getBounds()) : null;
1013: }
1014:
1015: static public IDrawingAreaControl getDrawingArea(
1016: TSEGraphWindow graphWindow) {
1017: return graphWindow instanceof ADGraphWindow ? ((ADGraphWindow) graphWindow)
1018: .getDrawingArea()
1019: : null;
1020: }
1021:
1022: static public IDrawInfo getDrawInfo(TSEGraphics graphics,
1023: IETGraphObjectUI ui) {
1024: // Don't draw invalid DrawEngines.
1025: if (ui.getDrawEngine() == null)
1026: return null;
1027:
1028: // Make sure the draw engine is initialized,
1029: if (!ui.getDrawEngine().isInitialized()) {
1030: // Interactive edges don't have DrawEngines let them continue.
1031: if (!(ui instanceof IETEdgeUI))
1032: return null;
1033: }
1034:
1035: IDrawInfo retVal;
1036: TSEGraphWindow graphWindow = graphics != null ? graphics
1037: .getGraphWindow() : null;
1038: if (graphWindow != null) {
1039: /*
1040: if (!graphWindow.getGraph().isBoundsUpdatingEnabled())
1041: {
1042: // Don't the objects draw on graphs with unknow bounds.
1043: return null;
1044: }
1045: */
1046: retVal = new ETDrawInfo(graphics);
1047:
1048: if (ui.getDrawingArea() == null) {
1049: // Make sure the ui has a valid poiter to the drawing area
1050: // This can happend durning the loading process.
1051: ui.setDrawingArea(getDrawingArea(graphWindow));
1052: }
1053:
1054: if (ui.getOwner() != null) {
1055: retVal.setBoundingRect(getLogicalBounds(ui));
1056: retVal.setDeviceBounds(getDeviceBounds(graphics, ui));
1057: retVal.setGraphObject((IETGraphObject) ui.getOwner());
1058: }
1059: } else {
1060: retVal = null;
1061: }
1062:
1063: // TODO: Determine what the DrawinToMainDrawingArea and AlwaysSetFont
1064: // Should be set to.
1065:
1066: return retVal;
1067: }
1068:
1069: /*
1070: * Returns true if any portion of the rect touches the view port.
1071: */
1072: public static boolean isLogicalRectOnScreen(
1073: TSEGraphWindow graphWindow, TSConstRect rect) {
1074: if (graphWindow != null) {
1075: TSConstRect worldBounds = graphWindow.getTransform()
1076: .getWorldBounds();
1077: TSRect uiBounds = new TSRect();
1078: graphWindow.getGraph()
1079: .getLocalToMainDisplayGraphTransform()
1080: .transformRect(rect, uiBounds);
1081: return worldBounds.intersects(uiBounds);
1082: }
1083: return false;
1084: }
1085:
1086: /*
1087: * Returns true if any portion of the rect touches the view port.
1088: */
1089: public static boolean isLogicalRectOnScreen(TSEGraph graph,
1090: TSConstRect rect) {
1091: return graph != null ? isLogicalRectOnScreen(
1092: ((TSEGraphManager) graph.getOwnerGraphManager())
1093: .getGraphWindow(), rect) : false;
1094: }
1095:
1096: /*
1097: * Returns true if any portion of the rect touches the view port.
1098: */
1099: public static boolean isLogicalRectOnScreen(TSEGraphics g,
1100: TSConstRect rect) {
1101: TSEGraphWindow graphWindow = g != null ? g.getGraphWindow()
1102: : null;
1103: if (graphWindow != null) {
1104: return isLogicalRectOnScreen(graphWindow, rect);
1105: }
1106: return false;
1107: }
1108:
1109: /*
1110: * Returns true if any portion of the UI is visible within the visible display area (view port). Graphics can be null.
1111: */
1112: public static boolean isOnTheScreen(TSEGraphics g,
1113: IETGraphObjectUI ui) {
1114: if (ui != null && ui.getOwner() != null
1115: && ui.getOwner().isVisible()) {
1116: TSEGraphWindow graphWindow = g != null ? g.getGraphWindow()
1117: : ui.getDrawEngine().getDrawingArea()
1118: .getGraphWindow();
1119: return isLogicalRectOnScreen(graphWindow, ui.getBounds());
1120: }
1121: return false;
1122: }
1123:
1124: /*
1125: public static TSConstRect getLogicalClippingRect(TSEGraphics graphics, IETGraphObjectUI ui)
1126: {
1127: TSConstRect logicalRect = ui.getBounds();
1128:
1129: TSRect clipRect = new TSRect();
1130: TSConstSize clipSize = new TSConstSize(logicalRect.getWidth() + 1.51, logicalRect.getHeight() + 1.51);
1131: clipRect.setBoundsFromCenter(logicalRect.getCenter(), clipSize);
1132: return clipRect;
1133: //return RectConversions.etRectToTSRect(logicalRect);
1134: }
1135: */
1136: }
|