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:
020: package org.netbeans.modules.bpel.design.decoration.providers;
021:
022: import java.util.ArrayList;
023: import java.util.HashMap;
024: import java.util.List;
025: import java.util.Map;
026: import javax.swing.Action;
027: import javax.swing.Icon;
028: import javax.swing.ImageIcon;
029: import javax.swing.JButton;
030: import org.netbeans.modules.bpel.design.DesignView;
031: import org.netbeans.modules.bpel.design.decoration.ComponentsDescriptor;
032: import org.netbeans.modules.bpel.design.decoration.Decoration;
033: import org.netbeans.modules.bpel.design.decoration.DecorationProvider;
034: import org.netbeans.modules.bpel.design.decoration.Descriptor;
035: import org.netbeans.modules.bpel.design.decoration.components.ContextToolBar;
036: import org.netbeans.modules.bpel.design.decoration.components.ContextToolBarButton;
037: import org.netbeans.modules.bpel.design.selection.DiagramSelectionListener;
038: import org.netbeans.modules.bpel.model.api.BpelEntity;
039: import org.netbeans.modules.bpel.model.api.EventHandlers;
040: import org.netbeans.modules.bpel.model.api.FaultHandlers;
041: import org.netbeans.modules.bpel.model.api.If;
042: import org.netbeans.modules.bpel.model.api.Invoke;
043: import org.netbeans.modules.bpel.model.api.PartnerLink;
044: import org.netbeans.modules.bpel.model.api.Pick;
045: import org.netbeans.modules.bpel.model.api.Receive;
046: import org.netbeans.modules.bpel.model.api.Reply;
047: import org.netbeans.modules.bpel.model.api.Scope;
048: import org.netbeans.modules.bpel.model.api.Process;
049: import org.netbeans.modules.bpel.model.api.support.UniqueId;
050: import org.netbeans.modules.bpel.nodes.BpelNode;
051: import org.netbeans.modules.bpel.nodes.actions.AddCatchAction;
052: import org.netbeans.modules.bpel.nodes.actions.AddCatchAllAction;
053: import org.netbeans.modules.bpel.nodes.actions.AddCompensationHandlerAction;
054: import org.netbeans.modules.bpel.nodes.actions.AddCorrelationSetAction;
055: import org.netbeans.modules.bpel.nodes.actions.AddElseIfAction;
056: import org.netbeans.modules.bpel.nodes.actions.AddEventHandlersAction;
057: import org.netbeans.modules.bpel.nodes.actions.AddFaultHandlersAction;
058: import org.netbeans.modules.bpel.nodes.actions.AddOnAlarmAction;
059: import org.netbeans.modules.bpel.nodes.actions.AddOnEventAction;
060: import org.netbeans.modules.bpel.nodes.actions.AddOnMessageAction;
061: import org.netbeans.modules.bpel.nodes.actions.AddTerminationHandlerAction;
062: import org.netbeans.modules.bpel.nodes.actions.AddVariableAction;
063: import org.netbeans.modules.bpel.nodes.actions.BpelNodeAction;
064: import org.netbeans.modules.bpel.nodes.actions.OpenPartnerLinkInEditor;
065: import org.netbeans.modules.bpel.nodes.actions.ShowPropertyEditorAction;
066: import org.openide.actions.NewAction;
067: import org.openide.awt.Actions;
068: import org.openide.nodes.Node;
069:
070: /**
071: *
072: * @author aa160298
073: */
074: public class ToolbarDecorationProvider extends DecorationProvider
075: implements DiagramSelectionListener {
076:
077: public ToolbarDecorationProvider(DesignView designView) {
078: super (designView);
079: getDesignView().getSelectionModel().addSelectionListener(this );
080: }
081:
082: public Decoration getDecoration(BpelEntity entity) {
083:
084: UniqueId entityID = entity.getUID();
085: UniqueId selectedEntityID = getDesignView().getSelectionModel()
086: .getSelectedID();
087:
088: if (entityID != null && entityID.equals(selectedEntityID)) {
089: List<Descriptor> descriptors = new ArrayList<Descriptor>();
090:
091: List<Action> actions = getContextActions(entity);
092:
093: ContextToolBar toolBar = new ContextToolBar();
094:
095: Action collapseExpandAction = getDesignView()
096: .getCollapseExpandDecorationProvider()
097: .createCollapseExpandAction(entity);
098:
099: if (collapseExpandAction != null) {
100: toolBar.add(new ContextToolBarButton(
101: collapseExpandAction));
102: }
103:
104: for (int i = 0; i < actions.size(); i++) {
105: Action a = actions.get(i);
106: JButton button = new ContextToolBarButton(CONTEXT_ICONS
107: .get(a.getClass()));
108: Actions.connect(button, a);
109: toolBar.add(button);
110: }
111:
112: if (!actions.isEmpty() || (collapseExpandAction != null)) {
113: ComponentsDescriptor components = new ComponentsDescriptor();
114: components.add(toolBar);
115: descriptors.add(components);
116: }
117:
118: return new Decoration(descriptors
119: .toArray(new Descriptor[descriptors.size()]));
120: }
121: return null;
122: }
123:
124: public void selectionChanged(BpelEntity oldSelection,
125: BpelEntity newSelection) {
126: fireDecorationChanged();
127: }
128:
129: private Map<Class, Action> extractActions(BpelEntity entity) {
130: Map<Class, Action> result = new HashMap<Class, Action>();
131:
132: Node node = getDesignView().getNodeForPattern(
133: getDesignView().getModel().getPattern(entity));
134:
135: if ((node == null) || !(node instanceof BpelNode)) {
136: return result;
137: }
138:
139: BpelNode bpelNode = (BpelNode) node;
140:
141: Action[] actions = node.getActions(true);
142:
143: if (actions == null) {
144: return result;
145: }
146:
147: for (Action action : actions) {
148: if (action == null) {
149: continue;
150: }
151:
152: if (action instanceof NewAction) {
153: List<BpelNodeAction> addActions = bpelNode
154: .getAddActions();
155: if (addActions != null) {
156: for (Action addAction : addActions) {
157: if (addAction == null) {
158: continue;
159: }
160: result.put(addAction.getClass(), addAction);
161: }
162: }
163: } else {
164: result.put(action.getClass(), action);
165: }
166: }
167:
168: return result;
169: }
170:
171: private List<Action> getContextActions(BpelEntity entity) {
172: List<Action> result = new ArrayList<Action>();
173:
174: Map<Class, Action> classToActionMap = extractActions(entity);
175:
176: List<Class> contextActionsClasses = null;
177:
178: for (Class bpelEntityClass : CONTEXT_ACTIONS.keySet()) {
179: if (bpelEntityClass.isAssignableFrom(entity.getClass())) {
180: contextActionsClasses = CONTEXT_ACTIONS
181: .get(bpelEntityClass);
182: break;
183: }
184: }
185:
186: if (contextActionsClasses == null) {
187: return result;
188: }
189:
190: for (Class contextActionClass : contextActionsClasses) {
191: Action action = classToActionMap.get(contextActionClass);
192: if (action != null) {
193: result.add(action);
194: }
195: }
196:
197: return result;
198: }
199:
200: private static final Map<Class, List<Class>> CONTEXT_ACTIONS;
201: private static final Map<Class, Icon> CONTEXT_ICONS;
202:
203: private static Icon loadPNGIcon(String name) {
204: return new ImageIcon(Decoration.class.getResource("resources/"
205: + name + ".png")); // NOI18N
206: }
207:
208: static {
209: CONTEXT_ACTIONS = new HashMap<Class, List<Class>>();
210:
211: CONTEXT_ACTIONS.put(Invoke.class, new ArrayList<Class>());
212: CONTEXT_ACTIONS.get(Invoke.class).add(
213: ShowPropertyEditorAction.class);
214: CONTEXT_ACTIONS.get(Invoke.class).add(AddCatchAllAction.class);
215: CONTEXT_ACTIONS.get(Invoke.class).add(AddCatchAction.class);
216: CONTEXT_ACTIONS.get(Invoke.class).add(
217: AddCompensationHandlerAction.class);
218:
219: CONTEXT_ACTIONS.put(Receive.class, new ArrayList<Class>());
220: CONTEXT_ACTIONS.get(Receive.class).add(
221: ShowPropertyEditorAction.class);
222:
223: CONTEXT_ACTIONS.put(Reply.class, new ArrayList<Class>());
224: CONTEXT_ACTIONS.get(Reply.class).add(
225: ShowPropertyEditorAction.class);
226:
227: CONTEXT_ACTIONS.put(PartnerLink.class, new ArrayList<Class>());
228: CONTEXT_ACTIONS.get(PartnerLink.class).add(
229: ShowPropertyEditorAction.class);
230: CONTEXT_ACTIONS.get(PartnerLink.class).add(
231: OpenPartnerLinkInEditor.class);
232:
233: CONTEXT_ACTIONS.put(If.class, new ArrayList<Class>());
234: CONTEXT_ACTIONS.get(If.class).add(AddElseIfAction.class);
235:
236: CONTEXT_ACTIONS.put(Process.class, new ArrayList<Class>());
237: CONTEXT_ACTIONS.get(Process.class).add(AddVariableAction.class);
238: CONTEXT_ACTIONS.get(Process.class).add(
239: AddCorrelationSetAction.class);
240: CONTEXT_ACTIONS.get(Process.class).add(
241: AddEventHandlersAction.class);
242: CONTEXT_ACTIONS.get(Process.class).add(
243: AddFaultHandlersAction.class);
244:
245: CONTEXT_ACTIONS.put(Scope.class, new ArrayList<Class>());
246: CONTEXT_ACTIONS.get(Scope.class).add(AddVariableAction.class);
247: CONTEXT_ACTIONS.get(Scope.class).add(
248: AddCorrelationSetAction.class);
249: CONTEXT_ACTIONS.get(Scope.class).add(
250: AddEventHandlersAction.class);
251: CONTEXT_ACTIONS.get(Scope.class).add(
252: AddCompensationHandlerAction.class);
253: CONTEXT_ACTIONS.get(Scope.class).add(
254: AddTerminationHandlerAction.class);
255: CONTEXT_ACTIONS.get(Scope.class).add(
256: AddFaultHandlersAction.class);
257:
258: CONTEXT_ACTIONS.put(Pick.class, new ArrayList<Class>());
259: CONTEXT_ACTIONS.get(Pick.class).add(AddOnMessageAction.class);
260: CONTEXT_ACTIONS.get(Pick.class).add(AddOnAlarmAction.class);
261:
262: CONTEXT_ACTIONS
263: .put(EventHandlers.class, new ArrayList<Class>());
264: CONTEXT_ACTIONS.get(EventHandlers.class).add(
265: AddOnEventAction.class);
266: CONTEXT_ACTIONS.get(EventHandlers.class).add(
267: AddOnAlarmAction.class);
268:
269: CONTEXT_ACTIONS
270: .put(FaultHandlers.class, new ArrayList<Class>());
271: CONTEXT_ACTIONS.get(FaultHandlers.class).add(
272: AddCatchAllAction.class);
273: CONTEXT_ACTIONS.get(FaultHandlers.class).add(
274: AddCatchAction.class);
275:
276: CONTEXT_ICONS = new HashMap<Class, Icon>();
277: CONTEXT_ICONS.put(AddCatchAction.class,
278: loadPNGIcon("add_catch")); // NOI18N
279: CONTEXT_ICONS.put(AddCatchAllAction.class,
280: loadPNGIcon("add_catch_all")); // NOI18N
281: CONTEXT_ICONS.put(AddElseIfAction.class,
282: loadPNGIcon("add_else_if")); // NOI18N
283: CONTEXT_ICONS.put(AddCompensationHandlerAction.class,
284: loadPNGIcon("add_compensation_handler")); // NOI18N
285: CONTEXT_ICONS.put(AddVariableAction.class,
286: loadPNGIcon("add_variable")); // NOI18N
287: CONTEXT_ICONS.put(AddCorrelationSetAction.class,
288: loadPNGIcon("add_correlation_set")); // NOI18N
289: CONTEXT_ICONS.put(AddEventHandlersAction.class,
290: loadPNGIcon("add_event_handlers")); // NOI18N
291: CONTEXT_ICONS.put(AddTerminationHandlerAction.class,
292: loadPNGIcon("add_termination_handler")); // NOI18N
293: CONTEXT_ICONS.put(AddFaultHandlersAction.class,
294: loadPNGIcon("add_fault_handlers")); // NOI18N
295: CONTEXT_ICONS.put(AddOnMessageAction.class,
296: loadPNGIcon("add_on_message")); // NOI18N
297: CONTEXT_ICONS.put(AddOnAlarmAction.class,
298: loadPNGIcon("add_on_alarm")); // NOI18N
299: CONTEXT_ICONS.put(AddOnEventAction.class,
300: loadPNGIcon("add_on_event")); // NOI18N
301: CONTEXT_ICONS.put(ShowPropertyEditorAction.class,
302: loadPNGIcon("edit_properties")); // NOI18N
303: CONTEXT_ICONS.put(OpenPartnerLinkInEditor.class,
304: loadPNGIcon("open_wsdl")); // NOI18N
305: }
306: }
|