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.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 44 public class SelectNodeAction extends LookupSensitiveAction implements Presenter.Menu, Presenter.Popup { 45 46 private static final Icon SELECT_IN_PROJECTS_ICON = new ImageIcon ( Utilities.loadImage( "org/netbeans/modules/project/ui/resources/projectTab.gif" ) ); private static final Icon SELECT_IN_FILES_ICON = new ImageIcon ( Utilities.loadImage( "org/netbeans/modules/project/ui/resources/filesTab.gif" ) ); 50 private static final String SELECT_IN_PROJECTS_NAME = NbBundle.getMessage( CloseProject.class, "LBL_SelectInProjectsAction_Name" ); private static final String SELECT_IN_FILES_NAME = NbBundle.getMessage( CloseProject.class, "LBL_SelectInFilesAction_Name" ); 53 private static final String SELECT_IN_PROJECTS_NAME_MENU = NbBundle.getMessage( CloseProject.class, "LBL_SelectInProjectsAction_MenuName" ); private static final String SELECT_IN_FILES_NAME_MENU = NbBundle.getMessage( CloseProject.class, "LBL_SelectInFilesAction_MenuName" ); private static final String SELECT_IN_PROJECTS_NAME_MAIN_MENU = NbBundle.getMessage( CloseProject.class, "LBL_SelectInProjectsAction_MainMenuName" ); private static final String SELECT_IN_FILES_NAME_MAIN_MENU = NbBundle.getMessage( CloseProject.class, "LBL_SelectInFilesAction_MainMenuName" ); 58 private String command; 59 private ProjectActionPerformer performer; 60 private String namePattern; 61 62 private String findIn; 63 64 public static Action 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 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 81 public SelectNodeAction( Icon icon, String name ) { 82 super( icon, null, new Class [] { DataObject.class, FileObject.class } ); 83 this.setDisplayName( name ); 84 } 85 86 private SelectNodeAction(String command, ProjectActionPerformer performer, String namePattern, Icon icon, Lookup lookup) { 87 super( icon, lookup, new Class [] { 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 getCommand() { 109 return command; 110 } 111 112 protected final String getNamePattern() { 113 return namePattern; 114 } 115 116 118 public JMenuItem 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 128 public JMenuItem 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 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 buildPresenter (String title) { 152 JMenuItem menuPresenter = new JMenuItem (this); 153 menuPresenter.setText (title); 154 menuPresenter.setIcon(null); 155 156 return menuPresenter; 157 } 158 159 160 } 161 | Popular Tags |