1 19 20 package org.openide.awt; 21 22 23 import java.awt.event.*; 24 import java.beans.*; 25 import java.lang.reflect.Method ; 26 import javax.swing.*; 27 import org.openide.nodes.Node; 28 import org.openide.util.Exceptions; 29 import org.openide.util.Lookup; 30 31 36 class ExecBridge extends Object implements ActionListener, PropertyChangeListener { 37 38 private Node node; 39 40 private AbstractButton button; 41 42 private static Class execCookieClass = null; 43 private static synchronized Class getExecCookieClass() { 44 if (execCookieClass == null) { 45 try { 46 execCookieClass = Class.forName("org.openide.cookies.ExecCookie", true, Lookup.getDefault().lookup(ClassLoader .class)); } catch (ClassNotFoundException cnfe) { 48 execCookieClass = ExecBridge.class; 49 } 50 } 51 if (execCookieClass == ExecBridge.class) { 52 return null; 53 } else { 54 return execCookieClass; 55 } 56 } 57 58 59 private ExecBridge (Node node, AbstractButton button) { 60 this.node = node; 61 this.button = button; 62 63 button.addActionListener (this); 64 node.addPropertyChangeListener (org.openide.util.WeakListeners.propertyChange (this, node)); 65 66 updateState (); 67 } 68 69 71 public void actionPerformed (ActionEvent ev) { 72 Class c = getExecCookieClass(); 73 if (c == null) { 74 return; 75 } 76 Node.Cookie ec = node.getCookie(c); 77 if (ec != null) { 78 try { 79 Method m = getExecCookieClass().getMethod("start"); 80 m.invoke(ec); 81 } catch (Exception e) { 82 Exceptions.printStackTrace(e); 83 } 84 } 85 } 86 87 89 public void propertyChange (PropertyChangeEvent ev) { 90 if (Node.PROP_COOKIE.equals (ev.getPropertyName ())) { 91 updateState (); 92 } 93 if (Node.PROP_DISPLAY_NAME.equals (ev.getPropertyName ())) { 94 updateState (); 95 } 96 if (Node.PROP_ICON.equals (ev.getPropertyName ())) { 97 updateState (); 98 } 99 } 100 101 103 private void updateState () { 104 button.setText (node.getDisplayName ()); 105 Icon icon = new ImageIcon (node.getIcon (BeanInfo.ICON_COLOR_16x16)); 106 button.setIcon (icon); 107 108 Class c = getExecCookieClass(); 109 button.setEnabled(c != null && node.getCookie(c) != null); 110 } 111 112 115 public static JMenuItem createMenuItem (org.openide.loaders.DataObject obj) { 116 if (!obj.isValid()) return null; 117 Node n = obj.getNodeDelegate (); 118 Class c = getExecCookieClass(); 119 if (c == null || n.getCookie(c) == null) return null; 120 JMenuItem item = new JMenuItem (); 121 new ExecBridge (n, item); 122 return item; 123 } 124 125 128 public static JButton createButton (org.openide.loaders.DataObject obj) { 129 if (!obj.isValid()) return null; 130 Node n = obj.getNodeDelegate (); 131 Class c = getExecCookieClass(); 132 if (c == null || n.getCookie(c) == null) return null; 133 JButton item = new JButton (); 134 new ExecBridge (n, item); 135 return item; 136 } 137 138 } 139 | Popular Tags |