KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > awt > ExecBridge


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.awt;
21
22
23 import java.awt.event.*;
24 import java.beans.*;
25 import java.lang.reflect.Method JavaDoc;
26 import javax.swing.*;
27 import org.openide.nodes.Node;
28 import org.openide.util.Exceptions;
29 import org.openide.util.Lookup;
30
31 /** Menu item associated with data object. When
32 * pressed it executes the data object.
33 *
34 * @author Jaroslav Tulach
35 */

36 class ExecBridge extends Object JavaDoc implements ActionListener, PropertyChangeListener {
37     /** object to execute */
38     private Node node;
39     /** associated button */
40     private AbstractButton button;
41
42     private static Class JavaDoc execCookieClass = null;
43     private static synchronized Class JavaDoc getExecCookieClass() {
44         if (execCookieClass == null) {
45             try {
46                 execCookieClass = Class.forName("org.openide.cookies.ExecCookie", true, Lookup.getDefault().lookup(ClassLoader JavaDoc.class)); // NOI18N
47
} catch (ClassNotFoundException JavaDoc cnfe) {
48                 execCookieClass = ExecBridge.class;
49             }
50         }
51         if (execCookieClass == ExecBridge.class) {
52             return null;
53         } else {
54             return execCookieClass;
55         }
56     }
57
58     /** Creates new ExecMenuItem */
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     /** Executes the object.
70     */

71     public void actionPerformed (ActionEvent ev) {
72         Class JavaDoc c = getExecCookieClass();
73         if (c == null) {
74             return;
75         }
76         Node.Cookie ec = node.getCookie(c);
77         if (ec != null) {
78             try {
79                 Method JavaDoc m = getExecCookieClass().getMethod("start");
80                 m.invoke(ec);
81             } catch (Exception JavaDoc e) {
82                 Exceptions.printStackTrace(e);
83             }
84         }
85     }
86
87     /** Listen to changes of exec cookie on the data object.
88     */

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     /** Updates state
102     */

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 JavaDoc c = getExecCookieClass();
109         button.setEnabled(c != null && node.getCookie(c) != null);
110     }
111
112     /** Creates menu item associated with the data object.
113      * May be null if there is no ExecCookie.
114     */

115     public static JMenuItem createMenuItem (org.openide.loaders.DataObject obj) {
116         if (!obj.isValid()) return null;
117         Node n = obj.getNodeDelegate ();
118         Class JavaDoc 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     /** Creates toolbar component associated with the object.
126      * May be null if there is no ExecCookie.
127     */

128     public static JButton createButton (org.openide.loaders.DataObject obj) {
129         if (!obj.isValid()) return null;
130         Node n = obj.getNodeDelegate ();
131         Class JavaDoc 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