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.editors.api.nodes.actions.*;
022: import org.netbeans.modules.bpel.nodes.BpelNode;
023: import org.netbeans.modules.bpel.editors.api.nodes.actions.ActionType;
024: import java.util.ArrayList;
025: import java.util.List;
026: import java.util.concurrent.Callable;
027: import org.netbeans.modules.bpel.model.api.BpelEntity;
028: import org.netbeans.modules.bpel.model.api.BpelModel;
029: import org.netbeans.modules.xml.xam.ui.XAMUtils;
030: import org.openide.ErrorManager;
031: import org.openide.nodes.Node;
032: import org.openide.util.HelpCtx;
033: import org.openide.util.actions.NodeAction;
034:
035: /**
036: *
037: * @author Vitaly Bychkov
038: *
039: */
040: public abstract class BpelNodeAction extends NodeAction implements
041: BpelNodeTypedAction {
042:
043: public BpelNodeAction() {
044: name = getBundleName();
045: }
046:
047: protected abstract String getBundleName();
048:
049: protected abstract void performAction(BpelEntity[] bpelEntities);
050:
051: protected boolean enable(BpelEntity[] bpelEntities) {
052: if (bpelEntities == null)
053: return false;
054: if (bpelEntities.length != 1)
055: return false;
056: if (bpelEntities[0] == null)
057: return false;
058:
059: BpelModel bpelModel = bpelEntities[0].getBpelModel();
060:
061: if (bpelModel == null)
062: return false;
063:
064: boolean readonly = !XAMUtils.isWritable(bpelModel);
065:
066: if (readonly && isChangeAction()) {
067: return false;
068: }
069:
070: return true;
071: }
072:
073: protected boolean asynchronous() {
074: return false;
075: }
076:
077: public void performAction(Node[] nodes) {
078: final BpelEntity[] bpelEntities = getBpelEntities(nodes);
079:
080: if (!enable(bpelEntities)) {
081: return;
082: }
083: BpelModel model = getBpelModel(nodes[0]);
084: if (model == null) {
085: return;
086: }
087: try {
088: model.invoke(new Callable<Object>() {
089: public Object call() {
090: performAction(bpelEntities);
091: return null;
092: }
093: }, this );
094: } catch (Exception e) {
095: ErrorManager.getDefault().notify(e);
096: }
097: }
098:
099: public boolean enable(final Node[] nodes) {
100: if (nodes == null || nodes.length < 1) {
101: return false;
102: }
103: for (Node node : nodes) {
104: if (!(node instanceof BpelNode)) {
105: return false;
106: }
107: }
108:
109: BpelModel model = getBpelModel(nodes[0]);
110: // model == null in case dead element
111: if (model == null) {
112: return false;
113: }
114: boolean isEnable = false;
115: if (model.isIntransaction()) {
116: return enable(getBpelEntities(nodes));
117: }
118: try {
119: class CheckEnabled implements Runnable {
120: public boolean enabled = false;
121:
122: public void run() {
123: this .enabled = enable(getBpelEntities(nodes));
124: }
125: }
126:
127: CheckEnabled check = new CheckEnabled();
128:
129: model.invoke(check);
130:
131: return check.enabled;
132:
133: } catch (Exception ex) {
134: ErrorManager.getDefault().notify(ex);
135: }
136:
137: return false;
138: }
139:
140: public String getName() {
141: return name;
142: }
143:
144: public boolean isChangeAction() {
145: return true;
146: }
147:
148: public BpelModel getBpelModel(Node node) {
149: BpelModel bpelModel = (BpelModel) node.getLookup().lookup(
150: BpelModel.class);
151: if (bpelModel == null && node instanceof BpelNode) {
152: Object ref = ((BpelNode) node).getReference();
153: if (ref instanceof BpelEntity) {
154: bpelModel = ((BpelEntity) ref).getBpelModel();
155: }
156: }
157: return bpelModel;
158: }
159:
160: public BpelModel getBpelModel(BpelEntity entity) {
161: return entity == null ? null : entity.getBpelModel();
162: }
163:
164: public HelpCtx getHelpCtx() {
165: return HelpCtx.DEFAULT_HELP;
166: }
167:
168: protected static final BpelEntity[] getBpelEntities(Node[] nodes) {
169: List<BpelEntity> entities = new ArrayList<BpelEntity>();
170:
171: Object tmpRefObj = null;
172: for (Node node : nodes) {
173: if (node instanceof BpelNode
174: && (tmpRefObj = ((BpelNode) node).getReference()) instanceof BpelEntity) {
175: entities.add((BpelEntity) tmpRefObj);
176: }
177: }
178:
179: BpelEntity[] entitiesArray = entities.size() < 1 ? null
180: : new BpelEntity[entities.size()];
181: entitiesArray = entitiesArray == null ? null : entities
182: .toArray(entitiesArray);
183:
184: return entitiesArray;
185: }
186:
187: private String name;
188: }
|