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.action.ActionFactory;
044: import org.netbeans.api.visual.anchor.Anchor;
045: import org.netbeans.api.visual.vmd.VMDNodeWidget;
046: import org.netbeans.api.visual.vmd.VMDFactory;
047: import org.netbeans.api.visual.widget.LabelWidget;
048: import org.netbeans.api.visual.widget.Widget;
049: import org.netbeans.modules.vmd.api.flow.FlowNodePresenter;
050: import org.netbeans.modules.vmd.api.flow.visual.FlowDescriptor;
051: import org.netbeans.modules.vmd.api.flow.visual.FlowNodeDescriptor;
052: import org.netbeans.modules.vmd.api.flow.visual.FlowPinDescriptor;
053: import org.netbeans.modules.vmd.api.flow.visual.FlowScene;
054: import org.netbeans.modules.vmd.api.model.ComponentProducer;
055: import org.netbeans.modules.vmd.api.model.DesignComponent;
056: import org.netbeans.modules.vmd.api.model.DesignEventFilter;
057: import org.netbeans.modules.vmd.api.model.common.AcceptSupport;
058: import org.netbeans.modules.vmd.api.model.presenters.InfoPresenter;
059: import org.netbeans.modules.vmd.api.model.presenters.actions.ActionsPresenter;
060: import org.netbeans.modules.vmd.api.screen.actions.DesignerEditAction;
061:
062: import javax.swing.*;
063: import java.awt.datatransfer.Transferable;
064: import java.util.*;
065:
066: /**
067: * @author David Kaspar
068: */
069: public final class FlowInfoNodePresenter extends FlowNodePresenter {
070:
071: private final InfoNodeDecoratorBehaviour ctrl = new InfoNodeDecoratorBehaviour();
072:
073: private FlowNodeDescriptor descriptor;
074:
075: public FlowInfoNodePresenter() {
076: }
077:
078: public void notifyAttached(DesignComponent component) {
079: super .notifyAttached(component);
080: addDependency(component, InfoPresenter.class);
081: descriptor = new FlowNodeDescriptor(component, FlowIDSupport
082: .createNodeID4SingleNode(component));
083: }
084:
085: protected boolean isVisible() {
086: return super .isVisible()
087: && getComponent().getParentComponent() != null;
088: }
089:
090: public FlowNodeDescriptor getNodeDescriptor() {
091: return descriptor;
092: }
093:
094: public void updateDescriptors() {
095: }
096:
097: public DesignEventFilter getEventFilter() {
098: return new DesignEventFilter().addComponentFilter(
099: getComponent(), false).addHierarchyFilter(
100: getComponent(), false);
101: }
102:
103: public FlowNodeDescriptor.NodeDecorator getDecorator() {
104: return ctrl;
105: }
106:
107: public FlowNodeDescriptor.NodeBehaviour getBehaviour() {
108: return ctrl;
109: }
110:
111: private class InfoNodeDecoratorBehaviour implements
112: FlowNodeDescriptor.NodeDecorator,
113: FlowNodeDescriptor.NodeBehaviour,
114: FlowDescriptor.AcceptActionBehaviour,
115: FlowDescriptor.RenameActionBehaviour,
116: FlowDescriptor.EditActionBehaviour {
117:
118: public Widget createWidget(FlowNodeDescriptor descriptor,
119: FlowScene scene) {
120: VMDNodeWidget widget = new VMDNodeWidget(scene, VMDFactory
121: .getNetBeans60Scheme());
122: scene.addNodeCommonActions(widget);
123: LabelWidget nodeNameWidget = widget.getNodeNameWidget();
124: widget.getActions().addAction(
125: ActionFactory.createForwardKeyEventsAction(
126: nodeNameWidget, null));
127: nodeNameWidget.getActions().addAction(
128: scene.createRenameAction());
129: return widget;
130: }
131:
132: public void update(FlowNodeDescriptor descriptor,
133: FlowScene scene) {
134: InfoPresenter presenter = descriptor
135: .getRepresentedComponent().getPresenter(
136: InfoPresenter.class);
137: if (presenter == null)
138: return;
139: VMDNodeWidget widget = (VMDNodeWidget) scene
140: .findWidget(descriptor);
141: widget.setNodeImage(presenter
142: .getIcon(InfoPresenter.IconType.COLOR_16x16));
143: widget.setNodeName(presenter
144: .getDisplayName(InfoPresenter.NameType.PRIMARY));
145: widget.setToolTipText(InfoPresenter.getToolTip(presenter));
146: scene.scheduleNodeDescriptorForOrdering(descriptor);
147: }
148:
149: public void attachPinWidget(FlowNodeDescriptor descriptor,
150: FlowScene scene, Widget pinWidget) {
151: Widget widget = scene.findWidget(descriptor);
152: ((VMDNodeWidget) widget).attachPinWidget(pinWidget);
153: }
154:
155: public Anchor createAnchor(FlowNodeDescriptor node,
156: FlowScene scene) {
157: Widget widget = scene.findWidget(node);
158: return ((VMDNodeWidget) widget).getNodeAnchor();
159: }
160:
161: public DesignComponent getComponentWithPinOrderPresenters() {
162: return getComponent();
163: }
164:
165: public void orderPins(FlowNodeDescriptor node, FlowScene scene,
166: Map<String, List<FlowPinDescriptor>> categories) {
167: VMDNodeWidget nodeWidget = (VMDNodeWidget) scene
168: .findWidget(node);
169:
170: HashMap<String, List<Widget>> order = new HashMap<String, List<Widget>>();
171: for (Map.Entry<String, List<FlowPinDescriptor>> entry : categories
172: .entrySet()) {
173: ArrayList<Widget> widgets = new ArrayList<Widget>();
174: for (FlowPinDescriptor pin : entry.getValue())
175: widgets.add(scene.findWidget(pin));
176: order.put(entry.getKey(), widgets);
177: }
178:
179: nodeWidget.sortPins(order);
180: }
181:
182: public boolean isAcceptable(FlowDescriptor descriptor,
183: Transferable transferable) {
184: return AcceptSupport.isAcceptable(descriptor
185: .getRepresentedComponent(), transferable, null);
186: }
187:
188: public void accept(FlowDescriptor descriptor,
189: Transferable transferable) {
190: ComponentProducer.Result result = AcceptSupport.accept(
191: descriptor.getRepresentedComponent(), transferable,
192: null);
193: AcceptSupport.selectComponentProducerResult(result);
194: }
195:
196: public boolean isEditable(FlowDescriptor descriptor) {
197: InfoPresenter presenter = descriptor
198: .getRepresentedComponent().getPresenter(
199: InfoPresenter.class);
200: return presenter != null && presenter.isEditable();
201: }
202:
203: public String getText(FlowDescriptor descriptor) {
204: InfoPresenter presenter = descriptor
205: .getRepresentedComponent().getPresenter(
206: InfoPresenter.class);
207: assert presenter != null;
208: return presenter.getEditableName();
209: }
210:
211: public void setText(FlowDescriptor descriptor, String text) {
212: InfoPresenter presenter = descriptor
213: .getRepresentedComponent().getPresenter(
214: InfoPresenter.class);
215: assert presenter != null;
216: presenter.setEditableName(text);
217: }
218:
219: public void edit(FlowDescriptor descriptor) {
220: Collection<? extends ActionsPresenter> presenters = descriptor
221: .getRepresentedComponent().getPresenters(
222: ActionsPresenter.class);
223: for (ActionsPresenter presenter : presenters) {
224: for (Action action : presenter.getActions()) {
225: if (action instanceof DesignerEditAction) {
226: if (action.isEnabled())
227: action.actionPerformed(null);
228: return;
229: }
230: }
231: }
232: }
233: }
234:
235: }
|