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: package org.netbeans.modules.refactoring.spi.impl;
042:
043: import java.io.IOException;
044: import java.util.ArrayList;
045: import java.util.Iterator;
046: import javax.swing.*;
047: import javax.swing.text.JTextComponent;
048: import javax.swing.text.TextAction;
049: import org.openide.awt.Actions;
050: import org.openide.awt.JMenuPlus;
051: import org.openide.awt.Mnemonics;
052: import org.openide.cookies.InstanceCookie;
053: import org.openide.filesystems.FileObject;
054: import org.openide.filesystems.FileSystem;
055: import org.openide.filesystems.Repository;
056: import org.openide.loaders.DataFolder;
057: import org.openide.loaders.DataObject;
058: import org.openide.nodes.Node;
059: import org.openide.util.Lookup;
060: import org.openide.util.LookupListener;
061: import org.openide.util.NbBundle;
062: import org.openide.util.actions.Presenter;
063:
064: /**
065: *
066: * @author Martin Matula
067: */
068: public final class RefactoringSubMenuAction extends TextAction
069: implements Presenter.Menu, Presenter.Popup {
070: private final boolean showIcons;
071:
072: public static RefactoringSubMenuAction create(FileObject o) {
073: return new RefactoringSubMenuAction(true);
074: }
075:
076: public static JMenu createMenu() {
077: RefactoringSubMenuAction action = new RefactoringSubMenuAction(
078: true);
079: return (JMenu) action.getMenuPresenter();
080: }
081:
082: /** Creates a new instance of TestMenu */
083: RefactoringSubMenuAction(boolean showIcons) {
084: super (NbBundle.getMessage(RefactoringSubMenuAction.class,
085: "LBL_Action"));
086: this .showIcons = showIcons;
087: }
088:
089: public void actionPerformed(java.awt.event.ActionEvent e) {
090: }
091:
092: public javax.swing.JMenuItem getMenuPresenter() {
093: return new SubMenu();
094: }
095:
096: public javax.swing.JMenuItem getPopupPresenter() {
097: return getMenuPresenter();
098: }
099:
100: public boolean equals(Object o) {
101: return (o instanceof RefactoringSubMenuAction);
102: }
103:
104: public int hashCode() {
105: return 1;
106: }
107:
108: private class SubMenu extends JMenuPlus implements LookupListener {
109: private ArrayList actions = null;
110: private Lookup.Result nodes = null;
111: private boolean nodesChanged = true;
112:
113: public SubMenu() {
114: super ((String) RefactoringSubMenuAction.this
115: .getValue(Action.NAME));
116: if (showIcons)
117: setMnemonic(NbBundle.getMessage(
118: RefactoringSubMenuAction.class,
119: "LBL_ActionMnemonic").charAt(0));
120: }
121:
122: /** Gets popup menu. Overrides superclass. Adds lazy menu items creation. */
123: public JPopupMenu getPopupMenu() {
124: if (actions == null) {
125: createMenuItems();
126: }
127: return super .getPopupMenu();
128: }
129:
130: /** Creates items when actually needed. */
131: private void createMenuItems() {
132: actions = new ArrayList();
133: removeAll();
134: FileSystem dfs = Repository.getDefault()
135: .getDefaultFileSystem();
136: FileObject fo = dfs.findResource("Menu/Refactoring"); // NOI18N
137: DataFolder df = DataFolder.findFolder(fo);
138:
139: if (df != null) {
140: DataObject actionObjects[] = df.getChildren();
141: for (int i = 0; i < actionObjects.length; i++) {
142: InstanceCookie ic = (InstanceCookie) actionObjects[i]
143: .getCookie(InstanceCookie.class);
144: if (ic == null)
145: continue;
146: Object instance;
147: try {
148: instance = ic.instanceCreate();
149: } catch (IOException e) {
150: // ignore
151: e.printStackTrace();
152: continue;
153: } catch (ClassNotFoundException e) {
154: // ignore
155: e.printStackTrace();
156: continue;
157: }
158: if (instance instanceof RefactoringGlobalAction) {
159: // if the action is the refactoring action, pass it information
160: // whether it is in editor, popup or main menu
161: actions.add(instance);
162: JMenuItem mi = new JMenuItem();
163: Actions.connect(mi, (Action) instance, true);
164: if (!showIcons)
165: mi.setIcon(null);
166: add(mi);
167: } else if (instance instanceof JSeparator) {
168: add((JSeparator) instance);
169: } else if (instance instanceof Presenter.Popup) {
170: JMenuItem temp = ((Presenter.Popup) instance)
171: .getPopupPresenter();
172: if (!showIcons)
173: temp.setIcon(null);
174: add(temp);
175: } else if (instance instanceof Action) {
176: JMenuItem mi = new JMenuItem();
177: Actions.connect(mi, (Action) instance, true);
178: if (!showIcons)
179: mi.setIcon(null);
180: add(mi);
181: }
182: }
183: }
184: }
185:
186: private void setText(JMenuItem t, Action a) {
187: String name = (String) a.getValue(Action.NAME);
188: int i = Mnemonics.findMnemonicAmpersand(name);
189:
190: if (i < 0)
191: t.setText(name);
192: else
193: t.setText(name.substring(0, i) + name.substring(i + 1));
194: }
195:
196: public void resultChanged(org.openide.util.LookupEvent ev) {
197: nodesChanged = true;
198: }
199:
200: }
201: }
|