KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > runtime > actions > DeleteResourceAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.sun.ide.runtime.actions;
20
21 import org.openide.ErrorManager;
22 import org.openide.NotifyDescriptor;
23 import org.openide.nodes.Node;
24 import org.openide.util.Lookup;
25 import org.openide.util.actions.NodeAction;
26 import org.openide.util.NbBundle;
27 import org.openide.util.HelpCtx;
28
29 import org.netbeans.modules.j2ee.sun.bridge.apis.AppserverMgmtNode;
30 import org.netbeans.modules.j2ee.sun.bridge.apis.Removable;
31 import org.netbeans.modules.j2ee.sun.ide.runtime.nodes.ConnectionPoolNode;
32 import org.netbeans.modules.j2ee.sun.util.NodeTypes;
33 import org.netbeans.modules.j2ee.sun.util.GUIUtils;
34
35 import org.netbeans.modules.j2ee.sun.bridge.apis.RefreshCookie;
36
37 /**
38  *
39  *
40  */

41 public class DeleteResourceAction extends NodeAction {
42     
43     
44     /**
45      *
46      *
47      */

48     protected void performAction(Node[] activatedNodes) {
49         if (activatedNodes==null){
50             return;
51         }
52         
53         for (int i=0;i<activatedNodes.length;i++){
54             Node node = activatedNodes[i];
55             Lookup lookup = node.getLookup();
56             
57             Object JavaDoc obj2 = lookup.lookup(AppserverMgmtNode.class);
58             String JavaDoc nodeType = null;
59             
60             if(obj2 instanceof AppserverMgmtNode) {
61                 AppserverMgmtNode appMgmtNode = (AppserverMgmtNode) obj2;
62                 nodeType = appMgmtNode.getNodeType();
63             }
64             
65             try {
66             /*
67              check if jdbc connection pool resource and get dependent reources
68              to delete
69              */

70                 if(NodeTypes.CONNECTION_POOL.equals(nodeType)) {
71                     Object JavaDoc proceed =
72                             GUIUtils.showWarning(
73                             getLocalizedString("dependent_resources_notify"));
74                     if(proceed == NotifyDescriptor.OK_OPTION){
75                         deleteDependentResources(node);
76                         removeResource(node);
77                     }
78                 } else {
79                     removeResource(node);
80                 }
81                 
82                 //refresh parent node of that which was deleted
83
Node parentNode = node.getParentNode();
84                 RefreshCookie refreshAction =
85                         (RefreshCookie)parentNode.getCookie(RefreshCookie.class);
86                 if (refreshAction != null){
87                     refreshAction.refresh();
88                 }
89             } catch(java.lang.RuntimeException JavaDoc rex) {
90                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,rex);
91             }
92         }
93     }
94     
95     
96     /**
97      * Removes the resource given the netbeans node.
98      *
99      * @param node The netbeans node.
100      */

101     private void removeResource(final Node node) {
102         Lookup lookup = node.getLookup();
103         Object JavaDoc obj = lookup.lookup(Removable.class);
104         if(obj instanceof Removable) {
105             Removable removeableObj = (Removable)obj;
106             removeableObj.remove();
107         }
108     }
109     
110     
111     /**
112      *
113      *
114      */

115     private void deleteDependentResources(final Node node) {
116         Lookup lookup = node.getLookup();
117         Object JavaDoc obj = lookup.lookup(ConnectionPoolNode.class);
118         if(obj instanceof ConnectionPoolNode) {
119             ConnectionPoolNode connPoolNode = (ConnectionPoolNode) obj;
120             connPoolNode.removeDependentJDBCResources();
121         }
122         refreshJDBCResourcesFolder(node);
123     }
124     
125     /**
126      *
127      *
128      */

129     private void refreshJDBCResourcesFolder(final Node node){
130         try {
131             Node parentNode = node.getParentNode();
132             org.openide.nodes.Children ch = parentNode.getParentNode().getChildren();
133             Node[] resourceNodes = ch.getNodes(true);
134             if(resourceNodes.length > 0){
135                 Node jdbcMgmtNode = resourceNodes[0];
136                 RefreshCookie refreshAction = (RefreshCookie)jdbcMgmtNode.getCookie(RefreshCookie.class);
137                 if (refreshAction != null) {
138                     refreshAction.refresh();
139                 }
140             }
141         }catch(Exception JavaDoc ex){
142             //Failed to refresh peer container node containing JDBC resources
143
//Manual refresh of UI will show updated list
144
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex);
145         }
146     }
147     
148     /**
149      *
150      *
151      */

152     protected boolean enable(Node[] nodes) {
153         return ((nodes != null) && (nodes.length >= 1)) ? true : false;
154     }
155     
156     
157     /**
158      *
159      *
160      */

161     protected boolean asynchronous() {
162         return false;
163     }
164     
165     
166     /**
167      *
168      *
169      */

170     public HelpCtx getHelpCtx() {
171         return HelpCtx.DEFAULT_HELP;
172     }
173     
174     /**
175      *
176      */

177     public String JavaDoc getName() {
178         return NbBundle.getMessage(DeleteResourceAction.class,
179                 "LBL_DeleteResourceAction");
180     }
181     
182     
183     /**
184      *
185      *
186      */

187     private static String JavaDoc getLocalizedString(final String JavaDoc bundleStrProp) {
188         return NbBundle.getMessage(DeleteResourceAction.class, bundleStrProp);
189     }
190     
191 }
192
Popular Tags