KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > actions > ProjectActionGroup


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.ui.actions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.IAdaptable;
18
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.resources.IWorkspace;
21 import org.eclipse.core.resources.ResourcesPlugin;
22
23 import org.eclipse.swt.widgets.Shell;
24
25 import org.eclipse.jface.action.IMenuManager;
26 import org.eclipse.jface.viewers.ISelection;
27 import org.eclipse.jface.viewers.ISelectionChangedListener;
28 import org.eclipse.jface.viewers.ISelectionProvider;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.jface.viewers.SelectionChangedEvent;
31 import org.eclipse.jface.viewers.StructuredSelection;
32
33 import org.eclipse.ui.IActionBars;
34 import org.eclipse.ui.IViewPart;
35 import org.eclipse.ui.IWorkbenchSite;
36 import org.eclipse.ui.IWorkingSet;
37 import org.eclipse.ui.actions.ActionGroup;
38 import org.eclipse.ui.actions.CloseResourceAction;
39 import org.eclipse.ui.actions.CloseUnrelatedProjectsAction;
40
41 import org.eclipse.ui.ide.IDEActionFactory;
42
43 import org.eclipse.jdt.core.IJavaProject;
44
45 import org.eclipse.jdt.ui.IContextMenuConstants;
46
47 /**
48  * Adds actions to open and close a project to the global menu bar.
49  *
50  * <p>
51  * This class may be instantiated; it is not intended to be subclassed.
52  * </p>
53  *
54  * @since 2.0
55  */

56 public class ProjectActionGroup extends ActionGroup {
57
58     private IWorkbenchSite fSite;
59
60     private OpenProjectAction fOpenAction;
61     private CloseResourceAction fCloseAction;
62     private CloseResourceAction fCloseUnrelatedAction;
63     
64     private ISelectionChangedListener fSelectionChangedListener;
65
66     /**
67      * Creates a new <code>ProjectActionGroup</code>. The group requires
68      * that the selection provided by the site's selection provider is of type <code>
69      * org.eclipse.jface.viewers.IStructuredSelection</code>.
70      *
71      * @param part the view part that owns this action group
72      */

73     public ProjectActionGroup(IViewPart part) {
74         fSite = part.getSite();
75         Shell shell= fSite.getShell();
76         ISelectionProvider provider= fSite.getSelectionProvider();
77         ISelection selection= provider.getSelection();
78         
79         fCloseAction= new CloseResourceAction(shell);
80         fCloseAction.setActionDefinitionId("org.eclipse.ui.project.closeProject"); //$NON-NLS-1$
81

82         fCloseUnrelatedAction= new CloseUnrelatedProjectsAction(shell);
83         fCloseUnrelatedAction.setActionDefinitionId("org.eclipse.ui.project.closeUnrelatedProjects"); //$NON-NLS-1$
84

85         fOpenAction= new OpenProjectAction(fSite);
86         fOpenAction.setActionDefinitionId("org.eclipse.ui.project.openProject"); //$NON-NLS-1$
87
if (selection instanceof IStructuredSelection) {
88             IStructuredSelection s= (IStructuredSelection)selection;
89             fOpenAction.selectionChanged(s);
90             fCloseAction.selectionChanged(s);
91             fCloseUnrelatedAction.selectionChanged(s);
92         }
93         
94         fSelectionChangedListener= new ISelectionChangedListener() {
95             public void selectionChanged(SelectionChangedEvent event) {
96                 ISelection s= event.getSelection();
97                 if (s instanceof IStructuredSelection) {
98                     performSelectionChanged((IStructuredSelection) s);
99                 }
100             }
101         };
102         provider.addSelectionChangedListener(fSelectionChangedListener);
103
104         IWorkspace workspace= ResourcesPlugin.getWorkspace();
105         workspace.addResourceChangeListener(fOpenAction);
106         workspace.addResourceChangeListener(fCloseAction);
107         workspace.addResourceChangeListener(fCloseUnrelatedAction);
108     }
109     
110     protected void performSelectionChanged(IStructuredSelection structuredSelection) {
111         Object JavaDoc[] array= structuredSelection.toArray();
112         ArrayList JavaDoc openProjects= new ArrayList JavaDoc();
113         int selectionStatus= evaluateSelection(array, openProjects);
114         StructuredSelection sel= new StructuredSelection(openProjects);
115
116         fOpenAction.setEnabled(selectionStatus == CLOSED_PROJECTS_SELECTED || (selectionStatus == 0 && hasClosedProjectsInWorkspace()));
117         fCloseAction.selectionChanged(sel);
118         fCloseUnrelatedAction.selectionChanged(sel);
119     }
120     
121     private int CLOSED_PROJECTS_SELECTED= 1;
122     private int NON_PROJECT_SELECTED= 2;
123
124     private int evaluateSelection(Object JavaDoc[] array, List JavaDoc allOpenProjects) {
125         int status= 0;
126         for (int i= 0; i < array.length; i++) {
127             Object JavaDoc curr= array[i];
128             if (curr instanceof IJavaProject) {
129                 curr= ((IJavaProject) curr).getProject();
130             }
131             if (curr instanceof IProject) {
132                 IProject project= (IProject) curr;
133                 if (project.isOpen()) {
134                     allOpenProjects.add(project);
135                 } else {
136                     status |= CLOSED_PROJECTS_SELECTED;
137                 }
138             } else {
139                 if (curr instanceof IWorkingSet) {
140                     int res= evaluateSelection(((IWorkingSet) curr).getElements(), allOpenProjects);
141                     status |= res;
142                 } else {
143                     status |= NON_PROJECT_SELECTED;
144                 }
145             }
146         }
147         return status;
148     }
149         
150     private boolean hasClosedProjectsInWorkspace() {
151         IProject[] projects= ResourcesPlugin.getWorkspace().getRoot().getProjects();
152         for (int i = 0; i < projects.length; i++) {
153             if (!projects[i].isOpen())
154                 return true;
155         }
156         return false;
157     }
158     
159     /* (non-Javadoc)
160      * Method declared in ActionGroup
161      */

162     public void fillActionBars(IActionBars actionBars) {
163         super.fillActionBars(actionBars);
164         actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_PROJECT.getId(), fCloseAction);
165         actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_UNRELATED_PROJECTS.getId(), fCloseUnrelatedAction);
166         actionBars.setGlobalActionHandler(IDEActionFactory.OPEN_PROJECT.getId(), fOpenAction);
167     }
168     
169     /* (non-Javadoc)
170      * Method declared in ActionGroup
171      */

172     public void fillContextMenu(IMenuManager menu) {
173         super.fillContextMenu(menu);
174         if (fOpenAction.isEnabled())
175             menu.appendToGroup(IContextMenuConstants.GROUP_BUILD, fOpenAction);
176         if (fCloseAction.isEnabled())
177             menu.appendToGroup(IContextMenuConstants.GROUP_BUILD, fCloseAction);
178         if (fCloseUnrelatedAction.isEnabled() && areOnlyProjectsSelected(fCloseUnrelatedAction.getStructuredSelection()))
179             menu.appendToGroup(IContextMenuConstants.GROUP_BUILD, fCloseUnrelatedAction);
180     }
181     
182     /**
183      * Returns the open project action contained in this project action group.
184      *
185      * @return returns the open project action
186      *
187      * @since 3.3
188      */

189     public OpenProjectAction getOpenProjectAction() {
190         return fOpenAction;
191     }
192
193     private boolean areOnlyProjectsSelected(IStructuredSelection selection) {
194         if (selection.isEmpty())
195             return false;
196         
197         Iterator JavaDoc iter= selection.iterator();
198         while (iter.hasNext()) {
199             Object JavaDoc obj= iter.next();
200             if (obj instanceof IAdaptable) {
201                 if (((IAdaptable)obj).getAdapter(IProject.class) == null)
202                     return false;
203             }
204         }
205         return true;
206     }
207
208     /*
209      * @see ActionGroup#dispose()
210      */

211     public void dispose() {
212         ISelectionProvider provider= fSite.getSelectionProvider();
213         provider.removeSelectionChangedListener(fSelectionChangedListener);
214         
215         IWorkspace workspace = ResourcesPlugin.getWorkspace();
216         workspace.removeResourceChangeListener(fOpenAction);
217         workspace.removeResourceChangeListener(fCloseAction);
218         workspace.removeResourceChangeListener(fCloseUnrelatedAction);
219         super.dispose();
220     }
221 }
222
Popular Tags