KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > ui > customizer > 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.clientproject.ui.customizer;
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.spi.project.support.ant.EditableProperties;
30
31 import java.util.Iterator JavaDoc;
32 import java.util.Set JavaDoc;
33 import org.netbeans.api.project.Project;
34 import org.netbeans.api.project.ProjectManager;
35 import org.openide.ErrorManager;
36
37 /**
38  * Action for removing an ClassPathRoot. The action looks up
39  * the {@link RemoveClassPathRootAction.Removable} in the
40  * activated node's Lookups and delegates to it.
41  * @author Tomas Zezula
42  */

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

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

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

71         public abstract Project remove ();
72     }
73
74     protected void performAction(final Node[] activatedNodes) {
75         final Set JavaDoc<Project> changedProjectsSet = new HashSet JavaDoc<Project>();
76         
77         ProjectManager.mutex().writeAccess(new Runnable JavaDoc() {
78             public void run() {
79                 for (int i = 0; i < activatedNodes.length; i++) {
80                     Removable removable = (Removable) activatedNodes[i].getLookup().lookup(Removable.class);
81                     if (removable == null) {
82                         continue;
83                     }
84
85                     Project p = removable.remove();
86                     if (p != null) {
87                         changedProjectsSet.add(p);
88                     }
89                 }
90
91                 for (Project p : changedProjectsSet) {
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