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