001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.sun.manager.jbi.actions;
042:
043: import java.util.HashSet;
044: import java.util.Set;
045: import javax.swing.Action;
046: import javax.swing.JMenu;
047: import javax.swing.JMenuItem;
048: import javax.swing.SwingUtilities;
049: import org.netbeans.modules.sun.manager.jbi.nodes.Refreshable;
050: import org.netbeans.modules.sun.manager.jbi.nodes.Undeployable;
051: import org.openide.awt.Actions;
052: import org.openide.nodes.Node;
053: import org.openide.util.HelpCtx;
054: import org.openide.util.Lookup;
055: import org.openide.util.NbBundle;
056: import org.openide.util.RequestProcessor;
057: import org.openide.util.actions.NodeAction;
058: import org.openide.util.actions.Presenter;
059: import org.openide.util.actions.SystemAction;
060:
061: /**
062: * Action to undeploy one or more JBI Service Assemblies.
063: *
064: * @author jqian
065: */
066: public abstract class UndeployAction extends NodeAction {
067:
068: protected void performAction(final Node[] activatedNodes) {
069: RequestProcessor.getDefault().post(new Runnable() {
070: public void run() {
071: try {
072: // a set of nodes that need refreshing
073: final Set<Node> parentNodes = new HashSet<Node>();
074:
075: for (Node node : activatedNodes) {
076: Lookup lookup = node.getLookup();
077: Undeployable undeployable = lookup
078: .lookup(Undeployable.class);
079:
080: if (undeployable != null) {
081: Node parentNode = node.getParentNode();
082: if (parentNode != null) {
083: parentNodes.add(node.getParentNode());
084: }
085: undeployable.undeploy(isForceAction());
086: }
087: }
088:
089: SwingUtilities.invokeLater(new Runnable() {
090: public void run() {
091: for (Node parentNode : parentNodes) {
092: final Refreshable refreshable = parentNode
093: .getLookup().lookup(
094: Refreshable.class);
095: if (refreshable != null) {
096: refreshable.refresh();
097: }
098: }
099: }
100: });
101: } catch (RuntimeException rex) {
102: //gobble up exception
103: }
104: }
105: });
106: }
107:
108: protected boolean enable(Node[] activatedNodes) {
109: boolean ret = false;
110:
111: if (activatedNodes != null && activatedNodes.length > 0) {
112: ret = true;
113: for (Node node : activatedNodes) {
114: Undeployable undeployable = node.getLookup().lookup(
115: Undeployable.class);
116: try {
117: if (undeployable != null
118: && !undeployable.canUndeploy()) {
119: ret = false;
120: break;
121: }
122: } catch (RuntimeException rex) {
123: //gobble up exception
124: }
125: }
126: }
127:
128: return ret;
129: }
130:
131: protected boolean asynchronous() {
132: return false;
133: }
134:
135: public HelpCtx getHelpCtx() {
136: return HelpCtx.DEFAULT_HELP;
137: }
138:
139: protected abstract boolean isForceAction();
140:
141: /**
142: * Normal undeploy action.
143: */
144: public static class Normal extends UndeployAction {
145:
146: protected boolean isForceAction() {
147: return false;
148: }
149:
150: public String getName() {
151: return NbBundle.getMessage(ShutdownAction.class,
152: "LBL_UndeployAction"); // NOI18N
153: }
154: }
155:
156: /**
157: * Force undeploy action.
158: */
159: public static class Force extends UndeployAction implements
160: Presenter.Popup {
161:
162: public String getName() {
163: return NbBundle.getMessage(ShutdownAction.class,
164: "LBL_ForceUndeployAction"); // NOI18N
165: }
166:
167: public JMenuItem getPopupPresenter() {
168: JMenu result = new JMenu(NbBundle.getMessage(
169: ShutdownAction.class, "LBL_Advanced")); // NOI18N
170:
171: //result.add(new JMenuItem(SystemAction.get(ShutdownAction.Force.class)));
172: Action forceShutdownAction = SystemAction
173: .get(ShutdownAction.Force.class);
174: JMenuItem forceShutdownMenuItem = new JMenuItem();
175: Actions.connect(forceShutdownMenuItem, forceShutdownAction,
176: false);
177: result.add(forceShutdownMenuItem);
178:
179: //result.add(new JMenuItem(this));
180: Action forceUndeployAction = SystemAction
181: .get(UndeployAction.Force.class);
182: JMenuItem forceUndeployMenuItem = new JMenuItem();
183: Actions.connect(forceUndeployMenuItem, forceUndeployAction,
184: false);
185: result.add(forceUndeployMenuItem);
186:
187: return result;
188: }
189:
190: protected boolean isForceAction() {
191: return true;
192: }
193: }
194: }
|