1 19 20 package org.netbeans.modules.project.ui.actions; 21 22 import java.awt.event.ActionEvent ; 23 import java.io.IOException ; 24 import java.util.Arrays ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.Set ; 28 import javax.swing.AbstractAction ; 29 import javax.swing.Action ; 30 import javax.swing.JComponent ; 31 import javax.swing.JMenuItem ; 32 import org.netbeans.api.project.Project; 33 import org.netbeans.api.project.ProjectManager; 34 import org.netbeans.api.project.ProjectUtils; 35 import org.netbeans.modules.project.ui.OpenProjectList; 36 import org.openide.ErrorManager; 37 import org.openide.awt.DynamicMenuContent; 38 import org.openide.loaders.DataFolder; 39 import org.openide.util.ContextAwareAction; 40 import org.openide.util.Lookup; 41 import org.openide.util.NbBundle; 42 import org.openide.util.RequestProcessor; 43 import org.openide.util.actions.Presenter; 44 45 53 public final class OpenProjectFolderAction extends AbstractAction implements ContextAwareAction { 54 55 public OpenProjectFolderAction() { 56 super(NbBundle.getMessage(OpenProjectFolderAction.class, "OpenProjectFolderAction.LBL_action")); 58 } 59 60 public void actionPerformed(ActionEvent e) { 61 assert false; 63 } 64 65 public Action createContextAwareInstance(Lookup context) { 66 return new ContextAction(context); 67 } 68 69 private final class ContextAction extends AbstractAction implements Presenter.Popup { 70 71 72 private final Set <Project> projects; 73 74 public ContextAction(Lookup context) { 75 projects = new HashSet <Project>(); 76 for (DataFolder d: context.lookupAll(DataFolder.class)) { 78 Project p = null; 79 try { 80 p = ProjectManager.getDefault().findProject(d.getPrimaryFile()); 81 } catch (IOException e) { 82 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 83 } 84 if (p != null) { 85 projects.add(p); 86 } 87 } 89 projects.removeAll(Arrays.asList(OpenProjectList.getDefault().getOpenProjects())); 91 int size = projects.size(); 92 if (size == 1) { 93 String name = ProjectUtils.getInformation((Project) projects.iterator().next()).getDisplayName(); 94 putValue(Action.NAME, NbBundle.getMessage(OpenProjectFolderAction.class, "OpenProjectFolderAction.LBL_menu_one", name)); 95 } else if (size > 1) { 96 putValue(Action.NAME, NbBundle.getMessage(OpenProjectFolderAction.class, "OpenProjectFolderAction.LBL_menu_multiple", new Integer (size))); 97 } 98 } 99 100 public void actionPerformed(ActionEvent e) { 101 RequestProcessor.getDefault().post(new Runnable () { 103 public void run() { 104 OpenProjectList.getDefault().open(projects.toArray(new Project[projects.size()]), false, true); 105 } 106 }); 107 } 108 109 public JMenuItem getPopupPresenter() { 110 class Presenter extends JMenuItem implements DynamicMenuContent { 111 public Presenter() { 112 super(ContextAction.this); 113 } 114 public JComponent [] getMenuPresenters() { 115 if (!projects.isEmpty()) { 116 return new JComponent [] {this, null}; 117 } else { 118 return new JComponent [0]; 120 } 121 } 122 public JComponent [] synchMenuPresenters(JComponent [] items) { 123 return getMenuPresenters(); 124 } 125 } 126 return new Presenter(); 127 } 128 129 } 130 131 } 132 | Popular Tags |