KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > freeform > customcommands > Command


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.netbeans.modules.ant.freeform.customcommands;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.Serializable JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.List JavaDoc;
31 import javax.swing.Action JavaDoc;
32 import javax.swing.ImageIcon JavaDoc;
33 import javax.swing.JButton JavaDoc;
34 import javax.swing.JMenuItem JavaDoc;
35 import org.netbeans.spi.project.ui.support.FileSensitiveActions;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.Repository;
38 import org.openide.loaders.DataFolder;
39 import org.openide.loaders.DataObject;
40 import org.openide.loaders.InstanceDataObject;
41 import org.openide.util.ContextAwareAction;
42 import org.openide.util.Lookup;
43 import org.openide.util.actions.Presenter;
44
45 /**
46  * Persistent version of {@link FileSensitiveActions#fileCommandAction}.
47  * @author Jesse Glick
48  */

49 public final class Command implements Serializable JavaDoc, Action JavaDoc, Presenter.Menu, Presenter.Toolbar, Presenter.Popup, ContextAwareAction {
50     
51     private static final long serialVersionUID = 1L;
52     
53     private final String JavaDoc command;
54     private final String JavaDoc namePattern;
55     private final URL JavaDoc icon;
56     private transient Action JavaDoc delegate;
57     
58     Command(String JavaDoc command, String JavaDoc namePattern, URL JavaDoc icon) {
59         this.command = command;
60         this.namePattern = namePattern;
61         this.icon = icon;
62     }
63     
64     private Action JavaDoc getDelegate() {
65         if (delegate == null) {
66             delegate = FileSensitiveActions.fileCommandAction(command, namePattern, icon != null ? new ImageIcon JavaDoc(icon) : null);
67         }
68         return delegate;
69     }
70     
71     private static DataFolder folder(String JavaDoc path) {
72         FileObject f = Repository.getDefault().getDefaultFileSystem().findResource(path);
73         return f != null ? DataFolder.findFolder(f) : null;
74     }
75
76     public void create(DataFolder menuFolder, int position) throws IOException JavaDoc {
77         DataFolder ap = folder("Actions/Project"); // NOI18N
78
if (ap == null) {
79             ap = folder("Actions"); // NOI18N
80
assert ap != null;
81         }
82         DataObject instance = InstanceDataObject.create(ap, command, this, null, true);
83         String JavaDoc menuName = menuFolder.getName();
84         if (menuName.endsWith("Project")) { // NOI18N
85
menuName = menuName.substring(0, menuName.length() - "Project".length()); // NOI18N
86
}
87         DataFolder oda = folder("OptionsDialog/Actions/" + menuName); // NOI18N
88
if (oda == null) {
89             oda = folder("OptionsDialog/Actions/Build"); // NOI18N
90
}
91         if (oda != null) {
92             instance.createShadow(oda);
93         }
94         DataObject nue = instance.createShadow(menuFolder);
95         List JavaDoc children = new ArrayList JavaDoc(Arrays.asList(menuFolder.getChildren()));
96         assert children.contains(nue);
97         children.remove(nue);
98         children.add(position, nue);
99         menuFolder.setOrder((DataObject[]) children.toArray(new DataObject[children.size()]));
100     }
101
102     public Object JavaDoc getValue(String JavaDoc key) {
103         return getDelegate().getValue(key);
104     }
105
106     public void putValue(String JavaDoc key, Object JavaDoc value) {
107         getDelegate().putValue(key, value);
108     }
109
110     public void setEnabled(boolean b) {
111         getDelegate().setEnabled(b);
112     }
113
114     public boolean isEnabled() {
115         return getDelegate().isEnabled();
116     }
117
118     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
119         getDelegate().addPropertyChangeListener(listener);
120     }
121
122     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
123         getDelegate().removePropertyChangeListener(listener);
124     }
125
126     public void actionPerformed(ActionEvent JavaDoc e) {
127         getDelegate().actionPerformed(e);
128     }
129
130     public JMenuItem JavaDoc getMenuPresenter() {
131         if (getDelegate() instanceof Presenter.Menu) {
132             return ((Presenter.Menu) getDelegate()).getMenuPresenter();
133         } else {
134             return new JMenuItem JavaDoc(this);
135         }
136     }
137
138     public Component JavaDoc getToolbarPresenter() {
139         if (getDelegate() instanceof Presenter.Toolbar) {
140             return ((Presenter.Toolbar) getDelegate()).getToolbarPresenter();
141         } else {
142             return new JButton JavaDoc(this);
143         }
144     }
145
146     public JMenuItem JavaDoc getPopupPresenter() {
147         if (getDelegate() instanceof Presenter.Popup) {
148             return ((Presenter.Popup) getDelegate()).getPopupPresenter();
149         } else {
150             return new JMenuItem JavaDoc(this);
151         }
152     }
153     
154     public Action JavaDoc createContextAwareInstance(Lookup actionContext) {
155         if (getDelegate() instanceof ContextAwareAction) {
156             return ((ContextAwareAction) getDelegate()).createContextAwareInstance(actionContext);
157         } else {
158             return this;
159         }
160     }
161
162 }
163
Popular Tags