KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > earproject > ui > ArchiveViews


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.earproject.ui;
21
22 import java.awt.Image JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.swing.Action JavaDoc;
27 import javax.swing.event.ChangeEvent JavaDoc;
28 import javax.swing.event.ChangeListener JavaDoc;
29 import javax.swing.event.EventListenerList JavaDoc;
30 import org.netbeans.api.project.Project;
31 import org.netbeans.api.queries.VisibilityQuery;
32 import org.netbeans.modules.j2ee.earproject.ui.customizer.EarProjectProperties;
33 import org.netbeans.modules.j2ee.spi.ejbjar.support.J2eeProjectView;
34 import org.netbeans.spi.project.support.ant.AntProjectHelper;
35 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
36 import org.openide.actions.FindAction;
37 import org.openide.filesystems.FileAttributeEvent;
38 import org.openide.filesystems.FileChangeListener;
39 import org.openide.filesystems.FileEvent;
40 import org.openide.filesystems.FileObject;
41 import org.openide.filesystems.FileRenameEvent;
42 import org.openide.filesystems.FileUtil;
43 import org.openide.loaders.ChangeableDataFilter;
44 import org.openide.loaders.DataFilter;
45 import org.openide.loaders.DataFolder;
46 import org.openide.loaders.DataObject;
47 import org.openide.nodes.Children;
48 import org.openide.nodes.FilterNode;
49 import org.openide.nodes.Node;
50 import org.openide.util.NbBundle;
51 import org.openide.util.Utilities;
52 import org.openide.util.WeakListeners;
53 import org.openide.util.actions.SystemAction;
54
55 class ArchiveViews {
56         
57     private ArchiveViews() {
58     }
59     
60     static final class LogicalViewChildren extends Children.Keys/*<FileObject>*/ implements FileChangeListener {
61         
62         // XXX does not react correctly to addition or removal of src/ subdir
63

64         private static final String JavaDoc KEY_DOC_BASE = "docBase"; //NOI18N
65
private static final String JavaDoc KEY_SETUP_DIR = "setupDir"; //NOI18N
66

67         private final Project project;
68         private final AntProjectHelper helper;
69         private final PropertyEvaluator evaluator;
70         private final FileObject projectDir;
71
72         public LogicalViewChildren (Project project, AntProjectHelper helper, PropertyEvaluator evaluator) {
73             assert project != null;
74             this.project = project;
75             assert helper != null;
76             this.helper = helper;
77             projectDir = helper.getProjectDirectory();
78             this.evaluator = evaluator;
79         }
80         
81         protected void addNotify() {
82             super.addNotify();
83             projectDir.addFileChangeListener(FileUtil.weakFileChangeListener(this, projectDir));
84             createNodes();
85         }
86         
87         private void createNodes() {
88             List JavaDoc<String JavaDoc> keys = new ArrayList JavaDoc<String JavaDoc>();
89            
90             DataFolder docBaseDir = getFolder(EarProjectProperties.META_INF);
91             if (docBaseDir != null) {
92                 keys.add(KEY_DOC_BASE);
93             }
94             keys.add(KEY_SETUP_DIR);
95             
96             setKeys(keys);
97         }
98         
99         protected void removeNotify() {
100             setKeys(Collections.EMPTY_SET);
101             projectDir.removeFileChangeListener(this);
102             super.removeNotify();
103         }
104         
105         protected Node[] createNodes(Object JavaDoc key) {
106             Node n = null;
107             if (key == KEY_DOC_BASE) {
108                 n = new DocBaseNode (getFolder(EarProjectProperties.META_INF));
109             } else if (key == KEY_SETUP_DIR) {
110                 n = J2eeProjectView.createServerResourcesNode(project);
111             }
112             return n == null ? new Node[0] : new Node[] {n};
113         }
114             
115         private DataFolder getFolder(String JavaDoc propName) {
116             String JavaDoc prop = evaluator.getProperty (propName);
117             if (prop != null) {
118                 FileObject fo = helper.resolveFileObject(prop);
119                 if (fo != null && fo.isValid() && fo.isFolder()) {
120                     DataFolder df = DataFolder.findFolder(fo);
121                     return df;
122                 }
123             }
124             return null;
125         }
126         
127         // file change events in the project directory
128
public void fileAttributeChanged(FileAttributeEvent fe) {
129         }
130         
131         public void fileChanged(FileEvent fe) {
132         }
133         
134         public void fileDataCreated(FileEvent fe) {
135         }
136         
137         public void fileDeleted(FileEvent fe) {
138             // setup folder deleted
139
createNodes();
140         }
141         
142         public void fileFolderCreated(FileEvent fe) {
143             // setup folder could be created
144
createNodes();
145         }
146         
147         public void fileRenamed(FileRenameEvent fe) {
148             // setup folder could be renamed
149
createNodes();
150         }
151     }
152     
153     private static final class VisibilityQueryDataFilter implements ChangeListener JavaDoc, ChangeableDataFilter {
154         
155         EventListenerList JavaDoc ell = new EventListenerList JavaDoc();
156         
157         public VisibilityQueryDataFilter() {
158             VisibilityQuery.getDefault().addChangeListener( this );
159         }
160                 
161         public boolean acceptDataObject(DataObject obj) {
162             FileObject fo = obj.getPrimaryFile();
163             return VisibilityQuery.getDefault().isVisible( fo );
164         }
165         
166         public void stateChanged( ChangeEvent JavaDoc e) {
167             Object JavaDoc[] listeners = ell.getListenerList();
168             ChangeEvent JavaDoc event = null;
169             for (int i = listeners.length-2; i>=0; i-=2) {
170                 if (listeners[i] == ChangeListener JavaDoc.class) {
171                     if ( event == null) {
172                         event = new ChangeEvent JavaDoc( this );
173                     }
174                     ((ChangeListener JavaDoc)listeners[i+1]).stateChanged( event );
175                 }
176             }
177         }
178     
179         public void addChangeListener( ChangeListener JavaDoc listener ) {
180             ell.add( ChangeListener JavaDoc.class, listener );
181         }
182                         
183         public void removeChangeListener( ChangeListener JavaDoc listener ) {
184             ell.remove( ChangeListener JavaDoc.class, listener );
185         }
186         
187     }
188
189     private static final class DocBaseNode extends FilterNode {
190
191         private static final DataFilter VISIBILITY_QUERY_FILTER = new VisibilityQueryDataFilter();
192         private static final Image JavaDoc CONFIGURATION_FILES_BADGE = Utilities.loadImage( "org/netbeans/modules/j2ee/earproject/ui/resources/archive.gif", true ); // NOI18N
193

194         public DocBaseNode(DataFolder folder) {
195             super(folder.getNodeDelegate(), folder.createNodeChildren(VISIBILITY_QUERY_FILTER));
196         }
197         
198         public Image JavaDoc getIcon( int type ) {
199             return computeIcon( false, type );
200         }
201         
202         public Image JavaDoc getOpenedIcon( int type ) {
203             return computeIcon( true, type );
204         }
205         
206         private Image JavaDoc computeIcon( boolean opened, int type ) {
207             Node folderNode = getOriginal();
208             Image JavaDoc image = opened ? folderNode.getOpenedIcon( type ) : folderNode.getIcon( type );
209             return Utilities.mergeImages( image, CONFIGURATION_FILES_BADGE, 7, 7 );
210         }
211         
212         public boolean canCopy() {
213             return false;
214         }
215         
216         public boolean canCut() {
217             return false;
218         }
219         
220         public boolean canRename() {
221             return false;
222         }
223         
224         public boolean canDestroy() {
225             return false;
226         }
227         
228         public Action JavaDoc[] getActions( boolean context ) {
229             return new Action JavaDoc[] {
230 // CommonProjectActions.newFileAction(),
231
// null,
232
// SystemAction.get(FileSystemRefreshAction.class),
233
// null,
234
SystemAction.get(FindAction.class),
235 // null,
236
// SystemAction.get(PasteAction.class),
237
// null,
238
// SystemAction.get(ToolsAction.class),
239
};
240         }
241         
242         public String JavaDoc getDisplayName() {
243             return NbBundle.getMessage(ArchiveViews.class, "LBL_Node_DocBase"); //NOI18N
244
}
245     }
246 }
247
Popular Tags