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: /*
0043: * DiagramElementOperator.java
0044: *
0045: */
0046:
0047: package org.netbeans.test.umllib;
0048:
0049: import java.awt.Color;
0050: import java.awt.Component;
0051: import java.awt.Font;
0052: import java.awt.Point;
0053: import java.awt.Rectangle;
0054: import java.awt.event.InputEvent;
0055: import java.awt.event.KeyEvent;
0056: import java.awt.event.MouseEvent;
0057: import java.util.ArrayList;
0058: import java.util.Arrays;
0059: import java.util.Comparator;
0060: import java.util.HashSet;
0061: import java.util.Iterator;
0062: import java.util.List;
0063: import org.netbeans.jellytools.MainWindowOperator;
0064: import org.netbeans.jellytools.PaletteOperator;
0065: import org.netbeans.jellytools.properties.Property;
0066: import org.netbeans.jellytools.properties.PropertySheetOperator;
0067: import org.netbeans.jemmy.EventTool;
0068: import org.netbeans.jemmy.JemmyException;
0069: import org.netbeans.jemmy.JemmyProperties;
0070: import org.netbeans.jemmy.Timeout;
0071: import org.netbeans.jemmy.Timeouts;
0072: import org.netbeans.jemmy.Waitable;
0073: import org.netbeans.jemmy.Waiter;
0074: import org.netbeans.jemmy.drivers.input.MouseRobotDriver;
0075: import org.netbeans.jemmy.operators.ComponentOperator;
0076: import org.netbeans.jemmy.operators.JPopupMenuOperator;
0077: import org.netbeans.jemmy.operators.Operator;
0078: import org.netbeans.jemmy.operators.Operator.StringComparator;
0079: import org.netbeans.modules.uml.core.metamodel.core.foundation.IElement;
0080: import org.netbeans.modules.uml.core.metamodel.core.foundation.INamedElement;
0081: import org.netbeans.modules.uml.core.metamodel.core.foundation.IPresentationElement;
0082: import org.netbeans.modules.uml.core.support.umlsupport.ETDeviceRect;
0083: import org.netbeans.modules.uml.core.support.umlsupport.ETRect;
0084: import org.netbeans.modules.uml.core.support.umlsupport.IETPoint;
0085: import org.netbeans.modules.uml.core.support.umlsupport.IETRect;
0086: import org.netbeans.modules.uml.core.support.umlutils.ETList;
0087: import org.netbeans.modules.uml.project.ui.palette.UMLPalette;
0088: import org.netbeans.modules.uml.ui.products.ad.drawengines.INodeDrawEngine;
0089: import org.netbeans.modules.uml.ui.support.applicationmanager.NodePresentation;
0090: import org.netbeans.modules.uml.ui.support.viewfactorysupport.ETRectEx;
0091: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IETEdge;
0092: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IETGraphObject;
0093: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IETNode;
0094: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IETNodeUI;
0095: import org.netbeans.modules.uml.ui.swing.drawingarea.ADGraphWindow;
0096: import org.netbeans.modules.uml.ui.swing.drawingarea.IDrawingAreaControl;
0097: import org.netbeans.test.umllib.actions.Actionable;
0098: import org.netbeans.test.umllib.actions.ActionablePoint;
0099: import org.netbeans.test.umllib.actions.LabelsNameElementAction;
0100: import org.netbeans.test.umllib.exceptions.NotFoundException;
0101: import org.netbeans.test.umllib.exceptions.UMLCommonException;
0102: import org.netbeans.test.umllib.util.JPopupByPointChooser;
0103:
0104: /**
0105: * This is common class for the all diagram elements
0106: * @author Alexei Mokeev
0107: */
0108: public class DiagramElementOperator extends Operator implements
0109: Actionable {
0110:
0111: private DiagramOperator diagramOperator = null;
0112: private IETGraphObject elementGraphObject = null;
0113:
0114: /**
0115: * Default delay for Diagram Element Waiter. Real value can be changed
0116: * as for the all other operators(For example via JemmyProperties)
0117: */
0118: public static final long WAIT_GRAPHOBJECT_TIMEOUT = 60000;
0119:
0120: /**
0121: * Construct DiagramElementOperator by visible name
0122: * @param diagramOperator Diagram to look for element
0123: * @param elementVN Element visible name. Visible name is the name as you see it on a diagram
0124: * @throws qa.uml.exceptions.NotFoundException when no suitable element found
0125: */
0126: public DiagramElementOperator(DiagramOperator diagramOperator,
0127: String elementVN) throws NotFoundException {
0128: this (diagramOperator, new ElementByVNChooser(elementVN,
0129: ElementTypes.ANY), 0);
0130: }
0131:
0132: /**
0133: * Construct DiagramElementOperator by visible name, type and index
0134: * @param diagramOperator Diagram to look for element
0135: * @param elementVN Element visible name. Visible name is the name as you see it on a diagram
0136: * @param elementType Type of element(e.g Class, Component, Actor, etc)
0137: * @param index index
0138: * @throws qa.uml.exceptions.NotFoundException when no suitable element found
0139: * @see qa.uml.util.ElementTypes
0140: */
0141:
0142: public DiagramElementOperator(DiagramOperator diagramOperator,
0143: String elementVN, ExpandedElementTypes elementType,
0144: int index) throws NotFoundException {
0145: this (diagramOperator, new ElementByVNChooser(elementVN,
0146: elementType), index);
0147: }
0148:
0149: /**
0150: * Construct DiagramElementOperator by visible name, type and index
0151: * @param diagramOperator Diagram to look for element
0152: * @param elementVN Element visible name. Visible name is the name as you see it on a diagram
0153: * @param elementType Type of element(e.g Class, Component, Actor, etc)
0154: * @param index index
0155: * @throws qa.uml.exceptions.NotFoundException when no suitable element found
0156: * @see qa.uml.util.ElementTypes
0157: */
0158: public DiagramElementOperator(DiagramOperator diagramOperator,
0159: String elementVN, ElementTypes elementType, int index)
0160: throws NotFoundException {
0161: this (diagramOperator, new ElementByVNChooser(elementVN,
0162: elementType), index);
0163: }
0164:
0165: /**
0166: *
0167: * @param diagramOperator
0168: * @param elementVN
0169: * @param elementType
0170: * @throws qa.uml.exceptions.NotFoundException
0171: */
0172: public DiagramElementOperator(DiagramOperator diagramOperator,
0173: String elementVN, ElementTypes elementType)
0174: throws NotFoundException {
0175: this (diagramOperator, new ElementByVNChooser(elementVN,
0176: elementType), 0);
0177: }
0178:
0179: /**
0180: * Construct DiagramElementOperator by visible name and index
0181: * @param diagramOperator Diagram to look for element
0182: * @param elementVN Element visible name. Visible name is the name as you see it on a diagram
0183: * @param index index
0184: * @throws qa.uml.exceptions.NotFoundException when no suitable element found
0185: */
0186: public DiagramElementOperator(DiagramOperator diagramOperator,
0187: String elementVN, int index) throws NotFoundException {
0188: this (diagramOperator, new ElementByVNChooser(elementVN,
0189: ElementTypes.ANY), index);
0190: }
0191:
0192: /**
0193: * Construct DiagramElementOperator by custom finder and index
0194: * @param diagramOperator Diagram to look for element
0195: * @param elementFinder custom finder
0196: * @param index index
0197: * @throws qa.uml.exceptions.NotFoundException when no suitable element found
0198: */
0199: public DiagramElementOperator(DiagramOperator diagramOperator,
0200: DiagramElementChooser elementFinder, int index)
0201: throws NotFoundException {
0202: this (diagramOperator, waitForGraphObject(diagramOperator,
0203: elementFinder, index));
0204: }
0205:
0206: /**
0207: *
0208: * @param diagramOperator
0209: * @param elementFinder
0210: * @param index
0211: * @param lookForAllElements
0212: * @throws qa.uml.exceptions.NotFoundException
0213: */
0214: public DiagramElementOperator(DiagramOperator diagramOperator,
0215: DiagramElementChooser elementFinder, int index,
0216: boolean lookForAllElements) throws NotFoundException {
0217: this (diagramOperator, waitForGraphObject(diagramOperator,
0218: elementFinder, index, lookForAllElements));
0219: }
0220:
0221: /**
0222: * Construct DiagramElementOperator by given graphObject
0223: * @param diagramOperator Diagram to look for element
0224: * @param graphObject given graph object
0225: */
0226: public DiagramElementOperator(DiagramOperator diagramOperator,
0227: IETGraphObject graphObject) {
0228: this .diagramOperator = diagramOperator;
0229: this .elementGraphObject = graphObject;
0230: }
0231:
0232: /**
0233: * Search for suitable graph object
0234: * @return Graph object if found
0235: * @param lookForAllElements
0236: * @param diagramOperator Diagram to look for element
0237: * @param elementFinder custom finder
0238: * @param index index
0239: * @throws qa.uml.exceptions.NotFoundException when nothing suitable found
0240: */
0241: public static IETGraphObject findGraphObject(
0242: DiagramOperator diagramOperator,
0243: DiagramElementChooser elementFinder, int index,
0244: boolean lookForAllElements) throws NotFoundException {
0245: ArrayList<IETGraphObject> elementsFound = new ArrayList<IETGraphObject>();
0246:
0247: //searching for elements matching elemenFinder criteria
0248: IDrawingAreaControl cntrl = diagramOperator
0249: .getDrawingAreaControl();
0250: ETList<IETGraphObject> allGraphs = cntrl.getAllItems6();
0251: Iterator<IETGraphObject> tsIt = allGraphs.iterator();
0252: while (tsIt.hasNext()) {
0253: IETGraphObject graphObject = tsIt.next();
0254: IPresentationElement presElement = graphObject
0255: .getPresentationElement();
0256: if (presElement == null) {
0257: continue;
0258: }
0259:
0260: if (!lookForAllElements) {
0261: if (!(presElement instanceof NodePresentation)) {
0262: //We are looking only for nodes here
0263: continue;
0264: }
0265: }
0266:
0267: if (elementFinder.checkElement(graphObject)) {
0268: elementsFound.add(graphObject);
0269: }
0270: }
0271:
0272: //sorting found elements
0273: DiagramElementComparator<IETGraphObject> c = new DiagramElementComparator<IETGraphObject>();
0274: IETGraphObject[] arr = (IETGraphObject[]) elementsFound
0275: .toArray(new IETGraphObject[0]);
0276: Arrays.sort(arr, c);
0277: if (arr.length > index) {
0278: return arr[index];
0279: } else {
0280: throw new NotFoundException(
0281: "Element matching the criteria not found on diagram "
0282: + diagramOperator.getDiagramName() + ":"
0283: + elementFinder.getDescription());
0284: }
0285: }
0286:
0287: /**
0288: *
0289: * @param diagramOperator
0290: * @param elementFinder
0291: * @param index
0292: * @throws qa.uml.exceptions.NotFoundException
0293: * @return
0294: */
0295: public static IETGraphObject findGraphObject(
0296: DiagramOperator diagramOperator,
0297: DiagramElementChooser elementFinder, int index)
0298: throws NotFoundException {
0299: return findGraphObject(diagramOperator, elementFinder, index,
0300: false);
0301: }
0302:
0303: /**
0304: * Wait for suitable graph object
0305: * @return Element's GraphObject if found
0306: * @param lookForAllElements
0307: * @param diagramOperator Diagram to look for element
0308: * @param elementFinder custom finder
0309: * @param index index
0310: */
0311: public static IETGraphObject waitForGraphObject(
0312: final DiagramOperator diagramOperator,
0313: final DiagramElementChooser elementFinder, final int index,
0314: final boolean lookForAllElements) {
0315: try {
0316: Waiter w = new Waiter(new Waitable() {
0317:
0318: public Object actionProduced(Object obj) {
0319: try {
0320: IETGraphObject go = findGraphObject(
0321: diagramOperator, elementFinder, index,
0322: lookForAllElements);
0323: return go;
0324: } catch (NotFoundException e) {
0325: return null;
0326: }
0327: }
0328:
0329: public String getDescription() {
0330: return "Wait for " + elementFinder.getDescription();
0331: }
0332: });
0333: Timeouts t = JemmyProperties.getCurrentTimeouts();
0334: t
0335: .setTimeout(
0336: "Waiter.WaitingTime",
0337: t
0338: .getTimeout("DiagramElementOperator.WaitDiagramElementTime"));
0339: return (IETGraphObject) w.waitAction(null);
0340: } catch (InterruptedException ie) {
0341: return null;
0342: }
0343: }
0344:
0345: /**
0346: *
0347: * @param diagramOperator
0348: * @param elementFinder
0349: * @param index
0350: * @return
0351: */
0352: public static IETGraphObject waitForGraphObject(
0353: final DiagramOperator diagramOperator,
0354: final DiagramElementChooser elementFinder, final int index) {
0355: return waitForGraphObject(diagramOperator, elementFinder,
0356: index, false);
0357: }
0358:
0359: /**
0360: * Wait for suitable graph object disaapearnce
0361: * @param diagramOperator Diagram to look for element
0362: * @param elementFinder custom finder
0363: * @param index index
0364: * @return true if element disaapears and false otherwise
0365: */
0366: public static boolean waitForDeletion(
0367: final DiagramOperator diagramOperator,
0368: final DiagramElementChooser elementFinder, final int index) {
0369: try {
0370: Waiter w = new Waiter(new Waitable() {
0371:
0372: public Object actionProduced(Object obj) {
0373: try {
0374: IETGraphObject go = findGraphObject(
0375: diagramOperator, elementFinder, index);
0376: if (go != null) {
0377: return null;
0378: }
0379: } catch (NotFoundException e) {
0380: }
0381: return new Object();
0382: }
0383:
0384: public String getDescription() {
0385: return "Wait for deletion of "
0386: + elementFinder.getDescription();
0387: }
0388: });
0389: Timeouts t = JemmyProperties.getCurrentTimeouts();
0390: t
0391: .setTimeout(
0392: "Waiter.WaitingTime",
0393: t
0394: .getTimeout("DiagramElementOperator.WaitDiagramElementTime"));
0395: Object o = w.waitAction(null);
0396: if (o != null) {
0397: return true;
0398: } else {
0399: return false;
0400: }
0401: } catch (InterruptedException ie) {
0402: return false;
0403: }
0404: }
0405:
0406: /**
0407: * Wait for selection state, timeout 5 seconds
0408: * @param selected
0409: */
0410: public void waitSelection(boolean selected) {
0411: waitSelection(selected, 5000);
0412: }
0413:
0414: /**
0415: * Wait for selection state
0416: * @param selected
0417: * @param timeout
0418: */
0419: public void waitSelection(boolean selected, long timeout) {
0420: for (int i = 0; i < (timeout / 50); i++) {
0421: try {
0422: Thread.sleep(100);
0423: } catch (Exception ex) {
0424: }
0425: if (isSelected() == selected) {
0426: return;
0427: }
0428: }
0429: throw new UMLCommonException("Failed to wait for '"
0430: + selected
0431: + "' selection state//debug: "
0432: + elementGraphObject.isSelected()
0433: + ":"
0434: + elementGraphObject
0435: + ":"
0436: + elementGraphObject.getText()
0437: + ":"
0438: + elementGraphObject.isEdge()
0439: + ":"
0440: + elementGraphObject.isNode()
0441: + ":"
0442: + elementGraphObject.getPresentationElement()
0443: + ":"
0444: + elementGraphObject.getPresentationElement()
0445: .getFirstSubject()
0446: + ":"
0447: + elementGraphObject.getPresentationElement()
0448: .getFirstSubject().getExpandedElementType());
0449: }
0450:
0451: /**
0452: *
0453: * @return
0454: */
0455: public Color getBorderColor() {
0456: try {
0457: INodeDrawEngine engine = (INodeDrawEngine) elementGraphObject
0458: .getEngine();
0459: return engine.getBorderColor();
0460: } catch (Exception e) {
0461: return null;
0462: }
0463: }
0464:
0465: /**
0466: *
0467: * @return
0468: */
0469: public Color getBackgroundColor() {
0470: try {
0471: INodeDrawEngine engine = (INodeDrawEngine) elementGraphObject
0472: .getEngine();
0473: return engine.getFillColor();
0474: } catch (Exception e) {
0475: return null;
0476: }
0477: }
0478:
0479: /**
0480: *
0481: * @return
0482: */
0483: public Font getFont() {
0484: try {
0485: IETNodeUI ui = (IETNodeUI) elementGraphObject.getETUI();
0486: return ui.getFont().getFont();
0487: } catch (Exception e) {
0488: return null;
0489: }
0490: }
0491:
0492: /**
0493: * Return element type
0494: * @return Type of element
0495: */
0496: public String getElementType() {
0497: return elementGraphObject.getEngine().getElementType();
0498: }
0499:
0500: /**
0501: * The method has to resize the element by x in width and y in height
0502: * CAUTION! May not work when scrolling is involved
0503: * @param x
0504: * @param y
0505: */
0506: public void resize(int x, int y) {
0507: //TODO: add scrolling support
0508: IETRect rect = elementGraphObject.getEngine().getBoundingRect();
0509: Point point = rect.getBottomRight();
0510: this .select();
0511: new Timeout("", 500).sleep();
0512: diagramOperator.getDrawingArea().moveMouse(point.x + 4,
0513: point.y + 4);
0514: new Timeout("", 500).sleep();
0515: diagramOperator.getDrawingArea().dragNDrop(point.x + 4,
0516: point.y + 4, point.x + x, point.y + y);
0517: }
0518:
0519: /**
0520: * We must implement this method to extend Operator, but since
0521: * we are not working with Component this method always return null;
0522: * @return null
0523: */
0524: public Component getSource() {
0525: return null;
0526: }
0527:
0528: /**
0529: * Returns GraphObject for this diagram element
0530: * @return GraphObject for this diagram element
0531: */
0532: public IETGraphObject getGraphObject() {
0533: return elementGraphObject;
0534: }
0535:
0536: /**
0537: * Wrapper for elementGraphObject.getEngine().getBoundingRect().getRectangle();
0538: * @return Rectangle
0539: */
0540: public Rectangle getElementRectangle() {
0541: return elementGraphObject.getEngine().getBoundingRect()
0542: .getRectangle();
0543: }
0544:
0545: /**
0546: * Return Diagram, where this element is placed
0547: * @return Diagram, where this element is placed
0548: */
0549: public DiagramOperator getDiagram() {
0550: return diagramOperator;
0551: }
0552:
0553: /**
0554: * Returns name of element
0555: * @return name of element
0556: */
0557:
0558: public String getName() {
0559:
0560: String name = "";
0561:
0562: ETList<IElement> subjects = getGraphObject()
0563: .getPresentationElement().getSubjects();
0564:
0565: if (subjects.size() > 0) {
0566: name = subjects.get(0).toString();
0567: }
0568:
0569: if (name.equals("")) {
0570: name = getGraphObject().getText();
0571: }
0572:
0573: return name;
0574: }
0575:
0576: /**
0577: * Returns type or null if we have several subjects. Should be overriden in subclasses
0578: * @return type or null if we have several subjects. Should be overriden in subclasses
0579: */
0580: public String getType() {
0581: if (elementGraphObject.getPresentationElement()
0582: .getSubjectCount() == 1) {
0583: return elementGraphObject.getPresentationElement()
0584: .getFirstSubject().getElementType();
0585: }
0586: return null; //We can't detect type
0587: }
0588:
0589: /**
0590: * Returns expanded type or null if we have several subjects. Should be overriden in subclasses
0591: * @return expanded type or null if we have several subjects. Should be overriden in subclasses
0592: */
0593: public String getExpandedType() {
0594: if (elementGraphObject.getPresentationElement()
0595: .getSubjectCount() == 1) {
0596: return elementGraphObject.getPresentationElement()
0597: .getFirstSubject().getExpandedElementType();
0598: }
0599: return null; //We can't detect type
0600: }
0601:
0602: /**
0603: * Returns all links from and to the diagram element
0604: * @return all links from and to the diagram element
0605: */
0606: public HashSet<LinkOperator> getLinks() {
0607: HashSet<LinkOperator> links = new HashSet<LinkOperator>();
0608: IETGraphObject sObject = getGraphObject();
0609: if ((sObject != null) && (sObject instanceof IETNode)) {
0610: IETNode node = (IETNode) sObject;
0611:
0612: ETList<IETEdge> list = node.getEdges();
0613: Iterator<IETEdge> it = list.iterator();
0614: while (it.hasNext()) {
0615: IETEdge edge = (IETEdge) it.next();
0616: IPresentationElement presentation = edge
0617: .getPresentationElement();
0618: if (presentation != null) {
0619: links.add(new LinkOperator(diagramOperator, edge));
0620: }
0621: }
0622: }
0623: return links;
0624: }
0625:
0626: /**
0627: * Returns all incoming links
0628: * @return All incoming links
0629: */
0630: public HashSet<LinkOperator> getInLinks() {
0631: HashSet<LinkOperator> links = new HashSet<LinkOperator>();
0632: IETGraphObject sObject = getGraphObject();
0633: if ((sObject != null) && (sObject instanceof IETNode)) {
0634: IETNode node = (IETNode) sObject;
0635: List list = node.getInEdges();
0636: Iterator it = list.iterator();
0637: while (it.hasNext()) {
0638: IETEdge edge = (IETEdge) it.next();
0639: IPresentationElement presentation = edge
0640: .getPresentationElement();
0641: if (presentation != null) {
0642: links.add(new LinkOperator(diagramOperator, edge));
0643: }
0644: }
0645: }
0646: return links;
0647: }
0648:
0649: /**
0650: * Returns all outcoming links
0651: * @return All outcoming links
0652: */
0653: public HashSet<LinkOperator> getOutLinks() {
0654: HashSet<LinkOperator> links = new HashSet<LinkOperator>();
0655: IETGraphObject sObject = getGraphObject();
0656: if ((sObject != null) && (sObject instanceof IETNode)) {
0657: IETNode node = (IETNode) sObject;
0658: List list = node.getOutEdges();
0659: Iterator it = list.iterator();
0660: while (it.hasNext()) {
0661: IETEdge edge = (IETEdge) it.next();
0662: IPresentationElement presentation = edge
0663: .getPresentationElement();
0664: if (presentation != null) {
0665: links.add(new LinkOperator(diagramOperator, edge));
0666: }
0667: }
0668: }
0669: return links;
0670: }
0671:
0672: /**
0673: * Returns center point of this diagram element
0674: * @return Center point of this diagram element
0675: */
0676: public Point getCenterPoint() {
0677: Rectangle tmp = getBoundingRect();
0678: Point ret = tmp.getLocation();
0679: ret.translate(tmp.width / 2, tmp.height / 2);
0680: return ret;
0681: //return elementGraphObject.getEngine().getBoundingRect().getCenterPoint();
0682: }
0683:
0684: /**
0685: *
0686: * @return bounding rect for element
0687: */
0688: public Rectangle getBoundingRect() {
0689: IETRect tmpRect = elementGraphObject.getEngine()
0690: .getBoundingRect();
0691: ETDeviceRect tmpDevRect = null;
0692: int x;
0693: int y;
0694: //transform to device coordinates
0695: if (tmpRect instanceof ETRect || tmpRect instanceof ETRectEx) {
0696: // This special case is for all the code that depends
0697: // on the bounding rectangle in device coordinates.
0698: tmpDevRect = ((ETRect) tmpRect).getAsDeviceRect();
0699: } else if (tmpRect instanceof ETDeviceRect) {
0700: tmpDevRect = ((ETDeviceRect) tmpRect);
0701: }
0702: return tmpDevRect.getBounds();
0703: }
0704:
0705: /**
0706: *
0707: * @param clickCount
0708: * @param mouseButton
0709: * @param modifiers
0710: */
0711: public void clickOnCenter(int clickCount, int mouseButton,
0712: int modifiers) {
0713: clickOn(getCenterPoint(), clickCount, mouseButton, modifiers);
0714: }
0715:
0716: /**
0717: *
0718: * @param clickCount
0719: * @param mouseButton
0720: */
0721: public void clickOnCenter(int clickCount, int mouseButton) {
0722: clickOn(getCenterPoint(), clickCount, mouseButton, 0);
0723: }
0724:
0725: public void clickOnCenter() {
0726: clickOn(getCenterPoint(), 1, InputEvent.BUTTON1_MASK, 0);
0727: }
0728:
0729: /**
0730: *
0731: * @param p
0732: * @param clickCount
0733: * @param mouseButton
0734: * @param modifiers
0735: */
0736: public void clickOn(Point p, int clickCount, int mouseButton,
0737: int modifiers) {
0738: p = makeVisible(p);
0739: diagramOperator.getDrawingArea().clickMouse(p.x, p.y,
0740: clickCount, mouseButton, modifiers);
0741: }
0742:
0743: public void clickForPopup() {
0744: if (ElementTypes.COMBINED_FRAGMENT.toString().equals(getType())) {
0745: //workaround for 90586
0746: Point loc = getBoundingRect().getLocation();
0747: loc.translate(2, 2);
0748: clickOn(loc, 1, MouseEvent.BUTTON3_MASK, 0);
0749: } else {
0750: clickOn(getCenterPoint(), 1, InputEvent.BUTTON3_MASK, 0);
0751: }
0752: }
0753:
0754: private void dummy() {
0755: //diagramOperator.getDrawingArea
0756: //com.embarcadero.uml.ui.swing.drawingarea.ADGraphWindow a;
0757: // a.sc
0758: }
0759:
0760: public void center() {
0761: center(false, false);
0762: }
0763:
0764: /**
0765: *
0766: * @param selectIt
0767: * @param deselectOthers
0768: */
0769: public void center(boolean selectIt, boolean deselectOthers) {
0770: try {
0771: Thread.sleep(100);
0772: } catch (Exception ex) {
0773: }
0774: ADGraphWindow area = diagramOperator.getDrawingArea().getArea();
0775: try {
0776: Thread.sleep(100);
0777: } catch (Exception ex) {
0778: }
0779: area.getDrawingArea().centerPresentationElement(
0780: elementGraphObject.getPresentationElement(), selectIt,
0781: deselectOthers);
0782: try {
0783: Thread.sleep(100);
0784: } catch (Exception ex) {
0785: }
0786: }
0787:
0788: /**
0789: * gets point inside a component and, if component is not visible centers window in this component.
0790: * The updated device point is returned
0791: * @param point
0792: * @return
0793: */
0794: public Point makeVisible(Point point) {
0795: ADGraphWindow area = diagramOperator.getDrawingArea().getArea();
0796: IDrawingAreaControl daControl = area.getDrawingArea();
0797:
0798: IETPoint etPoint = daControl.deviceToLogicalPoint(point.x,
0799: point.y);
0800:
0801: IETRect eDeviceAreaRect = new ETRect(area.getVisibleRect());
0802: IETRect eVisibleAreaRect = daControl
0803: .deviceToLogicalRect(eDeviceAreaRect);
0804: IETRect eElementRect = elementGraphObject.getEngine()
0805: .getLogicalBoundingRect(true);
0806:
0807: if (!eVisibleAreaRect.contains(eElementRect)) {
0808: center();
0809: new Timeout("", 500);
0810: }
0811: return daControl.logicalToDevicePoint(etPoint).asPoint();
0812: }
0813:
0814: /**
0815: *
0816: * @return
0817: */
0818: public ArrayList<String> getSubjectVNs() {
0819: ArrayList<String> al = new ArrayList<String>();
0820: ETList<IElement> subjects = getGraphObject()
0821: .getPresentationElement().getSubjects();
0822: Iterator<IElement> itSubj = subjects.iterator();
0823: while (itSubj.hasNext()) {
0824: IElement sbj = (IElement) itSubj.next();
0825: if (sbj instanceof INamedElement) {
0826: al.add(((INamedElement) sbj).getName());
0827: }
0828: }
0829: return al;
0830: }
0831:
0832: /**
0833: * Call popup from up-left corner of element
0834: * @return popup
0835: */
0836: public JPopupMenuOperator getGeneralPopup() {
0837: Point loc = getCenterPoint();
0838: loc = getBoundingRect().getLocation();
0839: loc.translate(10, 5);
0840:
0841: //
0842: try {
0843: Thread.sleep(100);
0844: } catch (Exception ex) {
0845: }
0846: loc = makeVisible(loc);
0847: //workarround for Issue 79519
0848: if (System.getProperty("os.name").toLowerCase().indexOf(
0849: "windows") == -1) {
0850: clickOn(loc, 1, InputEvent.BUTTON1_MASK, 0);
0851: try {
0852: Thread.sleep(100);
0853: } catch (Exception ex) {
0854: }
0855: }
0856: clickOn(loc, 1, InputEvent.BUTTON3_MASK, 0);
0857: try {
0858: Thread.sleep(100);
0859: } catch (Exception ex) {
0860: }
0861: JPopupMenuOperator ret = new JPopupMenuOperator(
0862: JPopupMenuOperator.waitJPopupMenu(
0863: (java.awt.Container) (MainWindowOperator
0864: .getDefault().getSource()),
0865: new JPopupByPointChooser(loc, diagramOperator
0866: .getDrawingArea().getSource(), 0)));
0867:
0868: return ret;
0869: }
0870:
0871: //Methods from Actionable interface
0872: /**
0873: * Call popup from central of element
0874: * @return
0875: */
0876: public JPopupMenuOperator getPopup() {
0877: Point loc = getCenterPoint();
0878: if (ElementTypes.COMBINED_FRAGMENT.toString().equals(getType())) {
0879: //workaround for 90586
0880: //works with big fragments only
0881: loc = getBoundingRect().getLocation();
0882: loc.translate(10, 5);
0883: }
0884: //
0885: try {
0886: Thread.sleep(100);
0887: } catch (Exception ex) {
0888: }
0889: loc = makeVisible(loc);
0890: //workarround for Issue 79519
0891: if (System.getProperty("os.name").toLowerCase().indexOf(
0892: "windows") == -1) {
0893: clickOn(loc, 1, InputEvent.BUTTON1_MASK, 0);
0894: try {
0895: Thread.sleep(100);
0896: } catch (Exception ex) {
0897: }
0898: }
0899: clickOn(loc, 1, InputEvent.BUTTON3_MASK, 0);
0900: try {
0901: Thread.sleep(100);
0902: } catch (Exception ex) {
0903: }
0904: JPopupMenuOperator ret = new JPopupMenuOperator(
0905: JPopupMenuOperator.waitJPopupMenu(
0906: (java.awt.Container) (MainWindowOperator
0907: .getDefault().getSource()),
0908: new JPopupByPointChooser(loc, diagramOperator
0909: .getDrawingArea().getSource(), 0)));
0910:
0911: return ret;
0912: }
0913:
0914: public void select() {
0915: if (!isSelected()) {
0916: if (ElementTypes.COMBINED_FRAGMENT.toString().equals(
0917: getType())) {
0918: //workaround for 90586
0919: Point loc = getBoundingRect().getLocation();
0920: loc.translate(2, 2);
0921: clickOn(loc, 1, MouseEvent.BUTTON1_MASK, 0);
0922: try {
0923: waitSelection(true);
0924: } catch (Exception ex) {
0925: //workaround to workaround (very thin active area)
0926: loc.translate(-1, -1);
0927: clickOn(loc, 1, MouseEvent.BUTTON1_MASK, 0);
0928: waitSelection(true);
0929: }
0930: } else {
0931: clickOnCenter();
0932: waitSelection(true);
0933: }
0934: } else {
0935: waitSelection(true);
0936: }
0937: }
0938:
0939: /**
0940: *
0941: * @param avoidcollitionsandretry
0942: */
0943: public void select(boolean avoidcollitionsandretry) {
0944: if (avoidcollitionsandretry) {
0945: int count = 0;
0946: Point click = new Point(getCenterPoint());
0947: //
0948: int r = 5;
0949: int dx = 0;
0950: int dy = 0;
0951:
0952: while (!isSelected() && count < 10) {
0953: count++;
0954: clickOn(click, 1, InputEvent.BUTTON1_MASK, 0);
0955: try {
0956: waitSelection(true);
0957: } catch (Exception ex) {
0958: if (dx > 0 && dy > 0) {
0959: dx = 0;
0960: dy = -2 * dy;
0961: } else if (dx == 0 && dy < 0) {
0962: dx = dy;
0963: dy = 0;
0964: } else if (dx < 0 && dy == 0) {
0965: dy = -dx;
0966: dx = 0;
0967: } else if (dx == 0 && dy > 0) {
0968: dx = dy;
0969: dy = 0;
0970: } else {
0971: dx = r;
0972: dy = r;
0973: r *= 2;
0974: click = new Point(getCenterPoint());
0975: }
0976: click.translate(dx, dy);
0977: }
0978: }
0979: } else {
0980: select();
0981: }
0982: }
0983:
0984: /**
0985: *
0986: * @return
0987: */
0988: public boolean isSelected() {
0989: return elementGraphObject.isSelected();
0990: }
0991:
0992: public void addToSelection() {
0993: new Timeout("", 10).sleep();
0994: if (ElementTypes.COMBINED_FRAGMENT.toString().equals(getType())) {
0995: //workaround for 90586
0996: Point loc = getBoundingRect().getLocation();
0997: loc.translate(2, 2);
0998: clickOn(loc, 1, MouseEvent.BUTTON1_MASK, KeyEvent.CTRL_MASK);
0999: try {
1000: waitSelection(true);
1001: } catch (Exception ex) {
1002: //workaround to workaround (very thin active area)
1003: loc.translate(-1, -1);
1004: clickOn(loc, 1, MouseEvent.BUTTON1_MASK,
1005: KeyEvent.CTRL_MASK);
1006: waitSelection(true);
1007: }
1008: } else {
1009: clickOnCenter(1, InputEvent.BUTTON1_MASK,
1010: KeyEvent.CTRL_MASK);
1011: waitSelection(true);
1012: }
1013: }
1014:
1015: /**
1016: * Change element size with usage of mouse robot driver
1017: * @param width
1018: * @param height
1019: */
1020: public void setSize(int width, int height) {
1021: if (!isSelected()) {
1022: select();
1023: }
1024: java.awt.Rectangle parB = getBoundingRect();
1025: DrawingAreaOperator drA = diagramOperator.getDrawingArea();
1026: int shift = (int) (Math.round(4.0 * drA.getZoomLevel()));
1027: MouseRobotDriver driver = new MouseRobotDriver(new Timeout("",
1028: 250));
1029: driver.moveMouse(drA, parB.x + parB.width + shift, parB.y
1030: + parB.height + shift);
1031: driver.pressMouse(drA, parB.x + parB.width + shift, parB.y
1032: + parB.height + shift, InputEvent.BUTTON1_MASK, 0);
1033: new Timeout("", 500).sleep();
1034: driver.moveMouse(drA, parB.x + width + 2 * shift, parB.y
1035: + height + 2 * shift);
1036: new Timeout("", 500).sleep();
1037: driver.releaseMouse(drA, parB.x + width + 2 * shift, parB.y
1038: + height + 2 * shift, InputEvent.BUTTON1_MASK, 0);
1039: new Timeout("", 500).sleep();
1040: }
1041:
1042: /**
1043: *
1044: * @param width
1045: * @param height
1046: */
1047: public void setSize(double width, double height) {
1048: setSize((int) width, (int) height);
1049: }
1050:
1051: /**
1052: * Change element position with usage of mouse robot driver
1053: * @param x
1054: * @param y
1055: */
1056: public void moveTo(int x, int y) {
1057: java.awt.Rectangle parB = getBoundingRect();
1058: DrawingAreaOperator drA = diagramOperator.getDrawingArea();
1059: MouseRobotDriver driver = new MouseRobotDriver(new Timeout("",
1060: 250));
1061: int corner_shift = 4;
1062: driver.moveMouse(drA, parB.x + corner_shift, parB.y
1063: + corner_shift);
1064: driver.pressMouse(drA, parB.x + corner_shift, parB.y
1065: + corner_shift, InputEvent.BUTTON1_MASK, 0);
1066: new Timeout("", 500).sleep();
1067: driver.moveMouse(drA, x, y);
1068: new Timeout("", 500).sleep();
1069: driver.releaseMouse(drA, x, y, InputEvent.BUTTON1_MASK, 0);
1070: new Timeout("", 500).sleep();
1071: }
1072:
1073: /**
1074: *
1075: * @param x
1076: * @param y
1077: */
1078: public void moveTo(double x, double y) {
1079: moveTo((int) x, (int) y);
1080: }
1081:
1082: /**
1083: * Change element position with usage of mouse robot driver
1084: * @param dx
1085: * @param dy
1086: */
1087: public void shift(int dx, int dy) {
1088: java.awt.Rectangle parB = getBoundingRect();
1089: DrawingAreaOperator drA = diagramOperator.getDrawingArea();
1090: MouseRobotDriver driver = new MouseRobotDriver(new Timeout("",
1091: 250));
1092: int corner_shift = 4;
1093: driver.moveMouse(drA, parB.x + corner_shift, parB.y
1094: + corner_shift);
1095: driver.pressMouse(drA, parB.x + corner_shift, parB.y
1096: + corner_shift, InputEvent.BUTTON1_MASK, 0);
1097: new Timeout("", 500).sleep();
1098: driver.moveMouse(drA, parB.x + dx, parB.y + dy);
1099: new Timeout("", 500).sleep();
1100: driver.releaseMouse(drA, parB.x + dx, parB.y + dy,
1101: InputEvent.BUTTON1_MASK, 0);
1102: new Timeout("", 500).sleep();
1103: }
1104:
1105: /**
1106: *
1107: * @param dx
1108: * @param dy
1109: */
1110: public void shift(double dx, double dy) {
1111: shift((int) dx, (int) dy);
1112: }
1113:
1114: /**
1115: *
1116: * @param el
1117: * @return
1118: */
1119: public boolean equals(Object el) {
1120: if (el instanceof DiagramElementOperator) {
1121: return this .elementGraphObject == ((DiagramElementOperator) el).elementGraphObject;
1122: } else {
1123: return false;
1124: }
1125: }
1126:
1127: static {
1128: Timeouts.initDefault(
1129: "DiagramElementOperator.WaitDiagramElementTime",
1130: WAIT_GRAPHOBJECT_TIMEOUT);
1131: }
1132:
1133: /**
1134: *
1135: */
1136:
1137: public static class DiagramElementComparator<C extends IETGraphObject>
1138: implements Comparator<C> {
1139:
1140: /**
1141: *
1142: * @param o1
1143: * @param o2
1144: * @return
1145: */
1146: public int compare(C o1, C o2) {
1147: Point o1Center = o1.getEngine().getBoundingRect()
1148: .getCenterPoint();
1149: Point o2Center = o2.getEngine().getBoundingRect()
1150: .getCenterPoint();
1151: if (o1Center.y > o2Center.y) {
1152: return 1;
1153: } else if (o1Center.y == o2Center.y) {
1154: if (o1Center.x > o2Center.y) {
1155: return -1;
1156: } else {
1157: return 1;
1158: }
1159: } else {
1160: return -1;
1161: }
1162: }
1163: }
1164:
1165: public static class ElementByVNChooser implements
1166: DiagramElementChooser {
1167:
1168: private String vn = null;
1169: private String elementType = null;
1170: private ElementTypes elemTypeEnu = null;
1171: private ExpandedElementTypes elemExTypeEnu = null;
1172: private StringComparator comparator = null;
1173:
1174: /**
1175: *
1176: * @param vn
1177: * @param elementType
1178: * @param comparator
1179: */
1180: public ElementByVNChooser(String vn, ElementTypes elementType,
1181: StringComparator comparator) {
1182: this .vn = vn;
1183: this .elementType = elementType.toString();
1184: this .elemTypeEnu = elementType;
1185: this .comparator = comparator;
1186: }
1187:
1188: /**
1189: *
1190: * @param vn
1191: * @param elementType
1192: */
1193: public ElementByVNChooser(String vn, ElementTypes elementType) {
1194: this (vn, elementType, new Operator.DefaultStringComparator(
1195: true, true));
1196: }
1197:
1198: /**
1199: *
1200: * @param vn
1201: * @param elementType
1202: * @param comparator
1203: */
1204: public ElementByVNChooser(String vn,
1205: ExpandedElementTypes elementType,
1206: StringComparator comparator) {
1207: this .vn = vn;
1208: this .elementType = elementType.toString();
1209: this .elemExTypeEnu = elementType;
1210: this .comparator = comparator;
1211: }
1212:
1213: /**
1214: *
1215: * @param vn
1216: * @param elementType
1217: */
1218: public ElementByVNChooser(String vn,
1219: ExpandedElementTypes elementType) {
1220: this (vn, elementType, new Operator.DefaultStringComparator(
1221: true, true));
1222: }
1223:
1224: /**
1225: *
1226: * @param graphObject
1227: * @return
1228: */
1229: public boolean checkElement(IETGraphObject graphObject) {
1230: //check type
1231: String inType = null;
1232: String any = null;
1233: if (graphObject.getPresentationElement().getSubjectCount() == 1) {
1234: if (elemTypeEnu != null) {
1235: inType = graphObject.getPresentationElement()
1236: .getFirstSubject().getElementType();
1237: any = elemTypeEnu.ANY.toString();
1238: } else if (elemExTypeEnu != null) {
1239: inType = graphObject.getPresentationElement()
1240: .getFirstSubject().getExpandedElementType();
1241: any = elemExTypeEnu.ANY.toString();
1242: }
1243: }
1244:
1245: if ((elementType == null)
1246: || (!elementType.equals(inType) && !elementType
1247: .equals(any))) {
1248: return false;
1249: }
1250:
1251: IPresentationElement presElement = graphObject
1252: .getPresentationElement();
1253: ETList<IElement> subjects = presElement.getSubjects();
1254: Iterator<IElement> itSubj = subjects.iterator();
1255: while (itSubj.hasNext()) {
1256: IElement sbj = (IElement) itSubj.next();
1257: if (sbj instanceof INamedElement) {
1258: if (comparator.equals(((INamedElement) sbj)
1259: .getName(), vn)) {
1260: return true;
1261: } else if (vn == null
1262: && "".equals(((INamedElement) sbj)
1263: .getName())) {
1264: //consider requested null name as empty name
1265: return true;
1266: }
1267: }
1268: }
1269: return false;
1270: }
1271:
1272: /**
1273: *
1274: * @return
1275: */
1276: public String getDescription() {
1277: //
1278: return "Choose element with Name: " + vn + "; Type: "
1279: + elemTypeEnu + "; or exType: " + elemExTypeEnu
1280: + "; string type: " + elementType + ";";
1281: }
1282: }
1283:
1284: public static class ElementByTypeChooser implements
1285: DiagramElementChooser {
1286:
1287: private String elementType = null;
1288:
1289: /**
1290: *
1291: * @param elementType
1292: */
1293: public ElementByTypeChooser(ElementTypes elementType) {
1294: this .elementType = elementType.toString();
1295: }
1296:
1297: /**
1298: *
1299: * @param graphObject
1300: * @return
1301: */
1302: public boolean checkElement(IETGraphObject graphObject) {
1303: String inType = null;
1304: if (graphObject.getPresentationElement().getSubjectCount() == 1) {
1305: inType = graphObject.getPresentationElement()
1306: .getFirstSubject().getElementType();
1307: }
1308:
1309: if ((elementType == null) || (!elementType.equals(inType))) {
1310: return false;
1311: } else {
1312: return true;
1313: }
1314: }
1315:
1316: /**
1317: *
1318: * @return
1319: */
1320: public String getDescription() {
1321: return "Choose element by Type:" + elementType;
1322: }
1323: }
1324:
1325: public static class DefaultNamer implements SetName {
1326:
1327: public DefaultNamer() {
1328: }
1329:
1330: /**
1331: *
1332: * @param drawingArea
1333: * @param x
1334: * @param y
1335: * @param name
1336: */
1337: public void setName(ComponentOperator drawingArea, int x,
1338: int y, String name) {
1339: new EventTool().waitNoEvent(1000);
1340: for (int i = 0; i < name.length(); i++) {
1341: drawingArea.typeKey(name.charAt(i));
1342: }
1343: drawingArea.typeKey('\n');
1344: new Timeout("", 500).sleep();
1345: }
1346: }
1347:
1348: public static class LabelsNamer implements SetName {
1349:
1350: public LabelsNamer() {
1351: }
1352:
1353: /**
1354: *
1355: * @param drawingArea
1356: * @param x
1357: * @param y
1358: * @param name
1359: */
1360: public void setName(ComponentOperator drawingArea, int x,
1361: int y, String name) {
1362: new LabelsNameElementAction()
1363: .performPopup(new ActionablePoint(drawingArea, x, y));
1364: for (int i = 0; i < name.length(); i++) {
1365: drawingArea.typeKey(name.charAt(i));
1366: }
1367: drawingArea.typeKey('\n');
1368: new Timeout("", 500).sleep();
1369: }
1370: }
1371:
1372: public static class PropertyNamer implements SetName {
1373:
1374: public PropertyNamer() {
1375: }
1376:
1377: /**
1378: *
1379: * @param drawingArea
1380: * @param x
1381: * @param y
1382: * @param name
1383: */
1384: public void setName(ComponentOperator drawingArea, int x,
1385: int y, String name) {
1386: PropertySheetOperator ps = new PropertySheetOperator();
1387: Property nmProp = new Property(ps, "Name");
1388: double nmPntX = ps.tblSheet().getCellRect(nmProp.getRow(),
1389: 1, false).getCenterX();
1390: double nmPntY = ps.tblSheet().getCellRect(nmProp.getRow(),
1391: 1, false).getCenterY();
1392: ps.clickMouse((int) nmPntX, (int) nmPntY, 1);
1393: for (int i = 0; i < name.length(); i++) {
1394: ps.typeKey(name.charAt(i));
1395: }
1396: ps.pushKey(KeyEvent.VK_ENTER);
1397: new Timeout("", 500).sleep();
1398: }
1399: }
1400: }
|