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.cnd.actions;
043:
044: import java.awt.event.ActionEvent;
045: import java.awt.event.ActionListener;
046: import javax.swing.ImageIcon;
047: import javax.swing.JFrame;
048: import javax.swing.JMenu;
049: import javax.swing.JMenuItem;
050: import javax.swing.JPopupMenu;
051: import javax.swing.JSeparator;
052: import org.netbeans.modules.cnd.builds.MakeExecSupport;
053: import org.netbeans.modules.cnd.builds.TargetEditor;
054: import org.openide.nodes.Node;
055: import org.openide.util.HelpCtx;
056: import org.openide.util.Utilities;
057: import org.openide.util.actions.Presenter;
058: import org.openide.util.actions.SystemAction;
059: import org.openide.windows.WindowManager;
060:
061: /**
062: * Implements Make target Action
063: */
064: public class MakeTargetAction extends MakeBaseAction implements
065: Presenter.Popup {
066:
067: /* target mnemonics for the first 10 targets */
068: private static String mnemonics = "1234567890"; // NOI18N
069:
070: public String getName() {
071: return getString("BTN_Target"); // NOI18N
072: }
073:
074: public HelpCtx getHelpCtx() {
075: return new HelpCtx(MakeTargetAction.class); // FIXUP ???
076: }
077:
078: public JMenuItem getPopupPresenter() {
079: Node[] activeNodes = WindowManager.getDefault().getRegistry()
080: .getActivatedNodes();
081: return new TargetPopupMenu(this , enable(activeNodes),
082: activeNodes);
083: }
084:
085: private class TargetPopupMenu extends JMenu {
086: private boolean initialized = false;
087: private SystemAction action = null;
088: private Node[] activeNodes;
089:
090: public TargetPopupMenu(SystemAction action, boolean en,
091: Node[] activeNodes) {
092: super ();
093: this .action = action;
094: this .activeNodes = activeNodes;
095: setEnabled(en);
096: setText(action.getName());
097: }
098:
099: public JPopupMenu getPopupMenu() {
100: JPopupMenu popup = super .getPopupMenu();
101: if (!initialized) {
102: if (activeNodes == null || activeNodes.length != 1) {
103: return null;
104: }
105:
106: Node activedNode = activeNodes[0];
107: MakeExecSupport mes = (MakeExecSupport) activedNode
108: .getCookie(MakeExecSupport.class);
109: String[] targets = mes.getMakeTargetsArray();
110:
111: //popup.add(new PopupItemDefaultTarget(activedNode, getString("DEFAULT_TARGET"))); // NOI18N
112: //if (targets.length > 0)
113: //popup.add(new JSeparator());
114: for (int i = 0; i < targets.length; i++) {
115: popup.add(new PopupItemTarget(activedNode,
116: targets[i], -1));
117: }
118: if (targets.length > 0)
119: popup.add(new JSeparator());
120: popup.add(new PopupItemAddTarget(activedNode)); // NOI18N
121: initialized = true;
122: }
123: return popup;
124: }
125:
126: }
127:
128: /**
129: * Compose new name with a mnemonic: <targetname> (<mnemonic>)
130: * Compose new name with a mnemonic: <mnemonic> <targetname>
131: */
132: private String nameWithMnemonic(String name, int mne) {
133: if (mne >= 0 && mne < mnemonics.length()) {
134: return "" + mnemonics.charAt(mne) + " " + name; // NOI18N
135: //return name + " (" + mnemonics.charAt(mne) + ")"; // NOI18N
136: } else {
137: return name; // no mnemonic
138: }
139: }
140:
141: protected class PopupItemTarget extends JMenuItem implements
142: ActionListener {
143: Node node = null;
144: String target = null;
145:
146: public PopupItemTarget(Node activeNode, String name, int mne) {
147: //super(nameWithMnemonic(name, mne), new ImageIcon(Utilities.loadImage("org/netbeans/modules/cnd/resources/blank.gif", true)));
148: super (nameWithMnemonic(name, mne));
149: node = activeNode;
150: target = name;
151: addActionListener(this );
152: if (mne >= 0 && mne < mnemonics.length()) {
153: setMnemonic(mnemonics.charAt(mne));
154: }
155: }
156:
157: /** Invoked when an action occurs. */
158: public void actionPerformed(ActionEvent e) {
159: performAction(node, target);
160: }
161: }
162:
163: protected class PopupItemAddTarget extends JMenuItem implements
164: ActionListener {
165: Node node = null;
166:
167: public PopupItemAddTarget(Node activeNode) {
168: //super(getString("ADD_NEW_TARGET"), new ImageIcon(Utilities.loadImage("org/netbeans/modules/cnd/resources/AddMakeTargetAction.gif", true))); // NOI18N
169: super (getString("ADD_NEW_TARGET")); // NOI18N
170: node = activeNode;
171: addActionListener(this );
172: setMnemonic(getString("ADD_NEW_TARGET_MNEMONIC").charAt(0));
173: }
174:
175: /** Invoked when an action occurs. */
176: public void actionPerformed(ActionEvent e) {
177: MakeExecSupport mes = (MakeExecSupport) node
178: .getCookie(MakeExecSupport.class);
179: TargetEditor targetEditor = new TargetEditor(mes
180: .getMakeTargetsArray(), null, null);
181: int ret = targetEditor
182: .showOpenDialog((JFrame) WindowManager.getDefault()
183: .getMainWindow());
184: if (ret == TargetEditor.OK_OPTION) {
185: mes.setMakeTargets(targetEditor.getTargets());
186: }
187: }
188: }
189: }
|