KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xam > ui > actions > GoToAction


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xam.ui.actions;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.swing.Action JavaDoc;
27 import javax.swing.JMenuItem JavaDoc;
28 import javax.swing.event.ChangeListener JavaDoc;
29 import javax.swing.event.EventListenerList JavaDoc;
30 import org.netbeans.modules.xml.xam.Component;
31 import org.netbeans.modules.xml.xam.ui.XAMUtils;
32 import org.netbeans.modules.xml.xam.ui.cookies.GotoCookie;
33 import org.netbeans.modules.xml.xam.ui.cookies.ViewComponentCookie;
34 import org.openide.awt.Actions;
35 import org.openide.nodes.Node;
36 import org.openide.util.HelpCtx;
37 import org.openide.util.Lookup;
38 import org.openide.util.NbBundle;
39 import org.openide.util.actions.CookieAction;
40 import org.openide.util.actions.Presenter;
41 import org.openide.windows.WindowManager;
42
43 /**
44  * Action which provides a means of showing a component in a particular view.
45  * Nodes may provide this action in their set of supported actions, but they
46  * must also implement the GotoCookie, which provides the set of supported
47  * GotoTypes. These types are the means by which the component is shown in
48  * one view or another.
49  *
50  * @author Ajit Bhate
51  * @author Nathan Fiedler
52  */

53 public class GoToAction extends CookieAction {
54     /** silence compiler warnings */
55     private static final long serialVersionUID = 1L;
56     private static ActSubMenuModel model = new ActSubMenuModel(null);
57
58     public String JavaDoc getName() {
59         return model.createName();
60     }
61
62     /**
63      * Getter for array of activated goto types.
64      *
65      * @param activatedNodes array of activated nodes.
66      * @return array of GotoType.
67      */

68     private static GotoType[] getGotoTypes(Node[] activatedNodes) {
69         List JavaDoc<GotoType> types = new ArrayList JavaDoc<GotoType>();
70         if (activatedNodes != null || activatedNodes.length == 1) {
71             Node node = activatedNodes[0];
72             GotoCookie cookie = node.getCookie(GotoCookie.class);
73             if (cookie != null) {
74                 for (GotoType type : cookie.getGotoTypes()) {
75                     Component comp = type.getComponent(node);
76                     // Return only the types that are going to work properly.
77
ViewComponentCookie.View view = type.getView();
78                     if (XAMUtils.getViewCookie(comp, view) != null) {
79                         types.add(type);
80                     }
81                 }
82             }
83         }
84         return types.toArray(new GotoType[types.size()]);
85     }
86
87     public HelpCtx getHelpCtx() {
88         return HelpCtx.DEFAULT_HELP;
89     }
90
91     protected void performAction(org.openide.nodes.Node[] activatedNodes) {
92         model.performActionAt(0);
93     }
94
95     public JMenuItem JavaDoc getMenuPresenter() {
96         return new Actions.SubMenu(this, model, false);
97     }
98
99     public JMenuItem JavaDoc getPopupPresenter() {
100         return new Actions.SubMenu(this, model, true);
101     }
102
103     protected int mode() {
104         return MODE_EXACTLY_ONE;
105     }
106
107     protected Class JavaDoc[] cookieClasses() {
108         return new Class JavaDoc[] {
109             GotoCookie.class
110         };
111     }
112
113     protected boolean asynchronous() {
114         return false;
115     }
116
117     public Action JavaDoc createContextAwareInstance(Lookup actionContext) {
118         return new DelegateAction(this, actionContext);
119     }
120
121     /** Implementation of Actions.SubMenuModel */
122     private static class ActSubMenuModel extends EventListenerList JavaDoc implements Actions.SubMenuModel {
123         static final long serialVersionUID = -4273674308662494596L;
124 // private transient Lookup lookup;
125

126         ActSubMenuModel(Lookup lookup) {
127 // Per IZ#86250, avoid caching lookup to prevent memory leak
128
// this.lookup = lookup;
129
}
130
131         private Node[] nodes() {
132 // if (lookup != null) {
133
// java.util.Collection c = lookup.lookup(new Lookup.Template(Node.class)).allItems();
134
//
135
// if (c.size() == 1) {
136
// java.util.Iterator it = c.iterator();
137
//
138
// while (it.hasNext()) {
139
// Lookup.Item item = (Lookup.Item) it.next();
140
// Node n = (Node) item.getInstance();
141
//
142
// if (n != null) {
143
// return new Node[]{n};
144
// }
145
// }
146
// }
147
// }
148
return WindowManager.getDefault().getRegistry().getCurrentNodes();
149         }
150
151         private String JavaDoc createName() {
152             GotoType[] types = getGotoTypes(nodes());
153             if (types != null && types.length == 1) {
154                 return NbBundle.getMessage(GoToAction.class,
155                         "LBL_GoTo_Name", types[0].getName());
156             } else {
157                 return NbBundle.getMessage(GoToAction.class, "LBL_GoTo");
158             }
159         }
160
161         public int getCount() {
162             return getGotoTypes(nodes()).length;
163         }
164
165         public String JavaDoc getLabel(int index) {
166             GotoType[] types = getGotoTypes(nodes());
167             if (types.length <= index) {
168                 return null;
169             } else {
170                 return types[index].getName();
171             }
172         }
173
174         public HelpCtx getHelpCtx(int index) {
175             GotoType[] types = getGotoTypes(nodes());
176             if (types.length <= index) {
177                 return null;
178             } else {
179                 return types[index].getHelpCtx();
180             }
181         }
182
183         public void performActionAt(int index) {
184             Node[] nodes = nodes();
185             GotoType[] types = getGotoTypes(nodes);
186             if (types.length > index) {
187                 types[index].show(nodes[0]);
188             }
189         }
190
191         /** Adds change listener for changes of the model.
192          */

193         public void addChangeListener(ChangeListener JavaDoc l) {
194             add(ChangeListener JavaDoc.class, l);
195         }
196
197         /** Removes change listener for changes of the model.
198          */

199         public void removeChangeListener(ChangeListener JavaDoc l) {
200             remove(ChangeListener JavaDoc.class, l);
201         }
202     }
203
204     /**
205      * A delegate action that is usually associated with a specific lookup and
206      * extract the nodes it operates on from it. Otherwise it delegates to the
207      * regular NodeAction.
208      */

209     private static final class DelegateAction implements
210             Action JavaDoc, Presenter.Menu, Presenter.Popup {
211         /** Action to delegate to. */
212         private final CookieAction delegate;
213
214         /** Associated model to use. */
215         private final ActSubMenuModel model;
216
217         public DelegateAction(CookieAction a, Lookup actionContext) {
218             this.delegate = a;
219             this.model = new ActSubMenuModel(actionContext);
220         }
221
222         /** Overrides superclass method, adds delegate description. */
223         public String JavaDoc toString() {
224             return super.toString() + "[delegate=" + delegate + "]"; // NOI18N
225
}
226
227         /** Invoked when an action occurs.
228          */

229         public void actionPerformed(ActionEvent JavaDoc e) {
230             model.performActionAt(0);
231         }
232
233         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
234         }
235
236         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
237         }
238
239         public void putValue(String JavaDoc key, Object JavaDoc o) {
240         }
241
242         public Object JavaDoc getValue(String JavaDoc key) {
243             if (Action.NAME.equals(key)) {
244                 return model.createName();
245             } else {
246                 return delegate.getValue(key);
247             }
248         }
249
250         public boolean isEnabled() {
251             return model.getCount() > 0;
252         }
253
254         public void setEnabled(boolean b) {
255         }
256
257         public JMenuItem JavaDoc getMenuPresenter() {
258             return new Actions.SubMenu(this, model, false);
259         }
260
261         public JMenuItem JavaDoc getPopupPresenter() {
262             return new Actions.SubMenu(this, model, true);
263         }
264     }
265 }
266
Popular Tags