]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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; | |
14 | import java.util.Iterator; | |
15 | import java.util.List; | |
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.jface.action.IMenuManager; | |
24 | import org.eclipse.jface.viewers.ISelection; | |
25 | import org.eclipse.jface.viewers.ISelectionChangedListener; | |
26 | import org.eclipse.jface.viewers.ISelectionProvider; | |
27 | import org.eclipse.jface.viewers.IStructuredSelection; | |
28 | import org.eclipse.jface.viewers.SelectionChangedEvent; | |
29 | import org.eclipse.jface.viewers.StructuredSelection; | |
30 | ||
31 | import org.eclipse.ui.IActionBars; | |
32 | import org.eclipse.ui.IViewPart; | |
33 | import org.eclipse.ui.IWorkbenchCommandConstants; | |
34 | import org.eclipse.ui.IWorkbenchSite; | |
35 | import org.eclipse.ui.IWorkingSet; | |
36 | import org.eclipse.ui.actions.ActionGroup; | |
37 | import org.eclipse.ui.actions.CloseResourceAction; | |
38 | import org.eclipse.ui.actions.CloseUnrelatedProjectsAction; | |
39 | import org.eclipse.ui.ide.IDEActionFactory; | |
40 | ||
41 | import org.eclipse.jdt.core.IJavaProject; | |
42 | ||
43 | import org.eclipse.jdt.ui.IContextMenuConstants; | |
44 | ||
45 | ||
46 | /** | |
47 | * Adds actions to open and close a project to the global menu bar. | |
48 | * | |
49 | * <p> | |
50 | * This class may be instantiated; it is not intended to be subclassed. | |
51 | * </p> | |
52 | * | |
53 | * @since 2.0 | |
54 | * | |
55 | * @noextend This class is not intended to be subclassed by clients. | |
56 | */ | |
57 | public class ProjectActionGroup extends ActionGroup { | |
58 | ||
59 | private ISelectionProvider fSelectionProvider; | |
60 | ||
61 | OpenProjectAction fOpenAction; | |
62 | boolean fEnableOpenInContextMenu= true; | |
63 | CloseResourceAction fCloseAction; | |
64 | CloseResourceAction fCloseUnrelatedAction; | |
65 | ||
66 | private ISelectionChangedListener fSelectionChangedListener; | |
67 | ||
68 | ||
69 | /** | |
70 | * Creates a new <code>ProjectActionGroup</code>. The group requires | |
71 | * that the selection provided by the site's selection provider is of type <code> | |
72 | * org.eclipse.jface.viewers.IStructuredSelection</code>. | |
73 | * | |
74 | * @param part the view part that owns this action group | |
75 | */ | |
76 | public ProjectActionGroup(IViewPart part) { | |
77 | this(part.getSite(), part.getSite().getSelectionProvider()); | |
78 | } | |
79 | ||
80 | /** | |
81 | * Creates a new <code>ProjectActionGroup</code>. The group requires | |
82 | * that the selection provided by the given selection provider is of type | |
83 | * {@link IStructuredSelection}. | |
84 | * | |
85 | * @param site the site that will own the action group. | |
86 | * @param selectionProvider the selection provider used instead of the | |
87 | * page selection provider. | |
88 | * | |
89 | * @since 3.4 | |
90 | */ | |
91 | public ProjectActionGroup(IWorkbenchSite site, ISelectionProvider selectionProvider) { | |
92 | fSelectionProvider= selectionProvider; | |
93 | ISelection selection= selectionProvider.getSelection(); | |
94 | ||
95 | fCloseAction= new CloseResourceAction(site); | |
96 | fCloseAction.setActionDefinitionId(IWorkbenchCommandConstants.PROJECT_CLOSE_PROJECT); | |
97 | ||
98 | fCloseUnrelatedAction= new CloseUnrelatedProjectsAction(site); | |
99 | fCloseUnrelatedAction.setActionDefinitionId(IWorkbenchCommandConstants.PROJECT_CLOSE_UNRELATED_PROJECTS); | |
100 | ||
101 | fOpenAction= new OpenProjectAction(site); | |
102 | fOpenAction.generated_7585570843243629004(this, selection); | |
103 | ||
104 | fSelectionChangedListener= new ISelectionChangedListener() { | |
105 | public void selectionChanged(SelectionChangedEvent event) { | |
106 | ISelection s= event.getSelection(); | |
107 | if (s instanceof IStructuredSelection) { | |
108 | performSelectionChanged((IStructuredSelection) s); | |
109 | } | |
110 | } | |
111 | }; | |
112 | selectionProvider.addSelectionChangedListener(fSelectionChangedListener); | |
113 | ||
114 | IWorkspace workspace= ResourcesPlugin.getWorkspace(); | |
115 | workspace.addResourceChangeListener(fOpenAction); | |
116 | workspace.addResourceChangeListener(fCloseAction); | |
117 | workspace.addResourceChangeListener(fCloseUnrelatedAction); | |
118 | } | |
119 | ||
120 | protected void performSelectionChanged(IStructuredSelection structuredSelection) { | |
121 | Object[] array= structuredSelection.toArray(); | |
122 | ArrayList<IProject> openProjects= new ArrayList<IProject>(); | |
123 | int selectionStatus= evaluateSelection(array, openProjects); | |
124 | StructuredSelection sel= new StructuredSelection(openProjects); | |
125 | ||
126 | fOpenAction.setEnabled(hasClosedProjectsInWorkspace()); | |
127 | fEnableOpenInContextMenu= (selectionStatus & CLOSED_PROJECTS_SELECTED) != 0 | |
128 | || (selectionStatus == 0 && array.length == 0 && hasClosedProjectsInWorkspace()); | |
129 | fCloseAction.selectionChanged(sel); | |
130 | fCloseUnrelatedAction.selectionChanged(sel); | |
131 | } | |
132 | ||
133 | private int CLOSED_PROJECTS_SELECTED= 1; | |
134 | private int NON_PROJECT_SELECTED= 2; | |
135 | ||
136 | private int evaluateSelection(Object[] array, List<IProject> allOpenProjects) { | |
137 | int status= 0; | |
138 | for (int i= 0; i < array.length; i++) { | |
139 | Object curr= array[i]; | |
140 | if (curr instanceof IJavaProject) { | |
141 | curr= ((IJavaProject) curr).getProject(); | |
142 | } | |
143 | if (curr instanceof IProject) { | |
144 | IProject project= (IProject) curr; | |
145 | if (project.isOpen()) { | |
146 | allOpenProjects.add(project); | |
147 | } else { | |
148 | status |= CLOSED_PROJECTS_SELECTED; | |
149 | } | |
150 | } else { | |
151 | if (curr instanceof IWorkingSet) { | |
152 | int res= evaluateSelection(((IWorkingSet) curr).getElements(), allOpenProjects); | |
153 | status |= res; | |
154 | } else { | |
155 | status |= NON_PROJECT_SELECTED; | |
156 | } | |
157 | } | |
158 | } | |
159 | return status; | |
160 | } | |
161 | ||
162 | private boolean hasClosedProjectsInWorkspace() { | |
163 | IProject[] projects= ResourcesPlugin.getWorkspace().getRoot().getProjects(); | |
164 | for (int i = 0; i < projects.length; i++) { | |
165 | if (!projects[i].isOpen()) | |
166 | return true; | |
167 | } | |
168 | return false; | |
169 | } | |
170 | ||
171 | /* (non-Javadoc) | |
172 | * Method declared in ActionGroup | |
173 | */ | |
174 | @Override | |
175 | public void fillActionBars(IActionBars actionBars) { | |
176 | super.fillActionBars(actionBars); | |
177 | actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_PROJECT.getId(), fCloseAction); | |
178 | actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_UNRELATED_PROJECTS.getId(), fCloseUnrelatedAction); | |
179 | actionBars.setGlobalActionHandler(IDEActionFactory.OPEN_PROJECT.getId(), fOpenAction); | |
180 | } | |
181 | ||
182 | /* (non-Javadoc) | |
183 | * Method declared in ActionGroup | |
184 | */ | |
185 | @Override | |
186 | public void fillContextMenu(IMenuManager menu) { | |
187 | super.fillContextMenu(menu); | |
188 | fOpenAction.generated_6752124981828432881(this, menu); | |
189 | if (fCloseUnrelatedAction.isEnabled() && areOnlyProjectsSelected(fCloseUnrelatedAction.getStructuredSelection())) | |
190 | menu.appendToGroup(IContextMenuConstants.GROUP_BUILD, fCloseUnrelatedAction); | |
191 | } | |
192 | ||
193 | /** | |
194 | * Returns the open project action contained in this project action group. | |
195 | * | |
196 | * @return returns the open project action | |
197 | * | |
198 | * @since 3.3 | |
199 | */ | |
200 | public OpenProjectAction getOpenProjectAction() { | |
201 | return fOpenAction; | |
202 | } | |
203 | ||
204 | private boolean areOnlyProjectsSelected(IStructuredSelection selection) { | |
205 | if (selection.isEmpty()) | |
206 | return false; | |
207 | ||
208 | Iterator<?> iter= selection.iterator(); | |
209 | while (iter.hasNext()) { | |
210 | Object obj= iter.next(); | |
211 | if (obj instanceof IAdaptable) { | |
212 | if (((IAdaptable)obj).getAdapter(IProject.class) == null) | |
213 | return false; | |
214 | } | |
215 | } | |
216 | return true; | |
217 | } | |
218 | ||
219 | /* | |
220 | * @see ActionGroup#dispose() | |
221 | */ | |
222 | @Override | |
223 | public void dispose() { | |
224 | fSelectionProvider.removeSelectionChangedListener(fSelectionChangedListener); | |
225 | fSelectionProvider= null; | |
226 | ||
227 | IWorkspace workspace = ResourcesPlugin.getWorkspace(); | |
228 | workspace.removeResourceChangeListener(fOpenAction); | |
229 | workspace.removeResourceChangeListener(fCloseAction); | |
230 | workspace.removeResourceChangeListener(fCloseUnrelatedAction); | |
231 | super.dispose(); | |
232 | } | |
233 | } |