KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > ui > 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.web.project.ui;
21
22
23 import java.io.IOException JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import org.openide.nodes.Node;
26 import org.openide.util.NbBundle;
27 import org.openide.util.HelpCtx;
28 import org.openide.util.actions.NodeAction;
29 import org.netbeans.api.project.Project;
30 import org.netbeans.spi.project.support.ant.EditableProperties;
31
32
33 import java.util.Iterator JavaDoc;
34 import java.util.Set JavaDoc;
35 import org.netbeans.api.project.ProjectManager;
36 import org.openide.ErrorManager;
37
38 /**
39  * Action for removing an ClassPathRoot. The action looks up
40  * the {@link RemoveClassPathRootAction.Removable} in the
41  * activated node's Lookups and delegates to it.
42  * @author Tomas Zezula
43  */

44 final class RemoveClassPathRootAction extends NodeAction {
45
46     /**
47      * Implementation of this interfaces has to be placed
48      * into the node's Lookup to allow {@link RemoveClassPathRootAction}
49      * on the node.
50      */

51     static interface Removable {
52         /**
53          * Checks if the classpath root can be removed
54          * @return returns true if the action should be enabled
55          */

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

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

135     public static boolean isReferenced (EditableProperties[] props, String JavaDoc reference) {
136         for (int i=0; i< props.length; i++) {
137             for (Iterator JavaDoc it = props[i].values().iterator(); it.hasNext();) {
138                 String JavaDoc value = (String JavaDoc) it.next ();
139                 if (value != null && value.indexOf(reference)>=0) {
140                     return true;
141                 }
142             }
143         }
144         return false;
145     }
146 }
147
Popular Tags