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.controls.drawingarea;
0043:
0044: import java.awt.Toolkit;
0045: import java.awt.image.BufferedImage;
0046: import java.util.Comparator;
0047: import java.util.Iterator;
0048: import java.util.List;
0049: import java.util.Stack;
0050: import java.util.TreeMap;
0051: import java.util.Vector;
0052: import org.netbeans.modules.uml.common.generics.IteratorT;
0053: import org.netbeans.modules.uml.core.metamodel.core.foundation.IElement;
0054: import org.netbeans.modules.uml.core.metamodel.core.foundation.INamedElement;
0055: import org.netbeans.modules.uml.core.metamodel.core.foundation.IPresentationElement;
0056: import org.netbeans.modules.uml.core.metamodel.diagrams.EdgeMapLocation;
0057: import org.netbeans.modules.uml.core.metamodel.diagrams.GraphicExportDetails;
0058: import org.netbeans.modules.uml.core.metamodel.diagrams.IDiagram;
0059: import org.netbeans.modules.uml.core.metamodel.diagrams.IEdgeMapLocation;
0060: import org.netbeans.modules.uml.core.metamodel.diagrams.IGraphicExportDetails;
0061: import org.netbeans.modules.uml.core.metamodel.diagrams.IGraphicMapLocation;
0062: import org.netbeans.modules.uml.core.metamodel.diagrams.ILabelMapLocation;
0063: import org.netbeans.modules.uml.core.metamodel.diagrams.INodeMapLocation;
0064: import org.netbeans.modules.uml.core.metamodel.diagrams.LabelMapLocation;
0065: import org.netbeans.modules.uml.core.metamodel.diagrams.NodeMapLocation;
0066: import org.netbeans.modules.uml.core.support.umlsupport.ETDeviceRect;
0067: import org.netbeans.modules.uml.core.support.umlsupport.ETPoint;
0068: import org.netbeans.modules.uml.core.support.umlsupport.ETRect;
0069: import org.netbeans.modules.uml.core.support.umlsupport.IETPoint;
0070: import org.netbeans.modules.uml.core.support.umlsupport.IETRect;
0071: import org.netbeans.modules.uml.core.support.umlsupport.Log;
0072: import org.netbeans.modules.uml.core.support.umlutils.ETArrayList;
0073: import org.netbeans.modules.uml.core.support.umlutils.ETList;
0074: import org.netbeans.modules.uml.ui.products.ad.graphobjects.ETGraph;
0075: import org.netbeans.modules.uml.ui.products.ad.graphobjects.ETNode;
0076: import org.netbeans.modules.uml.ui.products.ad.viewfactory.ETEGraphImageEncoder;
0077: import org.netbeans.modules.uml.ui.support.ImageTransferable;
0078: import org.netbeans.modules.uml.ui.support.applicationmanager.IGraphPresentation;
0079: import org.netbeans.modules.uml.ui.support.applicationmanager.INodePresentation;
0080: import org.netbeans.modules.uml.ui.support.viewfactorysupport.ETRectEx;
0081: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IETEdge;
0082: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IETLabel;
0083: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IETNode;
0084: import org.netbeans.modules.uml.ui.support.viewfactorysupport.TypeConversions;
0085: import com.tomsawyer.diagramming.TSCutCopyPasteControl;
0086: import com.tomsawyer.drawing.TSDGraph;
0087: import com.tomsawyer.drawing.TSDNode;
0088: import com.tomsawyer.drawing.TSPEdge;
0089: import com.tomsawyer.editor.TSEEdge;
0090: import com.tomsawyer.editor.TSEGraph;
0091: import com.tomsawyer.editor.TSEGraphManager;
0092: import com.tomsawyer.editor.TSEGraphWindow;
0093: import com.tomsawyer.editor.TSENode;
0094: import com.tomsawyer.graph.TSEdge;
0095: import com.tomsawyer.graph.TSGraphObject;
0096: import com.tomsawyer.graph.TSNode;
0097: import com.tomsawyer.drawing.geometry.TSConstPoint;
0098: import com.tomsawyer.drawing.geometry.TSConstRect;
0099: import com.tomsawyer.drawing.geometry.TSRect;
0100: import com.tomsawyer.editor.TSTransform;
0101: import com.tomsawyer.editor.TSEGraphImageEncoder;
0102: import com.tomsawyer.editor.complexity.TSEHidingManager;
0103: import com.tomsawyer.editor.complexity.command.TSEHideCommand;
0104: import com.tomsawyer.editor.complexity.command.TSEUnhideCommand;
0105: import com.tomsawyer.editor.tool.TSEPasteTool;
0106: import org.netbeans.modules.uml.ui.swing.drawingarea.ADGraphWindow;
0107: import org.netbeans.modules.uml.ui.support.visitors.ETFirstSubjectSameVistor;
0108: import org.netbeans.modules.uml.ui.support.visitors.ETGraphObjectTraversal;
0109: import org.netbeans.modules.uml.ui.support.visitors.ETXMIIDEqualsVisitor;
0110: import org.netbeans.modules.uml.ui.support.visitors.IETGraphObjectVisitor;
0111: import org.netbeans.modules.uml.ui.swing.drawingarea.IDrawingAreaControl;
0112: import org.netbeans.modules.uml.ui.swing.drawingarea.diagramtools.ADPasteState;
0113: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IETGraphObject;
0114: import org.netbeans.modules.uml.core.support.Debug;
0115:
0116: /**
0117: * @author sumitabhk
0118: *
0119: */
0120: public class GetHelper {
0121:
0122: /**
0123: *
0124: */
0125: public GetHelper() {
0126: super ();
0127: }
0128:
0129: public static boolean getShowGrid(TSEGraphWindow curEditor) {
0130: return curEditor != null ? curEditor.hasGrid() : false;
0131: }
0132:
0133: public static boolean saveAsGraphic(TSEGraphWindow curEditor,
0134: String sFilename, int nKind) {
0135: ETEGraphImageEncoder imageGenerator = new ETEGraphImageEncoder(
0136: curEditor, sFilename, nKind);
0137: return imageGenerator.save(1);
0138: }
0139:
0140: public static IGraphicExportDetails saveAsGraphic2(
0141: TSEGraphWindow curEditor, String sFilename, int nKind) {
0142: return saveAsGraphic2(curEditor, sFilename, nKind, 1);
0143: }
0144:
0145: public static IGraphicExportDetails saveAsGraphic2(
0146: TSEGraphWindow curEditor, String sFilename, int nKind,
0147: double scale) {
0148: ETEGraphImageEncoder imageGenerator = new ETEGraphImageEncoder(
0149: curEditor, sFilename, nKind);
0150: if (imageGenerator.save(scale)) {
0151: TSConstRect graphBounds = (TSConstRect) curEditor
0152: .getGraph().getBounds();
0153:
0154: return generateExportMap(curEditor,
0155: graphBounds != null ? (int) graphBounds.getWidth()
0156: : 0,
0157: graphBounds != null ? (int) graphBounds.getHeight()
0158: : 0, curEditor.getZoomLevel(),
0159: imageGenerator.getEncoderTransform());
0160: }
0161: return null;
0162: }
0163:
0164: //
0165: // public static IGraphicExportDetails generateImage(TSEGraphWindow curEditor, String sFilename, int nKind, double scale)
0166: // {
0167: // curEditor.setZoomLevel(scale, false);
0168: // ETEGraphImageEncoder imageGenerator = new ETEGraphImageEncoder(curEditor, sFilename, nKind);
0169: //
0170: // if (imageGenerator.save())
0171: // {
0172: // TSConstRect graphBounds = (TSConstRect) curEditor.getGraph().getBounds();
0173: // TSTransform transform = imageGenerator.getEncoderTransform();
0174: // return generateExportMap(curEditor, graphBounds != null ? (int) graphBounds.getWidth() : 0, graphBounds != null ? (int) graphBounds.getHeight() : 0, scale, transform);
0175: // }
0176: // return null;
0177: // }
0178: //
0179:
0180: /**
0181: * This method creates a buffered image of the graph the graph window holds.
0182: * If the visibleAreaOnly is true, then the zoomType setting is ignored and
0183: * whatever is drawn in the viewport is saved to the image. Otherwise, the
0184: * zoomType setting is taken into account.
0185: *
0186: * @return The image of the graph.
0187: */
0188: public static BufferedImage exportAsImage(TSEGraphWindow curEditor,
0189: boolean visibleAreaOnly, int zoomType, boolean drawGrid,
0190: boolean selectedOnly) {
0191: TSEGraphImageEncoder imageGenerator = new TSEGraphImageEncoder(
0192: curEditor);
0193:
0194: return imageGenerator.getBufferedGraphImage(visibleAreaOnly,
0195: zoomType, drawGrid, selectedOnly);
0196: }
0197:
0198: public static void transferImageToClipboard(TSEGraphWindow wnd) {
0199: BufferedImage image = GetHelper.exportAsImage(wnd, false, 0,
0200: false, true);
0201: if (image != null) {
0202: ImageTransferable transferable = new ImageTransferable(
0203: image);
0204: Toolkit.getDefaultToolkit().getSystemClipboard()
0205: .setContents(transferable, null);
0206: }
0207: }
0208:
0209: /**
0210: Creates an export map for use with the HTML report
0211: *
0212: */
0213: public static IGraphicExportDetails generateExportMap(
0214: TSEGraphWindow pCurrentEditor, int widthInWorldCoordinates,
0215: int heightInWorldCoordinates, double scaleFactor,
0216: final TSTransform imageTransform) {
0217: if (pCurrentEditor == null)
0218: return null;
0219:
0220: IGraphicExportDetails tempDetails = new GraphicExportDetails();
0221: try {
0222: final double scale = scaleFactor;
0223:
0224: // Get the list of locations
0225: final ETList<IGraphicMapLocation> pLocations = tempDetails
0226: .getMapLocations();
0227:
0228: // Set the rectangle on the output map
0229: IETRect pBoundingRect = new ETRect();
0230:
0231: if (pBoundingRect != null) {
0232: pBoundingRect.setSides(0, heightInWorldCoordinates,
0233: widthInWorldCoordinates, 0);
0234: tempDetails.setGraphicBoundingRect(pBoundingRect);
0235: IETRect frameRect = new ETRect();
0236: frameRect.setSides(0, (int) pCurrentEditor.getGraph()
0237: .getFrameBounds().getHeight(),
0238: (int) pCurrentEditor.getGraph()
0239: .getFrameBounds().getWidth(), 0);
0240: tempDetails.setFrameBoundingRect(frameRect);
0241: }
0242:
0243: TSEGraph pCurrentGraph = pCurrentEditor.getGraph();
0244: // Get the size of the bounding rectangle
0245: if (pCurrentGraph != null) {
0246: // Calculate the scale factor for each object
0247: // determine the bounding rectangle for the diagram
0248: final IETRect pObjectsBoundingRect = new ETRectEx(
0249: pCurrentGraph.getUI().getBounds());
0250: final int boundingRectLeft;
0251: final int boundingRectRight;
0252: final int boundingRectTop;
0253: final int boundingRectBottom;
0254: final int boundingRectWidth;
0255: final int boundingRectHeight;
0256:
0257: // Get the details on the bounding rect
0258: if (pObjectsBoundingRect != null) {
0259: boundingRectLeft = pObjectsBoundingRect.getLeft();
0260: boundingRectRight = pObjectsBoundingRect.getRight();
0261: boundingRectTop = pObjectsBoundingRect.getTop();
0262: boundingRectBottom = pObjectsBoundingRect
0263: .getBottom();
0264: boundingRectWidth = pObjectsBoundingRect
0265: .getIntWidth();
0266: boundingRectHeight = pObjectsBoundingRect
0267: .getIntHeight();
0268: } else {
0269: boundingRectLeft = 0;
0270: boundingRectRight = 0;
0271: boundingRectTop = 0;
0272: boundingRectBottom = 0;
0273: boundingRectWidth = 0;
0274: boundingRectHeight = 0;
0275: }
0276:
0277: if (boundingRectWidth > 0 && boundingRectHeight > 0) {
0278: // Now go through the objects within the diagram
0279: GetHelper.visit(pCurrentEditor,
0280: new IETGraphObjectVisitor() {
0281: public boolean visit(
0282: IETGraphObject cpObject) {
0283: IGraphicMapLocation pLocation;
0284: if (cpObject.isNode()) {
0285: pLocation = getNodeMapLocation(
0286: (IETNode) cpObject,
0287: scale,
0288: boundingRectLeft,
0289: boundingRectBottom,
0290: imageTransform);
0291: } else if (cpObject.isEdge()) {
0292: pLocation = getEdgeMapLocation(
0293: (IETEdge) cpObject,
0294: scale,
0295: boundingRectLeft,
0296: boundingRectBottom,
0297: imageTransform);
0298: } else if (cpObject.isLabel()) {
0299: pLocation = getLabelMapLocation(
0300: (IETLabel) cpObject,
0301: scale,
0302: boundingRectLeft,
0303: boundingRectBottom,
0304: imageTransform);
0305: } else {
0306: pLocation = null;
0307: }
0308:
0309: if (pLocation != null) {
0310: pLocations.add(pLocation);
0311: }
0312: return true;
0313: }
0314: });
0315: }
0316: }
0317: } catch (Exception e) {
0318: Log.stackTrace(e);
0319: }
0320: return tempDetails;
0321: }
0322:
0323: /**
0324: Creates an INodeMapLocation from an argument node
0325: *
0326: @param pNode [in] The node to generate a location map for
0327: @param scaleFactor [in] The current scale factor
0328: @param boundingRectLeft [in] The bounding rect left side
0329: @param boundingRectBottom [in] The bounding rect bottom side
0330: @param pLocation [out] The location of this node
0331: */
0332: public static boolean getObjectMapLocation(IETGraphObject node,
0333: double scaleFactor, int boundingRectLeft,
0334: int boundingRectBottom, IGraphicMapLocation pMapLocation,
0335: final TSTransform imageTransform) {
0336: boolean hr = false;
0337: try {
0338: // Get the coordinate of the node
0339: IElement pNodeElement = TypeConversions.getElement(node);
0340:
0341: if (pNodeElement != null) {
0342: String sXMIID = pNodeElement.getXMIID();
0343:
0344: String sElementType = pNodeElement.getElementType();
0345:
0346: INamedElement pNamedElement = pNodeElement instanceof INamedElement ? (INamedElement) pNodeElement
0347: : null;
0348: String sName = pNamedElement != null ? pNamedElement
0349: .getQualifiedName() : null;
0350:
0351: if (sXMIID != null && sXMIID.length() > 0) {
0352: // Convert the world rect into image device.
0353: IETRect nodeRect = new ETDeviceRect(imageTransform
0354: .boundsToDevice(node.getBounds()));
0355:
0356: if (pMapLocation != null) {
0357: IETRect pRect = (IETRect) nodeRect.clone();
0358:
0359: if (pMapLocation instanceof INodeMapLocation)
0360: ((INodeMapLocation) pMapLocation)
0361: .setLocation(pRect);
0362: else if (pMapLocation instanceof ILabelMapLocation)
0363: ((ILabelMapLocation) pMapLocation)
0364: .setLocation(pRect);
0365:
0366: hr = populate(pNodeElement, pMapLocation);
0367: }
0368: } else {
0369: Debug.out.println("Null XMIID");
0370: }
0371: }
0372: } catch (Exception e) {
0373: Log.stackTrace(e);
0374: }
0375: return hr;
0376: }
0377:
0378: /**
0379: Creates an INodeMapLocation from an argument node
0380: *
0381: @param pNode [in] The node to generate a location map for
0382: @param scaleFactor [in] The current scale factor
0383: @param boundingRectLeft [in] The bounding rect left side
0384: @param boundingRectBottom [in] The bounding rect bottom side
0385: @param pLocation [out] The location of this node
0386: */
0387: public static INodeMapLocation getNodeMapLocation(IETNode node,
0388: double scaleFactor, int boundingRectLeft,
0389: int boundingRectBottom, final TSTransform imageTransform) {
0390: if (node == null)
0391: return null;
0392:
0393: INodeMapLocation pMapLocation = new NodeMapLocation();
0394: if (getObjectMapLocation(node, scaleFactor, boundingRectLeft,
0395: boundingRectBottom, pMapLocation, imageTransform))
0396: return pMapLocation;
0397: else
0398: return null;
0399: }
0400:
0401: /**
0402: Creates an ILabelMapLocation from an argument label
0403: *
0404: @param pLabel [in] The label to generate a location map for
0405: @param scaleFactor [in] The current scale factor
0406: @param boundingRectLeft [in] The bounding rect left side
0407: @param boundingRectBottom [in] The bounding rect bottom side
0408: @param pLocation [out] The location of this label
0409: */
0410: public static ILabelMapLocation getLabelMapLocation(IETLabel label,
0411: double scaleFactor, int boundingRectLeft,
0412: int boundingRectBottom, final TSTransform imageTransform) {
0413: if (label == null)
0414: return null;
0415:
0416: ILabelMapLocation pLocation = new LabelMapLocation();
0417:
0418: if (getObjectMapLocation(label, scaleFactor, boundingRectLeft,
0419: boundingRectBottom, pLocation, imageTransform))
0420: return pLocation;
0421: else
0422: return null;
0423: }
0424:
0425: /**
0426: Creates an IEdgeMapLocation from an argument node
0427: *
0428: @param pEdge [in] The edge to generate a location map for
0429: @param scaleFactor [in] The current scale factor
0430: @param boundingRectLeft [in] The bounding rect left side
0431: @param boundingRectBottom [in] The bounding rect bottom side
0432: @param pLocation [out] The location of this edge
0433: */
0434: public static IEdgeMapLocation getEdgeMapLocation(IETEdge pEdge,
0435: double scaleFactor, int boundingRectLeft,
0436: int boundingRectBottom, final TSTransform imageTransform) {
0437: if (pEdge == null)
0438: return null;
0439:
0440: IEdgeMapLocation pMapLocation = null;
0441: try {
0442: // Get the coordinate of the node
0443: IElement pEdgeElement = TypeConversions.getElement(pEdge);
0444:
0445: if (pEdgeElement != null) {
0446: Iterator<TSPEdge> pathEdgeIter = ((TSEEdge) pEdge)
0447: .pathIterator();
0448: ETList<IETPoint> pointList = new ETArrayList<IETPoint>();
0449: IETPoint lastPoint = null;
0450: boolean bFirst = true;
0451:
0452: while (pathEdgeIter.hasNext()) {
0453: TSPEdge pPathEdge = (TSPEdge) pathEdgeIter.next();
0454: TSConstPoint tsSourcePoint = pPathEdge
0455: .getLocalSourcePoint();
0456: TSConstPoint tsTargetPoint = pPathEdge
0457: .getLocalTargetPoint();
0458: IETPoint tempSourcePoint = new ETPoint(
0459: imageTransform.pointToDevice(tsSourcePoint));
0460: IETPoint tempTargetPoint = new ETPoint(
0461: imageTransform.pointToDevice(tsTargetPoint));
0462:
0463: // Add the point, but make sure we don't duplicate
0464: if (bFirst) {
0465: lastPoint = tempSourcePoint;
0466: pointList.add(tempSourcePoint);
0467: bFirst = false;
0468: }
0469:
0470: if (lastPoint != tempSourcePoint) {
0471: lastPoint = tempSourcePoint;
0472: pointList.add(tempSourcePoint);
0473: }
0474:
0475: if (lastPoint != tempTargetPoint) {
0476: lastPoint = tempTargetPoint;
0477: pointList.add(tempTargetPoint);
0478: }
0479: }
0480:
0481: // Create an object to return
0482: pMapLocation = new EdgeMapLocation();
0483: if (pMapLocation != null) {
0484: populate(pEdgeElement, pMapLocation);
0485:
0486: // Go over the point vector and add to the map
0487: ETList<IETPoint> pPointList = new ETArrayList<IETPoint>();
0488: if (pPointList != null) {
0489: IteratorT<IETPoint> iterator = new IteratorT<IETPoint>(
0490: pointList);
0491:
0492: while (iterator.hasNext()) {
0493: IETPoint tempPoint = iterator.next();
0494:
0495: if (tempPoint != null) {
0496: pPointList.add(tempPoint);
0497: }
0498: }
0499: }
0500: pMapLocation.setPoints(pPointList);
0501: }
0502: }
0503: } catch (Exception e) {
0504: Log.stackTrace(e);
0505: }
0506: return pMapLocation;
0507: }
0508:
0509: /**
0510: Populates the basic location information
0511: *
0512: @param pElement [in] The element that controls this edge or node
0513: @param pLocation [in] The location object that is to be populated
0514: */
0515: public static boolean populate(IElement pElement,
0516: IGraphicMapLocation pLocation) {
0517: if (pElement == null || pLocation == null)
0518: return false;
0519:
0520: boolean hr = true;
0521: try {
0522: String sXMIID = pElement.getXMIID();
0523: String sName = null;
0524: String sElementType = pElement.getElementType();
0525:
0526: // Get the information
0527:
0528: INamedElement pNamedElement = pElement instanceof INamedElement ? (INamedElement) pElement
0529: : null;
0530: if (pNamedElement != null) {
0531: sName = pNamedElement.getQualifiedName();
0532: }
0533:
0534: // Populate the information
0535: pLocation.setName(sName);
0536: pLocation.setElementXMIID(sXMIID);
0537: pLocation.setElementType(sElementType);
0538: pLocation.setElement(pElement);
0539: } catch (Exception e) {
0540: Log.stackTrace(e);
0541: }
0542: return hr;
0543: }
0544:
0545: /**
0546: * Reverses the incoming list
0547: *
0548: * @param pCurrentEditor [in] The current graph editor
0549: * @param pList [in] The list to reverse.
0550: */
0551: public static boolean reverseList(TSEGraphWindow curEditor,
0552: List pList) {
0553: if (pList == null)
0554: return false;
0555:
0556: Stack stack = new Stack();
0557: Iterator iter = pList.iterator();
0558: while (iter.hasNext()) {
0559: stack.push(iter.next());
0560: }
0561:
0562: pList.clear();
0563:
0564: while (stack.size() > 0) {
0565: pList.add(stack.pop());
0566: }
0567: return true;
0568: }
0569:
0570: /*
0571: * Returns the presentation element on the drawing area control with the specified xml id
0572: *
0573: * @param pCurrentEditor [in] The editor this GETHelper is reponsible for
0574: * @param pDiagram [in] The diagram that is our parent
0575: * @param sXMLID [in] The presentation element to search for on the diagram.
0576: * @param pPresentationElement [out,retval] The found presentation element.
0577: */
0578: public static IPresentationElement findPresentationElement(
0579: TSEGraphWindow curEditor, IDiagram pDia, String xmiid) {
0580: ETGraph graph = curEditor.getGraph() instanceof ETGraph ? (ETGraph) curEditor
0581: .getGraph()
0582: : null;
0583: ETGraphObjectTraversal traversal = new ETGraphObjectTraversal(
0584: graph);
0585: ETXMIIDEqualsVisitor visitor = new ETXMIIDEqualsVisitor(xmiid);
0586: traversal.addVisitor(visitor);
0587: return !traversal.traverse() ? visitor.getFoundPresentation()
0588: : null;
0589: /*
0590: IPresentationElement retObj = null;
0591: ETList < IPresentationElement > elems = getAllItems(curEditor, pDia);
0592: if (elems != null)
0593: {
0594: int count = elems.size();
0595: for (int i = 0; i < count; i++)
0596: {
0597: IPresentationElement pEle = elems.get(i);
0598: String eleXMIID = pEle.getXMIID();
0599: if (eleXMIID != null && eleXMIID.equals(xmiid))
0600: {
0601: retObj = pEle;
0602: break;
0603: }
0604: }
0605: }
0606: return retObj;
0607: */
0608: }
0609:
0610: /**
0611: * Returns a list of all the items
0612: */
0613: public static ETList<IPresentationElement> getAllItems(
0614: TSEGraphWindow curEditor, IDiagram pDia) {
0615: return getAllItems2(curEditor, pDia, null);
0616: }
0617:
0618: /**
0619: * Returns a list of all the items that represent the IElement
0620: */
0621: public static ETList<IPresentationElement> getAllItems2(
0622: TSEGraphWindow curEditor, IDiagram pDia, IElement modelEle) {
0623: final ETList<IPresentationElement> retObj = new ETArrayList<IPresentationElement>();
0624: if (modelEle != null && pDia != null) {
0625: IteratorT<IPresentationElement> iter = new IteratorT<IPresentationElement>(
0626: modelEle.getPresentationElements());
0627: while (iter.hasNext()) {
0628: IPresentationElement pEle = iter.next();
0629: if (pEle instanceof IGraphPresentation
0630: && ((IGraphPresentation) pEle)
0631: .getIsOnDiagram(pDia)) {
0632: retObj.add(pEle);
0633: }
0634: }
0635: } else if (curEditor != null) {
0636: ETGraph graph = curEditor.getGraph() instanceof ETGraph ? (ETGraph) curEditor
0637: .getGraph()
0638: : null;
0639: if (modelEle != null && graph != null) {
0640: ETGraphObjectTraversal traversal = new ETGraphObjectTraversal(
0641: graph);
0642: traversal.addVisitor(new ETFirstSubjectSameVistor(
0643: retObj, modelEle));
0644: traversal.traverse();
0645: } else if (graph != null) {
0646: ETGraphObjectTraversal traversal = new ETGraphObjectTraversal(
0647: graph);
0648: traversal.addVisitor(new IETGraphObjectVisitor() {
0649: public boolean visit(IETGraphObject object) {
0650: IPresentationElement presItem = object
0651: .getPresentationElement();
0652: if (presItem != null) {
0653: // Add it to the list
0654: retObj.add(presItem);
0655: }
0656: return true;
0657: }
0658: });
0659: traversal.traverse();
0660: }
0661:
0662: }
0663: return retObj;
0664: }
0665:
0666: /**
0667: * Returns all the graph objects
0668: * The stacking order of the output list is from bottom to top.
0669: */
0670: public static ETList<IETGraphObject> getAllGraphObjects(
0671: TSEGraphWindow curEditor) {
0672: ETGraph graph = curEditor != null
0673: && curEditor.getGraph() instanceof ETGraph ? (ETGraph) curEditor
0674: .getGraph()
0675: : null;
0676: return graph != null ? graph.getAllETGraphObjects() : null;
0677: }
0678:
0679: /**
0680: * Return all the objects on the diagram that are of the indicated type
0681: *
0682: * @param editor The editor this GETHelper is reponsible for
0683: * @param type The element type for the query
0684: * @return The presentation elements of this type
0685: */
0686: public static ETList<IPresentationElement> getAllByType(
0687: TSEGraphWindow editor, String type) {
0688: ETList<IPresentationElement> retVal = new ETArrayList<IPresentationElement>();
0689:
0690: if (editor != null && type != null) {
0691: ETGraphObjectTraversal traversal = new ETGraphObjectTraversal(
0692: (ETGraph) editor.getGraph());
0693: traversal.addVisitor(new ETElementTypesEqualVisitor(retVal,
0694: type));
0695: traversal.traverse();
0696: }
0697:
0698: return retVal;
0699: }
0700:
0701: public static ETList<IPresentationElement> getSelected(
0702: ETGraph etGraph) {
0703: if (etGraph != null) {
0704: ETList<IPresentationElement> selectdPes = new ETArrayList<IPresentationElement>();
0705: ETList<TSGraphObject> selected = etGraph
0706: .getSelectedObjects(false, false);
0707: if (selected != null) {
0708: Iterator<TSGraphObject> iter = selected.iterator();
0709: while (iter.hasNext()) {
0710: TSGraphObject tsObj = iter.next();
0711: IETGraphObject obj = tsObj instanceof IETGraphObject ? (IETGraphObject) tsObj
0712: : null;
0713: IPresentationElement pe = obj != null ? obj
0714: .getPresentationElement() : null;
0715: if (pe != null)
0716: selectdPes.add(pe);
0717: }
0718: }
0719: return selectdPes.size() > 0 ? selectdPes : null;
0720: }
0721: return null;
0722: }
0723:
0724: /**
0725: * Returns a list of the selected items (nodes and edges).
0726: */
0727: public static ETList<IETGraphObject> getSelected2(ETGraph etGraph) {
0728: if (etGraph != null) {
0729: ETList<IETGraphObject> selected = new ETArrayList<IETGraphObject>();
0730: List selectedNodes = etGraph.selectedNodes();
0731: if (selectedNodes != null)
0732: selected.addAll(selectedNodes);
0733:
0734: List selectedEdges = etGraph.selectedEdges();
0735: if (selectedEdges != null)
0736: selected.addAll(selectedEdges);
0737:
0738: return selected.size() > 0 ? selected : null;
0739: }
0740: return null;
0741: }
0742:
0743: /**
0744: * Returns a list of the selected items (nodes, edges and nodelabels and edgeLabels).
0745: */
0746: public static ETList<IETGraphObject> getSelected3(ETGraph etGraph) {
0747: if (etGraph != null) {
0748: // Get the selected nodes and edges.
0749: ETList<IETGraphObject> selected = getSelected2(etGraph);
0750: selected = selected != null ? selected
0751: : new ETArrayList<IETGraphObject>();
0752:
0753: // Now tack on the labels.
0754: List selectedNodeLabels = etGraph.selectedNodeLabels();
0755: if (selectedNodeLabels != null)
0756: selected.addAll(selectedNodeLabels);
0757:
0758: List selectedEdgeLabels = etGraph.selectedEdgeLabels();
0759: if (selectedEdgeLabels != null)
0760: selected.addAll(selectedEdgeLabels);
0761:
0762: return selected.size() > 0 ? selected : null;
0763: }
0764: return null;
0765: }
0766:
0767: /**
0768: * Returns a list of the selected items (nodes).
0769: */
0770: public static ETList<IETGraphObject> getSelectedNodes(
0771: ETGraph etGraph) {
0772: if (etGraph != null) {
0773: ETList<IETGraphObject> selected = new ETArrayList<IETGraphObject>();
0774: List selectedNodes = etGraph.selectedNodes();
0775:
0776: if (selectedNodes != null)
0777: selected.addAll(selectedNodes);
0778:
0779: return selected.size() > 0 ? selected : null;
0780: }
0781:
0782: return null;
0783: }
0784:
0785: /**
0786: * Calculates the rectangle that encompasses all the specified graph objects' veiw bounds rectangles
0787: * This call is equivalent to the C++ TSGraph::getBoundingRectOfObjects
0788: *
0789: * @param rvecTSGraphObjects [in] The vector of Tom Sawyer graph objects used in the calculation
0790: */
0791: public static TSRect calculateTSGraphObjectsRect(
0792: ETList<TSGraphObject> tsGraphObjects) {
0793: TSRect retVal = new TSRect(0, 0, 0, 0);
0794:
0795: for (Iterator<TSGraphObject> iter = tsGraphObjects.iterator(); iter
0796: .hasNext();) {
0797: TSGraphObject curObject = iter.next();
0798:
0799: TSConstRect bounds = null;
0800: if (curObject instanceof TSENode) {
0801: TSENode node = (TSENode) curObject;
0802: bounds = (TSConstRect) node.getBounds();
0803: } else if (curObject instanceof TSEEdge) {
0804: TSEEdge edge = (TSEEdge) curObject;
0805: bounds = (TSConstRect) edge.getBounds();
0806: }
0807:
0808: if (bounds != null) {
0809: retVal.merge(bounds);
0810: }
0811: }
0812:
0813: return retVal;
0814: }
0815:
0816: /**
0817: * Does this node have children
0818: */
0819: public static boolean hasChildren(TSEGraphWindow curEditor,
0820: TSNode prodNode) {
0821: boolean hasChildren = false;
0822: if (curEditor != null && prodNode != null) {
0823: List newNodeList = new Vector();
0824: prodNode.findChildren(newNodeList, null, 1);
0825:
0826: if (!newNodeList.isEmpty()) {
0827: hasChildren = true;
0828: }
0829: }
0830: return hasChildren;
0831: }
0832:
0833: /**
0834: * Does this node have parents
0835: */
0836: public static boolean hasParents(TSEGraphWindow curEditor,
0837: TSNode prodNode) {
0838: boolean hasParents = false;
0839: if (curEditor != null && prodNode != null) {
0840: List newNodeList = new Vector();
0841: prodNode.findParents(newNodeList, null, 1);
0842: if (newNodeList != null) {
0843: if (!newNodeList.isEmpty()) {
0844: hasParents = true;
0845: }
0846: }
0847: }
0848: return hasParents;
0849: }
0850:
0851: /**
0852: * Get all children upto specified level
0853: */
0854: public static void findChildren(TSENode prodNode, List children,
0855: long levels) {
0856: if (levels >= 1 && prodNode != null) {
0857: if (children == null) {
0858: children = new Vector();
0859: }
0860: List myChildren = new Vector();
0861: prodNode.findChildren(myChildren, null, 1);
0862: if (myChildren != null) {
0863: myChildren.removeAll(children);
0864: children.addAll(myChildren);
0865: if (levels > 1) {
0866: int ctr = 0;
0867: while (ctr < myChildren.size()) {
0868: TSENode pNode = (TSENode) myChildren.get(ctr++);
0869: findChildren(pNode, children, levels - 1);
0870: }
0871: }
0872: }
0873: }
0874: }
0875:
0876: /**
0877: * Get all parents upto specified level
0878: */
0879: public static void findParents(TSENode prodNode, List parents,
0880: long levels) {
0881: if (levels >= 1 && prodNode != null) {
0882: if (parents == null) {
0883: parents = new Vector();
0884: }
0885: List myParents = new Vector();
0886: prodNode.findParents(myParents, null, 1);
0887: if (myParents != null) {
0888: myParents.removeAll(parents);
0889: parents.addAll(myParents);
0890: if (levels > 1) {
0891: int ctr = 0;
0892: while (ctr < myParents.size()) {
0893: TSENode pNode = (TSENode) myParents.get(ctr++);
0894: findParents(pNode, parents, levels - 1);
0895: }
0896: }
0897: }
0898: }
0899: }
0900:
0901: // Fixed IZ=78350. Modified this method to use TS API to correctly get hidden children nodes
0902: // at all specified search levels.
0903: /**
0904: * Finds all the hidden children of a given node up to a specified search level
0905: * @param prodNode The node whose hidden children are searched
0906: * @param hiddenChildren A list of children found up to the specified search level
0907: * @param level An integer indicating the search depth. If the level equals to
0908: * ADCoreEngine.FIND_DEPTH_ALL, the method gets the hidden children found at all levels.
0909: * @param hidingManager TomSawyer hidingManager that performs the search.
0910: */
0911:
0912: public static void findHiddenChildren(TSENode prodNode,
0913: List hiddenChildren, long level,
0914: TSEHidingManager hidingManager) {
0915: if (prodNode == null) {
0916: return;
0917: }
0918:
0919: if (hiddenChildren == null) {
0920: hiddenChildren = new Vector();
0921: }
0922:
0923: hidingManager.findHiddenChildren(prodNode, level);
0924: List myHiddenChildren = hidingManager.getResultNodeList();
0925: if (myHiddenChildren != null) {
0926: myHiddenChildren.removeAll(hiddenChildren);
0927: hiddenChildren.addAll(myHiddenChildren);
0928: }
0929: }
0930:
0931: // Fixed IZ=78350. Modified this method to use TS API to correctly get hidden parents nodes
0932: // at all specified search levels.
0933: /**
0934: * Finds all the hidden parents of a given node up to a specified search level
0935: * @param prodNode The node whose hidden parents are searched
0936: * @param hiddenParens A list of hidden parents found up to the specified level
0937: * @param level An integer indicating the search depth. If the level equals
0938: * to ADCoreEngine.FIND_DEPTH_ALL, the method gets the hidden parents at all levels.
0939: * @param hidingManager TomSawyer hidingManager that performs the search.
0940: */
0941:
0942: public static void findHiddenParents(TSENode prodNode,
0943: List hiddenParents, long level,
0944: TSEHidingManager hidingManager) {
0945: if (prodNode == null) {
0946: return;
0947: }
0948:
0949: if (hiddenParents == null) {
0950: hiddenParents = new Vector();
0951: }
0952:
0953: hidingManager.findHiddenParents(prodNode, level);
0954: List foundHiddenParent = hidingManager.getResultNodeList();
0955: if (foundHiddenParent != null) {
0956: foundHiddenParent.removeAll(hiddenParents);
0957: hiddenParents.addAll(foundHiddenParent);
0958: }
0959: }
0960:
0961: /**
0962: * Does this node have hidden children
0963: */
0964: public static boolean hasHiddenChildren(TSEGraphWindow curEditor,
0965: TSNode prodNode) {
0966: boolean hasChildren = false;
0967: if (curEditor != null && prodNode != null) {
0968: TSEHidingManager hidingManager = (TSEHidingManager) TSEHidingManager
0969: .getManager(curEditor.getGraphManager());
0970: hasChildren = hidingManager
0971: .hasHiddenChildren((TSDNode) prodNode);
0972: }
0973: return hasChildren;
0974: }
0975:
0976: /**
0977: * Does this node have hidden parents
0978: */
0979: public static boolean hasHiddenParents(TSEGraphWindow curEditor,
0980: TSNode prodNode) {
0981: boolean hasChildren = false;
0982: if (curEditor != null && prodNode != null) {
0983: TSEHidingManager hidingManager = (TSEHidingManager) TSEHidingManager
0984: .getManager(curEditor.getGraphManager());
0985: hasChildren = hidingManager
0986: .hasHiddenParents((TSDNode) prodNode);
0987: }
0988: return hasChildren;
0989: }
0990:
0991: /**
0992: * Does this node have folded items
0993: */
0994: public static boolean hasFoldedItems(TSEGraphWindow curEditor,
0995: TSNode prodNode) {
0996: boolean hasChildren = false;
0997: if (curEditor != null && prodNode != null) {
0998: hasChildren = prodNode.isExpanded();
0999: }
1000: return hasChildren;
1001: }
1002:
1003: public static void fold(TSEGraphWindow curEditor, TSNode pNode,
1004: int depth, boolean hideChildren) {
1005: //to do implement
1006: }
1007:
1008: /**
1009: * Edge navigation
1010: */
1011: public static void gotoNodeEnd(TSEGraphWindow curEditor,
1012: TSEdge pNode, boolean bSourceNode) {
1013: TSNode pFoundNode = null;
1014: if (bSourceNode) {
1015: pFoundNode = pNode.getSourceNode();
1016: } else {
1017: pFoundNode = pNode.getTargetNode();
1018: }
1019: if (pFoundNode != null) {
1020: IETGraphObject tsObject = TypeConversions
1021: .getETGraphObject(pFoundNode);
1022: if (tsObject != null) {
1023: curEditor.deselectAll(false);
1024: curEditor.selectObject(tsObject.getObject(), true);
1025: }
1026: ETNode node = (ETNode) pFoundNode;
1027: TSConstPoint goToPoint = (TSConstPoint) node.getCenter();
1028: if (goToPoint != null) {
1029: curEditor.centerPointInWindow(goToPoint, true);
1030: }
1031: }
1032: }
1033:
1034: /**
1035: * Removes this presentation element from its associated IElement.
1036: *
1037: * @param pPresentationElement [in] The presentation element to whack.
1038: */
1039: public static void removePresentationElement(
1040: IPresentationElement pEle) {
1041: // Go over all the model elements associated with this presentation element and remove them
1042: if (pEle != null) {
1043: IteratorT<IElement> iter = new IteratorT<IElement>(pEle
1044: .getSubjects());
1045: while (iter.hasNext()) {
1046: iter.next().removePresentationElement(pEle);
1047: }
1048: }
1049: }
1050:
1051: /**
1052: Returns the mid point of the link
1053: *
1054: @param pEdge [in] The edge to return the mid point for
1055: @param pPoint [out,retval] The centerpoint for the edge (mode of target point and source point)
1056: */
1057: public static IETPoint getMidPoint(IETEdge pEdge) {
1058: if (pEdge == null)
1059: return null;
1060:
1061: try {
1062: // Get the from and to points
1063: TSConstPoint sourcePoint = (TSConstPoint) ((TSEEdge) pEdge)
1064: .getSourcePoint();
1065: TSConstPoint targetPoint = (TSConstPoint) ((TSEEdge) pEdge)
1066: .getTargetPoint();
1067:
1068: return new ETPoint((int) (Math.min(sourcePoint.getX(),
1069: targetPoint.getX()) + Math.abs(sourcePoint.getX()
1070: - targetPoint.getX()) / 2), (int) (Math.min(
1071: sourcePoint.getY(), targetPoint.getY()) + Math
1072: .abs(sourcePoint.getY() - targetPoint.getY()) / 2));
1073: } catch (Exception e) {
1074: Log.stackTrace(e);
1075: }
1076: return null;
1077: }
1078:
1079: public static boolean hasGraphObjects(TSEGraphWindow wnd) {
1080: boolean retVal = false;
1081:
1082: TSDGraph graph = getCurrentGraph(wnd);
1083: if (graph != null) {
1084: retVal = true;
1085: List nodes = graph.nodes();
1086:
1087: // You can't have edges without nodes, or connectors.
1088: // This looks fishy. (Kevin)
1089: if ((nodes == null) || (nodes.size() <= 0)) {
1090: List edges = graph.edges();
1091: if ((edges == null) || (edges.size() <= 0)) {
1092: if (graph.numberOfLabels() <= 0) {
1093: retVal = false;
1094: }
1095: }
1096: }
1097: }
1098:
1099: return retVal;
1100: }
1101:
1102: /**
1103: * @param wnd
1104: * @return
1105: */
1106: public static TSDGraph getCurrentGraph(TSEGraphWindow wnd) {
1107: TSDGraph retVal = null;
1108:
1109: if (wnd != null) {
1110: TSEGraphManager manager = wnd.getGraphManager();
1111: if (manager != null) {
1112: retVal = manager.getMainDisplayGraph();
1113: }
1114: }
1115:
1116: return retVal;
1117: }
1118:
1119: public static ETList<TSDNode> getAllNodes(
1120: TSEGraphWindow currentWindow) {
1121: if (currentWindow == null)
1122: return null;
1123:
1124: ETList<TSDNode> listNodes = null;
1125:
1126: TSDGraph currentGraph = getCurrentGraph(currentWindow);
1127: if (currentGraph != null) {
1128: listNodes = new ETArrayList<TSDNode>();
1129:
1130: IteratorT<IETGraphObject> itrGO = new IteratorT<IETGraphObject>(
1131: getAllGraphObjects(currentWindow));
1132:
1133: while (itrGO.hasNext()) {
1134: IETGraphObject tsObject = itrGO.next();
1135:
1136: if (tsObject instanceof TSDNode)
1137: listNodes.add((TSDNode) tsObject);
1138: }
1139: }
1140:
1141: return listNodes != null && listNodes.size() > 0 ? listNodes
1142: : null;
1143: }
1144:
1145: public static ETList<TSDNode> getAllNodesByMatch(
1146: TSEGraphWindow currentWindow, String elementType,
1147: Comparator compare) {
1148: ETList<TSDNode> listNodes = null;
1149:
1150: ETList<TSDNode> listAllNodes = getAllNodes(currentWindow);
1151:
1152: if (listAllNodes != null) {
1153: TSDGraph currentGraph = getCurrentGraph(currentWindow);
1154:
1155: if (currentGraph != null) {
1156: listNodes = new ETArrayList<TSDNode>();
1157: IteratorT<TSDNode> itrNode = new IteratorT<TSDNode>(
1158: listAllNodes);
1159:
1160: while (itrNode.hasNext()) {
1161: TSDNode node = itrNode.next();
1162:
1163: if (compare.compare(node, elementType) == 0)
1164: listNodes.add(node);
1165: }
1166: }
1167: }
1168:
1169: return listNodes != null && listNodes.size() > 0 ? listNodes
1170: : null;
1171: }
1172:
1173: private static class MatchElementType implements Comparator {
1174: public int compare(Object left, Object right) {
1175: String elementType = (String) right;
1176: IElement element = TypeConversions
1177: .getElement((TSGraphObject) left);
1178:
1179: // Return 0 when the compared elements are equal, see Comparator.compare()
1180: return element != null
1181: && element.getElementType().equals(elementType) ? 0
1182: : 1;
1183: }
1184: }
1185:
1186: public static ETList<TSDNode> getAllNodesByElementType(
1187: TSEGraphWindow currentWindow, String elementType) {
1188: return getAllNodesByMatch(currentWindow, elementType,
1189: new MatchElementType());
1190: }
1191:
1192: public static ETList<TSDNode> sortObjectsLeftToRight(
1193: TSEGraphWindow currentWindow,
1194: ETList<TSDNode> listOriginalNodes) {
1195: ETList<TSDNode> listNodes = null;
1196:
1197: if (listOriginalNodes != null) {
1198: ETList<TSDNode> list = new ETArrayList<TSDNode>();
1199:
1200: TSDNode nodeOnLeft = null;
1201:
1202: double minX = Double.MAX_VALUE;
1203: int count = listOriginalNodes.getCount();
1204:
1205: while (count > 0) {
1206: Iterator<TSDNode> itrNode = listOriginalNodes
1207: .iterator();
1208:
1209: while (itrNode.hasNext()) {
1210: TSDNode node = itrNode.next();
1211:
1212: if (node != null) {
1213: if (nodeOnLeft == null) {
1214: nodeOnLeft = node;
1215: minX = nodeOnLeft.getCenter().getX();
1216: } else {
1217: double x = node.getCenter().getX();
1218: if (x < minX) {
1219: minX = x;
1220: nodeOnLeft = node;
1221: }
1222: }
1223: }
1224: }
1225:
1226: list.add(nodeOnLeft);
1227: listOriginalNodes.remove(nodeOnLeft);
1228: nodeOnLeft = null;
1229:
1230: count = listOriginalNodes.getCount();
1231: }
1232:
1233: listNodes = list;
1234: }
1235:
1236: return listNodes;
1237: }
1238:
1239: public static ETList<IPresentationElement> sortNodesLeftToRight(
1240: ETList<IPresentationElement> unsortedList) {
1241: ETList<IPresentationElement> sortedList = new ETArrayList<IPresentationElement>();
1242:
1243: if ((unsortedList != null) && (unsortedList.getCount() > 0)) {
1244: TreeMap<Integer, IPresentationElement> mapPEs = new TreeMap<Integer, IPresentationElement>();
1245:
1246: for (Iterator iter = unsortedList.iterator(); iter
1247: .hasNext();) {
1248: IPresentationElement pe = (IPresentationElement) iter
1249: .next();
1250: if (pe instanceof INodePresentation) {
1251: INodePresentation nodePE = (INodePresentation) pe;
1252:
1253: mapPEs.put(new Integer(nodePE.getCenter().getX()),
1254: pe);
1255: }
1256: }
1257:
1258: for (Iterator iterator = mapPEs.values().iterator(); iterator
1259: .hasNext();) {
1260: IPresentationElement pe = (IPresentationElement) iterator
1261: .next();
1262: sortedList.add(pe);
1263: }
1264: }
1265:
1266: return sortedList;
1267: }
1268:
1269: public static ETList<TSEEdge> getAllEdges(TSEGraphWindow graphWindow) {
1270: ETList<TSEEdge> listEdges = null;
1271:
1272: TSDGraph currentGraph = getCurrentGraph(graphWindow);
1273: if (currentGraph != null) {
1274: listEdges = new ETArrayList<TSEEdge>();
1275:
1276: IteratorT<TSEEdge> iter = new IteratorT<TSEEdge>(
1277: currentGraph.edges());
1278: while (iter.hasNext()) {
1279: listEdges.add(iter.next());
1280: }
1281: }
1282:
1283: return listEdges != null && listEdges.size() > 0 ? listEdges
1284: : null;
1285: }
1286:
1287: public static ETList<TSEEdge> getAllEdgesByMatch(
1288: TSEGraphWindow graphWindow, String elementType,
1289: Comparator compare) {
1290: ETList<TSEEdge> listEdges = null;
1291: ETList<TSEEdge> listAllEdges = getAllEdges(graphWindow);
1292:
1293: if (listAllEdges != null) {
1294: TSDGraph currentGraph = getCurrentGraph(graphWindow);
1295:
1296: if (currentGraph != null) {
1297: listEdges = new ETArrayList<TSEEdge>();
1298:
1299: Iterator<TSEEdge> itrEdge = listAllEdges.iterator();
1300:
1301: while (itrEdge.hasNext()) {
1302: TSEEdge edge = itrEdge.next();
1303:
1304: if (compare.compare(edge, elementType) == 0)
1305: listEdges.add(edge);
1306: }
1307: }
1308: }
1309: return listEdges != null && listEdges.size() > 0 ? listEdges
1310: : null;
1311: }
1312:
1313: public static ETList<TSEEdge> getAllEdgesByElementType(
1314: TSEGraphWindow graphWindow, String elementType) {
1315: return getAllEdgesByMatch(graphWindow, elementType,
1316: new MatchElementType());
1317: }
1318:
1319: public static boolean handleAccelerator(
1320: IDrawingAreaControl pCurrentEditor, String accelerator,
1321: boolean bSingleNodeOnly) {
1322: boolean bRetVal = false;
1323:
1324: ETList<IPresentationElement> pPresentationElements = pCurrentEditor
1325: .getSelected();
1326: if (pPresentationElements != null) {
1327: // fetch selected node count
1328: int count = pPresentationElements.size();
1329:
1330: // if we're to forward this accelerator to just a single node, return if none or 2+ nodes are selected
1331: if (bSingleNodeOnly && count != 1) {
1332: return bRetVal;
1333: }
1334:
1335: // either we have 1 node or OK for multiple nodes
1336: for (int i = 0; i < count; i++) {
1337: IPresentationElement pPresentationElement = pPresentationElements
1338: .get(i);
1339:
1340: if (pPresentationElement != null) {
1341: IETGraphObject pETGraphObject = TypeConversions
1342: .getETGraphObject(pPresentationElement);
1343:
1344: if (pETGraphObject != null) {
1345: bRetVal = pETGraphObject
1346: .handleAccelerator(accelerator);
1347: }
1348: }
1349: }
1350: }
1351: return bRetVal;
1352: }
1353:
1354: /**
1355: * Cut
1356: */
1357: public static void cut(ADGraphWindow pCurrentEditor) {
1358: transferImageToClipboard(pCurrentEditor);
1359:
1360: TSEGraphWindow pGraphEditor = pCurrentEditor;
1361: TSEGraph pGraph = pCurrentEditor.getGraph();
1362:
1363: if (pGraph != null && pGraphEditor != null) {
1364: // TSCutCopyPasteControl pPasteControl = pGraphEditor.getCutCopyPasteControl();
1365:
1366: // if (pPasteControl != null)
1367: // {
1368: // CComPtr < TSCOM : : TSCommand > pCommand;
1369: //
1370: // _VH(CreateCommand(pCurrentEditor, xstring(_T("TSCutCmd")), & pCommand));
1371: //
1372: // if (pCommand)
1373: // {
1374: // CComQIPtr < TSCOM : : TSCutCmd > pCutCmd(pCommand);
1375: // if (pCutCmd)
1376: // {
1377: // These objects are about to be transfered to the clipboard
1378: // Remove the PE from the model
1379:
1380: // IDrawingAreaControl drawingArea = pCurrentEditor.getDrawingArea();
1381: //
1382: // if (drawingArea != null)
1383: // {
1384: // ETList < IPresentationElement > pPresentationElements = drawingArea.getSelected();
1385: //
1386: // Iterator < IPresentationElement > iter = pPresentationElements.iterator();
1387: // while (iter.hasNext())
1388: // {
1389: // IPresentationElement pPresentationElement = iter.next();
1390: // removePresentationElement(pPresentationElement);
1391: // }
1392: // }
1393:
1394: try {
1395: pGraphEditor.cut();
1396: } catch (Exception e) {
1397: Log.stackTrace(e);
1398: }
1399:
1400: // long count = 0;
1401: // count = pPresentationElements.size();
1402: //
1403: // for (long i = 0; i < count; i++)
1404: // {
1405: // CComPtr < IPresentationElement > pPresentationElement;
1406: //
1407: // _VH(pPresentationElements - > Item(i, & pPresentationElement));
1408: // if (pPresentationElement)
1409: // {
1410: // _VH(RemovePresentationElement(pPresentationElement));
1411: // }
1412: // }
1413:
1414: //
1415: // _VH(pCutCmd - > init(pGraph, pPasteControl, 0, 0, 0, 0, 0));
1416: // _VH(ExecuteCommand(pCurrentEditor, pCutCmd));
1417: // }
1418: // }
1419: // }
1420: }
1421:
1422: }
1423:
1424: /**
1425: * Copy
1426: *
1427: * @param pCurrentEditor [in] The current editor
1428: */
1429: public static void copy(ADGraphWindow pCurrentEditor) {
1430: // First copy to the clipboard
1431: //boolean bSuccess = false;
1432: //_VH(pCurrentEditor->copyToClipboard(0,0,0,true, true, false, &bSuccess));
1433:
1434: transferImageToClipboard(pCurrentEditor);
1435:
1436: // Now copy to the TS clipboard manager
1437: TSEGraphWindow pGraphEditor = pCurrentEditor;
1438: TSEGraph pGraph = pCurrentEditor.getGraph();
1439:
1440: if (pGraph != null && pGraphEditor != null) {
1441: TSCutCopyPasteControl pPasteControl = pGraphEditor
1442: .getCutCopyPasteControl();
1443:
1444: if (pPasteControl != null) {
1445: pPasteControl.reset();
1446:
1447: try {
1448: pGraphEditor.copy();
1449: } catch (Exception e) {
1450: Log.stackTrace(e);
1451: }
1452: }
1453: }
1454: }
1455:
1456: /**
1457: * Paste
1458: */
1459: public static boolean paste(ADGraphWindow pCurrentEditor) {
1460: boolean bDidPaste = false;
1461:
1462: TSEGraphManager pGraphManager = pCurrentEditor
1463: .getGraphManager();
1464: TSEGraphWindow pGraphEditor = pCurrentEditor;
1465: TSEGraph pGraph = pCurrentEditor.getGraph();
1466:
1467: if (pGraphManager != null && pGraphEditor != null
1468: && pGraph != null) {
1469: TSCutCopyPasteControl pPasteControl = pGraphEditor
1470: .getCutCopyPasteControl();
1471:
1472: if (pPasteControl != null) {
1473: boolean bCanPaste = pPasteControl.canPaste();
1474:
1475: if (bCanPaste) {
1476: //if (!(pCurrentEditor.getCurrentState() instanceof TSEPasteState))
1477: if (!(pCurrentEditor.getCurrentState() instanceof TSEPasteTool)) {
1478: // pCurrentEditor.switchState(new ADPasteState());
1479: pCurrentEditor.switchTool(new ADPasteState());
1480: }
1481: bDidPaste = true;
1482: }
1483: }
1484: }
1485:
1486: return bDidPaste;
1487: }
1488:
1489: /**
1490: * ClearClipboard
1491: */
1492: public static void clearClipboard(ADGraphWindow pCurrentEditor) {
1493: if (pCurrentEditor == null)
1494: return;
1495: TSEGraphManager pGraphManager = pCurrentEditor
1496: .getGraphManager();
1497: TSEGraphWindow pGraphEditor = pCurrentEditor;
1498: TSEGraph pGraph = pCurrentEditor.getGraph();
1499:
1500: if (pGraphManager != null && pGraphEditor != null
1501: && pGraph != null) {
1502: TSCutCopyPasteControl pPasteControl = pGraphEditor
1503: .getCutCopyPasteControl();
1504:
1505: if (pPasteControl != null) {
1506: pPasteControl.reset();
1507:
1508: // This added code is similar to what that C++ code is doing.
1509: // We are not sure that the code below is necessary, but its working as is.
1510:
1511: //pPasteControl.copy(pGraphManager, new ArrayList(), new ArrayList(), new ArrayList(), new ArrayList(), new ArrayList());
1512: }
1513: }
1514: }
1515:
1516: /**
1517: * ItemsOnClipboard
1518: */
1519: public static boolean itemsOnClipboard(ADGraphWindow pCurrentEditor) {
1520: return pCurrentEditor.canPaste();
1521: }
1522:
1523: /**
1524: * Hide 'num' number of children levels for the input node. If -1 then we ask the user
1525: *
1526: * @param pCurrentEditor [in] The current graph editor
1527: * @param pPE [in] The presentation element whose children (or parents) we'll be hiding.
1528: * @param numLevels [in] The number of levels we should hide.
1529: * @param bChildren [in] Set to VARIANT_TRUE to hide children.
1530: */
1531: public static void hide(TSEGraphWindow pCurrentEditor,
1532: IPresentationElement pPE, long numLevels, boolean bChildren) {
1533: TSENode pTSNode = TypeConversions.getOwnerNode(pPE);
1534:
1535: if (pTSNode != null) {
1536: if (numLevels == -1) {
1537: // TODO
1538: // CComPtr < IGraphObjectsHidingDialog > pGraphObjectsHidingDialog;
1539: //
1540: // _VH(pGraphObjectsHidingDialog.CoCreateInstance(__uuidof(GraphObjectsHidingDialog)));
1541: // ATLASSERT(pGraphObjectsHidingDialog);
1542: // if (pGraphObjectsHidingDialog)
1543: // {
1544: // long nResult = 1L;
1545: // long nDefault = 1L;
1546: // VARIANT_BOOL bUserHitOK = VARIANT_FALSE;
1547: //
1548: // if (bChildren)
1549: // {
1550: // _VH(pGraphObjectsHidingDialog->DisplayHideChildrenDialog(nDefault,
1551: // &nResult,
1552: // &bUserHitOK));
1553: // }
1554: // else
1555: // {
1556: // _VH(pGraphObjectsHidingDialog->DisplayHideParentsDialog(nDefault,
1557: // &nResult,
1558: // &bUserHitOK));
1559: // }
1560: //
1561: // if (bUserHitOK)
1562: // {
1563: // _VH(Hide(pCurrentEditor, pPE, nResult, bChildren));
1564: // }
1565: // }
1566:
1567: //TODO remove once the above is implemented
1568: hide(pCurrentEditor, pPE, 999, bChildren);
1569: } else {
1570:
1571: List pDList = new Vector();
1572:
1573: if (bChildren) {
1574: findChildren(pTSNode, pDList, numLevels);
1575: } else {
1576: findParents(pTSNode, pDList, numLevels);
1577: }
1578:
1579: if (!pDList.isEmpty()) {
1580: // make sure this node is not in the list
1581: pDList.remove(pTSNode);
1582: TSEHidingManager hidingManager = (TSEHidingManager) TSEHidingManager
1583: .getManager(pCurrentEditor
1584: .getGraphManager());
1585: pCurrentEditor.transmit(new TSEHideCommand(pDList,
1586: null));
1587:
1588: }
1589: }
1590: }
1591: }
1592:
1593: /**
1594: * Hides the parents of the specified node 'numLevels' number of levels
1595: *
1596: * @param pCurrentEditor [in] The current graph editor
1597: * @param pPE [in] The presentation element whose children (or parents) we'll be showing.
1598: * @param numLevels [in] The number of levels we should showing.
1599: * @param bChildren [in] Set to VARIANT_TRUE to show children.
1600: */
1601: public static void unhide(TSEGraphWindow pCurrentEditor,
1602: IPresentationElement pPE, long numLevels, boolean bChildren) {
1603: TSENode pTSNode = TypeConversions.getOwnerNode(pPE);
1604:
1605: if (pTSNode != null) {
1606: if (numLevels == -1) {
1607: // TODO
1608: // CComPtr < IGraphObjectsHidingDialog > pGraphObjectsHidingDialog;
1609: //
1610: // _VH(pGraphObjectsHidingDialog.CoCreateInstance(__uuidof(GraphObjectsHidingDialog)));
1611: // ATLASSERT(pGraphObjectsHidingDialog);
1612: // if (pGraphObjectsHidingDialog)
1613: // {
1614: // long nResult = 1L;
1615: // long nDefault = 1L;
1616: // VARIANT_BOOL bUserHitOK = VARIANT_FALSE;
1617: //
1618: // if (bChildren)
1619: // {
1620: // _VH(pGraphObjectsHidingDialog->DisplayUnhideChildrenDialog(nDefault,
1621: // &nResult,
1622: // &bUserHitOK));
1623: // }
1624: // else
1625: // {
1626: // _VH(pGraphObjectsHidingDialog->DisplayUnhideParentsDialog(nDefault,
1627: // &nResult,
1628: // &bUserHitOK));
1629: // }
1630: // if (bUserHitOK)
1631: // {
1632: // _VH(Unhide(pCurrentEditor, pPE, nResult, bChildren));
1633: // }
1634: // }
1635:
1636: //TODO remove once the above is implemented
1637: unhide(pCurrentEditor, pPE, 999, bChildren);
1638:
1639: } else {
1640:
1641: List pDList = new Vector();
1642:
1643: TSEHidingManager hidingManager = (TSEHidingManager) TSEHidingManager
1644: .getManager(pCurrentEditor.getGraphManager());
1645:
1646: if (bChildren) {
1647: findHiddenChildren(pTSNode, pDList, numLevels,
1648: hidingManager);
1649: } else {
1650: findHiddenParents(pTSNode, pDList, numLevels,
1651: hidingManager);
1652: }
1653:
1654: if (!pDList.isEmpty()) {
1655: // make sure this node is not in the list
1656: pDList.remove(pTSNode);
1657: pCurrentEditor.transmit(new TSEUnhideCommand(
1658: pDList, null));
1659: }
1660: }
1661: }
1662: }
1663:
1664: /**
1665: * Reconnects the link.
1666: *
1667: * @param pLink [in] The link to reconnect
1668: * @param pOldNode [in] The old source or target node.
1669: * @param pNewNode [in] The new source or target node.
1670: * @Returns true if the reconnection was successful.
1671: */
1672: public static boolean reconnectLink(IPresentationElement pLink,
1673: IPresentationElement pOldNode, IPresentationElement pNewNode) {
1674: TSEEdge pTSEEdge = TypeConversions.getOwnerEdge(pLink, false);
1675: TSENode pTSEFromNode = TypeConversions.getOwnerNode(pOldNode);
1676: TSENode pTSEToNode = TypeConversions.getOwnerNode(pNewNode);
1677:
1678: if (pTSEEdge != null && pTSEFromNode != null
1679: && pTSEToNode != null) {
1680: pTSEEdge.setSourceNode(pTSEFromNode);
1681: pTSEEdge.setTargetNode(pTSEToNode);
1682: return true;
1683: }
1684: return false;
1685: }
1686:
1687: /**
1688: * Adds the TSCOM::TSGraphObjects in pList to the items list.
1689: *
1690: * @param pList [in] The list to which to extract presentation elements from
1691: * @param items [in,out] All presentation elements in pList are added to this list.
1692: */
1693: public static ETList<IPresentationElement> addToPresentationElements(
1694: ETList<IETGraphObject> objects,
1695: ETList<IPresentationElement> items) {
1696:
1697: ETArrayList<IPresentationElement> retVal = new ETArrayList<IPresentationElement>();
1698:
1699: retVal.addAll(items);
1700:
1701: if (objects != null) {
1702: for (Iterator<IETGraphObject> iter = objects.iterator(); iter
1703: .hasNext();) {
1704: IETGraphObject curObject = iter.next();
1705: retVal.add(curObject.getPresentationElement());
1706: }
1707: }
1708:
1709: return retVal;
1710: }
1711:
1712: /**
1713: * Creates IPresentationElements list from the TS lists
1714: */
1715: public static ETList<IPresentationElement> createPEList(
1716: ETList<IETGraphObject> objects) {
1717:
1718: ETArrayList<IPresentationElement> retVal = new ETArrayList<IPresentationElement>();
1719:
1720: for (Iterator<IETGraphObject> iter = objects.iterator(); iter
1721: .hasNext();) {
1722: IETGraphObject curObject = iter.next();
1723: retVal.add(curObject.getPresentationElement());
1724: }
1725:
1726: return retVal;
1727: }
1728:
1729: /*
1730: * Visits all objects on the graph in stacking order from top to bottom
1731: * return true of all objects were visited.
1732: */
1733: private static boolean visit(TSEGraphWindow curEditor,
1734: IETGraphObjectVisitor visitor) {
1735: ETGraph graph = curEditor != null
1736: && curEditor.getGraph() instanceof ETGraph ? (ETGraph) curEditor
1737: .getGraph()
1738: : null;
1739: if (graph != null && visitor != null) {
1740: ETGraphObjectTraversal traversal = new ETGraphObjectTraversal(
1741: graph);
1742: traversal.addVisitor(visitor);
1743: return traversal.traverseInReverseOrder();
1744: }
1745: return false;
1746: }
1747: }
|