1 19 20 package org.netbeans.modules.project.ui.actions; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.util.Arrays ; 27 import javax.swing.Action ; 28 import javax.swing.JMenu ; 29 import javax.swing.JMenuItem ; 30 import javax.swing.JRadioButtonMenuItem ; 31 import org.netbeans.api.project.Project; 32 import org.netbeans.api.project.ProjectUtils; 33 import org.netbeans.modules.project.ui.OpenProjectList; 34 import org.netbeans.api.project.ProjectInformation; 35 import org.openide.util.Lookup; 36 import org.openide.util.NbBundle; 37 import org.openide.util.WeakListeners; 38 import org.openide.util.actions.Presenter; 39 40 public class SetMainProject extends ProjectAction implements Presenter.Menu, PropertyChangeListener { 41 42 private static final String namePattern = NbBundle.getMessage( SetMainProject.class, "LBL_SetAsMainProjectAction_Name" ); 44 46 private static final String PROJECT_KEY = "org.netbeans.modules.project.ui.MainProjectItem"; 48 protected JMenu subMenu; 49 50 52 public SetMainProject() { 53 this( null ); 54 } 55 56 public SetMainProject( Lookup context ) { 57 super( SetMainProject.class.getName() , namePattern, null, context ); 58 if ( context == null ) { 61 OpenProjectList.getDefault().addPropertyChangeListener( WeakListeners.propertyChange( this, OpenProjectList.getDefault() ) ); 62 } 63 refresh( getLookup() ); 64 } 65 66 protected void actionPerformed( Lookup context ) { 67 Project[] projects = ActionsUtil.getProjectsFromLookup( context, null ); 68 69 if ( projects != null && projects.length > 0 ) 70 OpenProjectList.getDefault().setMainProject( projects[0] ); 71 72 } 73 74 public void refresh( Lookup context ) { 75 76 super.refresh( context ); 77 78 Project[] projects = ActionsUtil.getProjectsFromLookup( context, null ); 79 if ( projects.length != 1 ) { 80 setEnabled( false ); 81 } 82 else { 83 setEnabled( true ); 84 } 85 } 86 87 public Action createContextAwareInstance( Lookup actionContext ) { 88 return new SetMainProject( actionContext ); 89 } 90 91 public JMenuItem getMenuPresenter() { 92 createSubMenu(); 93 return subMenu; 94 } 95 96 private void createSubMenu() { 97 98 Project projects[] = OpenProjectList.getDefault().getOpenProjects(); 99 100 Arrays.sort( projects, OpenProjectList.PROJECT_BY_DISPLAYNAME ); 101 102 if ( projects == null || projects.length == 0 ) { 104 setEnabled( false ); 105 } 106 else { 107 setEnabled( true ); 108 } 109 110 if ( subMenu == null ) { 111 String label = NbBundle.getMessage( SetMainProject.class, "LBL_SetMainProjectAction_Name" ); subMenu = new JMenu ( label ); 113 subMenu.setMnemonic (NbBundle.getMessage (SetMainProject.class, "MNE_SetMainProjectAction_Name").charAt (0)); subMenu.putClientProperty(SetMainProject.class, this); 119 } 120 121 subMenu.removeAll(); 122 ActionListener jmiActionListener = new MenuItemActionListener(); 123 124 125 for ( int i = 0; i < projects.length; i++ ) { 127 ProjectInformation pi = ProjectUtils.getInformation(projects[i]); 128 JRadioButtonMenuItem jmi = new JRadioButtonMenuItem (pi.getDisplayName(), pi.getIcon(), false); 129 subMenu.add( jmi ); 130 jmi.putClientProperty( PROJECT_KEY, projects[i] ); 131 jmi.addActionListener( jmiActionListener ); 132 } 133 134 selectMainProject(); 136 137 subMenu.setEnabled( projects.length > 0 ); 138 139 } 140 141 private void selectMainProject() { 142 143 for( int i = 0; i < subMenu.getItemCount(); i++ ) { 144 JMenuItem jmi = subMenu.getItem( i ); 145 Project project = (Project)jmi.getClientProperty( PROJECT_KEY ); 146 147 if ( jmi instanceof JRadioButtonMenuItem ) { 148 if ( OpenProjectList.getDefault().isMainProject( project ) ) { 149 ((JRadioButtonMenuItem )jmi).setSelected( true ); 150 } 151 else { 152 ((JRadioButtonMenuItem )jmi).setSelected( false ); 153 } 154 } 155 } 156 157 } 158 159 161 162 public void propertyChange( PropertyChangeEvent e ) { 163 164 if ( OpenProjectList.PROPERTY_OPEN_PROJECTS.equals( e.getPropertyName() ) ) { 165 createSubMenu(); 166 } 167 else if ( OpenProjectList.PROPERTY_MAIN_PROJECT.equals( e.getPropertyName() ) && subMenu != null ) { 168 selectMainProject(); 169 } 170 171 172 } 173 174 176 private static class MenuItemActionListener implements ActionListener { 177 178 public void actionPerformed( ActionEvent e ) { 179 180 if ( e.getSource() instanceof JMenuItem ) { 181 JMenuItem jmi = (JMenuItem )e.getSource(); 182 Project project = (Project)jmi.getClientProperty( PROJECT_KEY ); 183 if ( project != null ) { 184 OpenProjectList.getDefault().setMainProject( project ); 185 } 186 187 } 188 189 } 190 191 } 192 193 197 public static final class PopupWithoutContext extends SetMainProject implements Presenter.Popup { 198 199 public PopupWithoutContext() {} 200 201 private PopupWithoutContext(Lookup actionContext) { 202 super(actionContext); 203 } 204 205 public JMenuItem getPopupPresenter() { 206 subMenu = null; 208 return getMenuPresenter(); 209 } 210 211 public Action createContextAwareInstance(Lookup actionContext) { 212 return new PopupWithoutContext(actionContext); 213 } 214 215 } 216 217 } 218 | Popular Tags |