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.event.ChangeListener;
027: import javax.swing.event.EventListenerList;
028: import org.netbeans.modules.bpel.model.api.Activity;
029: import org.netbeans.modules.bpel.model.api.BpelEntity;
030: import org.openide.awt.Actions;
031: import org.openide.nodes.Node;
032: import org.openide.util.HelpCtx;
033: import org.openide.util.NbBundle;
034: import org.openide.util.actions.SystemAction;
035: import org.openide.windows.WindowManager;
036:
037: /**
038: *
039: * @author Vitaly Bychkov
040: * @version 1.0
041: */
042: public class WrapAction extends BpelNodeAction {
043: private static ActSubMenuModel model = new ActSubMenuModel(null);
044: private static AbstractWrapWithAction[] WRAP_ACTIONS = new AbstractWrapWithAction[] {
045: (AbstractWrapWithAction) SystemAction
046: .get(WrapWithSequenceAction.class),
047: (AbstractWrapWithAction) SystemAction
048: .get(WrapWithScopeAction.class),
049: (AbstractWrapWithAction) SystemAction
050: .get(WrapWithFlowAction.class),
051: (AbstractWrapWithAction) SystemAction
052: .get(WrapWithForeachAction.class),
053: (AbstractWrapWithAction) SystemAction
054: .get(WrapWithRepeatUntilAction.class),
055: (AbstractWrapWithAction) SystemAction
056: .get(WrapWithWhileAction.class) };
057:
058: public WrapAction() {
059: }
060:
061: public final String getBundleName() {
062: return NbBundle.getMessage(BpelNodeAction.class,
063: "CTL_WrapAction"); // NOI18N
064: }
065:
066: public String getName() {
067: return model.getCount() == 1 ? super .getName() + " "
068: + model.getLabel(0) : super .getName(); // NOI18N
069: }
070:
071: public ActionType getType() {
072: return ActionType.WRAP;
073: }
074:
075: protected boolean enable(BpelEntity[] bpelEntities) {
076: model = new ActSubMenuModel(bpelEntities);
077: if (bpelEntities == null || bpelEntities.length < 0) {
078: return false;
079: }
080: // TODO m
081: if (bpelEntities[0] instanceof Activity) {
082: return true;
083: }
084:
085: return false;
086: }
087:
088: protected void performAction(BpelEntity[] bpelEntities) {
089: if (!enable(bpelEntities)) {
090: return;
091: }
092: performAction(bpelEntities, 0);
093: }
094:
095: private static final void performAction(BpelEntity[] bpelEntities,
096: int index) {
097: SystemAction[] wrapActions = getWrapActions(bpelEntities);
098: if (wrapActions == null || index < 0
099: || index > wrapActions.length) {
100: return;
101: }
102: performAction(bpelEntities, wrapActions[index]);
103: }
104:
105: private static final void performAction(BpelEntity[] bpelEntities,
106: SystemAction wrapAction) {
107: if (wrapAction instanceof BpelNodeAction) {
108: ((BpelNodeAction) wrapAction).performAction(bpelEntities);
109: }
110: }
111:
112: // TODO m
113: public static final AbstractWrapWithAction[] getWrapActions(
114: BpelEntity[] bpelEntities) {
115: return getWrapActions();
116: // TODO r
117: // List<AbstractWrapWithAction> availableWrapActions = new ArrayList<AbstractWrapWithAction>();
118: // if (bpelEntities != null && bpelEntities.length > 0) {
119: // for (AbstractWrapWithAction wrapAction : WRAP_ACTIONS) {
120: // if (wrapAction.enable(bpelEntities)) {
121: // availableWrapActions.add(wrapAction);
122: // }
123: // }
124: // return availableWrapActions.toArray(new AbstractWrapWithAction[availableWrapActions.size()]);
125: // }
126: // return null;
127: }
128:
129: public static final AbstractWrapWithAction[] getWrapActions(
130: Node[] nodes) {
131: return getWrapActions(getBpelEntities(nodes));
132: }
133:
134: public JMenuItem getPopupPresenter() {
135: return new Actions.SubMenu(this , model, true);
136: }
137:
138: public JMenuItem getMenuPresenter() {
139: return new Actions.SubMenu(this , model, false);
140: }
141:
142: protected boolean asynchronous() {
143: return false;
144: }
145:
146: private static final AbstractWrapWithAction[] getWrapActions() {
147: return WRAP_ACTIONS;
148: }
149:
150: private static final BpelEntity[] getCurrentEntities() {
151: return getBpelEntities(WindowManager.getDefault().getRegistry()
152: .getCurrentNodes());
153: }
154:
155: /** Implementation of ActSubMenuInt */
156: private static class ActSubMenuModel extends EventListenerList
157: implements Actions.SubMenuModel {
158: static final long serialVersionUID = -4273674308662494596L;
159:
160: private BpelEntity[] entities;
161:
162: ActSubMenuModel(BpelEntity[] entities) {
163: this .entities = entities;
164: }
165:
166: private BpelEntity[] getEntities() {
167: return entities == null ? getCurrentEntities() : entities;
168: }
169:
170: public int getCount() {
171: return getWrapActions(getEntities()).length;
172: }
173:
174: public String getLabel(int index) {
175: AbstractWrapWithAction[] wrapActions = getWrapActions(getEntities());
176: if (wrapActions != null && index >= 0
177: && index < wrapActions.length) {
178: return wrapActions[index].getName();
179: }
180: return null;
181: }
182:
183: public HelpCtx getHelpCtx(int index) {
184: AbstractWrapWithAction[] wrapActions = getWrapActions(getEntities());
185: if (wrapActions != null && index > 0
186: && index < wrapActions.length) {
187: return wrapActions[index].getHelpCtx();
188: }
189: return HelpCtx.DEFAULT_HELP;
190: }
191:
192: public void performActionAt(int index) {
193: AbstractWrapWithAction[] wrapActions = getWrapActions(getEntities());
194: if (wrapActions != null && index >= 0
195: && index < wrapActions.length) {
196: performAction(entities, index);
197: }
198: }
199:
200: /** Adds change listener for changes of the model.
201: */
202: public void addChangeListener(ChangeListener l) {
203: add(ChangeListener.class, l);
204: }
205:
206: /** Removes change listener for changes of the model.
207: */
208: public void removeChangeListener(ChangeListener l) {
209: remove(ChangeListener.class, l);
210: }
211: }
212: // end of ActSubMenuModel
213: }
|