KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > ui > warmup > MenuWarmUpTask


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.core.ui.warmup;
21
22 import java.lang.reflect.*;
23
24 import java.awt.Component JavaDoc;
25 import java.awt.Frame JavaDoc;
26 import java.awt.event.WindowAdapter JavaDoc;
27 import java.awt.event.WindowEvent JavaDoc;
28 import java.io.File JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.LinkedHashSet JavaDoc;
31 import javax.swing.JFrame JavaDoc;
32 import javax.swing.JMenu JavaDoc;
33 import javax.swing.SwingUtilities JavaDoc;
34 import org.openide.windows.WindowManager;
35 import org.openide.util.RequestProcessor;
36 import org.openide.filesystems.FileSystem;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.FileUtil;
39 import org.openide.filesystems.FileStateInvalidException;
40
41 /**
42  * A menu preheating task. It is referenced from the layer and may be performed
43  * by the core after the startup.
44  *
45  * Plus hooked WindowListener on main window (see {@link NbWindowsAdapter})
46  */

47 public final class MenuWarmUpTask implements Runnable JavaDoc {
48
49     private Component JavaDoc[] comps;
50     
51     /** Actually performs pre-heat.
52      */

53     public void run() {
54         try {
55             SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
56                 public void run() {
57                     Frame JavaDoc main = WindowManager.getDefault().getMainWindow();
58                     
59                     assert main != null;
60                     main.addWindowListener(new NbWindowsAdapter());
61                     
62                     if (main instanceof JFrame JavaDoc) {
63                         comps = ((JFrame JavaDoc) main).getJMenuBar().getComponents();
64                     }
65                 }
66             });
67         } catch (Exception JavaDoc e) { // bail out!
68
return;
69         }
70
71
72         if (comps != null) {
73             walkMenu(comps);
74             comps = null;
75         }
76
77         // tackle the Tools menu now? How?
78
}
79
80     private void walkMenu(Component JavaDoc[] items) {
81         for (int i=0; i<items.length; i++) {
82             if (! (items[i] instanceof JMenu JavaDoc)) continue;
83             try {
84                 Class JavaDoc cls = items[i].getClass();
85                 Method m = cls.getDeclaredMethod("doInitialize");
86                 m.setAccessible(true);
87                 m.invoke(items[i]);
88                 walkMenu(((JMenu JavaDoc)items[i]).getMenuComponents()); // recursive?
89
} catch (Exception JavaDoc e) {// do nothing, it may happen for user-provided menus
90
}
91         }
92     }
93
94     /**
95      * After activation of window is refreshed filesystem
96      */

97     private static class NbWindowsAdapter extends WindowAdapter JavaDoc implements Runnable JavaDoc{
98         private static final RequestProcessor rp = new RequestProcessor ("Refresh-After-WindowActivated");//NOI18N
99
private RequestProcessor.Task task = null;
100         
101         public void windowActivated(WindowEvent JavaDoc e) {
102             synchronized (rp) {
103                 if (task != null) {
104                     task.cancel();
105                 } else {
106                     task = rp.create(this);
107                 }
108                 task.schedule(1500);
109             }
110         }
111
112         public void windowDeactivated(WindowEvent JavaDoc e) {
113             synchronized (rp) {
114                 if (task != null) {
115                     task.cancel();
116                 }
117             }
118         }
119
120         public void run() {
121             FileSystem[] all = getFileSystems();
122             for (int i = 0; i < all.length; i++) {
123                 FileSystem fileSystem = all[i];
124                 fileSystem.refresh(false);
125             }
126             
127             synchronized (rp) {
128                 task = null;
129             }
130         }
131         
132         //copy - paste programming
133
//http://ant.netbeans.org/source/browse/ant/src-bridge/org/apache/tools/ant/module/bridge/impl/BridgeImpl.java.diff?r1=1.15&r2=1.16
134
//http:/java.netbeans.org/source/browse/java/javacore/src/org/netbeans/modules/javacore/Util.java
135
//http://core.netbeans.org/source/browse/core/ui/src/org/netbeans/core/ui/MenuWarmUpTask.java
136
//http://core.netbeans.org/source/browse/core/src/org/netbeans/core/actions/RefreshAllFilesystemsAction.java
137
//http://java.netbeans.org/source/browse/java/api/src/org/netbeans/api/java/classpath/ClassPath.java
138

139         private static FileSystem[] fileSystems;
140         
141         private static FileSystem[] getFileSystems() {
142             if (fileSystems != null) {
143                 return fileSystems;
144             }
145             File JavaDoc[] roots = File.listRoots();
146             Set JavaDoc<FileSystem> allRoots = new LinkedHashSet JavaDoc<FileSystem>();
147             assert roots != null && roots.length > 0 : "Could not list file roots"; // NOI18N
148

149             for (int i = 0; i < roots.length; i++) {
150                 File JavaDoc root = roots[i];
151                 FileObject random = FileUtil.toFileObject(root);
152                 if (random == null) continue;
153                 
154                 FileSystem fs;
155                 try {
156                     fs = random.getFileSystem();
157                     allRoots.add(fs);
158                     
159                     /*Because there is MasterFileSystem impl. that provides conversion to FileObject for all File.listRoots
160                     (except floppy drives and empty CD). Then there is useless to convert all roots into FileObjects including
161                     net drives that might cause performance regression.
162                     */

163                     
164                     if (fs != null) {
165                         break;
166                     }
167                 } catch (FileStateInvalidException e) {
168                     throw new AssertionError JavaDoc(e);
169                 }
170             }
171             FileSystem[] retVal = new FileSystem [allRoots.size()];
172             allRoots.toArray(retVal);
173 // assert retVal.length > 0 : "Could not get any filesystem"; // NOI18N
174

175             return fileSystems = retVal;
176         }
177         
178     }
179     
180 }
181
Popular Tags