KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjar > project > ui > ServerResourceNode


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.ejbjar.project.ui;
21
22 import java.awt.Image JavaDoc;
23 import org.openide.ErrorManager;
24 import org.openide.actions.FileSystemAction;
25 import org.openide.filesystems.FileChangeAdapter;
26 import org.openide.filesystems.Repository;
27 import org.openide.nodes.FilterNode;
28 import org.openide.nodes.Node;
29 import org.openide.util.Lookup;
30 import org.openide.util.NbBundle;
31 import org.openide.util.Utilities;
32
33 import javax.swing.Action JavaDoc;
34 import javax.swing.event.ChangeEvent JavaDoc;
35 import javax.swing.event.ChangeListener JavaDoc;
36 import javax.swing.event.EventListenerList JavaDoc;
37 import org.netbeans.api.project.Project;
38 import org.netbeans.api.queries.VisibilityQuery;
39 import org.netbeans.spi.project.ui.support.CommonProjectActions;
40 import org.openide.actions.FindAction;
41 import org.openide.actions.PasteAction;
42 import org.openide.filesystems.FileChangeListener;
43 import org.openide.filesystems.FileEvent;
44 import org.openide.filesystems.FileObject;
45 import org.openide.filesystems.FileRenameEvent;
46 import org.openide.filesystems.FileUtil;
47 import org.openide.loaders.ChangeableDataFilter;
48 import org.openide.loaders.DataFilter;
49 import org.openide.loaders.DataFolder;
50 import org.openide.loaders.DataObject;
51 import org.openide.loaders.DataObjectNotFoundException;
52 import org.openide.nodes.AbstractNode;
53 import org.openide.nodes.Children;
54 import org.openide.util.actions.SystemAction;
55 import org.openide.util.lookup.Lookups;
56
57 /**
58  * Node to represent the setup folder described in the blueprints.
59  *
60  * @author Chris Webster, Andrei Badea
61  */

62 public class ServerResourceNode extends FilterNode {
63     
64     private static final ErrorManager LOGGER = ErrorManager.getDefault().getInstance(ServerResourceNode.class.getName());
65     private static final boolean LOG = LOGGER.isLoggable(ErrorManager.INFORMATIONAL);
66     
67     private static final Image JavaDoc RESOURCE_FILE_BADGE = Utilities.loadImage( "org/netbeans/modules/j2ee/ejbjar/project/ui/resourcesBadge.gif", true ); // NOI18N
68
private static final String JavaDoc SETUP_DIR = "setup"; // NOI18N
69
private static final DataFilter VISIBILITY_QUERY_FILTER = new VisibilityQueryDataFilter();
70     
71     private Project project;
72     private FileChangeListener projectDirectoryListener;
73     
74     /** Creates a new instance of ServerResourceNode */
75     public ServerResourceNode(Project project) {
76         this(getSetupDataFolder(project), project);
77     }
78     
79     private ServerResourceNode(DataFolder folderDo, Project project) {
80         super(getDataFolderNode(folderDo, project), getDataFolderNodeChildren(folderDo));
81         projectDirectoryListener = new ProjectDirectoryListener();
82         if (LOG) {
83             LOGGER.log(ErrorManager.INFORMATIONAL, "Adding file listener to " + project.getProjectDirectory()); // NOI18N
84
}
85         project.getProjectDirectory().addFileChangeListener(FileUtil.weakFileChangeListener(projectDirectoryListener, project.getProjectDirectory()));
86         this.project = project;
87     }
88     
89     public Image JavaDoc getIcon(int type) {
90         return badgeIcon(super.getIcon(type));
91     }
92     
93     public Image JavaDoc getOpenedIcon( int type ) {
94         return badgeIcon(super.getOpenedIcon(type));
95     }
96     
97     private static Image JavaDoc badgeIcon(Image JavaDoc icon) {
98         return Utilities.mergeImages(icon, RESOURCE_FILE_BADGE, 7, 7);
99     }
100     
101     public String JavaDoc getDisplayName() {
102         return NbBundle.getMessage(ServerResourceNode.class, "LBL_Node_ResourceNode");
103     }
104
105     public boolean canCopy() {
106         return false;
107     }
108
109     public boolean canCut() {
110         return false;
111     }
112
113     public boolean canRename() {
114         return false;
115     }
116
117     public boolean canDestroy() {
118         return false;
119     }
120
121     public Action[] getActions( boolean context ) {
122         return new Action[] {
123             CommonProjectActions.newFileAction(),
124             null,
125             SystemAction.get(FileSystemAction.class),
126             null,
127             SystemAction.get(FindAction.class),
128             null,
129             SystemAction.get(PasteAction.class),
130         };
131     }
132         
133     private void refresh() {
134         if (LOG) {
135             LOGGER.log(ErrorManager.INFORMATIONAL, "Refreshing"); // NOI18N
136
}
137         final DataFolder folderDo = getSetupDataFolder(project);
138         if (LOG) {
139             LOGGER.log(ErrorManager.INFORMATIONAL, "The DataFolder is: " + folderDo); // NOI18N
140
}
141         // #64665: should not call FilterNode.changeOriginal() or Node.setChildren()
142
// under Children.MUTEX read access
143
Children.MUTEX.postWriteRequest(new Runnable JavaDoc() {
144             public void run() {
145                 changeOriginal(getDataFolderNode(folderDo, project), false);
146                 org.openide.nodes.Children children = getDataFolderNodeChildren(folderDo);
147                 setChildren(children);
148                 if (LOG) {
149                     LOGGER.log(ErrorManager.INFORMATIONAL, "Children count: " + children.getNodes(true).length); // NOI18N
150
}
151             }
152         });
153
154     }
155     
156     private static DataFolder getSetupDataFolder(Project project) {
157         FileObject folderFo = project.getProjectDirectory().getFileObject(SETUP_DIR);
158         DataFolder folderDo = null;
159         if (folderFo != null && folderFo.isFolder()) {
160             try {
161                 folderDo = DataFolder.findFolder(folderFo);
162             }
163             catch (IllegalArgumentException JavaDoc e) {}
164         }
165         return folderDo;
166     }
167     
168     private static Node getDataFolderNode(DataFolder folderDo, Project project) {
169         // The project in the placeholder node lookup is needed for the New File action.
170
return (folderDo != null) ? folderDo.getNodeDelegate() : new PlaceHolderNode(Lookups.singleton(project));
171     }
172     
173     private static org.openide.nodes.Children getDataFolderNodeChildren(DataFolder folderDo) {
174         return (folderDo != null) ? folderDo.createNodeChildren(VISIBILITY_QUERY_FILTER) : Children.LEAF;
175     }
176     
177     final private class ProjectDirectoryListener extends FileChangeAdapter {
178         
179         public void fileDeleted(FileEvent fe) {
180             if (isWatchedFile(getFileName(fe)))
181                 refresh();
182         }
183
184         public void fileFolderCreated(FileEvent fe) {
185             if (isWatchedFile(getFileName(fe))) {
186                 refresh();
187             }
188         }
189
190         public void fileRenamed(org.openide.filesystems.FileRenameEvent fe) {
191             if (isWatchedFile(getFileName(fe)) || isWatchedFile(getOldFileName(fe)))
192                 refresh();
193         }
194
195         private boolean isWatchedFile(String JavaDoc fileName) {
196             return fileName.equals(SETUP_DIR);
197         }
198
199         private String JavaDoc getFileName(FileEvent fe) {
200             return fe.getFile().getNameExt();
201         }
202
203         private String JavaDoc getOldFileName(FileRenameEvent fe) {
204             String JavaDoc result = fe.getName();
205             if (fe.getExt() != "") // NOI18N
206
result = result + "." + fe.getExt(); // NOI18N
207

208             return result;
209         }
210     }
211     
212     final private static class VisibilityQueryDataFilter implements ChangeListener JavaDoc, ChangeableDataFilter {
213         
214         private EventListenerList JavaDoc ell = new EventListenerList JavaDoc();
215         
216         public VisibilityQueryDataFilter() {
217             VisibilityQuery.getDefault().addChangeListener(this);
218         }
219                 
220         public boolean acceptDataObject(DataObject obj) {
221             FileObject fo = obj.getPrimaryFile();
222             return VisibilityQuery.getDefault().isVisible(fo);
223         }
224         
225         public void stateChanged( ChangeEvent JavaDoc e) {
226             Object JavaDoc[] listeners = ell.getListenerList();
227             ChangeEvent JavaDoc event = null;
228             for (int i = listeners.length - 2; i >= 0; i-= 2) {
229                 if (listeners[i] == ChangeListener JavaDoc.class) {
230                     if (event == null) {
231                         event = new ChangeEvent JavaDoc( this );
232                     }
233                     ((ChangeListener JavaDoc)listeners[i + 1]).stateChanged(event);
234                 }
235             }
236         }
237     
238         public void addChangeListener( ChangeListener JavaDoc listener ) {
239             ell.add(ChangeListener JavaDoc.class, listener);
240         }
241                         
242         public void removeChangeListener( ChangeListener JavaDoc listener ) {
243             ell.remove(ChangeListener JavaDoc.class, listener);
244         }
245     }
246     
247     /**
248      * A placeholder node for a folder node.
249      */

250     private static final class PlaceHolderNode extends AbstractNode {
251         
252         public PlaceHolderNode(Lookup lookup) {
253             super(Children.LEAF, lookup);
254         }
255
256         public Image JavaDoc getIcon(int type) {
257             Image JavaDoc image = null;
258             Node imageDelegate = getImageDelegate();
259             if (imageDelegate != null) {
260                 image = imageDelegate.getIcon(type);
261             }
262             if (image == null) {
263                 image = super.getIcon(type);
264             }
265             return image;
266         }
267         
268         public Image JavaDoc getOpenedIcon(int type) {
269             Image JavaDoc image = null;
270             Node imageDelegate = getImageDelegate();
271             if (imageDelegate != null) {
272                 image = imageDelegate.getOpenedIcon(type);
273             }
274             if (image == null) {
275                 image = super.getOpenedIcon(type);
276             }
277             return image;
278         }
279         
280         private static Node getImageDelegate() {
281             FileObject imageFo = Repository.getDefault().getDefaultFileSystem().getRoot();
282             if (imageFo != null) {
283                 try {
284                     DataObject imageDo = DataObject.find(imageFo);
285                     return imageDo.getNodeDelegate();
286                 } catch (DataObjectNotFoundException donfe) {
287                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, donfe);
288                 }
289             }
290             return null;
291         }
292     }
293 }
294
295
Popular Tags