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: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039: package org.netbeans.jellytools.widgets;
040:
041: import java.awt.Point;
042: import java.util.List;
043: import org.netbeans.api.visual.widget.ConnectionWidget;
044: import org.netbeans.api.visual.widget.Widget;
045: import org.netbeans.jellytools.TopComponentOperator;
046:
047: /**
048: * Handle org.netbeans.api.visual.widget.ConnectionWidget object which connects
049: * source and target widgets.
050: * <p>
051: * Usage:<br>
052: * <pre>
053: TopComponentOperator tco = new TopComponentOperator("My scene");
054: LabelWidgetOperator lwo0 = new LabelWidgetOperator(tco, "Label 0");
055: lwo0.performPopupAction("An action");
056: LabelWidgetOperator lwo1 = new LabelWidgetOperator(tco, "Label 1");
057: // drag from one widget to another to make connection
058: lwo0.dragNDrop(lwo1);
059: ConnectionWidgetOperator cwo = new ConnectionWidgetOperator(lwo0, lwo1);
060: Point sourceControlPoint = cwo.getSourceControlPoint();
061: Point targetControlPoint = cwo.getTargetControlPoint();
062: * </pre>
063: *
064: * @see WidgetOperator
065: * @author Jiri Skrivanek
066: */
067: public class ConnectionWidgetOperator extends WidgetOperator {
068:
069: /** Creates operator for given ConnectionWidget.
070: * @param widget ConnectionWidget to create operator for
071: */
072: public ConnectionWidgetOperator(ConnectionWidget widget) {
073: super (widget);
074: }
075:
076: /** Waits for index-th ConnectionWidget under given parent Widget.
077: * @param parentWidgetOper parent WidgetOperator
078: * @param index index of widget to be found
079: */
080: public ConnectionWidgetOperator(WidgetOperator parentWidgetOper,
081: int index) {
082: super (parentWidgetOper, new ConnectionWidgetChooser(), index);
083: }
084:
085: /** Waits for index-th ConnectionWidget under given parent Widget.
086: * @param parentWidgetOper parent WidgetOperator
087: */
088: public ConnectionWidgetOperator(WidgetOperator parentWidgetOper) {
089: this (parentWidgetOper, 0);
090: }
091:
092: /** Waits for ConnectionWidget with given source and target widgets.
093: * @param sourceWidgetOper source widget
094: * @param targetWidgetOper target widget
095: */
096: public ConnectionWidgetOperator(WidgetOperator sourceWidgetOper,
097: WidgetOperator targetWidgetOper) {
098: super (sourceWidgetOper.getSceneOperator(),
099: new ConnectionWidgetChooser(sourceWidgetOper,
100: targetWidgetOper));
101: }
102:
103: /** Waits for ConnectionWidget under given TopComponent.
104: * @param tco TopComponentOperator to find widgets in
105: */
106: public ConnectionWidgetOperator(TopComponentOperator tco) {
107: this (tco, 0);
108: }
109:
110: /** Waits for index-th ConnectionWidget under given TopComponent.
111: * @param tco TopComponentOperator to find widgets in
112: * @param index index of widget to be found
113: */
114: public ConnectionWidgetOperator(TopComponentOperator tco, int index) {
115: super (tco, new ConnectionWidgetChooser(), index);
116: }
117:
118: /** WidgetChooser to find ConnectionWidget with specified source and target widgets. */
119: private static final class ConnectionWidgetChooser implements
120: WidgetChooser {
121:
122: private WidgetOperator sourceWidgetOper;
123: private WidgetOperator targetWidgetOper;
124:
125: /** Used to find ConnectionWidget. */
126: public ConnectionWidgetChooser() {
127: }
128:
129: public ConnectionWidgetChooser(WidgetOperator sourceWidgetOper,
130: WidgetOperator targetWidgetOper) {
131: this .sourceWidgetOper = sourceWidgetOper;
132: this .targetWidgetOper = targetWidgetOper;
133: }
134:
135: public boolean checkWidget(Widget widget) {
136: if (widget instanceof ConnectionWidget) {
137: if (sourceWidgetOper != null
138: && targetWidgetOper != null) {
139: ConnectionWidgetOperator cwo = new ConnectionWidgetOperator(
140: (ConnectionWidget) widget);
141: if (sourceWidgetOper.getWidget() == cwo
142: .getSourceWidgetOperator().getWidget()
143: && targetWidgetOper.getWidget() == cwo
144: .getTargetWidgetOperator()
145: .getWidget()) {
146: return true;
147: }
148: } else {
149: return true;
150: }
151: }
152: return false;
153: }
154:
155: public String getDescription() {
156: return "ConnectionWidget"
157: + (sourceWidgetOper != null
158: && targetWidgetOper != null ? " with source "
159: + sourceWidgetOper
160: + " and target "
161: + targetWidgetOper
162: : "");
163: }
164: }
165:
166: /** Returns control point of source widget.
167: * @return Point representing control point of source widget.
168: */
169: public Point getSourceControlPoint() {
170: return (Point) runMapping(new MapAction("getFirstControlPoint") {
171:
172: public Object map() {
173: return ((ConnectionWidget) widget)
174: .getFirstControlPoint();
175: }
176: });
177: }
178:
179: /** Returns control point of target widget.
180: * @return Point representing control point of target widget.
181: */
182: public Point getTargetControlPoint() {
183: return (Point) runMapping(new MapAction("getLastControlPoint") {
184:
185: public Object map() {
186: return ((ConnectionWidget) widget)
187: .getLastControlPoint();
188: }
189: });
190: }
191:
192: /** Returns index-th control point.
193: * @param index index of requested control point
194: * @return Point representing control point of source widget.
195: */
196: public Point getControlPoint(final int index) {
197: return (Point) runMapping(new MapAction("getControlPoint") {
198:
199: public Object map() {
200: return ((ConnectionWidget) widget)
201: .getControlPoint(index);
202: }
203: });
204: }
205:
206: /** Returns list of control points.
207: * @return List<Point> of control points
208: */
209: @SuppressWarnings("unchecked")
210: public List<Point> getControlPoints() {
211: return (List<Point>) runMapping(new MapAction(
212: "getControlPoints") {
213:
214: public Object map() {
215: return ((ConnectionWidget) widget).getControlPoints();
216: }
217: });
218: }
219:
220: /** Returns WidgetOperator of source widget of this connection.
221: * @return WidgetOperator instance of source widget of this connection.
222: */
223: public WidgetOperator getSourceWidgetOperator() {
224: return new WidgetOperator((Widget) runMapping(new MapAction(
225: "getSourceAnchor().getRelatedWidget()") {
226:
227: public Object map() {
228: return ((ConnectionWidget) widget).getSourceAnchor()
229: .getRelatedWidget();
230: }
231: }));
232: }
233:
234: /** Returns WidgetOperator of target widget of this connection.
235: * @return WidgetOperator instance of target widget of this connection.
236: */
237: public WidgetOperator getTargetWidgetOperator() {
238: return new WidgetOperator((Widget) runMapping(new MapAction(
239: "getTargetAnchor().getRelatedWidget()") {
240:
241: public Object map() {
242: return ((ConnectionWidget) widget).getTargetAnchor()
243: .getRelatedWidget();
244: }
245: }));
246: }
247: }
|