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.project.ui.actions;
043:
044: import javax.swing.Action;
045: import javax.swing.Icon;
046: import javax.swing.ImageIcon;
047: import javax.swing.JMenuItem;
048: import org.netbeans.api.project.Project;
049: import org.netbeans.modules.project.ui.ProjectTab;
050: import org.netbeans.spi.project.ui.support.ProjectActionPerformer;
051: import org.openide.filesystems.FileObject;
052: import org.openide.loaders.DataObject;
053: import org.openide.util.Lookup;
054: import org.openide.util.NbBundle;
055: import org.openide.util.Utilities;
056: import org.openide.util.actions.Presenter;
057:
058: /** Action sensitive to current project
059: *
060: * @author Pet Hrebejk
061: */
062: public class SelectNodeAction extends LookupSensitiveAction implements
063: Presenter.Menu, Presenter.Popup {
064:
065: // XXX Better icons
066: private static final Icon SELECT_IN_PROJECTS_ICON = new ImageIcon(
067: Utilities
068: .loadImage("org/netbeans/modules/project/ui/resources/projectTab.png")); //NOI18N
069: private static final Icon SELECT_IN_FILES_ICON = new ImageIcon(
070: Utilities
071: .loadImage("org/netbeans/modules/project/ui/resources/filesTab.png")); //NOI18N
072:
073: private static final String SELECT_IN_PROJECTS_NAME = NbBundle
074: .getMessage(CloseProject.class,
075: "LBL_SelectInProjectsAction_Name"); // NOI18N
076: private static final String SELECT_IN_FILES_NAME = NbBundle
077: .getMessage(CloseProject.class,
078: "LBL_SelectInFilesAction_Name"); // NOI18N
079:
080: private static final String SELECT_IN_PROJECTS_NAME_MENU = NbBundle
081: .getMessage(CloseProject.class,
082: "LBL_SelectInProjectsAction_MenuName"); // NOI18N
083: private static final String SELECT_IN_FILES_NAME_MENU = NbBundle
084: .getMessage(CloseProject.class,
085: "LBL_SelectInFilesAction_MenuName"); // NOI18N
086: private static final String SELECT_IN_PROJECTS_NAME_MAIN_MENU = NbBundle
087: .getMessage(CloseProject.class,
088: "LBL_SelectInProjectsAction_MainMenuName"); // NOI18N
089: private static final String SELECT_IN_FILES_NAME_MAIN_MENU = NbBundle
090: .getMessage(CloseProject.class,
091: "LBL_SelectInFilesAction_MainMenuName"); // NOI18N
092:
093: private String command;
094: private ProjectActionPerformer performer;
095: private String namePattern;
096:
097: private String findIn;
098:
099: public static Action inProjects() {
100: SelectNodeAction a = new SelectNodeAction(
101: SELECT_IN_PROJECTS_ICON, SELECT_IN_PROJECTS_NAME);
102: a.findIn = ProjectTab.ID_LOGICAL;
103: return a;
104: }
105:
106: /** for test */
107: static Action inProjects(Lookup lookup) {
108: SelectNodeAction a = new SelectNodeAction(
109: SELECT_IN_PROJECTS_ICON, SELECT_IN_PROJECTS_NAME,
110: lookup);
111: a.findIn = ProjectTab.ID_LOGICAL;
112: return a;
113: }
114:
115: public static Action inFiles() {
116: SelectNodeAction a = new SelectNodeAction(SELECT_IN_FILES_ICON,
117: SELECT_IN_FILES_NAME);
118: a.findIn = ProjectTab.ID_PHYSICAL;
119: return a;
120: }
121:
122: /**
123: * Constructor for global actions. E.g. actions in main menu which
124: * listen to the global context.
125: *
126: */
127: public SelectNodeAction(Icon icon, String name) {
128: this (icon, name, null);
129: }
130:
131: private SelectNodeAction(Icon icon, String name, Lookup lookup) {
132: super (icon, lookup, new Class[] { DataObject.class,
133: FileObject.class });
134: this .setDisplayName(name);
135: }
136:
137: private SelectNodeAction(String command,
138: ProjectActionPerformer performer, String namePattern,
139: Icon icon, Lookup lookup) {
140: super (icon, lookup, new Class[] { Project.class,
141: DataObject.class, FileObject.class });
142: this .command = command;
143: this .performer = performer;
144: this .namePattern = namePattern;
145: refresh(getLookup());
146: }
147:
148: protected void actionPerformed(Lookup context) {
149:
150: FileObject fo = getFileFromLookup(context);
151: if (fo != null) {
152: ProjectTab pt = ProjectTab.findDefault(findIn);
153: pt.selectNodeAsync(fo);
154: }
155: }
156:
157: protected void refresh(Lookup context) {
158: FileObject fo = getFileFromLookup(context);
159: setEnabled(fo != null);
160: }
161:
162: protected final String getCommand() {
163: return command;
164: }
165:
166: protected final String getNamePattern() {
167: return namePattern;
168: }
169:
170: // Presenter.Menu implementation ------------------------------------------
171:
172: public JMenuItem getMenuPresenter() {
173: if (ProjectTab.ID_LOGICAL.equals(this .findIn)) {
174: return buildPresenter(SELECT_IN_PROJECTS_NAME_MAIN_MENU);
175: } else {
176: return buildPresenter(SELECT_IN_FILES_NAME_MAIN_MENU);
177: }
178: }
179:
180: // Presenter.Popup implementation ------------------------------------------
181:
182: public JMenuItem getPopupPresenter() {
183: if (ProjectTab.ID_LOGICAL.equals(this .findIn)) {
184: return buildPresenter(SELECT_IN_PROJECTS_NAME_MENU);
185: } else {
186: return buildPresenter(SELECT_IN_FILES_NAME_MENU);
187: }
188: }
189:
190: // Private methods ---------------------------------------------------------
191:
192: private FileObject getFileFromLookup(Lookup context) {
193:
194: FileObject fo = context.lookup(FileObject.class);
195: if (fo != null) {
196: return fo;
197: }
198:
199: DataObject dobj = context.lookup(DataObject.class);
200:
201: return dobj == null ? null : dobj.getPrimaryFile();
202: }
203:
204: private JMenuItem buildPresenter(String title) {
205: JMenuItem menuPresenter = new JMenuItem(this);
206: menuPresenter.setText(title);
207: menuPresenter.setIcon(null);
208:
209: return menuPresenter;
210: }
211:
212: }
|