upload tizen1.0 source
[sdk/ide/product.git] / org.eclipse.cdt.ui / src / org / eclipse / cdt / internal / ui / cview / OpenProjectGroup.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *     Markus Schorn (Wind River Systems)
11  *******************************************************************************/
12 package org.eclipse.cdt.internal.ui.cview;
13
14 import java.util.Iterator;
15
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;
32
33 import org.eclipse.cdt.ui.CUIPlugin;
34
35 /**
36  * This is the action group for actions such as Refresh Local, and Open/Close
37  * Project.
38  */
39 public class OpenProjectGroup extends CViewActionGroup {
40
41         private OpenResourceAction openProjectAction;
42         private CloseResourceAction closeProjectAction;
43         private RefreshAction refreshAction;
44
45         public OpenProjectGroup(CView cview) {
46                 super(cview);
47         }
48
49         @Override
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);
54         }
55
56         /**
57          * Adds the open project, close project and refresh resource actions to the
58          * context menu.
59          * <p>
60          * refresh-no closed project selected
61          * </p>
62          * <p>
63          * Both the open project and close project action may be on the menu at the
64          * same time.
65          * </p>
66          * <p>
67          * No disabled action should be on the context menu.
68          * </p>
69          * 
70          * @param menu
71          *            context menu to add actions to
72          */
73         @Override
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();
80
81                 while (resources.hasNext() && (!hasOpenProjects || !hasClosedProjects || isProjectSelection)) {
82                         Object next = resources.next();
83                         IProject project = null;
84
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;
91                                 }
92                         }
93
94                         if (project == null) {
95                                 isProjectSelection = false;
96                                 continue;
97                         }
98                         if (project.isOpen()) {
99                                 hasOpenProjects = true;
100                         } else {
101                                 hasClosedProjects = true;
102                         }
103                 }
104                 if (!hasClosedProjects) {
105                         refreshAction.selectionChanged(selection);
106                         menu.add(refreshAction);
107                 }
108                 if (isProjectSelection) {
109                         if (hasClosedProjects) {
110                                 openProjectAction.selectionChanged(selection);
111                                 menu.add(openProjectAction);
112                         }
113                         if (hasOpenProjects) {
114                                 closeProjectAction.selectionChanged(selection);
115                                 menu.add(closeProjectAction);
116                         }
117                 }
118         }
119
120         /**
121          * Handles a key pressed event by invoking the appropriate action.
122          */
123         @Override
124         public void handleKeyPressed(KeyEvent event) {
125                 if (event.keyCode == SWT.F5 && event.stateMask == 0) {
126                         if (refreshAction.isEnabled()) {
127                                 refreshAction.refreshAll();
128                         }
129                         // Swallow the event
130                         event.doit = false;
131                 }
132         }
133
134         @Override
135         protected void makeActions() {
136                 final IWorkbenchPartSite site= getCView().getSite();
137                 IWorkspace workspace = CUIPlugin.getWorkspace();
138
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$               
147         }
148
149         @Override
150         public void updateActionBars() {
151                 IStructuredSelection selection = (IStructuredSelection) getContext().getSelection();
152                 refreshAction.selectionChanged(selection);
153                 openProjectAction.selectionChanged(selection);
154                 closeProjectAction.selectionChanged(selection);
155         }
156
157         @Override
158         public void dispose() {
159                 IWorkspace workspace = CUIPlugin.getWorkspace();
160                 workspace.removeResourceChangeListener(closeProjectAction);
161                 workspace.removeResourceChangeListener(openProjectAction);
162         }
163
164 }