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