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.openide.awt;
043:
044: import java.awt.event.*;
045: import java.beans.*;
046: import java.lang.reflect.Method;
047: import javax.swing.*;
048: import org.openide.nodes.Node;
049: import org.openide.util.Exceptions;
050: import org.openide.util.Lookup;
051:
052: /** Menu item associated with data object. When
053: * pressed it executes the data object.
054: *
055: * @author Jaroslav Tulach
056: */
057: class ExecBridge extends Object implements ActionListener,
058: PropertyChangeListener {
059: /** object to execute */
060: private Node node;
061: /** associated button */
062: private AbstractButton button;
063:
064: private static Class execCookieClass = null;
065:
066: private static synchronized Class getExecCookieClass() {
067: if (execCookieClass == null) {
068: try {
069: execCookieClass = Class
070: .forName("org.openide.cookies.ExecCookie",
071: true, Lookup.getDefault().lookup(
072: ClassLoader.class)); // NOI18N
073: } catch (ClassNotFoundException cnfe) {
074: execCookieClass = ExecBridge.class;
075: }
076: }
077: if (execCookieClass == ExecBridge.class) {
078: return null;
079: } else {
080: return execCookieClass;
081: }
082: }
083:
084: /** Creates new ExecMenuItem */
085: private ExecBridge(Node node, AbstractButton button) {
086: this .node = node;
087: this .button = button;
088:
089: button.addActionListener(this );
090: node.addPropertyChangeListener(org.openide.util.WeakListeners
091: .propertyChange(this , node));
092:
093: updateState();
094: }
095:
096: /** Executes the object.
097: */
098: public void actionPerformed(ActionEvent ev) {
099: Class c = getExecCookieClass();
100: if (c == null) {
101: return;
102: }
103: Node.Cookie ec = node.getCookie(c);
104: if (ec != null) {
105: try {
106: Method m = getExecCookieClass().getMethod("start");
107: m.invoke(ec);
108: } catch (Exception e) {
109: Exceptions.printStackTrace(e);
110: }
111: }
112: }
113:
114: /** Listen to changes of exec cookie on the data object.
115: */
116: public void propertyChange(PropertyChangeEvent ev) {
117: if (Node.PROP_COOKIE.equals(ev.getPropertyName())) {
118: updateState();
119: }
120: if (Node.PROP_DISPLAY_NAME.equals(ev.getPropertyName())) {
121: updateState();
122: }
123: if (Node.PROP_ICON.equals(ev.getPropertyName())) {
124: updateState();
125: }
126: }
127:
128: /** Updates state
129: */
130: private void updateState() {
131: button.setText(node.getDisplayName());
132: Icon icon = new ImageIcon(node
133: .getIcon(BeanInfo.ICON_COLOR_16x16));
134: button.setIcon(icon);
135:
136: Class c = getExecCookieClass();
137: button.setEnabled(c != null && node.getCookie(c) != null);
138: }
139:
140: /** Creates menu item associated with the data object.
141: * May be null if there is no ExecCookie.
142: */
143: public static JMenuItem createMenuItem(
144: org.openide.loaders.DataObject obj) {
145: if (!obj.isValid())
146: return null;
147: Node n = obj.getNodeDelegate();
148: Class c = getExecCookieClass();
149: if (c == null || n.getCookie(c) == null)
150: return null;
151: JMenuItem item = new JMenuItem();
152: new ExecBridge(n, item);
153: return item;
154: }
155:
156: /** Creates toolbar component associated with the object.
157: * May be null if there is no ExecCookie.
158: */
159: public static JButton createButton(
160: org.openide.loaders.DataObject obj) {
161: if (!obj.isValid())
162: return null;
163: Node n = obj.getNodeDelegate();
164: Class c = getExecCookieClass();
165: if (c == null || n.getCookie(c) == null)
166: return null;
167: JButton item = new JButton();
168: new ExecBridge(n, item);
169: return item;
170: }
171:
172: }
|