001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.test.umllib;
043:
044: import java.awt.Point;
045: import java.awt.Rectangle;
046: import java.awt.event.InputEvent;
047: import java.awt.event.KeyEvent;
048: import java.util.ArrayList;
049: import java.util.Arrays;
050: import java.util.Comparator;
051: import java.util.Iterator;
052: import org.netbeans.jellytools.MainWindowOperator;
053: import org.netbeans.jemmy.JemmyProperties;
054: import org.netbeans.jemmy.Timeout;
055: import org.netbeans.jemmy.Timeouts;
056: import org.netbeans.jemmy.Waitable;
057: import org.netbeans.jemmy.Waiter;
058: import org.netbeans.jemmy.drivers.input.MouseRobotDriver;
059: import org.netbeans.jemmy.operators.JPopupMenuOperator;
060: import org.netbeans.jemmy.operators.Operator;
061: import org.netbeans.modules.uml.core.metamodel.core.foundation.IPresentationElement;
062: import org.netbeans.modules.uml.core.support.umlsupport.ETRect;
063: import org.netbeans.modules.uml.core.support.umlsupport.IETPoint;
064: import org.netbeans.modules.uml.core.support.umlsupport.IETRect;
065: import org.netbeans.modules.uml.core.support.umlutils.ETList;
066: import org.netbeans.modules.uml.ui.support.applicationmanager.LabelPresentation;
067: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IETGraphObject;
068: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IETLabel;
069: import org.netbeans.modules.uml.ui.support.viewfactorysupport.TSLabelKind;
070: import org.netbeans.modules.uml.ui.swing.drawingarea.ADGraphWindow;
071: import org.netbeans.modules.uml.ui.swing.drawingarea.IDrawingAreaControl;
072: import org.netbeans.test.umllib.actions.Actionable;
073: import org.netbeans.test.umllib.exceptions.NotFoundException;
074: import org.netbeans.test.umllib.util.JPopupByPointChooser;
075:
076: /**
077: * This is operator for labels.
078: */
079: public class DiagramLabelOperator implements Actionable {
080:
081: /**
082: * Default delay for Compartment Waiter. Real value can be changed
083: * as for the all other operators(For example via JemmyProperties)
084: */
085: public static final long WAIT_LABEL_TIMEOUT = 5000;
086:
087: static {
088: Timeouts.initDefault("LabelOperator.WaitLabelTime",
089: WAIT_LABEL_TIMEOUT);
090: }
091:
092: protected IETLabel sourceLabel = null;
093: private DiagramOperator diagramOperator = null;
094:
095: /**
096: *
097: * @return
098: */
099: protected IETLabel getSource() {
100: return sourceLabel;
101: }
102:
103: /**
104: * Constructs label by text
105: * @param dia Instance of DiagramOperator
106: * @param text text
107: * @throws qa.uml.exceptions.NotFoundException when no suitable label found
108: */
109: public DiagramLabelOperator(DiagramOperator dia, String text)
110: throws NotFoundException {
111: this (dia, waitForLabel(dia, new LabelByTextChooser(text), 0));
112: }
113:
114: /**
115: * Costruct label by text and index
116: * @param dia - current diagram operator
117: * @param text - label's text
118: * @param index - index of the label
119: * @throws qa.uml.exceptions.NotFoundException when no suitable label found
120: */
121: public DiagramLabelOperator(DiagramOperator dia, String text,
122: int index) throws NotFoundException {
123: this (dia,
124: waitForLabel(dia, new LabelByTextChooser(text), index));
125: }
126:
127: /**
128: * Costruct label by chooser and index
129: * @param dia - current diagram operator
130: * @param chooser - label's chooser
131: * @param index - index of the label
132: * @throws qa.uml.exceptions.NotFoundException when no suitable label found
133: */
134: public DiagramLabelOperator(DiagramOperator dia,
135: LabelChooser chooser, int index) throws NotFoundException {
136: this (dia, waitForLabel(dia, chooser, index));
137: }
138:
139: /**
140: * Construct label by label class
141: * @param dia
142: * @param label
143: */
144: public DiagramLabelOperator(DiagramOperator dia, IETLabel label) {
145: sourceLabel = label;
146: diagramOperator = dia;
147: }
148:
149: /**
150: *method return name of comparment from compartment source
151: *@return compartmant name
152: */
153: public String getText() {
154: return sourceLabel.getText();
155: }
156:
157: /**
158: * Search for suitable graph object
159: * @param diagramOperator Diagram to look for label
160: * @param labelChooser custom chooser
161: * @param index index
162: * @throws qa.uml.exceptions.NotFoundException when nothing suitable found
163: * @return Graph object if found
164: */
165: public static IETLabel findLabel(DiagramOperator diagramOperator,
166: LabelChooser labelChooser, int index)
167: throws NotFoundException {
168: ArrayList<IETLabel> elementsFound = new ArrayList<IETLabel>();
169:
170: //searching for elements matching elemenFinder criteria
171: ETList<IETGraphObject> allGraphs = diagramOperator
172: .getDrawingAreaControl().getAllItems6();
173: Iterator<IETGraphObject> tsIt = allGraphs.iterator();
174: while (tsIt.hasNext()) {
175: IETGraphObject graphObject = tsIt.next();
176: if (!(graphObject instanceof IETLabel)) {
177: continue;
178: }
179: IETLabel label = (IETLabel) graphObject;
180: IPresentationElement presElement = graphObject
181: .getPresentationElement();
182: if (presElement == null) {
183: continue;
184: }
185: if (!(presElement instanceof LabelPresentation)) { //We are looking only for labels here
186: continue;
187: }
188:
189: if (labelChooser.checkLabel(label)) {
190: elementsFound.add(label);
191: }
192: }
193:
194: //sorting found elements
195: LabelComparator<IETLabel> c = new LabelComparator<IETLabel>();
196: IETLabel[] arr = (IETLabel[]) elementsFound
197: .toArray(new IETLabel[0]);
198: Arrays.sort(arr, c);
199: if (arr.length > index) {
200: return arr[index];
201: } else {
202: throw new NotFoundException(
203: "Element matching the criteria not found on diagram "
204: + diagramOperator.getDiagramName());
205: }
206: }
207:
208: public static class LabelComparator<C extends IETLabel> implements
209: Comparator<C> {
210: /**
211: *
212: * @param o1
213: * @param o2
214: * @return
215: */
216: public int compare(C o1, C o2) {
217: Point o1Center = o1.getEngine().getBoundingRect()
218: .getCenterPoint();
219: Point o2Center = o2.getEngine().getBoundingRect()
220: .getCenterPoint();
221: if (o1Center.y > o2Center.y) {
222: return 1;
223: } else if (o1Center.y == o2Center.y) {
224: if (o1Center.x > o2Center.y) {
225: return -1;
226: } else {
227: return 1;
228: }
229: } else {
230: return -1;
231: }
232: }
233: }
234:
235: /**
236: * Wait for suitable graph object
237: * @return Element's GraphObject if found
238: * @param chooser
239: * @param diagramOperator Diagram to look for element
240: * @param index index
241: */
242: public static IETLabel waitForLabel(
243: final DiagramOperator diagramOperator,
244: final LabelChooser chooser, final int index) {
245: try {
246: Waiter w = new Waiter(new Waitable() {
247: public Object actionProduced(Object obj) {
248: try {
249: IETLabel go = findLabel(diagramOperator,
250: chooser, index);
251: return go;
252: } catch (NotFoundException e) {
253: return null;
254: }
255: }
256:
257: public String getDescription() {
258: return ("Wait for " + chooser.getDescription());
259: }
260: });
261: Timeouts t = JemmyProperties.getCurrentTimeouts();
262: t.setTimeout("Waiter.WaitingTime", t
263: .getTimeout("LabelOperator.WaitLabelTime"));
264: return (IETLabel) w.waitAction(null);
265:
266: } catch (InterruptedException ie) {
267: return null;
268: }
269: }
270:
271: //Methods from Actionable interface
272: /**
273: *
274: * @return
275: */
276: public JPopupMenuOperator getPopup() {
277: //workarround for Issue 79519
278: if (System.getProperty("os.name").toLowerCase().indexOf(
279: "windows") == -1) {
280: clickOnCenter();
281: try {
282: Thread.sleep(100);
283: } catch (Exception ex) {
284: }
285: }
286: clickOnCenter(1, InputEvent.BUTTON3_MASK, 0);
287: JPopupMenuOperator ret = new JPopupMenuOperator(
288: JPopupMenuOperator.waitJPopupMenu(
289: (java.awt.Container) (MainWindowOperator
290: .getDefault().getSource()),
291: new JPopupByPointChooser(getCenterPoint(),
292: diagramOperator.getDrawingArea()
293: .getSource(), 0)));
294: return ret;
295: }
296:
297: /**
298: * Select label by click on center
299: */
300: public void select() {
301: clickOnCenter();
302: new Timeout("", 50).sleep();
303: }
304:
305: public void addToSelection() {
306: new Timeout("", 10).sleep();
307: clickOnCenter(1, InputEvent.BUTTON1_MASK, KeyEvent.CTRL_MASK);
308: new Timeout("", 50).sleep();
309: }
310:
311: /**
312: *
313: * @param clickCount
314: * @param mouseButton
315: * @param modifiers
316: */
317: public void clickOnCenter(int clickCount, int mouseButton,
318: int modifiers) {
319: clickOn(getCenterPoint(), clickCount, mouseButton, modifiers);
320: }
321:
322: /**
323: *
324: * @param clickCount
325: * @param mouseButton
326: */
327: public void clickOnCenter(int clickCount, int mouseButton) {
328: clickOn(getCenterPoint(), clickCount, mouseButton, 0);
329: }
330:
331: public void clickOnCenter() {
332: clickOn(getCenterPoint(), 1, InputEvent.BUTTON1_MASK, 0);
333: }
334:
335: /**
336: *
337: * @param p
338: * @param clickCount
339: * @param mouseButton
340: * @param modifiers
341: */
342: public void clickOn(Point p, int clickCount, int mouseButton,
343: int modifiers) {
344: p = makeVisible(p);
345: diagramOperator.getDrawingArea().clickMouse(p.x, p.y,
346: clickCount, mouseButton, modifiers);
347: }
348:
349: public void clickForPopup() {
350: clickOn(getCenterPoint(), 1, InputEvent.BUTTON3_MASK, 0);
351: }
352:
353: private void dummy() {
354: //diagramOperator.getDrawingArea
355: ADGraphWindow a;
356: // a.sc
357: }
358:
359: public void center() {
360: center(false, false);
361: }
362:
363: /**
364: *
365: * @param selectIt
366: * @param deselectOthers
367: */
368: public void center(boolean selectIt, boolean deselectOthers) {
369: ADGraphWindow area = diagramOperator.getDrawingArea().getArea();
370: area.getDrawingArea().centerPresentationElement(
371: sourceLabel.getPresentationElement(), selectIt,
372: deselectOthers);
373: }
374:
375: /**
376: * @return bounding rect for label
377: */
378: public IETRect getBoundingRect() {
379: return sourceLabel.getEngine().getBoundingRect();
380: }
381:
382: /**
383: * Returns center point of this compartment
384: * @return center point of this compartment
385: */
386: public Point getCenterPoint() {
387: return getBoundingRect().getCenterPoint();
388: }
389:
390: /**
391: *
392: * @return true if it's name label
393: */
394: public boolean isLinkName() {
395: int kind = sourceLabel.getLabelKind();
396: //
397: boolean isName = kind == TSLabelKind.TSLK_ACTIVITYEDGE_NAME;
398: isName = isName || kind == TSLabelKind.TSLK_ASSOCIATION_NAME;
399: isName = isName || kind == TSLabelKind.TSLK_NAME;
400: /*isName=isName || kind==TSLabelKind.;
401: isName=isName || kind==TSLabelKind.;
402: isName=isName || kind==TSLabelKind.;
403: isName=isName || kind==TSLabelKind.;
404: isName=isName || kind==TSLabelKind.;
405: isName=isName || kind==TSLabelKind.;
406: isName=isName || kind==TSLabelKind.;
407: isName=isName || kind==TSLabelKind.;
408: isName=isName || kind==TSLabelKind.;
409: isName=isName || kind==TSLabelKind.;*/
410: return isName;
411: }
412:
413: /**
414: * gets point inside a component and, if component is not visible centers window in this component.
415: * The updated device point is returned
416: * @param point
417: * @return
418: */
419: public Point makeVisible(Point point) {
420: ADGraphWindow area = diagramOperator.getDrawingArea().getArea();
421: IDrawingAreaControl daControl = area.getDrawingArea();
422:
423: IETPoint etPoint = daControl.deviceToLogicalPoint(point.x,
424: point.y);
425:
426: IETRect eDeviceAreaRect = new ETRect(area.getVisibleRect());
427: IETRect eVisibleAreaRect = daControl
428: .deviceToLogicalRect(eDeviceAreaRect);
429: IETRect eElementRect = sourceLabel.getEngine()
430: .getLogicalBoundingRect(true);
431:
432: if (!eVisibleAreaRect.contains(eElementRect)) {
433: center();
434: new Timeout("", 500);
435: }
436: return daControl.logicalToDevicePoint(etPoint).asPoint();
437: }
438:
439: /**
440: * Change element position with usage of mouse robot driver
441: * @param x
442: * @param y
443: */
444: public void moveTo(int x, int y) {
445: Point parB = getCenterPoint();
446: DrawingAreaOperator drA = diagramOperator.getDrawingArea();
447: MouseRobotDriver driver = new MouseRobotDriver(new Timeout("",
448: 250));
449: driver.moveMouse(drA, parB.x, parB.y);
450: driver.pressMouse(drA, parB.x, parB.y, InputEvent.BUTTON1_MASK,
451: 0);
452: new Timeout("", 500).sleep();
453: driver.moveMouse(drA, x, y);
454: new Timeout("", 500).sleep();
455: driver.releaseMouse(drA, x, y, InputEvent.BUTTON1_MASK, 0);
456: new Timeout("", 500).sleep();
457: }
458:
459: /**
460: *
461: * @param x
462: * @param y
463: */
464: public void moveTo(double x, double y) {
465: moveTo((int) x, (int) y);
466: }
467:
468: /**
469: * Change element position with usage of mouse robot driver
470: * @param dx
471: * @param dy
472: */
473: public void shift(int dx, int dy) {
474: Point parB = getCenterPoint();
475: DrawingAreaOperator drA = diagramOperator.getDrawingArea();
476: MouseRobotDriver driver = new MouseRobotDriver(new Timeout("",
477: 250));
478: driver.moveMouse(drA, parB.x, parB.y);
479: driver.pressMouse(drA, parB.x, parB.y, InputEvent.BUTTON1_MASK,
480: 0);
481: new Timeout("", 500).sleep();
482: driver.moveMouse(drA, parB.x + dx, parB.y + dy);
483: new Timeout("", 500).sleep();
484: driver.releaseMouse(drA, parB.x + dx, parB.y + dy,
485: InputEvent.BUTTON1_MASK, 0);
486: new Timeout("", 500).sleep();
487: }
488:
489: /**
490: *
491: * @param dx
492: * @param dy
493: */
494: public void shift(double dx, double dy) {
495: shift((int) dx, (int) dy);
496: }
497:
498: /**
499: * Select compartmnet by name
500: */
501: public static class LabelByTextChooser implements LabelChooser {
502:
503: private String text = null;
504:
505: /**
506: * Select compartment by type
507: * @param text
508: */
509: public LabelByTextChooser(String text) {
510: this .text = text;
511: }
512:
513: /**
514: * Check param by it's type
515: * @return true if suitable and false otherwise
516: * @param label
517: */
518: public boolean checkLabel(IETLabel label) {
519: return text.equals(label.getText());
520: }
521:
522: /**
523: * Returns description of this chooser
524: * @return description of this chooser
525: */
526: public String getDescription() {
527: return "Chooser that accepts label with specific text";
528: }
529:
530: }
531:
532: /**
533: * Any compartment chooser. Used for looking by index.
534: */
535: public static class AnyLabelChooser implements LabelChooser {
536:
537: /**
538: * Any Compartment chooser. Selects any compartments
539: */
540: public AnyLabelChooser() {
541: }
542:
543: /**
544: * Check label
545: * @param lab label to check
546: * @return Always true
547: */
548: public boolean checkLabel(IETLabel lab) {
549: return true;
550: }
551:
552: /**
553: * Returns short description
554: * @return Description
555: */
556: public String getDescription() {
557: return "Chooser that accepts any label ";
558: }
559:
560: }
561:
562: }
|