KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > nodes > TMUtil


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.openide.nodes;
21
22 import java.util.Hashtable JavaDoc;
23 import org.openide.util.Mutex;
24
25 /** Class that serves as interface to various parts of OpenAPIs that need
26  * not be present.
27  *
28  * @author Jaroslav Tulach
29  */

30 abstract class TMUtil extends Object JavaDoc {
31     /** variable that will contain the argument to a call and then a result.
32      */

33     private static final ThreadLocal JavaDoc<Object JavaDoc> TALK = new ThreadLocal JavaDoc<Object JavaDoc>();
34
35     /** maps names of algorithms (that use the ARGUMENT and the RESULT)
36      * and runnables that has to be executed to compute the algorithm
37      * (String, Runnable) or (String, Exception)
38      */

39     private static Hashtable JavaDoc<String JavaDoc, Object JavaDoc> algorithms = new Hashtable JavaDoc<String JavaDoc, Object JavaDoc>(10);
40     private static java.awt.Frame JavaDoc owner;
41
42     /** Dynamically loads a class.
43      * @param className name of the class
44      * @return the class
45      */

46     private static Class JavaDoc loadClass(String JavaDoc className) throws Exception JavaDoc {
47         ClassLoader JavaDoc loader = org.openide.util.Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
48
49         if (loader == null) {
50             loader = NodeOperation.class.getClassLoader();
51         }
52
53         return Class.forName(className, true, loader);
54     }
55
56     /** Creates InstanceCookie, if available.
57      * @param bean the object to create cookie for
58      * @return Node.Cookie or null
59      */

60     static Node.Cookie createInstanceCookie(Object JavaDoc bean) {
61         try {
62             TALK.set(bean);
63
64             return exec("Bean") ? (Node.Cookie) TALK.get() : null; // NOI18N
65
} finally {
66             TALK.set(null);
67         }
68     }
69
70     /** Checks whether an object is instance of DialogDescriptor and if
71      * so it used top manager to create its instance.
72      * @param maybeDialogDescriptor an object
73      * @return a dialog or null
74      */

75     static java.awt.Dialog JavaDoc createDialog(Object JavaDoc maybeDialogDescriptor) {
76         try {
77             TALK.set(maybeDialogDescriptor);
78
79             return exec("Dial") ? (java.awt.Dialog JavaDoc) TALK.get() : null; // NOI18N
80
} finally {
81             TALK.set(null);
82         }
83     }
84
85     /** Attaches a customizer to given node.
86      * @param node the bean node
87      * @param cust customizer to attach
88      */

89     static void attachCustomizer(Node node, java.beans.Customizer JavaDoc cust) {
90         try {
91             TALK.set(new Object JavaDoc[] { node, cust });
92             exec("Cust"); // NOI18N
93
} finally {
94             TALK.set(null);
95         }
96     }
97
98     /** Finds main window.
99      * @return main window or null
100      */

101     static java.awt.Frame JavaDoc mainWindow() {
102         try {
103             if (exec("Win")) { // NOI18N
104

105                 return (java.awt.Frame JavaDoc) TALK.get();
106             } else {
107                 // default owner for JDialog
108
if (owner == null) {
109                     owner = (java.awt.Frame JavaDoc) new javax.swing.JDialog JavaDoc().getOwner();
110                 }
111
112                 return owner;
113             }
114         } finally {
115             TALK.set(null);
116         }
117     }
118
119     /** Finds usable list cell renderer.
120      */

121     static javax.swing.ListCellRenderer JavaDoc findListCellRenderer() {
122         try {
123             if (exec("Rend")) { // NOI18N
124

125                 return (javax.swing.ListCellRenderer JavaDoc) TALK.get();
126             } else {
127                 return new javax.swing.DefaultListCellRenderer JavaDoc();
128             }
129         } finally {
130             TALK.set(null);
131         }
132     }
133
134     /** Invoke an indexed customizer. */
135     static void showIndexedCustomizer(Index idx) {
136         try {
137             TALK.set(idx);
138
139             if (!exec("IndexC")) { // NOI18N
140

141                 // Fallback to simple method.
142
final IndexedCustomizer ic = new IndexedCustomizer();
143                 ic.setObject(idx);
144                 ic.setImmediateReorder(false);
145                 Mutex.EVENT.readAccess(
146                     new Mutex.Action<Void JavaDoc>() {
147                         public Void JavaDoc run() {
148                             ic.setVisible(true);
149
150                             return null;
151                         }
152                     }
153                 );
154             }
155         } finally {
156             TALK.set(null);
157         }
158     }
159
160     /** Executes algorithm of given name.
161      * @param name the name of algorithm
162      * @return true iff successfule
163      */

164     private static boolean exec(String JavaDoc name) {
165         Object JavaDoc obj = algorithms.get(name);
166
167         if (obj == null) {
168             try {
169                 Class JavaDoc c = Class.forName("org.openide.nodes.TMUtil$" + name); // NOI18N
170
obj = c.newInstance();
171             } catch (ClassNotFoundException JavaDoc ex) {
172                 obj = ex;
173                 NodeOp.exception(ex);
174             } catch (InstantiationException JavaDoc ex) {
175                 // that is ok, we should not be able to create an
176
// instance if some classes are missing
177
obj = ex;
178             } catch (IllegalAccessException JavaDoc ex) {
179                 obj = ex;
180                 NodeOp.exception(ex);
181             } catch (NoClassDefFoundError JavaDoc ex) {
182                 // that is ok, some classes need not be found
183
obj = ex;
184             }
185
186             algorithms.put(name, obj);
187         }
188
189         try {
190             if (obj instanceof Runnable JavaDoc) {
191                 ((Runnable JavaDoc) obj).run();
192
193                 return true;
194             }
195         } catch (NoClassDefFoundError JavaDoc ex) {
196             // in case of late linking the error can be thrown
197
// just when the runnable is executed
198
algorithms.put(name, ex);
199         }
200
201         return false;
202     }
203
204     /** Creates instance of InstanceCookie for given object.
205      * ARGUMENT contains the bean to create instance for.
206      */

207     static final class Bean implements Runnable JavaDoc, org.openide.cookies.InstanceCookie {
208         private Object JavaDoc bean;
209
210         public void run() {
211             Bean n = new Bean();
212             n.bean = TALK.get();
213             TALK.set(n);
214         }
215
216         public String JavaDoc instanceName() {
217             return bean.getClass().getName();
218         }
219
220         public Class JavaDoc instanceClass() {
221             return bean.getClass();
222         }
223
224         public Object JavaDoc instanceCreate() {
225             return bean;
226         }
227     }
228
229     /** Creates dialog from DialogDescriptor
230      * ARGUMENT contains the descriptor.
231      */

232     static final class Dial implements Runnable JavaDoc {
233         public void run() {
234             Object JavaDoc obj = TALK.get();
235
236             if (obj instanceof org.openide.DialogDescriptor) {
237                 TALK.set(org.openide.DialogDisplayer.getDefault().createDialog((org.openide.DialogDescriptor) obj));
238             } else {
239                 TALK.set(null);
240             }
241         }
242     }
243
244     /** Attaches the node to a customizer if it implements NodeCustomizer.
245      * ARGUMENT contains array of node and customizer
246      */

247     static final class Cust implements Runnable JavaDoc {
248         private static Class JavaDoc<?> nodeCustomizer;
249         private static java.lang.reflect.Method JavaDoc attach;
250
251         public void run() {
252             try {
253                 if (nodeCustomizer == null) {
254                     // load method
255
nodeCustomizer = loadClass("org.openide.explorer.propertysheet.editors.NodeCustomizer"); // NOI18N
256
attach = nodeCustomizer.getMethod("attach", Node.class); // NOI18N
257
}
258
259                 Object JavaDoc[] arr = (Object JavaDoc[]) TALK.get();
260
261                 Node n = (Node) arr[0];
262                 Object JavaDoc cust = arr[1];
263
264                 if (nodeCustomizer.isInstance(cust)) {
265                     // ((org.openide.explorer.propertysheet.editors.NodeCustomizer)cust).attach (n);
266
attach.invoke(cust, new Object JavaDoc[] { n });
267                 }
268             } catch (Exception JavaDoc ex) {
269                 throw new IllegalStateException JavaDoc(ex.getMessage());
270             }
271         }
272     }
273
274     /** Finds the main window.
275      */

276     static final class Win implements Runnable JavaDoc {
277         private static java.lang.reflect.Method JavaDoc getDefault;
278         private static java.lang.reflect.Method JavaDoc getMainWindow;
279
280         public void run() {
281             try {
282                 if (getDefault == null) {
283                     // load all methods
284
Class JavaDoc<?> wm = loadClass("org.openide.windows.WindowManager"); // NOI18N
285
getDefault = wm.getMethod("getDefault"); // NOI18N
286
getMainWindow = wm.getMethod("getMainWindow"); // NOI18N
287
}
288
289                 //
290
// call: WindowManager.getDefault().getMainWindow ()
291
//
292
Object JavaDoc[] param = new Object JavaDoc[0];
293                 TALK.set(getMainWindow.invoke(getDefault.invoke(null, param), param));
294             } catch (Exception JavaDoc ex) {
295                 throw new IllegalStateException JavaDoc(ex.getMessage());
296             }
297         }
298     }
299
300     /** Finds renderer.
301      */

302     static final class Rend implements Runnable JavaDoc {
303         private static Class JavaDoc nodeRenderer;
304
305         public void run() {
306             try {
307                 if (nodeRenderer == null) {
308                     nodeRenderer = loadClass("org.openide.explorer.view.NodeRenderer"); // NOI18N
309
}
310
311                 TALK.set(nodeRenderer.newInstance());
312             } catch (Exception JavaDoc ex) {
313                 throw new IllegalStateException JavaDoc(ex.getMessage());
314             }
315         }
316     }
317
318     static final class IndexC implements Runnable JavaDoc {
319         public void run() {
320             Index idx = (Index) TALK.get();
321             java.awt.Container JavaDoc p = new javax.swing.JPanel JavaDoc();
322             IndexedCustomizer ic = new IndexedCustomizer(p, false);
323             ic.setObject(idx);
324             ic.setImmediateReorder(false);
325
326             org.openide.DialogDescriptor dd = new org.openide.DialogDescriptor(p, Node.getString("LAB_order"));
327             dd.setModal(true);
328             dd.setOptionType(org.openide.DialogDescriptor.DEFAULT_OPTION);
329
330             Object JavaDoc result = org.openide.DialogDisplayer.getDefault().notify(dd);
331
332             if (result == org.openide.DialogDescriptor.OK_OPTION) {
333                 ic.doClose();
334             }
335         }
336     }
337 }
338
Popular Tags