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: package org.netbeans.modules.visual.action;
042:
043: import org.netbeans.api.visual.action.ConnectDecorator;
044: import org.netbeans.api.visual.action.ConnectProvider;
045: import org.netbeans.api.visual.action.WidgetAction;
046: import org.netbeans.api.visual.action.ConnectorState;
047: import org.netbeans.api.visual.anchor.Anchor;
048: import org.netbeans.api.visual.widget.ConnectionWidget;
049: import org.netbeans.api.visual.widget.Scene;
050: import org.netbeans.api.visual.widget.Widget;
051:
052: import java.awt.*;
053: import java.awt.event.MouseEvent;
054: import java.awt.event.KeyEvent;
055:
056: /**
057: * Override at least isTargetWidget and createConnection methods. isSourceWidget is always called before isTargetWidget.
058: *
059: * @author David Kaspar
060: */
061: public class ConnectAction extends WidgetAction.LockedAdapter {
062:
063: private static final int MIN_DIFFERENCE = 5;
064:
065: private ConnectDecorator decorator;
066: private Widget interractionLayer;
067: private ConnectProvider provider;
068:
069: private ConnectionWidget connectionWidget = null;
070: private Widget sourceWidget = null;
071: private Widget targetWidget = null;
072: private Point startingPoint = null;
073:
074: public ConnectAction(ConnectDecorator decorator,
075: Widget interractionLayer, ConnectProvider provider) {
076: this .decorator = decorator;
077: this .interractionLayer = interractionLayer;
078: this .provider = provider;
079: }
080:
081: protected boolean isLocked() {
082: return sourceWidget != null;
083: }
084:
085: public WidgetAction.State mousePressed(Widget widget,
086: WidgetAction.WidgetMouseEvent event) {
087: if (isLocked())
088: return WidgetAction.State.createLocked(widget, this );
089: return mousePressedCore(widget, event);
090: }
091:
092: protected State mousePressedCore(Widget widget,
093: WidgetMouseEvent event) {
094: if (event.getButton() == MouseEvent.BUTTON1
095: && event.getClickCount() == 1) {
096: if (provider.isSourceWidget(widget)) {
097: sourceWidget = widget;
098: targetWidget = null;
099: startingPoint = new Point(event.getPoint());
100: connectionWidget = decorator
101: .createConnectionWidget(interractionLayer
102: .getScene());
103: assert connectionWidget != null;
104: connectionWidget.setSourceAnchor(decorator
105: .createSourceAnchor(widget));
106: interractionLayer.addChild(connectionWidget);
107: return State.createLocked(widget, this );
108: }
109: }
110: return State.REJECTED;
111: }
112:
113: public WidgetAction.State mouseReleased(Widget widget,
114: WidgetAction.WidgetMouseEvent event) {
115: Point point = event.getPoint();
116: boolean state = move(widget, point);
117: if (state) {
118: if (targetWidget != null)
119: if (Math.abs(startingPoint.x - point.x) >= MIN_DIFFERENCE
120: || Math.abs(startingPoint.y - point.y) >= MIN_DIFFERENCE)
121: provider.createConnection(sourceWidget,
122: targetWidget);
123: cancel();
124: }
125: return state ? State.CONSUMED : State.REJECTED;
126: }
127:
128: private void cancel() {
129: sourceWidget = null;
130: targetWidget = null;
131: startingPoint = null;
132: connectionWidget.setSourceAnchor(null);
133: connectionWidget.setTargetAnchor(null);
134: interractionLayer.removeChild(connectionWidget);
135: connectionWidget = null;
136: }
137:
138: public WidgetAction.State mouseDragged(Widget widget,
139: WidgetAction.WidgetMouseEvent event) {
140: return move(widget, event.getPoint()) ? State.createLocked(
141: widget, this ) : State.REJECTED;
142: }
143:
144: private boolean move(Widget widget, Point point) {
145: if (sourceWidget != widget)
146: return false;
147:
148: Point targetSceneLocation = widget.convertLocalToScene(point);
149: targetWidget = resolveTargetWidgetCore(interractionLayer
150: .getScene(), targetSceneLocation);
151: Anchor targetAnchor = null;
152: if (targetWidget != null)
153: targetAnchor = decorator.createTargetAnchor(targetWidget);
154: if (targetAnchor == null)
155: targetAnchor = decorator
156: .createFloatAnchor(targetSceneLocation);
157: connectionWidget.setTargetAnchor(targetAnchor);
158:
159: return true;
160: }
161:
162: private Widget resolveTargetWidgetCore(Scene scene,
163: Point sceneLocation) {
164: if (provider != null)
165: if (provider.hasCustomTargetWidgetResolver(scene))
166: return provider.resolveTargetWidget(scene,
167: sceneLocation);
168: Point sceneOrigin = scene.getLocation();
169: sceneLocation = new Point(sceneLocation.x + sceneOrigin.x,
170: sceneLocation.y + sceneOrigin.y);
171: Widget[] result = new Widget[] { null };
172: resolveTargetWidgetCoreDive(result, scene, sceneLocation);
173: return result[0];
174: }
175:
176: private boolean resolveTargetWidgetCoreDive(Widget[] result,
177: Widget widget, Point parentLocation) {
178: if (interractionLayer.equals(widget))
179: return false;
180: Point widgetLocation = widget.getLocation();
181: Point location = new Point(parentLocation.x - widgetLocation.x,
182: parentLocation.y - widgetLocation.y);
183:
184: if (!widget.getBounds().contains(location))
185: return false;
186:
187: java.util.List<Widget> children = widget.getChildren();
188: for (int i = children.size() - 1; i >= 0; i--) {
189: if (resolveTargetWidgetCoreDive(result, children.get(i),
190: location))
191: return true;
192: }
193:
194: if (!widget.isHitAt(location))
195: return false;
196:
197: ConnectorState state = provider.isTargetWidget(sourceWidget,
198: widget);
199: if (state == ConnectorState.REJECT)
200: return false;
201: if (state == ConnectorState.ACCEPT)
202: result[0] = widget;
203: return true;
204: }
205:
206: public State keyPressed(Widget widget, WidgetKeyEvent event) {
207: if (isLocked() && event.getKeyCode() == KeyEvent.VK_ESCAPE) {
208: cancel();
209: return State.CONSUMED;
210: }
211: return super.keyPressed(widget, event);
212: }
213:
214: }
|