001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.bpel.nodes.actions;
020:
021: import org.netbeans.modules.bpel.nodes.actions.BpelNodeAction;
022: import org.netbeans.modules.bpel.editors.api.nodes.actions.ActionType;
023: import java.util.ArrayList;
024: import java.util.List;
025: import javax.swing.JMenuItem;
026: import javax.swing.KeyStroke;
027: import javax.swing.event.ChangeListener;
028: import javax.swing.event.EventListenerList;
029: import org.netbeans.modules.bpel.model.api.Activity;
030: import org.netbeans.modules.bpel.model.api.BpelEntity;
031: import org.openide.awt.Actions;
032: import org.openide.nodes.Node;
033: import org.openide.util.HelpCtx;
034: import org.openide.util.NbBundle;
035: import org.openide.util.actions.SystemAction;
036: import org.openide.windows.WindowManager;
037:
038: /**
039: *
040: * @author Vitaly Bychkov
041: * @version 1.0
042: */
043: public class GoToAction extends BpelNodeAction {
044:
045: private static final long serialVersionUID = 1L;
046:
047: private static ActSubMenuModel model = new ActSubMenuModel(null);
048: private static BpelNodeAction[] GO_TO_ACTIONS = new BpelNodeAction[] {
049: (BpelNodeAction) SystemAction.get(GoToDiagrammAction.class),
050: (BpelNodeAction) SystemAction.get(GoToSourceAction.class),
051: (BpelNodeAction) SystemAction
052: .get(ShowBpelMapperAction.class),
053: (BpelNodeAction) SystemAction.get(GoToLoggingAction.class) };
054:
055: public GoToAction() {
056: }
057:
058: public static KeyStroke getKeyStroke(
059: Class<? extends BpelNodeAction> clazz) {
060: if (clazz == null) {
061: return null;
062: }
063: SystemAction action = SystemAction.get(clazz);
064: KeyStroke key = null;
065: if (action != null) {
066: Object keyObj = action.getValue(ACCELERATOR_KEY);
067: if (keyObj instanceof KeyStroke) {
068: key = (KeyStroke) keyObj;
069: }
070: }
071:
072: return key;
073: }
074:
075: public final String getBundleName() {
076: return NbBundle.getMessage(BpelNodeAction.class,
077: "CTL_GoToAction"); // NOI18N
078: }
079:
080: @Override
081: public String getName() {
082: return model.getCount() == 1 ? super .getName() + " "
083: + model.getLabel(0) : super .getName(); // NOI18N
084: }
085:
086: public ActionType getType() {
087: return ActionType.GO_TO;
088: }
089:
090: @Override
091: public boolean enable(Node[] nodes) {
092: model = new ActSubMenuModel(nodes);
093: return super .enable(nodes);
094: // if (nodes == null || nodes.length < 0 ) {
095: // return false;
096: // }
097: // // TODO m
098: // if (nodes[0] instanceof BpelNode) {
099: // return true;
100: // }
101: //
102: // return false;
103: }
104:
105: @Override
106: public void performAction(Node[] nodes) {
107: if (!enable(nodes)) {
108: return;
109: }
110: performAction(nodes, 0);
111: }
112:
113: private static final void performAction(Node[] nodes, int index) {
114: SystemAction[] gotoActions = getGoToActions(nodes);
115: if (gotoActions == null || index < 0
116: || index > gotoActions.length) {
117: return;
118: }
119: performAction(nodes, gotoActions[index]);
120: }
121:
122: private static final void performAction(Node[] nodes,
123: SystemAction gotoAction) {
124: if (gotoAction instanceof BpelNodeAction) {
125: ((BpelNodeAction) gotoAction).performAction(nodes);
126: }
127: }
128:
129: // TODO m
130: public static final BpelNodeAction[] getGoToActions(Node[] nodes) {
131: List<BpelNodeAction> availableGotoActions = new ArrayList<BpelNodeAction>();
132: if (nodes != null && nodes.length > 0) {
133: for (BpelNodeAction gotoAction : GO_TO_ACTIONS) {
134: if (gotoAction.enable(nodes)) {
135: availableGotoActions.add(gotoAction);
136: }
137: }
138: return availableGotoActions
139: .toArray(new BpelNodeAction[availableGotoActions
140: .size()]);
141: }
142: return null;
143: }
144:
145: @Override
146: public JMenuItem getPopupPresenter() {
147: return new Actions.SubMenu(this , model, true);
148: }
149:
150: @Override
151: public JMenuItem getMenuPresenter() {
152: return new Actions.SubMenu(this , model, false);
153: }
154:
155: @Override
156: protected boolean asynchronous() {
157: return false;
158: }
159:
160: private static final BpelNodeAction[] getGoToActions() {
161: return GO_TO_ACTIONS;
162: }
163:
164: private static final Node[] getCurrentNodes() {
165: return WindowManager.getDefault().getRegistry()
166: .getCurrentNodes();
167: }
168:
169: /** Implementation of ActSubMenuInt */
170: private static class ActSubMenuModel extends EventListenerList
171: implements Actions.SubMenuModel {
172: static final long serialVersionUID = -4273674308662494596L;
173:
174: private Node[] nodes;
175:
176: ActSubMenuModel(Node[] nodes) {
177: this .nodes = nodes;
178: }
179:
180: private Node[] getNodes() {
181: return nodes == null ? getCurrentNodes() : nodes;
182: }
183:
184: public int getCount() {
185: return getGoToActions(getNodes()).length;
186: }
187:
188: public String getLabel(int index) {
189: BpelNodeAction[] gotoActions = getGoToActions(getNodes());
190: if (gotoActions != null && index >= 0
191: && index < gotoActions.length) {
192: return gotoActions[index].getName();
193: }
194: return null;
195: }
196:
197: public HelpCtx getHelpCtx(int index) {
198: BpelNodeAction[] gotoActions = getGoToActions(getNodes());
199: if (gotoActions != null && index > 0
200: && index < gotoActions.length) {
201: return gotoActions[index].getHelpCtx();
202: }
203: return HelpCtx.DEFAULT_HELP;
204: }
205:
206: public void performActionAt(int index) {
207: BpelNodeAction[] gotoActions = getGoToActions(getNodes());
208: if (gotoActions != null && index >= 0
209: && index < gotoActions.length) {
210: performAction(nodes, index);
211: }
212: }
213:
214: /** Adds change listener for changes of the model.
215: */
216: public void addChangeListener(ChangeListener l) {
217: add(ChangeListener.class, l);
218: }
219:
220: /** Removes change listener for changes of the model.
221: */
222: public void removeChangeListener(ChangeListener l) {
223: remove(ChangeListener.class, l);
224: }
225: }
226:
227: @Override
228: protected void performAction(BpelEntity[] bpelEntities) {
229: // do nothing
230: }
231: // end of ActSubMenuModel
232: }
|