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.customelements;
043:
044: import java.awt.Point;
045: import java.awt.event.InputEvent;
046: import java.util.ArrayList;
047: import java.util.HashSet;
048: import java.util.Iterator;
049: import org.netbeans.jemmy.JemmyProperties;
050: import org.netbeans.jemmy.Timeout;
051: import org.netbeans.jemmy.operators.JButtonOperator;
052: import org.netbeans.jemmy.operators.JDialogOperator;
053: import org.netbeans.modules.uml.core.metamodel.core.foundation.IPresentationElement;
054: import org.netbeans.modules.uml.core.support.umlsupport.ETPoint;
055: import org.netbeans.modules.uml.core.support.umlsupport.IETPoint;
056: import org.netbeans.modules.uml.core.support.umlsupport.IETRect;
057: import org.netbeans.modules.uml.core.support.umlutils.ETList;
058: import org.netbeans.modules.uml.ui.support.applicationmanager.NodePresentation;
059: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IETGraphObject;
060: import org.netbeans.modules.uml.ui.swing.drawingarea.IDrawingAreaControl;
061: import org.netbeans.test.umllib.CompartmentTypes;
062: import org.netbeans.test.umllib.DiagramElementOperator;
063: import org.netbeans.test.umllib.DiagramOperator;
064: import org.netbeans.test.umllib.ElementTypes;
065: import org.netbeans.test.umllib.LinkOperator;
066: import org.netbeans.test.umllib.LinkTypes;
067: import org.netbeans.test.umllib.SetName;
068: import org.netbeans.test.umllib.exceptions.NotFoundException;
069: import org.netbeans.test.umllib.util.LibProperties;
070:
071: public class SequenceDiagramOperator extends DiagramOperator {
072: private int logicalTop = 50;
073:
074: public SequenceDiagramOperator(String name) {
075: super (name);
076: IDrawingAreaControl daControl = getDrawingArea().getArea()
077: .getDrawingArea();
078: logicalTop = daControl.deviceToLogicalPoint(10, 50).getY();
079: }
080:
081: /**
082: * Returns free point for a new lifeline to be created at. The nearest element would be not closer than
083: * <CODE>span</CODE> points in horizontal direction.
084: * You may use such point to place an element or invoke popup menu
085: * @param span Minimal distance to the nearest element
086: * @return free point
087: */
088: public Point getPointForLifeline(int span) {
089: int width = getSource().getWidth();
090: int height = getSource().getHeight();
091: IDrawingAreaControl daControl = getDrawingArea().getArea()
092: .getDrawingArea();
093:
094: ETList<IETGraphObject> allGraphs = daControl.getAllItems6();
095: Iterator<IETGraphObject> tsIt = allGraphs.iterator();
096: ArrayList<IETRect> elementBounds = new ArrayList<IETRect>();
097: while (tsIt.hasNext()) {
098: IETGraphObject graphObject = tsIt.next();
099: IPresentationElement presElement = graphObject
100: .getPresentationElement();
101: if (presElement == null) {
102: continue;
103: }
104: if (!(presElement instanceof NodePresentation)) { //We are looking only for nodes here
105: continue;
106: }
107:
108: IETRect rect = graphObject.getEngine()
109: .getLogicalBoundingRect(true);
110: rect.inflate(10 + span);
111: elementBounds.add(rect);
112: }
113:
114: IETPoint p = null;
115: int h = daControl.logicalToDevicePoint(
116: new ETPoint(0, logicalTop)).getY();
117: for (int w = 50; w < width - 50; w += 10) {
118: p = daControl.deviceToLogicalPoint(w, h);
119: boolean pointIsFree = true;
120: for (int i = 0; i < elementBounds.size(); i++) {
121: if (elementBounds.get(i).getRight() > p.getX()) {
122: pointIsFree = false;
123: break;
124: }
125: }
126: if (pointIsFree) {
127: return daControl.logicalToDevicePoint(p).asPoint();
128: }
129: }
130:
131: return getDrawingArea().centerAtPoint(new Point(width + 50, h));
132: }
133:
134: /**
135: * Put element on diagram by pressing toolbar butoon and clicking on
136: * the diagram
137: * @param name Name for new element
138: * @param elementType Type of new element
139: * @throws qa.uml.exceptions.NotFoundException when Button for the element is not found on toolbar
140: * @return Operator for created element
141: */
142: public DiagramElementOperator putElementOnDiagram(String name,
143: ElementTypes elementType) throws NotFoundException {
144: Point p = null;
145: if (elementType.equals(ElementTypes.LIFELINE)
146: || elementType.equals(ElementTypes.ACTOR_LIFELINE)) {
147: p = getPointForLifeline(100);
148: } else {
149: p = getDrawingArea().getFreePoint();
150: }
151: return putElementOnDiagram(name, elementType, p.x, p.y);
152: }
153:
154: /**
155: * create an element on diagram by pressing toolbar butoon and clicking on
156: * the diagram
157: * @param name Name for new element
158: * @param elementType Type of new element
159: */
160: public void createGenericElementOnDiagram(String name,
161: ElementTypes elementType) throws NotFoundException {
162: Point p = null;
163: if (elementType.equals(ElementTypes.LIFELINE)
164: || elementType.equals(ElementTypes.ACTOR_LIFELINE)) {
165: p = getPointForLifeline(100);
166: } else {
167: p = getDrawingArea().getFreePoint();
168: }
169: createGenericElementOnDiagram(name, elementType, p.x, p.y,
170: LibProperties.getCurrentNamer(elementType));
171: }
172:
173: /**
174: * Put element on diagram by pressing toolbar butoon and clicking on
175: * the diagram
176: * @param name Name for new element
177: * @param elementType Type of new element
178: * @param x X cooordinate of point where element will be created
179: * @param y Y cooordinate of point where element will be created
180: * @param namer Namer for this element. Certain elements
181: * should be named in very special way
182: * @throws qa.uml.exceptions.NotFoundException when Button for the element is not found on toolbar
183: * @return Operator for created element
184: */
185: public DiagramElementOperator putElementOnDiagram(String name,
186: ElementTypes elementType, int x, int y, SetName namer)
187: throws NotFoundException {
188: createGenericElementOnDiagram(name, elementType, x, y, namer);
189:
190: //TODO: this should be an option, not a rule
191: long timeoutValDlg = JemmyProperties
192: .getCurrentTimeout("DialogWaiter.WaitDialogTimeout");
193: try {
194: JemmyProperties.setCurrentTimeout(
195: "DialogWaiter.WaitDialogTimeout", 2000);
196: JDialogOperator classifierCreated = new JDialogOperator(
197: "Classifier not found");
198: new JButtonOperator(classifierCreated, "Yes").push();
199: } catch (Exception excp) {
200: } finally {
201: JemmyProperties.setCurrentTimeout(
202: "DialogWaiter.WaitDialogTimeout", timeoutValDlg);
203: }
204:
205: if (elementType.equals(ElementTypes.LIFELINE)
206: || elementType.equals(ElementTypes.ACTOR_LIFELINE)) {
207: int semicolonPos = name.indexOf(':');
208: String lineName = name.substring(0, semicolonPos);
209: String classifierName = name.substring(semicolonPos + 1);
210: return new LifelineOperator(this , lineName, classifierName);
211: } else {
212: return new DiagramElementOperator(this , name);
213: }
214: }
215:
216: /**
217: * Draw link on diagram
218: * @param linkElementType Link type
219: * @param fromElement Source element
220: * @param toElement target element
221: * @throws qa.uml.exceptions.NotFoundException when source or target is not found
222: * @return Operator for created link
223: */
224: public LinkOperator createLinkOnDiagram(LinkTypes linkElementType,
225: DiagramElementOperator fromElement,
226: DiagramElementOperator toElement) throws NotFoundException {
227: createGenericRelationshipOnDiagram(linkElementType,
228: fromElement, toElement);
229: if (linkElementType.equals(LinkTypes.SYNC_MESSAGE)
230: || linkElementType.equals(LinkTypes.ASYNC_MESSAGE)
231: || linkElementType.equals(LinkTypes.MESSAGE_TO_SELF)
232: || linkElementType.equals(LinkTypes.CREATE_MESSAGE)) {
233: linkElementType = LinkTypes.MESSAGE;
234: }
235: return new LinkOperator(fromElement, toElement, linkElementType);
236: }
237:
238: /**
239: * Create generic relationship on diagram
240: * @param linkElementType Link type
241: * @param fromElement Source element
242: * @param toElement target element
243: * @throws qa.uml.exceptions.NotFoundException when source or target is not found
244: */
245: public void createGenericRelationshipOnDiagram(
246: LinkTypes linkElementType,
247: DiagramElementOperator fromElement,
248: DiagramElementOperator toElement) throws NotFoundException {
249: if (!fromElement.getElementType().equals(
250: ElementTypes.LIFELINE.toString())) {
251: super .createGenericRelationshipOnDiagram(linkElementType,
252: fromElement, toElement);
253: return;
254: }
255: HashSet<LinkOperator> links = fromElement.getLinks();
256: Iterator<LinkOperator> it = links.iterator();
257:
258: //calculating start and destination points
259: SequenceLifelineCompartment sourceCompartment = new SequenceLifelineCompartment(
260: fromElement,
261: CompartmentTypes.SEQUENCE_LIFELINE_COMPARTMENT);
262: SequenceLifelineCompartment destCompartment = new SequenceLifelineCompartment(
263: toElement,
264: CompartmentTypes.SEQUENCE_LIFELINE_COMPARTMENT);
265: int logicalSrcYPos = sourceCompartment.getLogicalRectangle().y;
266: int logicalSrcXPos = fromElement.getGraphObject().getEngine()
267: .getLogicalBoundingRect(true).getCenterPoint().x;
268: int logicalDestYPos = destCompartment.getLogicalRectangle().y;
269: int logicalDestXPos = toElement.getGraphObject().getEngine()
270: .getLogicalBoundingRect(true).getCenterPoint().x;
271:
272: int lowest = (logicalSrcYPos < logicalDestYPos ? logicalSrcYPos
273: : logicalDestYPos);
274:
275: while (it.hasNext()) {
276: LinkOperator lnk = it.next();
277: int bottom = lnk.getSource().getEngine()
278: .getLogicalBoundingRect(true).getBottom();
279: if (lowest < bottom) {
280: lowest = bottom;
281: }
282: }
283:
284: int ySrc = lowest - 50;
285: int xSrc = logicalSrcXPos;
286:
287: int yDest = ySrc;
288: int xDest = logicalDestXPos;
289: //resizeLifeLine if needed
290: //TODO: add resize function
291:
292: //creating message
293: paletter().selectToolByType(linkElementType);
294: fromElement.center();
295: new Timeout("", 500).sleep();
296: Point devSrcPoint = this .getDrawingAreaControl()
297: .logicalToDevicePoint(new ETPoint(xSrc, ySrc))
298: .asPoint();
299: fromElement.clickOn(devSrcPoint, 1, InputEvent.BUTTON1_MASK, 0);
300: new Timeout("", 500).sleep();
301:
302: toElement.center();
303: new Timeout("", 500).sleep();
304: Point devDestPoint = this .getDrawingAreaControl()
305: .logicalToDevicePoint(new ETPoint(xDest, yDest))
306: .asPoint();
307: toElement.clickOn(devDestPoint, 1, InputEvent.BUTTON1_MASK, 0);
308: new Timeout("", 500).sleep();
309: paletter().selectToolByType(linkElementType);
310: toolbar().selectDefault();
311: }
312:
313: }
|