KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > ui > logicalview > libraries > RemoveClassPathRootAction


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
20 package org.netbeans.modules.j2ee.ejbjarproject.ui.logicalview.libraries;
21
22
23 import java.io.IOException JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import org.openide.nodes.Node;
27 import org.openide.util.NbBundle;
28 import org.openide.util.HelpCtx;
29 import org.openide.util.actions.NodeAction;
30 import org.netbeans.spi.project.support.ant.EditableProperties;
31
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Set JavaDoc;
36 import org.netbeans.api.project.FileOwnerQuery;
37 import org.netbeans.api.project.Project;
38 import org.netbeans.api.project.ProjectManager;
39 import org.netbeans.modules.j2ee.ejbjarproject.classpath.ClassPathSupport;
40 import org.netbeans.modules.j2ee.ejbjarproject.ui.customizer.EjbJarProjectProperties;
41 import org.netbeans.spi.project.support.ant.AntProjectHelper;
42 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
43 import org.netbeans.spi.project.support.ant.ReferenceHelper;
44 import org.openide.ErrorManager;
45 import org.openide.util.Utilities;
46
47 /**
48  * Action for removing an ClassPathRoot. The action looks up
49  * the {@link RemoveClassPathRootAction.Removable} in the
50  * activated node's Lookups and delegates to it.
51  * @author Tomas Zezula
52  */

53 final class RemoveClassPathRootAction extends NodeAction {
54
55     /**
56      * Implementation of this interfaces has to be placed
57      * into the node's Lookup to allow {@link RemoveClassPathRootAction}
58      * on the node.
59      */

60     static interface Removable {
61         /**
62          * Checks if the classpath root can be removed
63          * @return returns true if the action should be enabled
64          */

65         public boolean canRemove ();
66
67         /**
68          * <p>Removes the classpath root. The caller has write access to
69          * ProjectManager. The implementation should <strong>not</strong> save the changed project.
70          * Instead, it should return the changed Project. The caller ensures
71          * that all the changed projects are saved.
72          *
73          * <p>The reason why the implementation shouldn't save the project is that
74          * changed made to the project may cause the build-impl.xml file to be
75          * recreated upon saving, which is slow. There will be performance issues (see #54160) if
76          * multiple references are removed and the project is saved after
77          * each removal.
78          *
79          * @return the changed project or null if no project has been changed.
80          */

81         public abstract Project remove ();
82     }
83
84     protected void performAction(final Node[] activatedNodes) {
85         final Set JavaDoc changedProjectsSet = new HashSet JavaDoc();
86         
87         ProjectManager.mutex().writeAccess(new Runnable JavaDoc() {
88             public void run() {
89                 for (int i = 0; i < activatedNodes.length; i++) {
90                     Removable removable = (Removable) activatedNodes[i].getLookup().lookup(Removable.class);
91                     if (removable == null)
92                         continue;
93
94                     Project p = removable.remove();
95                     if (p != null)
96                         changedProjectsSet.add(p);
97                 }
98
99                 for (Iterator JavaDoc i = changedProjectsSet.iterator(); i.hasNext();) {
100                     Project p = (Project)i.next();
101                     try {
102                         ProjectManager.getDefault().saveProject(p);
103                     }
104                     catch (IOException JavaDoc e) {
105                         ErrorManager.getDefault().notify(e);
106                     }
107                 }
108             }
109         });
110     }
111
112     protected boolean enable(Node[] activatedNodes) {
113         for (int i=0; i<activatedNodes.length; i++) {
114             Removable removable = (Removable) activatedNodes[i].getLookup().lookup(Removable.class);
115             if (removable==null) {
116                 return false;
117             }
118             if (!removable.canRemove()) {
119                 return false;
120             }
121         }
122         return true;
123     }
124
125     public String JavaDoc getName() {
126         return NbBundle.getMessage (RemoveClassPathRootAction.class,"CTL_RemoveProject");
127     }
128
129     public HelpCtx getHelpCtx() {
130         return new HelpCtx (RemoveClassPathRootAction.class);
131     }
132
133     protected boolean asynchronous() {
134         return false;
135     }
136
137     /**
138      * Checks if the reference is still used in the project.
139      * @param props the array of {@link EditableProperties} which
140      * should be checked.
141      * @param reference
142      * @return true if the reference is used
143      */

144     public static boolean isReferenced (EditableProperties[] props, String JavaDoc reference) {
145         for (int i=0; i< props.length; i++) {
146             for (Iterator JavaDoc it = props[i].values().iterator(); it.hasNext();) {
147                 String JavaDoc value = (String JavaDoc) it.next ();
148                 if (value != null && value.indexOf(reference)>=0) {
149                     return true;
150                 }
151             }
152         }
153         return false;
154     }
155 }
156
Popular Tags