KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > resources > actions > ResourceMgmtActionProvider


1 /*******************************************************************************
2  * Copyright (c) 2006 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
12 package org.eclipse.ui.internal.navigator.resources.actions;
13
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.resources.ICommand;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IncrementalProjectBuilder;
19 import org.eclipse.core.resources.ResourcesPlugin;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IAdaptable;
22 import org.eclipse.jface.action.IMenuManager;
23 import org.eclipse.jface.resource.ImageDescriptor;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.swt.widgets.Shell;
26 import org.eclipse.ui.IActionBars;
27 import org.eclipse.ui.actions.ActionFactory;
28 import org.eclipse.ui.actions.BuildAction;
29 import org.eclipse.ui.actions.CloseResourceAction;
30 import org.eclipse.ui.actions.CloseUnrelatedProjectsAction;
31 import org.eclipse.ui.actions.OpenResourceAction;
32 import org.eclipse.ui.actions.RefreshAction;
33 import org.eclipse.ui.ide.IDEActionFactory;
34 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
35 import org.eclipse.ui.navigator.CommonActionProvider;
36 import org.eclipse.ui.navigator.ICommonActionExtensionSite;
37 import org.eclipse.ui.navigator.ICommonMenuConstants;
38
39 /**
40  * @since 3.2
41  *
42  */

43 public class ResourceMgmtActionProvider extends CommonActionProvider {
44
45     private BuildAction buildAction;
46
47     private OpenResourceAction openProjectAction;
48
49     private CloseResourceAction closeProjectAction;
50     
51     private CloseUnrelatedProjectsAction closeUnrelatedProjectsAction;
52
53     private RefreshAction refreshAction;
54
55     private Shell shell;
56     
57     /* (non-Javadoc)
58      * @see org.eclipse.ui.navigator.CommonActionProvider#init(org.eclipse.ui.navigator.ICommonActionExtensionSite)
59      */

60     public void init(ICommonActionExtensionSite aSite) {
61             super.init(aSite);
62             shell = aSite.getViewSite().getShell();
63             makeActions();
64     }
65     
66     public void fillActionBars(IActionBars actionBars) {
67         actionBars.setGlobalActionHandler(ActionFactory.REFRESH.getId(),
68                 refreshAction);
69         actionBars.setGlobalActionHandler(IDEActionFactory.BUILD_PROJECT
70                 .getId(), buildAction);
71         actionBars.setGlobalActionHandler(
72                 IDEActionFactory.OPEN_PROJECT.getId(), openProjectAction);
73         actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_PROJECT
74                 .getId(), closeProjectAction);
75         actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_UNRELATED_PROJECTS
76                 .getId(), closeUnrelatedProjectsAction);
77         updateActionBars();
78     }
79     
80
81     /**
82      * Adds the build, open project, close project and refresh resource
83      * actions to the context menu.
84      * <p>
85      * The following conditions apply:
86      * build-only projects selected, auto build disabled, at least one
87      * builder present
88      * open project-only projects selected, at least one closed project
89      * close project-only projects selected, at least one open project
90      * refresh-no closed project selected
91      * </p>
92      * <p>
93      * Both the open project and close project action may be on the menu
94      * at the same time.
95      * </p>
96      * <p>
97      * No disabled action should be on the context menu.
98      * </p>
99      *
100      * @param menu context menu to add actions to
101      */

102     public void fillContextMenu(IMenuManager menu) {
103         IStructuredSelection selection = (IStructuredSelection) getContext()
104                 .getSelection();
105         boolean isProjectSelection = true;
106         boolean hasOpenProjects = false;
107         boolean hasClosedProjects = false;
108         boolean hasBuilder = true; // false if any project is closed or does not have builder
109
Iterator JavaDoc resources = selection.iterator();
110
111         while (resources.hasNext()
112                 && (!hasOpenProjects || !hasClosedProjects || hasBuilder || isProjectSelection)) {
113             Object JavaDoc next = resources.next();
114             IProject project = null;
115
116             if (next instanceof IProject) {
117                 project = (IProject) next;
118             } else if (next instanceof IAdaptable) {
119                 project = (IProject) ((IAdaptable) next)
120                         .getAdapter(IProject.class);
121             }
122
123             if (project == null) {
124                 isProjectSelection = false;
125                 continue;
126             }
127             if (project.isOpen()) {
128                 hasOpenProjects = true;
129                 if (hasBuilder && !hasBuilder(project)) {
130                     hasBuilder = false;
131                 }
132             } else {
133                 hasClosedProjects = true;
134                 hasBuilder = false;
135             }
136         }
137         if (!selection.isEmpty() && isProjectSelection
138                 && !ResourcesPlugin.getWorkspace().isAutoBuilding()
139                 && hasBuilder) {
140             // Allow manual incremental build only if auto build is off.
141
buildAction.selectionChanged(selection);
142             menu.appendToGroup(ICommonMenuConstants.GROUP_BUILD, buildAction);
143         }
144         if (!hasClosedProjects) {
145             refreshAction.selectionChanged(selection);
146             menu.appendToGroup(ICommonMenuConstants.GROUP_BUILD, refreshAction);
147         }
148         if (isProjectSelection) {
149             if (hasClosedProjects) {
150                 openProjectAction.selectionChanged(selection);
151                 menu.appendToGroup(ICommonMenuConstants.GROUP_BUILD, openProjectAction);
152             }
153             if (hasOpenProjects) {
154                 closeProjectAction.selectionChanged(selection);
155                 menu.appendToGroup(ICommonMenuConstants.GROUP_BUILD, closeProjectAction);
156                 closeUnrelatedProjectsAction.selectionChanged(selection);
157                 menu.appendToGroup(ICommonMenuConstants.GROUP_BUILD, closeUnrelatedProjectsAction);
158             }
159         }
160     }
161     
162
163     /**
164      * Returns whether there are builders configured on the given project.
165      *
166      * @return <code>true</code> if it has builders,
167      * <code>false</code> if not, or if this could not be determined
168      */

169     boolean hasBuilder(IProject project) {
170         try {
171             ICommand[] commands = project.getDescription().getBuildSpec();
172             if (commands.length > 0) {
173                 return true;
174             }
175         } catch (CoreException e) {
176             // Cannot determine if project has builders. Project is closed
177
// or does not exist. Fall through to return false.
178
}
179         return false;
180     }
181
182     protected void makeActions() {
183         //Shell shell = navigator.getSite().getShell();
184
openProjectAction = new OpenResourceAction(shell);
185         
186         closeProjectAction = new CloseResourceAction(shell);
187         
188         closeUnrelatedProjectsAction = new CloseUnrelatedProjectsAction(shell);
189         
190         refreshAction = new RefreshAction(shell);
191         refreshAction
192                 .setDisabledImageDescriptor(getImageDescriptor("dlcl16/refresh_nav.gif"));//$NON-NLS-1$
193
refreshAction
194                 .setImageDescriptor(getImageDescriptor("elcl16/refresh_nav.gif"));//$NON-NLS-1$
195
refreshAction.setActionDefinitionId("org.eclipse.ui.file.refresh"); //$NON-NLS-1$
196

197         buildAction = new BuildAction(shell,
198                 IncrementalProjectBuilder.INCREMENTAL_BUILD);
199         buildAction.setActionDefinitionId("org.eclipse.ui.project.buildProject"); //$NON-NLS-1$
200
}
201
202     /**
203      * Returns the image descriptor with the given relative path.
204      */

205     protected ImageDescriptor getImageDescriptor(String JavaDoc relativePath) {
206        return IDEWorkbenchPlugin.getIDEImageDescriptor(relativePath);
207      
208     }
209     public void updateActionBars() {
210         IStructuredSelection selection = (IStructuredSelection) getContext()
211                 .getSelection();
212         refreshAction.selectionChanged(selection);
213         buildAction.selectionChanged(selection);
214         openProjectAction.selectionChanged(selection);
215         closeUnrelatedProjectsAction.selectionChanged(selection);
216         closeProjectAction.selectionChanged(selection);
217     }
218
219 }
220
Popular Tags