KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > archive > ui > RootNode


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
21 package org.netbeans.modules.j2ee.archive.ui;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ResourceBundle JavaDoc;
28 import javax.swing.Action JavaDoc;
29 import javax.swing.JSeparator JavaDoc;
30 import org.netbeans.api.project.Project;
31 import org.netbeans.api.project.ProjectUtils;
32 import org.netbeans.modules.j2ee.archive.project.ArchiveProject;
33 import org.netbeans.modules.j2ee.archive.project.ArchiveProjectProperties;
34 import org.netbeans.modules.j2ee.spi.ejbjar.support.J2eeProjectView;
35 import org.netbeans.spi.project.ActionProvider;
36 import org.netbeans.spi.project.ui.support.CommonProjectActions;
37 import org.netbeans.spi.project.ui.support.ProjectSensitiveActions;
38 import org.openide.ErrorManager;
39 import org.openide.filesystems.FileChangeListener;
40 import org.openide.filesystems.FileObject;
41 import org.openide.filesystems.FileSystem;
42 import org.openide.filesystems.Repository;
43 import org.openide.loaders.DataFolder;
44 import org.openide.loaders.DataObject;
45 import org.openide.loaders.DataObjectNotFoundException;
46 import org.openide.loaders.FolderLookup;
47 import org.openide.nodes.Children;
48 import org.openide.nodes.Node;
49 //import org.openide.util.HelpCtx;
50
import org.openide.util.Lookup;
51 import org.openide.util.NbBundle;
52 import org.openide.util.actions.SystemAction;
53 import org.openide.util.lookup.Lookups;
54
55 public class RootNode extends org.openide.nodes.AbstractNode {
56     
57 // private boolean broken;
58

59 // private BrokenLinksAction brokenLinksAction;
60
//
61
// private BrokenServerAction brokenServerAction;
62

63     public RootNode(ArchiveProject ap) {
64         super(new LogicalViewChildren(ap), createLookup(ap));
65         super.setName(ProjectUtils.getInformation(ap).getDisplayName());
66         setIconBaseWithExtension("org/netbeans/modules/j2ee/archive/project/resources/application_assembler_16.gif"); // NOI18N
67
// if (hasBrokenLinks()) {
68
// broken = true;
69
// }
70
// brokenLinksAction = new BrokenLinksAction();
71
// brokenServerAction = new BrokenServerAction();
72

73         ArchiveProjectProperties app = ap.getArchiveProjectProperties();
74         String JavaDoc sourceArchive = ap.getPropertyEvaluator().evaluate((String JavaDoc)app.get(ArchiveProjectProperties.SOURCE_ARCHIVE));
75         setShortDescription(NbBundle.getMessage(RootNode.class,"LBL_ProjectToolTip", // NOI18N
76
sourceArchive));
77     }
78     
79     public Action JavaDoc[] getActions( boolean context ) {
80         Action JavaDoc[] retVal;
81         if ( context ) {
82             retVal = super.getActions( true );
83         } else {
84             retVal = getAdditionalActions();
85         }
86         return retVal;
87     }
88
89     private Action JavaDoc[] getAdditionalActions() {
90         
91         ResourceBundle JavaDoc bundle = NbBundle.getBundle(RootNode.class);
92         
93         List JavaDoc actions = new ArrayList JavaDoc(30);
94         actions.add(CommonProjectActions.newFileAction());
95         actions.add(null);
96         actions.add(ProjectSensitiveActions.projectCommandAction(
97                 ActionProvider.COMMAND_BUILD, bundle.getString("LBL_BuildAction_Name"), null)); // NOI18N
98
actions.add(ProjectSensitiveActions.projectCommandAction(
99                 "verify", bundle.getString("LBL_VerifyAction_Name"), null)); // NOI18N
100
actions.add(ProjectSensitiveActions.projectCommandAction(
101                 ActionProvider.COMMAND_RUN, bundle.getString("LBL_RunAction_Name"), null)); // NOI18N
102
actions.add(null);
103         actions.add(CommonProjectActions.setAsMainProjectAction());
104         actions.add(CommonProjectActions.openSubprojectsAction());
105         actions.add(CommonProjectActions.closeProjectAction());
106         actions.add(null);
107         actions.add(CommonProjectActions.renameProjectAction());
108         actions.add(CommonProjectActions.moveProjectAction());
109         actions.add(CommonProjectActions.copyProjectAction());
110         actions.add(CommonProjectActions.deleteProjectAction());
111         actions.add(null);
112         actions.add(SystemAction.get( org.openide.actions.FindAction.class ));
113         
114         // honor 57874 contact
115

116         try {
117             Repository repository = Repository.getDefault();
118             FileSystem sfs = repository.getDefaultFileSystem();
119             FileObject fo = sfs.findResource("Projects/Actions"); // NOI18N
120
if (fo != null) {
121                 DataObject dobj = DataObject.find(fo);
122                 FolderLookup actionRegistry = new FolderLookup((DataFolder)dobj);
123                 Lookup.Template query = new Lookup.Template(Object JavaDoc.class);
124                 Lookup lookup = actionRegistry.getLookup();
125                 Iterator JavaDoc it = lookup.lookup(query).allInstances().iterator();
126                 if (it.hasNext()) {
127                     actions.add(null);
128                 }
129                 while (it.hasNext()) {
130                     Object JavaDoc next = it.next();
131                     if (next instanceof Action JavaDoc) {
132                         actions.add(next);
133                     } else if (next instanceof JSeparator JavaDoc) {
134                         actions.add(null);
135                     }
136                 }
137             }
138         } catch (DataObjectNotFoundException ex) {
139             // data folder for exitinf fileobject expected
140
ErrorManager.getDefault().notify(ex);
141         }
142         
143 // actions.add(null);
144
// if (brokenLinksAction != null && brokenLinksAction.isEnabled()) {
145
// actions.add(brokenLinksAction);
146
// }
147
// if (brokenServerAction.isEnabled()) {
148
// actions.add(brokenServerAction);
149
// }
150
actions.add(CommonProjectActions.customizeProjectAction());
151         
152         return (Action JavaDoc[])actions.toArray(new Action JavaDoc[actions.size()]);
153     }
154     
155     static final class LogicalViewChildren extends Children.Keys/*<FileObject>*/ implements FileChangeListener {
156         
157         // XXX does not react correctly to addition or removal of src/ subdir
158

159         private static final String JavaDoc KEY_DOC_BASE = "docBase"; //NOI18N
160
private static final String JavaDoc KEY_SETUP_DIR = "setupDir"; //NOI18N
161

162         private ArchiveProject project;
163         
164         public LogicalViewChildren(ArchiveProject project) {
165             assert project != null;
166             this.project = project;
167         }
168         
169         protected void addNotify() {
170             super.addNotify();
171             project.getProjectDirectory().addFileChangeListener(this);
172             createNodes();
173         }
174
175         private void createNodes() {
176             List JavaDoc l = new ArrayList JavaDoc();
177             DataFolder docBaseDir = getFolder((String JavaDoc)project.getArchiveProjectProperties().get(ArchiveProjectProperties.PROXY_PROJECT_DIR)); // WEB_CONF);
178
if (docBaseDir != null) {
179                 l.add(KEY_DOC_BASE);
180             }
181             l.add(KEY_SETUP_DIR);
182             
183             String JavaDoc dir = (String JavaDoc)project.getArchiveProjectProperties().get(ArchiveProjectProperties.PROXY_PROJECT_DIR);
184             FileObject fo = project.getProjectDirectory().getFileObject("subarchives"); // NOI18N
185
if (null != fo) {
186                 FileObject subarchives[] = fo.getChildren();
187                 for (FileObject innerFo : subarchives) {
188                     FileObject innerProj = null;
189                     if (null != innerFo) {
190                         innerProj = innerFo.getFileObject("tmpproj"); // NOI18N
191
}
192                     if (null != innerProj && innerProj.isFolder()) {
193                         DataFolder folder = getFolder("subarchives/"+innerFo.getName()+"/"+dir); // NOI18N
194
if (null == folder) {
195                             continue;
196                         }
197                         Node n = new ModuleNode(folder,
198                                 NbBundle.getMessage(RootNode.class,"LBL_ModuleNode", // NOI18N
199
project.getEarPath(innerFo.getName())));
200                         l.add(n);
201                     }
202                 }
203             }
204             setKeys(l);
205         }
206         
207         protected void removeNotify() {
208             setKeys(Collections.EMPTY_SET);
209             project.getProjectDirectory().removeFileChangeListener(this);
210             super.removeNotify();
211         }
212         
213         protected Node[] createNodes(Object JavaDoc key) {
214             Node n = null;
215             if (key == KEY_DOC_BASE) {
216                 n = new ConfigFilesNode(getFolder((String JavaDoc)project.getArchiveProjectProperties().get(ArchiveProjectProperties.PROXY_PROJECT_DIR)));
217             } else if (key == KEY_SETUP_DIR) {
218                 n = J2eeProjectView.createServerResourcesNode(project);
219             } else {
220                 n = (Node) key;
221             }
222             return n == null ? new Node[0] : new Node[] {n};
223         }
224         
225         private DataFolder getFolder(String JavaDoc dir) {
226             FileObject fo = project.getProjectDirectory();
227             if (null == dir) {
228                 return null;
229             }
230             if (null != fo) {
231                 fo = fo.getFileObject(dir);
232             }
233             if (null == fo) {
234                 return null;
235             }
236             
237             // check for a web app
238
fo = fo.getFileObject("web"); // NOI18N
239
if (null != fo) {
240                 fo = fo.getFileObject("WEB-INF"); // NOI18N
241
if (fo != null) {
242                     DataFolder df = DataFolder.findFolder(fo);
243                     return df;
244                 } else {
245                     return null;
246                 }
247             }
248             fo = project.getProjectDirectory().getFileObject(dir);
249             if (null == fo) {
250                 return null;
251             }
252             fo = fo.getFileObject("src"); // NOI18N
253
if (null == fo) {
254                 return null;
255             }
256             fo = fo.getFileObject("conf"); // NOI18N
257
if (null == fo) {
258                 return null;
259             }
260             return DataFolder.findFolder(fo);
261         }
262         
263         // file change events in the project directory
264
public void fileAttributeChanged(org.openide.filesystems.FileAttributeEvent fe) {
265         }
266         
267         public void fileChanged(org.openide.filesystems.FileEvent fe) {
268         }
269         
270         public void fileDataCreated(org.openide.filesystems.FileEvent fe) {
271         }
272         
273         public void fileDeleted(org.openide.filesystems.FileEvent fe) {
274             // setup folder deleted
275
//this was playing in a deadlock stack of 74613.
276
// This needs to be re-evaluated
277
// Filed as 76844
278
//createNodes();
279
}
280         
281         public void fileFolderCreated(org.openide.filesystems.FileEvent fe) {
282             // setup folder could be created
283
createNodes();
284         }
285         
286         public void fileRenamed(org.openide.filesystems.FileRenameEvent fe) {
287             // setup folder could be renamed
288
createNodes();
289         }
290     }
291     
292     
293     private static Lookup createLookup( Project project ) {
294         DataFolder rootFolder = DataFolder.findFolder( project.getProjectDirectory() );
295         // XXX Remove root folder after FindAction rewrite
296
return Lookups.fixed( new Object JavaDoc[] { project, rootFolder } );
297     }
298     
299 // private static final String[] BREAKABLE_PROPERTIES = new String[] {
300
// ArchiveProjectProperties.SOURCE_ARCHIVE,
301
// };
302

303 // public boolean hasBrokenLinks() {
304
// return BrokenReferencesSupport.isBroken(helper.getAntProjectHelper(), resolver, getBreakableProperties(),
305
// new String[] {WebProjectProperties.JAVA_PLATFORM});
306
// }
307

308 // private String[] getBreakableProperties() {
309
//// SourceRoots roots = this.project.getSourceRoots();
310
//// String[] srcRootProps = roots.getRootProperties();
311
//// roots = this.project.getTestSourceRoots();
312
//// String[] testRootProps = roots.getRootProperties();
313
// String[] result = new String [BREAKABLE_PROPERTIES.length]; // + srcRootProps.length + testRootProps.length];
314
// System.arraycopy(BREAKABLE_PROPERTIES, 0, result, 0, BREAKABLE_PROPERTIES.length);
315
//// System.arraycopy(srcRootProps, 0, result, BREAKABLE_PROPERTIES.length, srcRootProps.length);
316
//// System.arraycopy(testRootProps, 0, result, BREAKABLE_PROPERTIES.length + srcRootProps.length, testRootProps.length);
317
// return result;
318
// }
319

320 // private static Image brokenProjectBadge = Utilities.loadImage( "org/netbeans/modules/web/project/ui/resources/brokenProjectBadge.gif" ); // NOI18N
321

322     /** This action is created only when project has broken references.
323      * Once these are resolved the action is disabled.
324      */

325 // private class BrokenLinksAction extends AbstractAction implements PropertyChangeListener, ChangeListener, Runnable {
326
//
327
// private RequestProcessor.Task task = null;
328
//
329
// private PropertyChangeListener weakPCL;
330
//
331
// public BrokenLinksAction() {
332
// putValue(Action.NAME, NbBundle.getMessage(RootNode.class, "LBL_Fix_Broken_Links_Action"));
333
// setEnabled(broken);
334
//// evaluator.addPropertyChangeListener( this );
335
// // When evaluator fires changes that platform properties were
336
// // removed the platform still exists in JavaPlatformManager.
337
// // That's why I have to listen here also on JPM:
338
// //weakPCL = WeakListeners.propertyChange( this, JavaPlatformManager.getDefault() );
339
// //JavaPlatformManager.getDefault().addPropertyChangeListener( weakPCL );
340
// RootNode.this.addChangeListener((ChangeListener)WeakListeners.change(this, RootNode.this));
341
// }
342
//
343
// public void actionPerformed(ActionEvent e) {
344
// try {
345
// helper.requestSave();
346
// BrokenReferencesSupport.showCustomizer(helper.getAntProjectHelper(), resolver, getBreakableProperties(), new String[]{WebProjectProperties.JAVA_PLATFORM});
347
// run();
348
// } catch (IOException ioe) {
349
// ErrorManager.getDefault().notify(ioe);
350
// }
351
// }
352
//
353
// public void propertyChange(PropertyChangeEvent evt) {
354
// refsMayChanged();
355
// }
356
//
357
// public void stateChanged(ChangeEvent evt) {
358
// refsMayChanged();
359
// }
360
//
361
// public synchronized void run() {
362
// boolean old = broken;
363
// broken = hasBrokenLinks();
364
// if (old != broken) {
365
// setEnabled(broken);
366
// fireIconChange();
367
// fireOpenedIconChange();
368
// fireDisplayNameChange(null, null);
369
// //project.getWebProjectProperties().save();
370
// }
371
// }
372
//
373
// public void refsMayChanged() {
374
// // check project state whenever there was a property change
375
// // or change in list of platforms.
376
// // Coalesce changes since they can come quickly:
377
// if (task == null) {
378
// task = BROKEN_LINKS_RP.create(this);
379
// }
380
// task.schedule(100);
381
// }
382
//
383
// }
384
//
385
// private class BrokenServerAction extends AbstractAction implements
386
// InstanceListener, PropertyChangeListener {
387
//
388
// private boolean brokenServer;
389
//
390
// public BrokenServerAction() {
391
// putValue(Action.NAME, NbBundle.getMessage(RootNode.class, "LBL_Fix_Missing_Server_Action")); // NOI18N
392
// evaluator.addPropertyChangeListener(this);
393
// checkMissingServer();
394
// }
395
//
396
// public boolean isEnabled() {
397
// return brokenServer;
398
// }
399
//
400
// public void actionPerformed(ActionEvent e) {
401
// BrokenServerSupport.showCustomizer(project, helper);
402
// checkMissingServer();
403
// }
404
//
405
// public void propertyChange(PropertyChangeEvent evt) {
406
// if (ArchiveProjectProperties.J2EE_SERVER_INSTANCE.equals(evt.getPropertyName())) {
407
// checkMissingServer();
408
// }
409
// }
410
//
411
// public void changeDefaultInstance(String oldServerInstanceID, String newServerInstanceID) {
412
// }
413
//
414
// public void instanceAdded(String serverInstanceID) {
415
// checkMissingServer();
416
// }
417
//
418
// public void instanceRemoved(String serverInstanceID) {
419
// checkMissingServer();
420
// }
421
//
422
// private void checkMissingServer() {
423
// boolean old = brokenServer;
424
// String servInstID = evaluator.getProperty(ArchiveProjectProperties.J2EE_SERVER_INSTANCE);
425
// brokenServer = BrokenServerSupport.isBroken(servInstID);
426
// if (old != brokenServer) {
427
// fireIconChange();
428
// fireOpenedIconChange();
429
// fireDisplayNameChange(null, null);
430
// }
431
// }
432
// }
433

434 }
435
Popular Tags