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