KickJava   Java API By Example, From Geeks To Geeks.

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


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.JMenuItem JavaDoc;
25 import javax.swing.KeyStroke JavaDoc;
26 import org.netbeans.api.project.Project;
27 import org.netbeans.spi.project.ActionProvider;
28 import org.netbeans.spi.project.ui.support.ProjectActionPerformer;
29 import org.openide.awt.Actions;
30 import org.openide.awt.Mnemonics;
31 import org.openide.loaders.DataObject;
32 import org.openide.util.ContextAwareAction;
33 import org.openide.util.Lookup;
34 import org.openide.util.actions.Presenter;
35
36 /** Action sensitive to current project
37  *
38  * @author Pet Hrebejk
39  */

40 public class ProjectAction extends LookupSensitiveAction implements Presenter.Menu, ContextAwareAction {
41     
42     private String JavaDoc command;
43     private ProjectActionPerformer performer;
44     private String JavaDoc namePattern;
45     private String JavaDoc presenterName;
46     private JMenuItem JavaDoc menuPresenter;
47     
48     /**
49      * Constructor for global actions. E.g. actions in main menu which
50      * listen to the global context.
51      *
52      */

53     public ProjectAction(String JavaDoc command, String JavaDoc namePattern, Icon JavaDoc icon, Lookup lookup) {
54         this( command, null, namePattern, icon, lookup );
55     }
56     
57     public ProjectAction( ProjectActionPerformer performer, String JavaDoc namePattern, Icon JavaDoc icon, Lookup lookup) {
58         this( null, performer, namePattern, icon, lookup );
59     }
60     
61     private ProjectAction( String JavaDoc command, ProjectActionPerformer performer, String JavaDoc namePattern, Icon JavaDoc icon, Lookup lookup ) {
62         super( icon, lookup, new Class JavaDoc[] { Project.class, DataObject.class } );
63         this.command = command;
64         if ( command != null ) {
65             ActionsUtil.SHORCUTS_MANAGER.registerAction( command, this );
66         }
67         this.performer = performer;
68         this.namePattern = namePattern;
69         presenterName = ActionsUtil.formatName( getNamePattern(), 0, "" );
70         setDisplayName( presenterName );
71         putValue(SHORT_DESCRIPTION, Actions.cutAmpersand(presenterName));
72     }
73     
74     public void putValue( String JavaDoc key, Object JavaDoc value ) {
75         super.putValue( key, value );
76         
77         if ( key == Action.ACCELERATOR_KEY ) {
78             ActionsUtil.SHORCUTS_MANAGER.registerShortcut( command, value );
79             
80             //#68616: make sure the accelarator is propagated into the menu:
81
if (menuPresenter != null) {
82                 menuPresenter.setAccelerator((KeyStroke JavaDoc) value);
83             }
84         }
85         
86     }
87        
88     protected void actionPerformed( Lookup context ) {
89         Project[] projects = ActionsUtil.getProjectsFromLookup( context, command );
90         
91         if ( projects.length == 1 ) {
92             if ( command != null ) {
93                 ActionProvider ap = projects[0].getLookup().lookup(ActionProvider.class);
94                 ap.invokeAction( command, Lookup.EMPTY );
95             }
96             else if ( performer != null ) {
97                 performer.perform( projects[0] );
98             }
99         }
100         
101     }
102     
103     protected void refresh( Lookup context ) {
104         Project[] projects = ActionsUtil.getProjectsFromLookup( context, command );
105         
106         if ( command != null ) {
107             setEnabled( projects.length == 1 );
108             presenterName = ActionsUtil.formatProjectSensitiveName( namePattern, projects );
109         }
110         else if ( performer != null && projects.length == 1 ) {
111             setEnabled( performer.enable( projects[0] ) );
112             presenterName = ActionsUtil.formatProjectSensitiveName( namePattern, projects );
113         }
114         else {
115             setEnabled( false );
116             presenterName = ActionsUtil.formatProjectSensitiveName( namePattern, projects );
117         }
118         
119         setLocalizedTextToMenuPresented(presenterName);
120                         
121         putValue(SHORT_DESCRIPTION, Actions.cutAmpersand(presenterName));
122     }
123     
124     protected final String JavaDoc getCommand() {
125         return command;
126     }
127     
128     protected final String JavaDoc getNamePattern() {
129         return namePattern;
130     }
131     
132     protected final void setLocalizedTextToMenuPresented(String JavaDoc presenterName) {
133         if ( menuPresenter != null ) {
134             Mnemonics.setLocalizedText( menuPresenter, presenterName );
135         }
136     }
137     
138     
139     // Implementation of Presenter.Menu ----------------------------------------
140

141     public JMenuItem JavaDoc getMenuPresenter () {
142         if ( menuPresenter == null ) {
143             menuPresenter = new JMenuItem JavaDoc( this );
144
145             Icon JavaDoc icon = null;
146             // ignore icon if noIconInMenu flag is set
147
if (!Boolean.TRUE.equals( getValue( "noIconInMenu" ) ) ) {
148                 icon = (Icon JavaDoc)getValue( Action.SMALL_ICON );
149             }
150             menuPresenter.setIcon( icon );
151             Mnemonics.setLocalizedText( menuPresenter, presenterName );
152         }
153         
154         return menuPresenter;
155     }
156     
157     
158     // Implementation of ContextAwareAction ------------------------------------
159

160     public Action JavaDoc createContextAwareInstance( Lookup actionContext ) {
161         return new ProjectAction( command, performer, namePattern, (Icon JavaDoc)getValue( SMALL_ICON ), actionContext );
162     }
163
164     
165 }
Popular Tags