1 19 20 package org.openide.nodes; 21 22 import java.util.Hashtable ; 23 import org.openide.util.Mutex; 24 25 30 abstract class TMUtil extends Object { 31 33 private static final ThreadLocal <Object > TALK = new ThreadLocal <Object >(); 34 35 39 private static Hashtable <String , Object > algorithms = new Hashtable <String , Object >(10); 40 private static java.awt.Frame owner; 41 42 46 private static Class loadClass(String className) throws Exception { 47 ClassLoader loader = org.openide.util.Lookup.getDefault().lookup(ClassLoader .class); 48 49 if (loader == null) { 50 loader = NodeOperation.class.getClassLoader(); 51 } 52 53 return Class.forName(className, true, loader); 54 } 55 56 60 static Node.Cookie createInstanceCookie(Object bean) { 61 try { 62 TALK.set(bean); 63 64 return exec("Bean") ? (Node.Cookie) TALK.get() : null; } finally { 66 TALK.set(null); 67 } 68 } 69 70 75 static java.awt.Dialog createDialog(Object maybeDialogDescriptor) { 76 try { 77 TALK.set(maybeDialogDescriptor); 78 79 return exec("Dial") ? (java.awt.Dialog ) TALK.get() : null; } finally { 81 TALK.set(null); 82 } 83 } 84 85 89 static void attachCustomizer(Node node, java.beans.Customizer cust) { 90 try { 91 TALK.set(new Object [] { node, cust }); 92 exec("Cust"); } finally { 94 TALK.set(null); 95 } 96 } 97 98 101 static java.awt.Frame mainWindow() { 102 try { 103 if (exec("Win")) { 105 return (java.awt.Frame ) TALK.get(); 106 } else { 107 if (owner == null) { 109 owner = (java.awt.Frame ) new javax.swing.JDialog ().getOwner(); 110 } 111 112 return owner; 113 } 114 } finally { 115 TALK.set(null); 116 } 117 } 118 119 121 static javax.swing.ListCellRenderer findListCellRenderer() { 122 try { 123 if (exec("Rend")) { 125 return (javax.swing.ListCellRenderer ) TALK.get(); 126 } else { 127 return new javax.swing.DefaultListCellRenderer (); 128 } 129 } finally { 130 TALK.set(null); 131 } 132 } 133 134 135 static void showIndexedCustomizer(Index idx) { 136 try { 137 TALK.set(idx); 138 139 if (!exec("IndexC")) { 141 final IndexedCustomizer ic = new IndexedCustomizer(); 143 ic.setObject(idx); 144 ic.setImmediateReorder(false); 145 Mutex.EVENT.readAccess( 146 new Mutex.Action<Void >() { 147 public Void run() { 148 ic.setVisible(true); 149 150 return null; 151 } 152 } 153 ); 154 } 155 } finally { 156 TALK.set(null); 157 } 158 } 159 160 164 private static boolean exec(String name) { 165 Object obj = algorithms.get(name); 166 167 if (obj == null) { 168 try { 169 Class c = Class.forName("org.openide.nodes.TMUtil$" + name); obj = c.newInstance(); 171 } catch (ClassNotFoundException ex) { 172 obj = ex; 173 NodeOp.exception(ex); 174 } catch (InstantiationException ex) { 175 obj = ex; 178 } catch (IllegalAccessException ex) { 179 obj = ex; 180 NodeOp.exception(ex); 181 } catch (NoClassDefFoundError ex) { 182 obj = ex; 184 } 185 186 algorithms.put(name, obj); 187 } 188 189 try { 190 if (obj instanceof Runnable ) { 191 ((Runnable ) obj).run(); 192 193 return true; 194 } 195 } catch (NoClassDefFoundError ex) { 196 algorithms.put(name, ex); 199 } 200 201 return false; 202 } 203 204 207 static final class Bean implements Runnable , org.openide.cookies.InstanceCookie { 208 private Object 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 instanceName() { 217 return bean.getClass().getName(); 218 } 219 220 public Class instanceClass() { 221 return bean.getClass(); 222 } 223 224 public Object instanceCreate() { 225 return bean; 226 } 227 } 228 229 232 static final class Dial implements Runnable { 233 public void run() { 234 Object 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 247 static final class Cust implements Runnable { 248 private static Class <?> nodeCustomizer; 249 private static java.lang.reflect.Method attach; 250 251 public void run() { 252 try { 253 if (nodeCustomizer == null) { 254 nodeCustomizer = loadClass("org.openide.explorer.propertysheet.editors.NodeCustomizer"); attach = nodeCustomizer.getMethod("attach", Node.class); } 258 259 Object [] arr = (Object []) TALK.get(); 260 261 Node n = (Node) arr[0]; 262 Object cust = arr[1]; 263 264 if (nodeCustomizer.isInstance(cust)) { 265 attach.invoke(cust, new Object [] { n }); 267 } 268 } catch (Exception ex) { 269 throw new IllegalStateException (ex.getMessage()); 270 } 271 } 272 } 273 274 276 static final class Win implements Runnable { 277 private static java.lang.reflect.Method getDefault; 278 private static java.lang.reflect.Method getMainWindow; 279 280 public void run() { 281 try { 282 if (getDefault == null) { 283 Class <?> wm = loadClass("org.openide.windows.WindowManager"); getDefault = wm.getMethod("getDefault"); getMainWindow = wm.getMethod("getMainWindow"); } 288 289 Object [] param = new Object [0]; 293 TALK.set(getMainWindow.invoke(getDefault.invoke(null, param), param)); 294 } catch (Exception ex) { 295 throw new IllegalStateException (ex.getMessage()); 296 } 297 } 298 } 299 300 302 static final class Rend implements Runnable { 303 private static Class nodeRenderer; 304 305 public void run() { 306 try { 307 if (nodeRenderer == null) { 308 nodeRenderer = loadClass("org.openide.explorer.view.NodeRenderer"); } 310 311 TALK.set(nodeRenderer.newInstance()); 312 } catch (Exception ex) { 313 throw new IllegalStateException (ex.getMessage()); 314 } 315 } 316 } 317 318 static final class IndexC implements Runnable { 319 public void run() { 320 Index idx = (Index) TALK.get(); 321 java.awt.Container p = new javax.swing.JPanel (); 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 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 |