KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ui > actions > SelectNodeAction


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

19
20 package org.netbeans.modules.project.ui.actions;
21
22 import javax.swing.Action JavaDoc;
23 import javax.swing.Icon JavaDoc;
24 import javax.swing.ImageIcon JavaDoc;
25 import javax.swing.JMenuItem JavaDoc;
26 import org.netbeans.api.project.Project;
27 import org.netbeans.modules.project.ui.ProjectTab;
28 import org.netbeans.modules.project.ui.ProjectUtilities;
29 import org.netbeans.spi.project.ActionProvider;
30 import org.netbeans.spi.project.ui.support.ProjectActionPerformer;
31 import org.openide.awt.StatusDisplayer;
32 import org.openide.filesystems.FileObject;
33 import org.openide.loaders.DataObject;
34 import org.openide.util.ContextAwareAction;
35 import org.openide.util.Lookup;
36 import org.openide.util.NbBundle;
37 import org.openide.util.Utilities;
38 import org.openide.util.actions.Presenter;
39
40 /** Action sensitive to current project
41  *
42  * @author Pet Hrebejk
43  */

44 public class SelectNodeAction extends LookupSensitiveAction implements Presenter.Menu, Presenter.Popup {
45     
46     // XXX Better icons
47
private static final Icon JavaDoc SELECT_IN_PROJECTS_ICON = new ImageIcon JavaDoc( Utilities.loadImage( "org/netbeans/modules/project/ui/resources/projectTab.gif" ) ); //NOI18N
48
private static final Icon JavaDoc SELECT_IN_FILES_ICON = new ImageIcon JavaDoc( Utilities.loadImage( "org/netbeans/modules/project/ui/resources/filesTab.gif" ) ); //NOI18N
49

50     private static final String JavaDoc SELECT_IN_PROJECTS_NAME = NbBundle.getMessage( CloseProject.class, "LBL_SelectInProjectsAction_Name" ); // NOI18N
51
private static final String JavaDoc SELECT_IN_FILES_NAME = NbBundle.getMessage( CloseProject.class, "LBL_SelectInFilesAction_Name" ); // NOI18N
52

53     private static final String JavaDoc SELECT_IN_PROJECTS_NAME_MENU = NbBundle.getMessage( CloseProject.class, "LBL_SelectInProjectsAction_MenuName" ); // NOI18N
54
private static final String JavaDoc SELECT_IN_FILES_NAME_MENU = NbBundle.getMessage( CloseProject.class, "LBL_SelectInFilesAction_MenuName" ); // NOI18N
55
private static final String JavaDoc SELECT_IN_PROJECTS_NAME_MAIN_MENU = NbBundle.getMessage( CloseProject.class, "LBL_SelectInProjectsAction_MainMenuName" ); // NOI18N
56
private static final String JavaDoc SELECT_IN_FILES_NAME_MAIN_MENU = NbBundle.getMessage( CloseProject.class, "LBL_SelectInFilesAction_MainMenuName" ); // NOI18N
57

58     private String JavaDoc command;
59     private ProjectActionPerformer performer;
60     private String JavaDoc namePattern;
61     
62     private String JavaDoc findIn;
63     
64     public static Action JavaDoc inProjects() {
65         SelectNodeAction a = new SelectNodeAction( SELECT_IN_PROJECTS_ICON, SELECT_IN_PROJECTS_NAME );
66         a.findIn = ProjectTab.ID_LOGICAL;
67         return a;
68     }
69     
70     public static Action JavaDoc inFiles() {
71         SelectNodeAction a = new SelectNodeAction( SELECT_IN_FILES_ICON, SELECT_IN_FILES_NAME );
72         a.findIn = ProjectTab.ID_PHYSICAL;
73         return a;
74     }
75     
76     /**
77      * Constructor for global actions. E.g. actions in main menu which
78      * listen to the global context.
79      *
80      */

81     public SelectNodeAction( Icon JavaDoc icon, String JavaDoc name ) {
82         super( icon, null, new Class JavaDoc[] { DataObject.class, FileObject.class } );
83         this.setDisplayName( name );
84     }
85     
86     private SelectNodeAction(String JavaDoc command, ProjectActionPerformer performer, String JavaDoc namePattern, Icon JavaDoc icon, Lookup lookup) {
87         super( icon, lookup, new Class JavaDoc[] { Project.class, DataObject.class, FileObject.class } );
88         this.command = command;
89         this.performer = performer;
90         this.namePattern = namePattern;
91         refresh( getLookup() );
92     }
93        
94     protected void actionPerformed( Lookup context ) {
95         
96         FileObject fo = getFileFromLookup( context );
97         if ( fo != null ) {
98             ProjectTab pt = ProjectTab.findDefault( findIn );
99             pt.selectNodeAsync( fo );
100         }
101     }
102     
103     protected void refresh( Lookup context ) {
104         FileObject fo = getFileFromLookup( context );
105         setEnabled( fo != null );
106     }
107     
108     protected final String JavaDoc getCommand() {
109         return command;
110     }
111     
112     protected final String JavaDoc getNamePattern() {
113         return namePattern;
114     }
115     
116     // Presenter.Menu implementation ------------------------------------------
117

118     public JMenuItem JavaDoc getMenuPresenter () {
119         if (ProjectTab.ID_LOGICAL.equals (this.findIn)) {
120             return buildPresenter(SELECT_IN_PROJECTS_NAME_MAIN_MENU);
121         } else {
122             return buildPresenter(SELECT_IN_FILES_NAME_MAIN_MENU);
123         }
124     }
125
126     // Presenter.Popup implementation ------------------------------------------
127

128     public JMenuItem JavaDoc getPopupPresenter() {
129         if (ProjectTab.ID_LOGICAL.equals (this.findIn)) {
130             return buildPresenter(SELECT_IN_PROJECTS_NAME_MENU);
131         } else {
132             return buildPresenter(SELECT_IN_FILES_NAME_MENU);
133         }
134     }
135
136     
137    // Private methods ---------------------------------------------------------
138

139     private FileObject getFileFromLookup( Lookup context ) {
140    
141         FileObject fo = (FileObject) context.lookup(FileObject.class);
142         if (fo != null) {
143             return fo;
144         }
145
146         DataObject dobj = (DataObject)context.lookup( DataObject.class );
147         
148         return dobj == null ? null : dobj.getPrimaryFile();
149     }
150     
151     private JMenuItem JavaDoc buildPresenter (String JavaDoc title) {
152         JMenuItem JavaDoc menuPresenter = new JMenuItem JavaDoc (this);
153         menuPresenter.setText (title);
154         menuPresenter.setIcon(null);
155         
156         return menuPresenter;
157     }
158
159     
160 }
161
Popular Tags