1 19 20 package org.netbeans.modules.project.ui.actions; 21 22 import javax.swing.Action ; 23 import javax.swing.Icon ; 24 import javax.swing.JMenuItem ; 25 import javax.swing.KeyStroke ; 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 40 public class ProjectAction extends LookupSensitiveAction implements Presenter.Menu, ContextAwareAction { 41 42 private String command; 43 private ProjectActionPerformer performer; 44 private String namePattern; 45 private String presenterName; 46 private JMenuItem menuPresenter; 47 48 53 public ProjectAction(String command, String namePattern, Icon icon, Lookup lookup) { 54 this( command, null, namePattern, icon, lookup ); 55 } 56 57 public ProjectAction( ProjectActionPerformer performer, String namePattern, Icon icon, Lookup lookup) { 58 this( null, performer, namePattern, icon, lookup ); 59 } 60 61 private ProjectAction( String command, ProjectActionPerformer performer, String namePattern, Icon icon, Lookup lookup ) { 62 super( icon, lookup, new Class [] { 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 key, Object value ) { 75 super.putValue( key, value ); 76 77 if ( key == Action.ACCELERATOR_KEY ) { 78 ActionsUtil.SHORCUTS_MANAGER.registerShortcut( command, value ); 79 80 if (menuPresenter != null) { 82 menuPresenter.setAccelerator((KeyStroke ) 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 getCommand() { 125 return command; 126 } 127 128 protected final String getNamePattern() { 129 return namePattern; 130 } 131 132 protected final void setLocalizedTextToMenuPresented(String presenterName) { 133 if ( menuPresenter != null ) { 134 Mnemonics.setLocalizedText( menuPresenter, presenterName ); 135 } 136 } 137 138 139 141 public JMenuItem getMenuPresenter () { 142 if ( menuPresenter == null ) { 143 menuPresenter = new JMenuItem ( this ); 144 145 Icon icon = null; 146 if (!Boolean.TRUE.equals( getValue( "noIconInMenu" ) ) ) { 148 icon = (Icon )getValue( Action.SMALL_ICON ); 149 } 150 menuPresenter.setIcon( icon ); 151 Mnemonics.setLocalizedText( menuPresenter, presenterName ); 152 } 153 154 return menuPresenter; 155 } 156 157 158 160 public Action createContextAwareInstance( Lookup actionContext ) { 161 return new ProjectAction( command, performer, namePattern, (Icon )getValue( SMALL_ICON ), actionContext ); 162 } 163 164 165 } | Popular Tags |