KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > 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.j2ee.clientproject.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.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          * Removes the classpath root
59          */

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

123     public static boolean isReferenced (EditableProperties[] props, String JavaDoc reference) {
124         for (int i=0; i< props.length; i++) {
125             for (Iterator JavaDoc it = props[i].values().iterator(); it.hasNext();) {
126                 String JavaDoc value = (String JavaDoc) it.next ();
127                 if (value != null && value.indexOf(reference)>=0) {
128                     return true;
129                 }
130             }
131         }
132         return false;
133     }
134 }
135
Popular Tags