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.anchor.AnchorFactory;
045: import org.netbeans.api.visual.vmd.VMDNodeWidget;
046: import org.netbeans.api.visual.vmd.VMDPinWidget;
047: import org.netbeans.api.visual.vmd.VMDFactory;
048: import org.netbeans.api.visual.widget.Widget;
049: import org.netbeans.modules.vmd.api.flow.FlowPinPresenter;
050: import org.netbeans.modules.vmd.api.flow.visual.*;
051: import org.netbeans.modules.vmd.api.model.ComponentProducer;
052: import org.netbeans.modules.vmd.api.model.DesignComponent;
053: import org.netbeans.modules.vmd.api.model.DesignEventFilter;
054: import org.netbeans.modules.vmd.api.model.common.AcceptSupport;
055: import org.netbeans.modules.vmd.midp.components.MidpDocumentSupport;
056:
057: import java.awt.*;
058: import java.awt.datatransfer.Transferable;
059: import java.util.ArrayList;
060: import java.util.List;
061:
062: /**
063: * @author David Kaspar
064: */
065: // TODO - rewrite to query target port to generate EventHandler component
066: // TODO - rewrite to use InfoPresenter
067: public abstract class FlowEventSourcePinPresenter extends
068: FlowPinPresenter {
069:
070: private EventSourcePinDecoratorBehaviour ctrl = new EventSourcePinDecoratorBehaviour();
071:
072: private FlowPinDescriptor pinDescriptor;
073:
074: protected abstract DesignComponent getComponentForAttachingPin();
075:
076: protected abstract String getDisplayName();
077:
078: protected abstract String getOrder();
079:
080: protected boolean canRename() {
081: return false;
082: }
083:
084: protected String getRenameName() {
085: return getDisplayName();
086: }
087:
088: protected void setRenameName(String name) {
089: }
090:
091: protected String getPinID() {
092: return FlowIDSupport.createEventSourcePinID(getComponent());
093: }
094:
095: protected boolean isVisible() {
096: return super .isVisible()
097: && getComponentForAttachingPin() != null;
098: }
099:
100: protected final FlowNodeDescriptor getNodeDescriptor() {
101: DesignComponent component = getComponentForAttachingPin();
102: FlowInfoNodePresenter presenter = component
103: .getPresenter(FlowInfoNodePresenter.class);
104: return presenter != null && presenter.isVisible() ? presenter
105: .getNodeDescriptor() : null;
106: }
107:
108: public final void updateDescriptors() {
109: pinDescriptor = isVisible() ? new FlowPinDescriptor(
110: getComponent(), getPinID()) : null;
111: }
112:
113: public final FlowPinDescriptor getPinDescriptor() {
114: return pinDescriptor;
115: }
116:
117: public FlowPinDescriptor.PinDecorator getDecorator() {
118: return ctrl;
119: }
120:
121: public FlowPinDescriptor.PinBehaviour getBehaviour() {
122: return ctrl;
123: }
124:
125: protected DesignEventFilter getEventFilter() {
126: return new DesignEventFilter().addComponentFilter(
127: getComponent(), false).addHierarchyFilter(
128: getComponent(), false);
129: }
130:
131: public static FlowNodeDescriptor getNodeDescriptor(
132: DesignComponent eventSourceComponent) {
133: if (eventSourceComponent == null)
134: return null;
135: FlowEventSourcePinPresenter presenter = eventSourceComponent
136: .getPresenter(FlowEventSourcePinPresenter.class);
137: return presenter != null && presenter.isVisible() ? presenter
138: .getNodeDescriptor() : null;
139: }
140:
141: public static FlowPinDescriptor getPinDescriptor(
142: DesignComponent eventSourceComponent) {
143: if (eventSourceComponent == null)
144: return null;
145: FlowEventSourcePinPresenter presenter = eventSourceComponent
146: .getPresenter(FlowEventSourcePinPresenter.class);
147: return presenter != null ? presenter.pinDescriptor : null;
148: }
149:
150: protected class EventSourcePinDecoratorBehaviour implements
151: FlowPinDescriptor.PinDecorator,
152: FlowPinDescriptor.PinBehaviour,
153: FlowDescriptor.BadgeDecorator,
154: FlowDescriptor.AcceptActionBehaviour,
155: FlowDescriptor.RenameActionBehaviour {
156:
157: public Widget createWidget(FlowPinDescriptor descriptor,
158: FlowScene scene) {
159: VMDPinWidget vmdPinWidget = new VMDPinWidget(scene,
160: VMDFactory.getNetBeans60Scheme());
161: scene.addPinCommonActions(vmdPinWidget);
162: vmdPinWidget.getActions().addAction(0,
163: scene.createRenameAction());
164: return vmdPinWidget;
165: }
166:
167: public Anchor createAnchor(FlowPinDescriptor descriptor,
168: FlowScene scene) {
169: FlowNodeDescriptor node = scene.getPinNode(descriptor);
170: Widget nodeWidget = scene.findWidget(node);
171: Anchor anchor = AnchorFactory.createDirectionalAnchor(scene
172: .findWidget(descriptor),
173: AnchorFactory.DirectionalAnchorKind.HORIZONTAL, 6);
174: return ((VMDNodeWidget) nodeWidget).createAnchorPin(anchor);
175: }
176:
177: public void update(FlowPinDescriptor descriptor, FlowScene scene) {
178: VMDPinWidget widget = (VMDPinWidget) scene
179: .findWidget(descriptor);
180: widget.setPinName(FlowEventSourcePinPresenter.this
181: .getDisplayName());
182: }
183:
184: public String getOrderCategory(FlowPinDescriptor descriptor) {
185: return getOrder();
186: }
187:
188: public void updateBadges(FlowDescriptor descriptor,
189: FlowScene scene, List<FlowBadgeDescriptor> badges) {
190: VMDPinWidget widget = (VMDPinWidget) scene
191: .findWidget(descriptor);
192: ArrayList<Image> images = new ArrayList<Image>();
193: for (FlowBadgeDescriptor badge : badges) {
194: Image image = scene.getDecorator(badge).getImage(badge);
195: if (image != null)
196: images.add(image);
197: }
198: widget.setGlyphs(images);
199: }
200:
201: public boolean isAcceptable(FlowDescriptor descriptor,
202: Transferable transferable) {
203: return AcceptSupport.isAcceptable(descriptor
204: .getRepresentedComponent(), transferable, null);
205: }
206:
207: public void accept(FlowDescriptor descriptor,
208: Transferable transferable) {
209: ComponentProducer.Result result = AcceptSupport.accept(
210: descriptor.getRepresentedComponent(), transferable,
211: null);
212: AcceptSupport.selectComponentProducerResult(result);
213: }
214:
215: public boolean isConnectionSource(FlowPinDescriptor pin) {
216: return true;
217: }
218:
219: public boolean isConnectionTarget(FlowPinDescriptor sourcePin,
220: FlowDescriptor target) {
221: if (target == null)
222: return true;
223: return MidpDocumentSupport.isCreatableEventHandlerTo(target
224: .getRepresentedComponent());
225: }
226:
227: public void createConnection(FlowPinDescriptor sourcePin,
228: FlowDescriptor target) {
229: MidpDocumentSupport.updateEventHandlerFromTarget(
230: getComponent(), target != null ? target
231: .getRepresentedComponent() : null);
232: }
233:
234: public boolean isEditable(FlowDescriptor descriptor) {
235: return FlowEventSourcePinPresenter.this .canRename();
236: }
237:
238: public String getText(FlowDescriptor descriptor) {
239: return FlowEventSourcePinPresenter.this .getRenameName();
240: }
241:
242: public void setText(FlowDescriptor descriptor, String text) {
243: FlowEventSourcePinPresenter.this.setRenameName(text);
244: }
245:
246: }
247:
248: }
|