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.ImageIcon ; 25 import javax.swing.JMenuItem ; 26 import org.netbeans.api.project.Project; 27 import org.netbeans.spi.project.ActionProvider; 28 import org.openide.awt.Actions; 29 import org.openide.awt.Mnemonics; 30 import org.openide.filesystems.FileObject; 31 import org.openide.util.Lookup; 32 import org.openide.util.Utilities; 33 import org.openide.util.actions.Presenter; 34 35 37 public final class FileCommandAction extends ProjectAction { 38 39 private String presenterName; 40 41 public FileCommandAction( String command, String namePattern, String iconResource, Lookup lookup ) { 42 this( command, namePattern, new ImageIcon ( Utilities.loadImage( iconResource ) ), lookup ); 43 } 44 45 public FileCommandAction( String command, String namePattern, Icon icon, Lookup lookup ) { 46 super( command, namePattern, icon, lookup ); 47 assert namePattern != null : "Name patern must not be null"; 48 presenterName = ActionsUtil.formatName( getNamePattern(), 0, "" ); 49 setDisplayName( presenterName ); 50 putValue(SHORT_DESCRIPTION, Actions.cutAmpersand(presenterName)); 51 } 52 53 protected void refresh( Lookup context ) { 54 Project[] projects = ActionsUtil.getProjectsFromLookup( context, getCommand() ); 55 56 if ( projects.length != 1 ) { 57 setEnabled( false ); presenterName = ActionsUtil.formatName( getNamePattern(), 0, "" ); 59 } 60 else { 61 FileObject[] files = ActionsUtil.getFilesFromLookup( context, projects[0] ); 62 setEnabled( true ); 63 presenterName = ActionsUtil.formatName( getNamePattern(), files.length, files.length > 0 ? files[0].getNameExt() : "" ); } 65 66 setLocalizedTextToMenuPresented(presenterName); 67 68 putValue(SHORT_DESCRIPTION, Actions.cutAmpersand(presenterName)); 69 } 70 71 protected void actionPerformed( Lookup context ) { 72 73 Project[] projects = ActionsUtil.getProjectsFromLookup( context, getCommand() ); 74 75 if ( projects.length == 1 ) { 76 ActionProvider ap = (ActionProvider)projects[0].getLookup().lookup( ActionProvider.class ); 77 ap.invokeAction( getCommand(), context ); 78 } 79 80 } 81 82 83 public Action createContextAwareInstance( Lookup actionContext ) { 84 85 return new FileCommandAction( getCommand(), getNamePattern(), (Icon )getValue( SMALL_ICON ), actionContext ); 86 } 87 88 89 } | Popular Tags |