KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ui > actions > OpenProjectFolderAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.project.ui.actions;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.swing.AbstractAction JavaDoc;
29 import javax.swing.Action JavaDoc;
30 import javax.swing.JComponent JavaDoc;
31 import javax.swing.JMenuItem JavaDoc;
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 /**
46  * Action to open the project(s) corresponding to selected folder(s).
47  * Enabled only for projects which are not already open, and thus useful
48  * for opening projects encountered in the Favorites tab, as well as nested
49  * projects found beneath open projects in the Files tab.
50  * @see "#54122"
51  * @author Jesse Glick
52  */

53 public final class OpenProjectFolderAction extends AbstractAction JavaDoc implements ContextAwareAction {
54     
55     public OpenProjectFolderAction() {
56         // Label not likely displayed in GUI.
57
super(NbBundle.getMessage(OpenProjectFolderAction.class, "OpenProjectFolderAction.LBL_action"));
58     }
59     
60     public void actionPerformed(ActionEvent JavaDoc e) {
61         // Cannot be invoked without any context.
62
assert false;
63     }
64     
65     public Action JavaDoc createContextAwareInstance(Lookup context) {
66         return new ContextAction(context);
67     }
68     
69     private final class ContextAction extends AbstractAction JavaDoc implements Presenter.Popup {
70         
71         /** Projects to be opened. */
72         private final Set JavaDoc<Project> projects;
73         
74         public ContextAction(Lookup context) {
75             projects = new HashSet JavaDoc<Project>();
76             // Collect projects corresponding to selected folders.
77
for (DataFolder d: context.lookupAll(DataFolder.class)) {
78                 Project p = null;
79                 try {
80                     p = ProjectManager.getDefault().findProject(d.getPrimaryFile());
81                 } catch (IOException JavaDoc e) {
82                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
83                 }
84                 if (p != null) {
85                     projects.add(p);
86                 }
87                 // Ignore folders not corresponding to projects (will not disable action if some correspond to projects).
88
}
89             // Ignore projects which are already open (will not disable action if some can be opened).
90
projects.removeAll(Arrays.asList(OpenProjectList.getDefault().getOpenProjects()));
91             int size = projects.size();
92             if (size == 1) {
93                 String JavaDoc 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 JavaDoc(size)));
97             }
98         }
99         
100         public void actionPerformed(ActionEvent JavaDoc e) {
101             // Run asynch so that UI is not blocked; might show progress dialog (?).
102
RequestProcessor.getDefault().post(new Runnable JavaDoc() {
103                 public void run() {
104                     OpenProjectList.getDefault().open(projects.toArray(new Project[projects.size()]), false, true);
105                 }
106             });
107         }
108         
109         public JMenuItem JavaDoc getPopupPresenter() {
110             class Presenter extends JMenuItem JavaDoc implements DynamicMenuContent {
111                 public Presenter() {
112                     super(ContextAction.this);
113                 }
114                 public JComponent JavaDoc[] getMenuPresenters() {
115                     if (!projects.isEmpty()) {
116                         return new JComponent JavaDoc[] {this, null};
117                     } else {
118                         // Disabled, so do not display at all.
119
return new JComponent JavaDoc[0];
120                     }
121                 }
122                 public JComponent JavaDoc[] synchMenuPresenters(JComponent JavaDoc[] items) {
123                     return getMenuPresenters();
124                 }
125             }
126             return new Presenter();
127         }
128         
129     }
130     
131 }
132
Popular Tags