KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > ui > DocBaseNodeFactory


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.project.ui;
21
22 import java.awt.Image JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import javax.swing.SwingUtilities JavaDoc;
29 import javax.swing.event.ChangeEvent JavaDoc;
30 import javax.swing.event.ChangeListener JavaDoc;
31 import javax.swing.event.EventListenerList JavaDoc;
32 import org.netbeans.api.project.Project;
33 import org.netbeans.api.queries.VisibilityQuery;
34 import org.netbeans.modules.web.project.UpdateHelper;
35 import org.netbeans.modules.web.project.WebProject;
36 import org.netbeans.modules.web.project.ui.SourceNodeFactory.PreselectPropertiesAction;
37 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties;
38 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
39 import org.netbeans.spi.project.ui.support.NodeFactory;
40 import org.netbeans.spi.project.ui.support.NodeList;
41 import org.openide.filesystems.FileObject;
42 import org.openide.loaders.ChangeableDataFilter;
43 import org.openide.loaders.DataFilter;
44 import org.openide.loaders.DataFolder;
45 import org.openide.loaders.DataObject;
46 import org.openide.nodes.FilterNode;
47 import org.openide.nodes.Node;
48 import org.openide.util.NbBundle;
49 import org.openide.util.Utilities;
50
51 /**
52  *
53  * @author mkleint
54  */

55 public final class DocBaseNodeFactory implements NodeFactory {
56     
57     /** Creates a new instance of LibrariesNodeFactory */
58     public DocBaseNodeFactory() {
59     }
60
61     public NodeList createNodes(Project p) {
62         WebProject project = (WebProject)p.getLookup().lookup(WebProject.class);
63         assert project != null;
64         return new DocBaseNodeList(project);
65     }
66
67     private static class DocBaseNodeList implements NodeList<String JavaDoc>, PropertyChangeListener JavaDoc {
68         private static final String JavaDoc DOC_BASE = "docBase"; //NOI18N
69

70         private final WebProject project;
71         private final ArrayList JavaDoc<ChangeListener JavaDoc> listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
72
73         private final PropertyEvaluator evaluator;
74         private final UpdateHelper helper;
75         
76         DocBaseNodeList(WebProject proj) {
77             project = proj;
78             WebLogicalViewProvider logView = (WebLogicalViewProvider) project.getLookup().lookup(WebLogicalViewProvider.class);
79             assert logView != null;
80             evaluator = logView.getEvaluator();
81             helper = logView.getUpdateHelper();
82         }
83         
84         public List JavaDoc<String JavaDoc> keys() {
85             List JavaDoc<String JavaDoc> result = new ArrayList JavaDoc<String JavaDoc>();
86             result.add(DOC_BASE);
87             return result;
88         }
89
90         public synchronized void addChangeListener(ChangeListener JavaDoc l) {
91             listeners.add(l);
92         }
93
94         public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
95             listeners.remove(l);
96         }
97         
98         private void fireChange() {
99             ArrayList JavaDoc<ChangeListener JavaDoc> list = new ArrayList JavaDoc<ChangeListener JavaDoc>();
100             synchronized (this) {
101                 list.addAll(listeners);
102             }
103             Iterator JavaDoc<ChangeListener JavaDoc> it = list.iterator();
104             while (it.hasNext()) {
105                 ChangeListener JavaDoc elem = it.next();
106                 elem.stateChanged(new ChangeEvent JavaDoc( this ));
107             }
108         }
109
110         public Node node(String JavaDoc key) {
111             if (key == DOC_BASE) {
112                 return new DocBaseNode(getFolder(WebProjectProperties.WEB_DOCBASE_DIR), project);
113             }
114             assert false: "No node for key: " + key;
115             return null;
116         }
117
118         public void addNotify() {
119         }
120
121         public void removeNotify() {
122         }
123
124         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
125             // The caller holds ProjectManager.mutex() read lock
126
SwingUtilities.invokeLater(new Runnable JavaDoc() {
127                 public void run() {
128                     fireChange();
129                 }
130             });
131         }
132         
133         private DataFolder getFolder(String JavaDoc propName) {
134             FileObject fo = getFileObject(propName);
135             if (fo != null) {
136                 DataFolder df = DataFolder.findFolder(fo);
137                 return df;
138             }
139             return null;
140         }
141         
142         private FileObject getFileObject(String JavaDoc propName) {
143             String JavaDoc foName = evaluator.getProperty (propName);
144             if (foName == null) {
145                 return null;
146             }
147             FileObject fo = helper.getAntProjectHelper().resolveFileObject(foName);
148             // when the project is deleted externally, the sources change could
149
// trigger a call to thid method before the project directory is
150
// notified about the deletion - invalid FileObject-s could be returned
151
return fo != null && fo.isValid() ? fo : null;
152         }
153         
154     }
155     
156     private static final class DocBaseNode extends FilterNode {
157         private javax.swing.Action JavaDoc actions[];
158         private static final DataFilter VISIBILITY_QUERY_FILTER = new VisibilityQueryDataFilter();
159         
160         private static Image JavaDoc WEB_PAGES_BADGE = Utilities.loadImage( "org/netbeans/modules/web/project/ui/resources/webPagesBadge.gif" ); //NOI18N
161

162         private final Project project;
163         
164         DocBaseNode (DataFolder folder, Project project) {
165             super (folder.getNodeDelegate(), folder.createNodeChildren(VISIBILITY_QUERY_FILTER));
166             this.project = project;
167         }
168         
169         public Image JavaDoc getIcon(int type) {
170             return computeIcon(false, type);
171         }
172
173         public Image JavaDoc getOpenedIcon(int type) {
174             return computeIcon(true, type);
175         }
176
177         private Node getDataFolderNodeDelegate() {
178             return ((DataFolder) getLookup().lookup(DataFolder.class)).getNodeDelegate();
179         }
180
181         private Image JavaDoc computeIcon(boolean opened, int type) {
182             Image JavaDoc image;
183
184             image = opened ? getDataFolderNodeDelegate().getOpenedIcon(type) : getDataFolderNodeDelegate().getIcon(type);
185             image = Utilities.mergeImages(image, WEB_PAGES_BADGE, 7, 7);
186
187             return image;
188         }
189         
190         public String JavaDoc getDisplayName () {
191             return NbBundle.getMessage(DocBaseNodeFactory.class, "LBL_Node_DocBase"); //NOI18N
192
}
193
194         public javax.swing.Action JavaDoc[] getActions( boolean context ) {
195             if ( actions == null ) {
196                 actions = new javax.swing.Action JavaDoc[9];
197                 actions[0] = org.netbeans.spi.project.ui.support.CommonProjectActions.newFileAction();
198                 actions[1] = null;
199                 actions[2] = org.openide.util.actions.SystemAction.get( org.openide.actions.FileSystemAction.class );
200                 actions[3] = null;
201                 actions[4] = org.openide.util.actions.SystemAction.get( org.openide.actions.FindAction.class );
202                 actions[5] = null;
203                 actions[6] = org.openide.util.actions.SystemAction.get( org.openide.actions.PasteAction.class );
204                 actions[7] = null;
205                 actions[8] = new PreselectPropertiesAction(project, "Sources"); //NOI18N
206
}
207             return actions;
208         }
209         
210         /* Can the original node be renamed?
211         *
212         * @return true if the node can be renamed
213         */

214         public boolean canRename() {
215             return false;
216         }
217
218     }
219
220     static final class VisibilityQueryDataFilter implements ChangeListener JavaDoc, ChangeableDataFilter {
221         
222         EventListenerList JavaDoc ell = new EventListenerList JavaDoc();
223         
224         public VisibilityQueryDataFilter() {
225             VisibilityQuery.getDefault().addChangeListener( this );
226         }
227                 
228         public boolean acceptDataObject(DataObject obj) {
229             FileObject fo = obj.getPrimaryFile();
230             return VisibilityQuery.getDefault().isVisible( fo );
231         }
232         
233         public void stateChanged( ChangeEvent JavaDoc e) {
234             Object JavaDoc[] listeners = ell.getListenerList();
235             ChangeEvent JavaDoc event = null;
236             for (int i = listeners.length-2; i>=0; i-=2) {
237                 if (listeners[i] == ChangeListener JavaDoc.class) {
238                     if ( event == null) {
239                         event = new ChangeEvent JavaDoc( this );
240                     }
241                     ((ChangeListener JavaDoc)listeners[i+1]).stateChanged( event );
242                 }
243             }
244         }
245     
246         public void addChangeListener( ChangeListener JavaDoc listener ) {
247             ell.add( ChangeListener JavaDoc.class, listener );
248         }
249                         
250         public void removeChangeListener( ChangeListener JavaDoc listener ) {
251             ell.remove( ChangeListener JavaDoc.class, listener );
252         }
253         
254     }
255
256 }
257
Popular Tags