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-2006 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.vmd.midp.flow;
042:
043: import org.netbeans.api.visual.anchor.Anchor;
044: import org.netbeans.api.visual.vmd.VMDConnectionWidget;
045: import org.netbeans.api.visual.vmd.VMDFactory;
046: import org.netbeans.api.visual.widget.ConnectionWidget;
047: import org.netbeans.api.visual.widget.Widget;
048: import org.netbeans.modules.vmd.api.flow.FlowEdgePresenter;
049: import org.netbeans.modules.vmd.api.flow.visual.*;
050: import org.netbeans.modules.vmd.api.model.DesignComponent;
051: import org.netbeans.modules.vmd.api.model.DesignEventFilter;
052: import org.netbeans.modules.vmd.api.model.presenters.actions.ActionsPresenter;
053: import org.netbeans.modules.vmd.midp.actions.GoToSourceAction;
054: import org.netbeans.modules.vmd.midp.components.MidpDocumentSupport;
055: import org.netbeans.modules.vmd.midp.components.handlers.EventHandlerCD;
056:
057: import javax.swing.*;
058: import java.util.Collection;
059: import java.util.List;
060:
061: /**
062: * @author David Kaspar
063: */
064: // TODO - extract abstract methods for acquiring source and target
065: // TODO - rewrite to get relationship to the EventSource just by looking at the parent component
066: public abstract class FlowEventHandlerEdgePresenter extends
067: FlowEdgePresenter {
068:
069: private final EventHandlerPinDecoratorBehaviour pinCtrl = new EventHandlerPinDecoratorBehaviour();
070: private final EventHandlerEdgeDecoratorBehaviour edgeCtrl = new EventHandlerEdgeDecoratorBehaviour();
071:
072: private FlowEdgeDescriptor edgeDescriptor;
073:
074: protected abstract DesignComponent getTargetComponent();
075:
076: protected String getEdgeID() {
077: return FlowIDSupport.createEventHandlerEdgeID(getComponent());
078: }
079:
080: protected String getTargetPinID(DesignComponent target) {
081: return FlowIDSupport.createEventHandlerTargetPinID(
082: getComponent(), target);
083: }
084:
085: protected boolean isDynamicTargetPin() {
086: return true;
087: }
088:
089: protected boolean isVisible() {
090: return super .isVisible() && getSourceComponent() != null;
091: }
092:
093: protected DesignComponent getSourceComponent() {
094: return getComponent().readProperty(
095: EventHandlerCD.PROP_EVENT_SOURCE).getComponent();
096: }
097:
098: protected String getSourcePinID(DesignComponent source) {
099: return FlowIDSupport.createEventSourcePinID(source);
100: }
101:
102: protected DesignComponent getRepresentedSourceComponent(
103: DesignComponent source) {
104: return source;
105: }
106:
107: protected DesignComponent getRepresentedTargetComponent(
108: DesignComponent target) {
109: return target;
110: }
111:
112: protected DesignComponent getRepresentedComponent() {
113: return getComponent();
114: }
115:
116: protected DesignEventFilter getEventFilter() {
117: return new DesignEventFilter().addParentFilter(getComponent(),
118: 1, false);
119: }
120:
121: public final void updateDescriptors() {
122: edgeDescriptor = null;
123: if (!isVisible())
124: return;
125: DesignComponent source = getSourceComponent();
126: if (source == null)
127: return;
128: DesignComponent target = getTargetComponent();
129: if (target == null)
130: return;
131:
132: edgeDescriptor = new FlowEdgeDescriptor(
133: getRepresentedComponent(), getEdgeID(),
134: new FlowPinDescriptor(
135: getRepresentedSourceComponent(source),
136: getSourcePinID(source)), false,
137: new FlowPinDescriptor(
138: getRepresentedTargetComponent(target),
139: getTargetPinID(target)), isDynamicTargetPin());
140: }
141:
142: protected final FlowNodeDescriptor getSourceNodeDescriptor(
143: FlowPinDescriptor sourcePinDescriptor) {
144: return FlowEventSourcePinPresenter
145: .getNodeDescriptor(getSourceComponent());
146: }
147:
148: protected final FlowNodeDescriptor getTargetNodeDescriptor(
149: FlowPinDescriptor targetPinDescriptor) {
150: DesignComponent targetComponent = getTargetComponent();
151: return new FlowNodeDescriptor(targetComponent, FlowIDSupport
152: .createNodeID4SingleNode(targetComponent));
153: }
154:
155: public final FlowEdgeDescriptor getEdgeDescriptor() {
156: return edgeDescriptor;
157: }
158:
159: public FlowEdgeDescriptor.EdgeDecorator getDecorator() {
160: return edgeCtrl;
161: }
162:
163: public FlowEdgeDescriptor.EdgeBehaviour getBehaviour() {
164: return edgeCtrl;
165: }
166:
167: public FlowPinDescriptor.PinDecorator getDynamicPinDecorator() {
168: return pinCtrl;
169: }
170:
171: public FlowPinDescriptor.PinBehaviour getDynamicPinBehaviour() {
172: return pinCtrl;
173: }
174:
175: public static class EventHandlerPinDecoratorBehaviour implements
176: FlowPinDescriptor.PinDecorator,
177: FlowPinDescriptor.PinBehaviour {
178:
179: public Widget createWidget(FlowPinDescriptor descriptor,
180: FlowScene scene) {
181: return null;
182: }
183:
184: public Anchor createAnchor(FlowPinDescriptor descriptor,
185: FlowScene scene) {
186: FlowNodeDescriptor node = scene.getPinNode(descriptor);
187: return scene.getDecorator(node).createAnchor(node, scene);
188: }
189:
190: public void update(FlowPinDescriptor descriptor, FlowScene scene) {
191: }
192:
193: public String getOrderCategory(FlowPinDescriptor descriptor) {
194: return null;
195: }
196:
197: public void updateBadges(FlowPinDescriptor descriptor,
198: FlowScene scene, List<FlowBadgeDescriptor> badges) {
199: }
200:
201: public boolean isConnectionSource(FlowPinDescriptor pin) {
202: return false;
203: }
204:
205: public boolean isConnectionTarget(FlowPinDescriptor sourcePin,
206: FlowDescriptor target) {
207: return false;
208: }
209:
210: public void createConnection(FlowPinDescriptor sourcePin,
211: FlowDescriptor target) {
212: }
213:
214: }
215:
216: public class EventHandlerEdgeDecoratorBehaviour implements
217: FlowEdgeDescriptor.EdgeDecorator,
218: FlowEdgeDescriptor.EdgeBehaviour,
219: FlowDescriptor.EditActionBehaviour {
220:
221: public Widget create(FlowEdgeDescriptor descriptor,
222: FlowScene scene) {
223: VMDConnectionWidget widget = new VMDConnectionWidget(scene,
224: VMDFactory.getNetBeans60Scheme());
225: widget.setRouter(scene.createEdgeRouter());
226: scene.addEdgeCommonActions(widget);
227: widget.getActions().addAction(
228: scene.createMoveControlPointAction());
229: return widget;
230: }
231:
232: public void update(FlowEdgeDescriptor descriptor,
233: FlowScene scene) {
234: }
235:
236: public void setSourceAnchor(FlowEdgeDescriptor descriptor,
237: FlowScene scene, Anchor sourceAnchor) {
238: ConnectionWidget widget = (ConnectionWidget) scene
239: .findWidget(descriptor);
240: widget.setSourceAnchor(sourceAnchor);
241: }
242:
243: public void setTargetAnchor(FlowEdgeDescriptor descriptor,
244: FlowScene scene, Anchor targetAnchor) {
245: ConnectionWidget widget = (ConnectionWidget) scene
246: .findWidget(descriptor);
247: widget.setTargetAnchor(targetAnchor);
248: }
249:
250: public boolean isSourceReconnectable(
251: FlowEdgeDescriptor descriptor) {
252: return false; // TODO - reconnecting source pin
253: }
254:
255: public boolean isTargetReconnectable(
256: FlowEdgeDescriptor descriptor) {
257: return true;
258: }
259:
260: public boolean isReplacement(FlowEdgeDescriptor descriptor,
261: FlowDescriptor replacementDescriptor,
262: boolean reconnectingSource) {
263: if (reconnectingSource)
264: return false; // TODO - reconnecting source pin
265: else
266: return MidpDocumentSupport
267: .isCreatableEventHandlerTo(replacementDescriptor != null ? replacementDescriptor
268: .getRepresentedComponent()
269: : null);
270: }
271:
272: public void setReplacement(FlowEdgeDescriptor descriptor,
273: FlowDescriptor replacementDescriptor,
274: boolean reconnectingSource) {
275: // TODO - reconnecting source pin
276: if (!reconnectingSource) {
277: DesignComponent eventSource = getSourceComponent();
278: if (eventSource != null)
279: MidpDocumentSupport
280: .updateEventHandlerFromTarget(
281: eventSource,
282: replacementDescriptor != null ? replacementDescriptor
283: .getRepresentedComponent()
284: : null);
285: }
286: }
287:
288: public void edit(FlowDescriptor descriptor) {
289: Collection<? extends ActionsPresenter> presenters = descriptor
290: .getRepresentedComponent().getPresenters(
291: ActionsPresenter.class);
292: for (ActionsPresenter presenter : presenters) {
293: for (Action action : presenter.getActions()) {
294: if (action instanceof GoToSourceAction) {
295: if (action.isEnabled())
296: action.actionPerformed(null);
297: return;
298: }
299: }
300: }
301: }
302:
303: }
304:
305: }
|