1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 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
9 * IBM Corporation - initial API and implementation
10 * Markus Schorn (Wind River Systems)
11 *******************************************************************************/
12 package org.eclipse.cdt.internal.ui.cview;
14 import java.util.Iterator;
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.IResourceChangeEvent;
19 import org.eclipse.core.resources.IWorkspace;
20 import org.eclipse.core.runtime.IAdaptable;
21 import org.eclipse.jface.action.IMenuManager;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.KeyEvent;
25 import org.eclipse.ui.IActionBars;
26 import org.eclipse.ui.IWorkbenchPartSite;
27 import org.eclipse.ui.actions.ActionFactory;
28 import org.eclipse.ui.actions.CloseResourceAction;
29 import org.eclipse.ui.actions.OpenResourceAction;
30 import org.eclipse.ui.actions.RefreshAction;
31 import org.eclipse.ui.ide.IDEActionFactory;
33 import org.eclipse.cdt.ui.CUIPlugin;
36 * This is the action group for actions such as Refresh Local, and Open/Close
39 public class OpenProjectGroup extends CViewActionGroup {
41 private OpenResourceAction openProjectAction;
42 private CloseResourceAction closeProjectAction;
43 private RefreshAction refreshAction;
45 public OpenProjectGroup(CView cview) {
50 public void fillActionBars(IActionBars actionBars) {
51 actionBars.setGlobalActionHandler(ActionFactory.REFRESH.getId(), refreshAction);
52 actionBars.setGlobalActionHandler(IDEActionFactory.OPEN_PROJECT.getId(), openProjectAction);
53 actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_PROJECT.getId(), closeProjectAction);
57 * Adds the open project, close project and refresh resource actions to the
60 * refresh-no closed project selected
63 * Both the open project and close project action may be on the menu at the
67 * No disabled action should be on the context menu.
71 * context menu to add actions to
74 public void fillContextMenu(IMenuManager menu) {
75 IStructuredSelection selection = (IStructuredSelection) getContext().getSelection();
76 boolean isProjectSelection = true;
77 boolean hasOpenProjects = false;
78 boolean hasClosedProjects = false;
79 Iterator<?> resources = selection.iterator();
81 while (resources.hasNext() && (!hasOpenProjects || !hasClosedProjects || isProjectSelection)) {
82 Object next = resources.next();
83 IProject project = null;
85 if (next instanceof IProject) {
86 project = (IProject) next;
87 } else if (next instanceof IAdaptable) {
88 IResource res = (IResource) ((IAdaptable) next).getAdapter(IResource.class);
89 if (res instanceof IProject) {
90 project = (IProject)res;
94 if (project == null) {
95 isProjectSelection = false;
98 if (project.isOpen()) {
99 hasOpenProjects = true;
101 hasClosedProjects = true;
104 if (!hasClosedProjects) {
105 refreshAction.selectionChanged(selection);
106 menu.add(refreshAction);
108 if (isProjectSelection) {
109 if (hasClosedProjects) {
110 openProjectAction.selectionChanged(selection);
111 menu.add(openProjectAction);
113 if (hasOpenProjects) {
114 closeProjectAction.selectionChanged(selection);
115 menu.add(closeProjectAction);
121 * Handles a key pressed event by invoking the appropriate action.
124 public void handleKeyPressed(KeyEvent event) {
125 if (event.keyCode == SWT.F5 && event.stateMask == 0) {
126 if (refreshAction.isEnabled()) {
127 refreshAction.refreshAll();
135 protected void makeActions() {
136 final IWorkbenchPartSite site= getCView().getSite();
137 IWorkspace workspace = CUIPlugin.getWorkspace();
139 openProjectAction = new OpenResourceAction(site);
140 workspace.addResourceChangeListener(openProjectAction, IResourceChangeEvent.POST_CHANGE);
141 closeProjectAction = new CloseResourceAction(site);
142 workspace.addResourceChangeListener(closeProjectAction, IResourceChangeEvent.POST_CHANGE);
143 refreshAction = new RefreshAction(site);
144 refreshAction.setDisabledImageDescriptor(getImageDescriptor("dlcl16/refresh_nav.gif"));//$NON-NLS-1$
145 refreshAction.setImageDescriptor(getImageDescriptor("elcl16/refresh_nav.gif"));//$NON-NLS-1$
146 // refreshAction.setHoverImageDescriptor(getImageDescriptor("clcl16/refresh_nav.gif"));//$NON-NLS-1$
150 public void updateActionBars() {
151 IStructuredSelection selection = (IStructuredSelection) getContext().getSelection();
152 refreshAction.selectionChanged(selection);
153 openProjectAction.selectionChanged(selection);
154 closeProjectAction.selectionChanged(selection);
158 public void dispose() {
159 IWorkspace workspace = CUIPlugin.getWorkspace();
160 workspace.removeResourceChangeListener(closeProjectAction);
161 workspace.removeResourceChangeListener(openProjectAction);