1 19 20 package org.netbeans.core.ui.warmup; 21 22 import java.lang.reflect.*; 23 24 import java.awt.Component ; 25 import java.awt.Frame ; 26 import java.awt.event.WindowAdapter ; 27 import java.awt.event.WindowEvent ; 28 import java.io.File ; 29 import java.util.Set ; 30 import java.util.LinkedHashSet ; 31 import javax.swing.JFrame ; 32 import javax.swing.JMenu ; 33 import javax.swing.SwingUtilities ; 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 47 public final class MenuWarmUpTask implements Runnable { 48 49 private Component [] comps; 50 51 53 public void run() { 54 try { 55 SwingUtilities.invokeAndWait(new Runnable () { 56 public void run() { 57 Frame main = WindowManager.getDefault().getMainWindow(); 58 59 assert main != null; 60 main.addWindowListener(new NbWindowsAdapter()); 61 62 if (main instanceof JFrame ) { 63 comps = ((JFrame ) main).getJMenuBar().getComponents(); 64 } 65 } 66 }); 67 } catch (Exception e) { return; 69 } 70 71 72 if (comps != null) { 73 walkMenu(comps); 74 comps = null; 75 } 76 77 } 79 80 private void walkMenu(Component [] items) { 81 for (int i=0; i<items.length; i++) { 82 if (! (items[i] instanceof JMenu )) continue; 83 try { 84 Class cls = items[i].getClass(); 85 Method m = cls.getDeclaredMethod("doInitialize"); 86 m.setAccessible(true); 87 m.invoke(items[i]); 88 walkMenu(((JMenu )items[i]).getMenuComponents()); } catch (Exception e) { } 91 } 92 } 93 94 97 private static class NbWindowsAdapter extends WindowAdapter implements Runnable { 98 private static final RequestProcessor rp = new RequestProcessor ("Refresh-After-WindowActivated"); private RequestProcessor.Task task = null; 100 101 public void windowActivated(WindowEvent 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 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 139 private static FileSystem[] fileSystems; 140 141 private static FileSystem[] getFileSystems() { 142 if (fileSystems != null) { 143 return fileSystems; 144 } 145 File [] roots = File.listRoots(); 146 Set <FileSystem> allRoots = new LinkedHashSet <FileSystem>(); 147 assert roots != null && roots.length > 0 : "Could not list file roots"; 149 for (int i = 0; i < roots.length; i++) { 150 File 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 163 164 if (fs != null) { 165 break; 166 } 167 } catch (FileStateInvalidException e) { 168 throw new AssertionError (e); 169 } 170 } 171 FileSystem[] retVal = new FileSystem [allRoots.size()]; 172 allRoots.toArray(retVal); 173 175 return fileSystems = retVal; 176 } 177 178 } 179 180 } 181 | Popular Tags |