001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.core.ui.warmup;
043:
044: import java.lang.reflect.*;
045:
046: import java.awt.Component;
047: import java.awt.Frame;
048: import java.awt.event.WindowAdapter;
049: import java.awt.event.WindowEvent;
050: import java.io.File;
051: import java.util.Set;
052: import java.util.LinkedHashSet;
053: import javax.swing.JFrame;
054: import javax.swing.JMenu;
055: import javax.swing.SwingUtilities;
056: import org.openide.windows.WindowManager;
057: import org.openide.util.RequestProcessor;
058: import org.openide.filesystems.FileSystem;
059: import org.openide.filesystems.FileObject;
060: import org.openide.filesystems.FileUtil;
061: import org.openide.filesystems.FileStateInvalidException;
062:
063: /**
064: * A menu preheating task. It is referenced from the layer and may be performed
065: * by the core after the startup.
066: *
067: * Plus hooked WindowListener on main window (see {@link NbWindowsAdapter})
068: */
069: public final class MenuWarmUpTask implements Runnable {
070:
071: private Component[] comps;
072:
073: /** Actually performs pre-heat.
074: */
075: public void run() {
076: try {
077: SwingUtilities.invokeAndWait(new Runnable() {
078: public void run() {
079: Frame main = WindowManager.getDefault()
080: .getMainWindow();
081:
082: assert main != null;
083: main.addWindowListener(new NbWindowsAdapter());
084:
085: if (main instanceof JFrame) {
086: comps = ((JFrame) main).getJMenuBar()
087: .getComponents();
088: }
089: }
090: });
091: } catch (Exception e) { // bail out!
092: return;
093: }
094:
095: if (comps != null) {
096: walkMenu(comps);
097: comps = null;
098: }
099:
100: // tackle the Tools menu now? How?
101: }
102:
103: private void walkMenu(Component[] items) {
104: for (int i = 0; i < items.length; i++) {
105: if (!(items[i] instanceof JMenu))
106: continue;
107: try {
108: Class cls = items[i].getClass();
109: Method m = cls.getDeclaredMethod("doInitialize");
110: m.setAccessible(true);
111: m.invoke(items[i]);
112: walkMenu(((JMenu) items[i]).getMenuComponents()); // recursive?
113: } catch (Exception e) {// do nothing, it may happen for user-provided menus
114: }
115: }
116: }
117:
118: /**
119: * After activation of window is refreshed filesystem
120: */
121: private static class NbWindowsAdapter extends WindowAdapter
122: implements Runnable {
123: private static final RequestProcessor rp = new RequestProcessor(
124: "Refresh-After-WindowActivated");//NOI18N
125: private RequestProcessor.Task task = null;
126:
127: public void windowActivated(WindowEvent e) {
128: synchronized (rp) {
129: if (task != null) {
130: task.cancel();
131: } else {
132: task = rp.create(this );
133: }
134: task.schedule(1500);
135: }
136: }
137:
138: public void windowDeactivated(WindowEvent e) {
139: synchronized (rp) {
140: if (task != null) {
141: task.cancel();
142: }
143: }
144: }
145:
146: public void run() {
147: FileUtil.refreshAll();
148: synchronized (rp) {
149: task = null;
150: }
151: }
152: }
153:
154: }
|