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.BpelContainer;
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 AddBasicActivitiesAction extends BpelNodeAction {
043: private static final long serialVersionUID = 1L;
044: private static ActSubMenuModel model = new ActSubMenuModel(null);
045: private static AddPaletteActivityAction[] BASIC_ACTIVITIES = new AddPaletteActivityAction[] {
046: (AddPaletteActivityAction) SystemAction
047: .get(AddAssignAction.class),
048: (AddPaletteActivityAction) SystemAction
049: .get(AddEmptyAction.class),
050: (AddPaletteActivityAction) SystemAction
051: .get(AddWaitAction.class),
052: (AddPaletteActivityAction) SystemAction
053: .get(AddThrowAction.class),
054: (AddPaletteActivityAction) SystemAction
055: .get(AddRethrowAction.class),
056: (AddPaletteActivityAction) SystemAction
057: .get(AddExitAction.class),
058: (AddPaletteActivityAction) SystemAction
059: .get(AddCompensateAction.class),
060: (AddPaletteActivityAction) SystemAction
061: .get(AddCompensateScopeAction.class), };
062:
063: public AddBasicActivitiesAction() {
064: }
065:
066: public final String getBundleName() {
067: return NbBundle.getMessage(BpelNodeAction.class,
068: "CTL_AddBasicActivitiesAction"); // NOI18N
069: }
070:
071: public String getName() {
072: return model.getCount() == 1 ? super .getName() + " "
073: + model.getLabel(0) : super .getName(); // NOI18N
074: }
075:
076: public ActionType getType() {
077: return ActionType.ADD_BASIC_ACTIVITIES;
078: }
079:
080: protected boolean enable(BpelEntity[] bpelEntities) {
081: model = new ActSubMenuModel(bpelEntities);
082: if (bpelEntities == null || bpelEntities.length < 0) {
083: return false;
084: }
085: // TODO m
086: if (bpelEntities[0] instanceof BpelContainer
087: && getPaletteActions(bpelEntities).length > 0) {
088: return true;
089: }
090:
091: return false;
092: }
093:
094: protected void performAction(BpelEntity[] bpelEntities) {
095: if (!enable(bpelEntities)) {
096: return;
097: }
098: performAction(bpelEntities, 0);
099: }
100:
101: private static final void performAction(BpelEntity[] bpelEntities,
102: int index) {
103: SystemAction[] paletteActions = getPaletteActions(bpelEntities);
104: if (paletteActions == null || index < 0
105: || index > paletteActions.length) {
106: return;
107: }
108: performAction(bpelEntities, paletteActions[index]);
109: }
110:
111: private static final void performAction(BpelEntity[] bpelEntities,
112: SystemAction wrapAction) {
113: if (wrapAction instanceof BpelNodeAction) {
114: ((BpelNodeAction) wrapAction).performAction(bpelEntities);
115: }
116: }
117:
118: // TODO m
119: public static final AddPaletteActivityAction[] getPaletteActions(
120: BpelEntity[] bpelEntities) {
121: //// return getPaletteActions();
122: List<AddPaletteActivityAction> availablePaletteActions = new ArrayList<AddPaletteActivityAction>();
123: if (bpelEntities != null && bpelEntities.length > 0) {
124: for (AddPaletteActivityAction paletteAction : BASIC_ACTIVITIES) {
125: if (paletteAction.enable(bpelEntities)) {
126: availablePaletteActions.add(paletteAction);
127: }
128: }
129: return availablePaletteActions
130: .toArray(new AddPaletteActivityAction[availablePaletteActions
131: .size()]);
132: }
133: return new AddPaletteActivityAction[0];
134: }
135:
136: public static final AddPaletteActivityAction[] getPaletteActions(
137: Node[] nodes) {
138: return getPaletteActions(getBpelEntities(nodes));
139: }
140:
141: public JMenuItem getPopupPresenter() {
142: return new Actions.SubMenu(this , model, true);
143: }
144:
145: public JMenuItem getMenuPresenter() {
146: return new Actions.SubMenu(this , model, true);
147: }
148:
149: protected boolean asynchronous() {
150: return false;
151: }
152:
153: private static final AddPaletteActivityAction[] getPaletteActions() {
154: return BASIC_ACTIVITIES;
155: }
156:
157: private static final BpelEntity[] getCurrentEntities() {
158: return getBpelEntities(WindowManager.getDefault().getRegistry()
159: .getCurrentNodes());
160: }
161:
162: /** Implementation of ActSubMenuInt */
163: private static class ActSubMenuModel extends EventListenerList
164: implements Actions.SubMenuModel {
165: static final long serialVersionUID = -4273674308662494596L;
166:
167: private BpelEntity[] entities;
168:
169: ActSubMenuModel(BpelEntity[] entities) {
170: this .entities = entities;
171: }
172:
173: private BpelEntity[] getEntities() {
174: return entities == null ? getCurrentEntities() : entities;
175: }
176:
177: public int getCount() {
178: assert getPaletteActions(getEntities()) != null;
179: return getPaletteActions(getEntities()).length;
180: }
181:
182: public String getLabel(int index) {
183: AddPaletteActivityAction[] paletteActions = getPaletteActions(getEntities());
184: if (paletteActions != null && index >= 0
185: && index < paletteActions.length) {
186: return paletteActions[index].getName();
187: }
188: return null;
189: }
190:
191: public HelpCtx getHelpCtx(int index) {
192: AddPaletteActivityAction[] paletteActions = getPaletteActions(getEntities());
193: if (paletteActions != null && index > 0
194: && index < paletteActions.length) {
195: return paletteActions[index].getHelpCtx();
196: }
197: return HelpCtx.DEFAULT_HELP;
198: }
199:
200: public void performActionAt(int index) {
201: AddPaletteActivityAction[] paletteActions = getPaletteActions(getEntities());
202: if (paletteActions != null && index >= 0
203: && index < paletteActions.length) {
204: performAction(entities, index);
205: }
206: }
207:
208: /** Adds change listener for changes of the model.
209: */
210: public void addChangeListener(ChangeListener l) {
211: add(ChangeListener.class, l);
212: }
213:
214: /** Removes change listener for changes of the model.
215: */
216: public void removeChangeListener(ChangeListener l) {
217: remove(ChangeListener.class, l);
218: }
219: }
220: // end of ActSubMenuModel
221: }
|