KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > editors > text > FileBufferOperationHandler


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 package org.eclipse.ui.editors.text;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.swt.widgets.Shell;
19
20 import org.eclipse.core.commands.AbstractHandler;
21 import org.eclipse.core.commands.ExecutionEvent;
22 import org.eclipse.core.commands.ExecutionException;
23
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IAdaptable;
26 import org.eclipse.core.runtime.IPath;
27 import org.eclipse.core.runtime.IProgressMonitor;
28 import org.eclipse.core.runtime.IStatus;
29 import org.eclipse.core.runtime.OperationCanceledException;
30 import org.eclipse.core.runtime.Status;
31 import org.eclipse.core.runtime.SubProgressMonitor;
32 import org.eclipse.core.runtime.jobs.Job;
33
34 import org.eclipse.core.resources.IFile;
35 import org.eclipse.core.resources.IResource;
36
37 import org.eclipse.core.filebuffers.FileBuffers;
38 import org.eclipse.core.filebuffers.manipulation.FileBufferOperationRunner;
39 import org.eclipse.core.filebuffers.manipulation.IFileBufferOperation;
40
41 import org.eclipse.jface.viewers.ISelection;
42 import org.eclipse.jface.viewers.IStructuredSelection;
43
44 import org.eclipse.jface.text.ITextSelection;
45
46 import org.eclipse.ui.IEditorInput;
47 import org.eclipse.ui.IEditorPart;
48 import org.eclipse.ui.IWorkbenchPart;
49 import org.eclipse.ui.IWorkbenchWindow;
50 import org.eclipse.ui.PlatformUI;
51
52 /**
53  * Operation handler for a file buffer.
54  * <p>
55  * This class may be instantiated or be subclassed.</p>
56  *
57  * @since 3.1
58  */

59 public class FileBufferOperationHandler extends AbstractHandler {
60
61     private IFileBufferOperation fFileBufferOperation;
62     private IWorkbenchWindow fWindow;
63     private IResource[] fResources;
64     private IPath fLocation;
65
66     /**
67      * Creates a new file buffer operation handler.
68      *
69      * @param fileBufferOperation the file buffer operation
70      */

71     public FileBufferOperationHandler(IFileBufferOperation fileBufferOperation) {
72         fFileBufferOperation= fileBufferOperation;
73     }
74
75     /**
76      * Initializes this file buffer operation handler with the given resources and the given location. The array of resources
77      * is adopted by this handler and may not be modified by clients after that method has been called.
78      *
79      * @param resources the resources to be adopted
80      * @param location the location
81      */

82     public void initialize(IResource[] resources, IPath location) {
83         if (resources != null) {
84             fResources= new IResource[resources.length];
85             System.arraycopy(resources, 0, fResources, 0, resources.length);
86         } else {
87             fResources= null;
88         }
89         fLocation= location;
90     }
91
92     /**
93      * Computes the selected resources.
94      */

95     protected final void computeSelectedResources() {
96
97         if (fResources != null || fLocation != null)
98             return;
99
100         ISelection selection= getSelection();
101         if (selection instanceof IStructuredSelection) {
102             IStructuredSelection structuredSelection= (IStructuredSelection) selection;
103             ArrayList JavaDoc resources= new ArrayList JavaDoc(structuredSelection.size());
104
105             Iterator JavaDoc e= structuredSelection.iterator();
106             while (e.hasNext()) {
107                 Object JavaDoc element= e.next();
108                 if (element instanceof IResource)
109                     resources.add(element);
110                 else if (element instanceof IAdaptable) {
111                     IAdaptable adaptable= (IAdaptable) element;
112                     Object JavaDoc adapter= adaptable.getAdapter(IResource.class);
113                     if (adapter instanceof IResource)
114                         resources.add(adapter);
115                 }
116             }
117
118             if (!resources.isEmpty())
119                 fResources= (IResource[]) resources.toArray(new IResource[resources.size()]);
120
121         } else if (selection instanceof ITextSelection) {
122             IWorkbenchWindow window= getWorkbenchWindow();
123             if (window != null) {
124                 IWorkbenchPart workbenchPart= window.getPartService().getActivePart();
125                 if (workbenchPart instanceof IEditorPart) {
126                     IEditorPart editorPart= (IEditorPart) workbenchPart;
127                     IEditorInput input= editorPart.getEditorInput();
128                     Object JavaDoc adapter= input.getAdapter(IResource.class);
129                     if (adapter instanceof IResource)
130                         fResources= new IResource[] { (IResource) adapter };
131                     else {
132                         adapter= input.getAdapter(ILocationProvider.class);
133                         if (adapter instanceof ILocationProvider) {
134                             ILocationProvider provider= (ILocationProvider) adapter;
135                             fLocation= provider.getPath(input);
136                         }
137                     }
138                 }
139             }
140         }
141     }
142
143     /**
144      * Returns the selection of the active workbench window.
145      *
146      * @return the current selection in the active workbench window or <code>null</code>
147      */

148     protected final ISelection getSelection() {
149         IWorkbenchWindow window= getWorkbenchWindow();
150         if (window != null)
151             return window.getSelectionService().getSelection();
152         return null;
153     }
154
155     /**
156      * Returns the active workbench window.
157      *
158      * @return the active workbench window or <code>null</code> if not available
159      */

160     protected final IWorkbenchWindow getWorkbenchWindow() {
161         if (fWindow == null)
162             fWindow= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
163         return fWindow;
164     }
165
166     /**
167      * Collects the files out of the given resources.
168      *
169      * @param resources the resources from which to get the files
170      * @return an array of files
171      */

172     protected IFile[] collectFiles(IResource[] resources) {
173         Set JavaDoc files= new HashSet JavaDoc();
174         for (int i= 0; i < resources.length; i++) {
175             IResource resource= resources[i];
176             if ((IResource.FILE & resource.getType()) > 0)
177                 files.add(resource);
178         }
179         return (IFile[]) files.toArray(new IFile[files.size()]);
180     }
181
182     /**
183      * Runs the given operation.
184      *
185      * @param files the file on which to run this operation
186      * @param location the file buffer location
187      * @param fileBufferOperation the operation to run
188      */

189     protected final void doRun(final IFile[] files, final IPath location, final IFileBufferOperation fileBufferOperation) {
190         Job job= new Job(fileBufferOperation.getOperationName()) {
191             protected IStatus run(IProgressMonitor monitor) {
192                 IStatus status;
193
194                 try {
195
196                     int ticks= 100;
197                     monitor.beginTask(fFileBufferOperation.getOperationName(), ticks);
198                     try {
199                         IPath[] locations;
200                         if (files != null) {
201                             ticks -= 30;
202                             locations= generateLocations(files, new SubProgressMonitor(monitor, 30));
203                         } else
204                             locations= new IPath[] { location };
205
206                         if (locations != null && locations.length > 0) {
207                             FileBufferOperationRunner runner= new FileBufferOperationRunner(FileBuffers.getTextFileBufferManager(), getShell());
208                             runner.execute(locations, fileBufferOperation, new SubProgressMonitor(monitor, ticks));
209                         }
210                         status= new Status(IStatus.OK, EditorsUI.PLUGIN_ID, IStatus.OK, "", null); //$NON-NLS-1$
211
} finally {
212                         monitor.done();
213                     }
214
215                 } catch (OperationCanceledException e) {
216                     status= new Status(IStatus.CANCEL, EditorsUI.PLUGIN_ID, IStatus.OK, "", null); //$NON-NLS-1$
217
} catch (CoreException e) {
218                     status= new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, "", e); //$NON-NLS-1$
219
}
220                 return status;
221             }
222         };
223
224         job.setUser(true);
225         job.schedule();
226     }
227
228     /**
229      * Returns the shell of the active workbench window.
230      *
231      * @return the shell
232      */

233     protected final Shell getShell() {
234         IWorkbenchWindow window= getWorkbenchWindow();
235         return window == null ? null : window.getShell();
236     }
237
238     /**
239      * Generates the file buffer locations out of the given files.
240      *
241      * @param files an array of files
242      * @param progressMonitor the progress monitor
243      * @return an array with the generated locations
244      */

245     protected final IPath[] generateLocations(IFile[] files, IProgressMonitor progressMonitor) {
246         progressMonitor.beginTask(TextEditorMessages.FileBufferOperationHandler_collectionFiles_label, files.length);
247         try {
248             Set JavaDoc locations= new HashSet JavaDoc();
249             for (int i= 0; i < files.length; i++) {
250                 IPath fullPath= files[i].getFullPath();
251                 if (isAcceptableLocation(fullPath))
252                     locations.add(fullPath);
253                 progressMonitor.worked(1);
254             }
255             return (IPath[]) locations.toArray(new IPath[locations.size()]);
256
257         } finally {
258             progressMonitor.done();
259         }
260     }
261
262     /**
263      * Tells whether the given location is accepted by this handler.
264      *
265      * @param location a file buffer location
266      * @return <code>true</code> if the given location is acceptable
267      */

268     protected boolean isAcceptableLocation(IPath location) {
269         return true;
270     }
271
272     /*
273      * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
274      * @since 3.1
275      */

276     public Object JavaDoc execute(ExecutionEvent event) throws ExecutionException {
277         computeSelectedResources();
278         try {
279
280             if (fResources != null && fResources.length > 0) {
281                 IFile[] files= collectFiles(fResources);
282                 if (files != null && files.length > 0)
283                     doRun(files, null, fFileBufferOperation);
284             } else if (isAcceptableLocation(fLocation))
285                 doRun(null, fLocation, fFileBufferOperation);
286
287             // Standard return value. DO NOT CHANGE.
288
return null;
289
290         } finally {
291             fResources= null;
292             fLocation= null;
293         }
294     }
295 }
296
Popular Tags