KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 org.eclipse.swt.widgets.Shell;
14
15 import org.eclipse.jface.action.IAction;
16 import org.eclipse.jface.action.IMenuManager;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.jface.viewers.ISelectionProvider;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20
21 import org.eclipse.core.resources.IncrementalProjectBuilder;
22 import org.eclipse.core.resources.ResourcesPlugin;
23
24 import org.eclipse.ui.IActionBars;
25 import org.eclipse.ui.IViewPart;
26 import org.eclipse.ui.IWorkbenchSite;
27 import org.eclipse.ui.actions.ActionFactory;
28 import org.eclipse.ui.actions.ActionGroup;
29 import org.eclipse.ui.actions.BuildAction;
30 import org.eclipse.ui.ide.IDEActionFactory;
31
32 import org.eclipse.jdt.core.IJavaProject;
33
34 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
35
36 import org.eclipse.jdt.ui.IContextMenuConstants;
37
38 /**
39  * Contributes all build related actions to the context menu and installs handlers for the
40  * corresponding global menu actions.
41  *
42  * <p>
43  * This class may be instantiated; it is not intended to be subclassed.
44  * </p>
45  *
46  * @since 2.0
47  */

48 public class BuildActionGroup extends ActionGroup {
49
50     private IWorkbenchSite fSite;
51     
52     private BuildAction fBuildAction;
53     private RefreshAction fRefreshAction;
54
55     /**
56      * Creates a new <code>BuildActionGroup</code>. The group requires that
57      * the selection provided by the view part's selection provider is of type
58      * <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
59      *
60      * @param part the view part that owns this action group
61      */

62     public BuildActionGroup(IViewPart part) {
63         fSite= part.getSite();
64         Shell shell= fSite.getShell();
65         ISelectionProvider provider= fSite.getSelectionProvider();
66         
67         fBuildAction= new BuildAction(shell, IncrementalProjectBuilder.INCREMENTAL_BUILD);
68         fBuildAction.setText(ActionMessages.BuildAction_label);
69         fBuildAction.setActionDefinitionId("org.eclipse.ui.project.buildProject"); //$NON-NLS-1$
70

71         fRefreshAction= new RefreshAction(fSite);
72         fRefreshAction.setActionDefinitionId("org.eclipse.ui.file.refresh"); //$NON-NLS-1$
73

74         provider.addSelectionChangedListener(fBuildAction);
75         provider.addSelectionChangedListener(fRefreshAction);
76     }
77     
78     /**
79      * Returns the refresh action managed by this group.
80      *
81      * @return the refresh action. If this group doesn't manage a refresh action
82      * <code>null</code> is returned
83      */

84     public IAction getRefreshAction() {
85         return fRefreshAction;
86     }
87
88     /* (non-Javadoc)
89      * Method declared in ActionGroup
90      */

91     public void fillActionBars(IActionBars actionBar) {
92         super.fillActionBars(actionBar);
93         setGlobalActionHandlers(actionBar);
94     }
95     
96     /* (non-Javadoc)
97      * Method declared in ActionGroup
98      */

99     public void fillContextMenu(IMenuManager menu) {
100         ISelection selection= getContext().getSelection();
101         if (!ResourcesPlugin.getWorkspace().isAutoBuilding() && isBuildTarget(selection)) {
102             appendToGroup(menu, fBuildAction);
103         }
104         appendToGroup(menu, fRefreshAction);
105         super.fillContextMenu(menu);
106     }
107     
108     /* (non-Javadoc)
109      * Method declared in ActionGroup
110      */

111     public void dispose() {
112         ISelectionProvider provider= fSite.getSelectionProvider();
113         provider.removeSelectionChangedListener(fBuildAction);
114         provider.removeSelectionChangedListener(fRefreshAction);
115         super.dispose();
116     }
117     
118     private void setGlobalActionHandlers(IActionBars actionBar) {
119         actionBar.setGlobalActionHandler(IDEActionFactory.BUILD_PROJECT.getId(), fBuildAction);
120         actionBar.setGlobalActionHandler(ActionFactory.REFRESH.getId(), fRefreshAction);
121     }
122     
123     private void appendToGroup(IMenuManager menu, IAction action) {
124         if (action.isEnabled())
125             menu.appendToGroup(IContextMenuConstants.GROUP_BUILD, action);
126     }
127     
128     private boolean isBuildTarget(ISelection s) {
129         if (!(s instanceof IStructuredSelection))
130             return false;
131         IStructuredSelection selection= (IStructuredSelection)s;
132         if (selection.size() != 1)
133             return false;
134         return selection.getFirstElement() instanceof IJavaProject;
135     }
136 }
137
Popular Tags