KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > workingsets > OpenCloseWorkingSetAction


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.internal.ui.workingsets;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IAdaptable;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.SubProgressMonitor;
22
23 import org.eclipse.core.resources.IProject;
24 import org.eclipse.core.resources.IResourceChangeEvent;
25 import org.eclipse.core.resources.IResourceChangeListener;
26 import org.eclipse.core.resources.IResourceDelta;
27 import org.eclipse.core.resources.IWorkspaceRunnable;
28 import org.eclipse.core.resources.ResourcesPlugin;
29
30 import org.eclipse.swt.widgets.Shell;
31
32 import org.eclipse.jface.action.IAction;
33 import org.eclipse.jface.viewers.IStructuredSelection;
34
35 import org.eclipse.ui.IActionBars;
36 import org.eclipse.ui.IViewSite;
37 import org.eclipse.ui.IWorkbenchSite;
38 import org.eclipse.ui.IWorkingSet;
39 import org.eclipse.ui.PlatformUI;
40
41 import org.eclipse.ui.ide.IDEActionFactory;
42
43 import org.eclipse.jdt.core.IJavaProject;
44
45 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
46
47 import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
48 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
49
50 public abstract class OpenCloseWorkingSetAction extends SelectionDispatchAction implements IResourceChangeListener {
51
52     private static final class CloseWorkingSetAction extends OpenCloseWorkingSetAction {
53         private IAction fProjectAction;
54         private CloseWorkingSetAction(IWorkbenchSite site, String JavaDoc label) {
55             super(site, label);
56             IActionBars actionBars= getActionBars();
57             if (actionBars != null) {
58                 fProjectAction= actionBars.getGlobalActionHandler(IDEActionFactory.CLOSE_PROJECT.getId());
59             }
60         }
61         protected boolean validate(IProject project) {
62             return project.isOpen();
63         }
64         protected void performOperation(IProject project, IProgressMonitor monitor) throws CoreException {
65             project.close(monitor);
66         }
67         protected void connectToActionBar(IActionBars actionBars) {
68             actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_PROJECT.getId(), this);
69             actionBars.updateActionBars();
70         }
71         protected void disconnectFromActionBar(IActionBars actionBars) {
72             actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_PROJECT.getId(), fProjectAction);
73             actionBars.updateActionBars();
74         }
75         protected String JavaDoc getErrorTitle() {
76             return WorkingSetMessages.OpenCloseWorkingSetAction_close_error_title;
77         }
78         protected String JavaDoc getErrorMessage() {
79             return WorkingSetMessages.OpenCloseWorkingSetAction_close_error_message;
80         }
81     }
82
83     private static final class OpenWorkingSetAction extends OpenCloseWorkingSetAction {
84         private IAction fProjectAction;
85         private OpenWorkingSetAction(IWorkbenchSite site, String JavaDoc label) {
86             super(site, label);
87             IActionBars actionBars= getActionBars();
88             if (actionBars != null) {
89                 fProjectAction= actionBars.getGlobalActionHandler(IDEActionFactory.OPEN_PROJECT.getId());
90             }
91         }
92         protected boolean validate(IProject project) {
93             return !project.isOpen();
94         }
95         protected void performOperation(IProject project, IProgressMonitor monitor) throws CoreException {
96             project.open(monitor);
97         }
98         protected void connectToActionBar(IActionBars actionBars) {
99             actionBars.setGlobalActionHandler(IDEActionFactory.OPEN_PROJECT.getId(), this);
100             actionBars.updateActionBars();
101         }
102         protected void disconnectFromActionBar(IActionBars actionBars) {
103             actionBars.setGlobalActionHandler(IDEActionFactory.OPEN_PROJECT.getId(), fProjectAction);
104             actionBars.updateActionBars();
105         }
106         protected String JavaDoc getErrorTitle() {
107             return WorkingSetMessages.OpenCloseWorkingSetAction_open_error_title;
108         }
109         protected String JavaDoc getErrorMessage() {
110             return WorkingSetMessages.OpenCloseWorkingSetAction_open_error_message;
111         }
112     }
113     
114     private OpenCloseWorkingSetAction(IWorkbenchSite site, String JavaDoc label) {
115         super(site);
116         setText(label);
117         ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE);
118     }
119     
120     public static OpenCloseWorkingSetAction createCloseAction(IWorkbenchSite site) {
121         return new CloseWorkingSetAction(site, WorkingSetMessages.OpenCloseWorkingSetAction_close_label);
122     }
123
124     public static OpenCloseWorkingSetAction createOpenAction(IWorkbenchSite site) {
125         return new OpenWorkingSetAction(site, WorkingSetMessages.OpenCloseWorkingSetAction_open_label);
126     }
127     
128     public void dispose() {
129         ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
130     }
131
132     public void selectionChanged(IStructuredSelection selection) {
133         List JavaDoc projects= getProjects(selection);
134         IActionBars actionBars= getActionBars();
135         if (projects != null && projects.size() > 0) {
136             setEnabled(true);
137             if (actionBars != null) {
138                 connectToActionBar(actionBars);
139             }
140         } else {
141             setEnabled(false);
142             if (actionBars != null) {
143                 disconnectFromActionBar(actionBars);
144             }
145         }
146     }
147     
148     public void run(IStructuredSelection selection) {
149         final List JavaDoc projects= getProjects(selection);
150         if (projects != null && projects.size() > 0) {
151             try {
152                 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(
153                     new WorkbenchRunnableAdapter(new IWorkspaceRunnable() {
154                         public void run(IProgressMonitor monitor) throws CoreException {
155                             monitor.beginTask("", projects.size()); //$NON-NLS-1$
156
for (Iterator JavaDoc iter= projects.iterator(); iter.hasNext();) {
157                                 IProject project= (IProject)iter.next();
158                                 performOperation(project, new SubProgressMonitor(monitor, 1));
159                             }
160                             monitor.done();
161                         }
162                     }));
163             } catch (InvocationTargetException JavaDoc e) {
164                 ExceptionHandler.handle(e, getShell(), getErrorTitle(), getErrorMessage());
165             } catch (InterruptedException JavaDoc e) {
166                 // do nothing. Got cancelled.
167
}
168         }
169     }
170     
171     protected abstract boolean validate(IProject project);
172     
173     protected abstract void performOperation(IProject project, IProgressMonitor monitor) throws CoreException;
174     
175     protected abstract void connectToActionBar(IActionBars actionBars);
176     
177     protected abstract void disconnectFromActionBar(IActionBars actionBars);
178     
179     protected abstract String JavaDoc getErrorTitle();
180
181     protected abstract String JavaDoc getErrorMessage();
182     
183     private List JavaDoc getProjects(IStructuredSelection selection) {
184         List JavaDoc result= new ArrayList JavaDoc();
185         List JavaDoc elements= selection.toList();
186         for (Iterator JavaDoc iter= elements.iterator(); iter.hasNext();) {
187             Object JavaDoc element= iter.next();
188             if (!(element instanceof IWorkingSet))
189                 return null;
190             List JavaDoc projects= getProjects((IWorkingSet)element);
191             if (projects == null)
192                 return null;
193             result.addAll(projects);
194         }
195         return result;
196     }
197
198     private List JavaDoc getProjects(IWorkingSet set) {
199         List JavaDoc result= new ArrayList JavaDoc();
200         IAdaptable[] elements= set.getElements();
201         for (int i= 0; i < elements.length; i++) {
202             Object JavaDoc element= elements[i];
203             IProject project= null;
204             if (element instanceof IProject) {
205                 project= (IProject)element;
206             } else if (element instanceof IJavaProject) {
207                 project= ((IJavaProject)element).getProject();
208             }
209             if (project != null && validate(project))
210                 result.add(project);
211         }
212         return result;
213     }
214     
215     protected IActionBars getActionBars() {
216         if (getSite() instanceof IViewSite) {
217             return ((IViewSite)getSite()).getActionBars();
218         } else {
219             return null;
220         }
221     }
222     
223     /**
224      * {@inheritDoc}
225      */

226     public void resourceChanged(IResourceChangeEvent event) {
227         IResourceDelta delta = event.getDelta();
228         if (delta != null) {
229             IResourceDelta[] projDeltas = delta.getAffectedChildren(IResourceDelta.CHANGED);
230             for (int i = 0; i < projDeltas.length; ++i) {
231                 IResourceDelta projDelta = projDeltas[i];
232                 if ((projDelta.getFlags() & IResourceDelta.OPEN) != 0) {
233                     Shell shell= getShell();
234                     if (!shell.isDisposed()) {
235                         shell.getDisplay().asyncExec(new Runnable JavaDoc() {
236                             public void run() {
237                                 update(getSelection());
238                             }
239                         });
240                     }
241                     return;
242                 }
243             }
244         }
245     }
246 }
Popular Tags