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-2007 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.netbeans.modules.xml.xam.ui.actions;
043:
044: import java.awt.event.ActionEvent;
045: import java.beans.PropertyChangeListener;
046: import java.util.ArrayList;
047: import java.util.List;
048: import javax.swing.Action;
049: import javax.swing.JMenuItem;
050: import javax.swing.event.ChangeListener;
051: import javax.swing.event.EventListenerList;
052: import org.netbeans.modules.xml.xam.Component;
053: import org.netbeans.modules.xml.xam.ui.XAMUtils;
054: import org.netbeans.modules.xml.xam.ui.cookies.GotoCookie;
055: import org.netbeans.modules.xml.xam.ui.cookies.ViewComponentCookie;
056: import org.openide.awt.Actions;
057: import org.openide.nodes.Node;
058: import org.openide.util.HelpCtx;
059: import org.openide.util.Lookup;
060: import org.openide.util.NbBundle;
061: import org.openide.util.actions.CookieAction;
062: import org.openide.util.actions.Presenter;
063: import org.openide.windows.WindowManager;
064:
065: /**
066: * Action which provides a means of showing a component in a particular view.
067: * Nodes may provide this action in their set of supported actions, but they
068: * must also implement the GotoCookie, which provides the set of supported
069: * GotoTypes. These types are the means by which the component is shown in
070: * one view or another.
071: *
072: * @author Ajit Bhate
073: * @author Nathan Fiedler
074: */
075: public class GoToAction extends CookieAction {
076: /** silence compiler warnings */
077: private static final long serialVersionUID = 1L;
078: private ActSubMenuModel model = new ActSubMenuModel(null);
079:
080: public String getName() {
081: return model.createName();
082: }
083:
084: public HelpCtx getHelpCtx() {
085: return HelpCtx.DEFAULT_HELP;
086: }
087:
088: protected void performAction(org.openide.nodes.Node[] activatedNodes) {
089: model.performActionAt(0);
090: }
091:
092: public JMenuItem getMenuPresenter() {
093: return new Actions.SubMenu(this , model, false);
094: }
095:
096: public JMenuItem getPopupPresenter() {
097: return new Actions.SubMenu(this , model, true);
098: }
099:
100: protected int mode() {
101: return MODE_EXACTLY_ONE;
102: }
103:
104: protected Class[] cookieClasses() {
105: return new Class[] { GotoCookie.class };
106: }
107:
108: @Override
109: protected boolean asynchronous() {
110: return false;
111: }
112:
113: @Override
114: public Action createContextAwareInstance(Lookup actionContext) {
115: return new DelegateAction(this , actionContext);
116: }
117:
118: /** Implementation of Actions.SubMenuModel */
119: private class ActSubMenuModel extends EventListenerList implements
120: Actions.SubMenuModel {
121: static final long serialVersionUID = -4273674308662494596L;
122:
123: //IZ: 115374: Cache the last selected gotoNode and gotoTypes
124: //getRegistry().getCurrentNodes() returns a different one
125: //when user clicks on goto.
126: private transient Node gotoNode;
127: private transient GotoType[] gotoTypes;
128:
129: ActSubMenuModel(Lookup lookup) {
130: }
131:
132: private Node getSelectedNode() {
133: //return the cached one, if exists
134: if (gotoNode != null)
135: return gotoNode;
136:
137: //new one
138: Node[] nodes = WindowManager.getDefault().getRegistry()
139: .getCurrentNodes();
140: if (nodes != null && nodes.length == 1
141: && nodes[0] != gotoNode) {
142: gotoNode = nodes[0];
143: }
144: //worst case
145: return gotoNode;
146: }
147:
148: /**
149: * Getter for array of activated goto types.
150: *
151: * @param activatedNodes array of activated nodes.
152: * @return array of GotoType.
153: */
154: private GotoType[] getGotoTypes(Node node) {
155: if (node == null)
156: return null;
157: if ((gotoNode == node) && (gotoTypes != null))
158: return gotoTypes;
159: List<GotoType> types = new ArrayList<GotoType>();
160: GotoCookie cookie = node.getCookie(GotoCookie.class);
161: if (cookie != null) {
162: for (GotoType type : cookie.getGotoTypes()) {
163: Component comp = type.getComponent(node);
164: // Return only the types that are going to work properly.
165: ViewComponentCookie.View view = type.getView();
166: if (XAMUtils.getViewCookie(comp, view) != null) {
167: types.add(type);
168: }
169: }
170: }
171: gotoTypes = types.toArray(new GotoType[types.size()]);
172: return gotoTypes;
173: }
174:
175: private String createName() {
176: GotoType[] types = getGotoTypes(getSelectedNode());
177: if (types != null && types.length == 1) {
178: return NbBundle.getMessage(GoToAction.class,
179: "LBL_GoTo_Name", types[0].getName());
180: } else {
181: return NbBundle
182: .getMessage(GoToAction.class, "LBL_GoTo");
183: }
184: }
185:
186: public int getCount() {
187: return getSelectedNode() == null ? 0
188: : getGotoTypes(getSelectedNode()).length;
189: }
190:
191: public String getLabel(int index) {
192: GotoType[] types = getGotoTypes(getSelectedNode());
193: if ((types == null) || (types.length <= index)) {
194: return null;
195: } else {
196: return types[index].getName();
197: }
198: }
199:
200: public HelpCtx getHelpCtx(int index) {
201: GotoType[] types = getGotoTypes(getSelectedNode());
202: if ((types == null) || (types.length <= index)) {
203: return null;
204: } else {
205: return types[index].getHelpCtx();
206: }
207: }
208:
209: public void performActionAt(int index) {
210: Node node = getSelectedNode();
211: GotoType[] types = getGotoTypes(getSelectedNode());
212: if ((types != null) && (types.length > index)) {
213: types[index].show(node);
214: }
215: this .gotoNode = null;
216: this .gotoTypes = null;
217: }
218:
219: /** Adds change listener for changes of the model.
220: */
221: public void addChangeListener(ChangeListener l) {
222: add(ChangeListener.class, l);
223: }
224:
225: /** Removes change listener for changes of the model.
226: */
227: public void removeChangeListener(ChangeListener l) {
228: remove(ChangeListener.class, l);
229: }
230: }
231:
232: /**
233: * A delegate action that is usually associated with a specific lookup and
234: * extract the nodes it operates on from it. Otherwise it delegates to the
235: * regular NodeAction.
236: */
237: private final class DelegateAction implements Action,
238: Presenter.Menu, Presenter.Popup {
239: /** Action to delegate to. */
240: private final CookieAction delegate;
241:
242: /** Associated model to use. */
243: private final ActSubMenuModel model;
244:
245: public DelegateAction(CookieAction a, Lookup actionContext) {
246: this .delegate = a;
247: this .model = new ActSubMenuModel(actionContext);
248: }
249:
250: /** Overrides superclass method, adds delegate description. */
251: public String toString() {
252: return super .toString() + "[delegate=" + delegate + "]"; // NOI18N
253: }
254:
255: /** Invoked when an action occurs.
256: */
257: public void actionPerformed(ActionEvent e) {
258: model.performActionAt(0);
259: }
260:
261: public void addPropertyChangeListener(
262: PropertyChangeListener listener) {
263: }
264:
265: public void removePropertyChangeListener(
266: PropertyChangeListener listener) {
267: }
268:
269: public void putValue(String key, Object o) {
270: }
271:
272: public Object getValue(String key) {
273: if (Action.NAME.equals(key)) {
274: return model.createName();
275: } else {
276: return delegate.getValue(key);
277: }
278: }
279:
280: public boolean isEnabled() {
281: return model.getCount() > 0;
282: }
283:
284: public void setEnabled(boolean b) {
285: }
286:
287: public JMenuItem getMenuPresenter() {
288: return new Actions.SubMenu(this , model, false);
289: }
290:
291: public JMenuItem getPopupPresenter() {
292: return new Actions.SubMenu(this , model, true);
293: }
294: }
295: }
|