KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashSet JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IResource;
19
20 import org.eclipse.core.runtime.Assert;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IAdaptable;
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.core.runtime.IStatus;
26 import org.eclipse.core.runtime.OperationCanceledException;
27 import org.eclipse.core.runtime.Status;
28 import org.eclipse.core.runtime.SubProgressMonitor;
29 import org.eclipse.core.runtime.jobs.Job;
30
31 import org.eclipse.core.filebuffers.FileBuffers;
32 import org.eclipse.core.filebuffers.manipulation.FileBufferOperationRunner;
33 import org.eclipse.core.filebuffers.manipulation.IFileBufferOperation;
34
35 import org.eclipse.swt.widgets.Shell;
36
37 import org.eclipse.jface.action.Action;
38 import org.eclipse.jface.action.IAction;
39 import org.eclipse.jface.viewers.ISelection;
40 import org.eclipse.jface.viewers.IStructuredSelection;
41
42 import org.eclipse.jface.text.ITextSelection;
43
44 import org.eclipse.ui.IEditorInput;
45 import org.eclipse.ui.IEditorPart;
46 import org.eclipse.ui.IWorkbenchPart;
47 import org.eclipse.ui.IWorkbenchWindow;
48 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
49 import org.eclipse.ui.PlatformUI;
50
51 /**
52  * File buffer operation action.
53  *
54  * @since 3.1
55  */

56 public class FileBufferOperationAction extends Action implements IWorkbenchWindowActionDelegate {
57
58     private Set JavaDoc fResources;
59     private IPath fLocation;
60     private IWorkbenchWindow fWindow;
61     protected IFileBufferOperation fFileBufferOperation;
62
63     protected FileBufferOperationAction(IFileBufferOperation fileBufferOperation) {
64         Assert.isNotNull(fileBufferOperation);
65         fFileBufferOperation= fileBufferOperation;
66     }
67
68     /*
69      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
70      */

71     public void dispose() {
72         fResources= null;
73         fWindow= null;
74         fFileBufferOperation= null;
75     }
76
77     /*
78      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
79      */

80     public void init(IWorkbenchWindow window) {
81         fWindow= window;
82     }
83
84     /*
85      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
86      */

87     public void selectionChanged(IAction action, ISelection selection) {
88
89         fResources= new HashSet JavaDoc();
90         fLocation= null;
91
92         if (selection instanceof IStructuredSelection) {
93             IStructuredSelection structuredSelection= (IStructuredSelection) selection;
94
95             Iterator JavaDoc e= structuredSelection.iterator();
96             while (e.hasNext()) {
97                 Object JavaDoc element= e.next();
98                 if (element instanceof IResource)
99                     fResources.add(element);
100                 else if (element instanceof IAdaptable) {
101                     IAdaptable adaptable= (IAdaptable) element;
102                     Object JavaDoc adapter= adaptable.getAdapter(IResource.class);
103                     if (adapter instanceof IResource)
104                         fResources.add(adapter);
105                 }
106             }
107         }
108         
109         if (selection instanceof ITextSelection) {
110             IWorkbenchWindow window= getWorkbenchWindow();
111             if (window != null) {
112                 IWorkbenchPart workbenchPart= window.getPartService().getActivePart();
113                 if (workbenchPart instanceof IEditorPart) {
114                     IEditorPart editorPart= (IEditorPart) workbenchPart;
115                     IEditorInput input= editorPart.getEditorInput();
116                     Object JavaDoc adapter= input.getAdapter(IResource.class);
117                     if (adapter instanceof IResource)
118                         fResources.add(adapter);
119                     else {
120                         adapter= input.getAdapter(ILocationProvider.class);
121                         if (adapter instanceof ILocationProvider) {
122                             ILocationProvider provider= (ILocationProvider) adapter;
123                             fLocation= provider.getPath(input);
124                         }
125                     }
126                 }
127             }
128         }
129
130         action.setText(getText());
131         action.setEnabled(!fResources.isEmpty() || fLocation != null);
132     }
133
134     protected final IWorkbenchWindow getWorkbenchWindow() {
135         if (fWindow == null)
136             fWindow= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
137         return fWindow;
138     }
139
140     protected final Shell getShell() {
141         IWorkbenchWindow window= getWorkbenchWindow();
142         return window == null ? null : window.getShell();
143     }
144
145     /*
146      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
147      */

148     public void run(IAction action) {
149         if (fResources != null && !fResources.isEmpty()) {
150             IFile[] files= collectFiles((IResource[]) fResources.toArray(new IResource[fResources.size()]));
151             if (files != null && files.length > 0)
152                 doRun(files, null, fFileBufferOperation);
153         } else if (isAcceptableLocation(fLocation))
154             doRun(null, fLocation, fFileBufferOperation);
155     }
156
157     protected IFile[] collectFiles(IResource[] resources) {
158         Set JavaDoc files= new HashSet JavaDoc();
159         for (int i= 0; i < resources.length; i++) {
160             IResource resource= resources[i];
161             if ((IResource.FILE & resource.getType()) > 0)
162                 files.add(resource);
163         }
164         return (IFile[]) files.toArray(new IFile[files.size()]);
165     }
166
167     protected final void doRun(final IFile[] files, final IPath location, final IFileBufferOperation fileBufferOperation) {
168         Job job= new Job(fileBufferOperation.getOperationName()) {
169             protected IStatus run(IProgressMonitor monitor) {
170                 IStatus status;
171
172                 try {
173
174                     int ticks= 100;
175                     monitor.beginTask(fFileBufferOperation.getOperationName(), ticks);
176                     try {
177                         IPath[] locations;
178                         if (files != null) {
179                             ticks -= 30;
180                             locations= generateLocations(files, new SubProgressMonitor(monitor, 30));
181                         } else
182                             locations= new IPath[] { location };
183
184                         if (locations != null && locations.length > 0) {
185                             FileBufferOperationRunner runner= new FileBufferOperationRunner(FileBuffers.getTextFileBufferManager(), getShell());
186                             runner.execute(locations, fileBufferOperation, new SubProgressMonitor(monitor, ticks));
187                         }
188                         status= new Status(IStatus.OK, EditorsUI.PLUGIN_ID, IStatus.OK, "", null); //$NON-NLS-1$
189
} finally {
190                         monitor.done();
191                     }
192
193                 } catch (OperationCanceledException e) {
194                     status= new Status(IStatus.CANCEL, EditorsUI.PLUGIN_ID, IStatus.OK, "", null); //$NON-NLS-1$
195
} catch (CoreException e) {
196                     status= new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, "", e); //$NON-NLS-1$
197
}
198                 return status;
199             }
200         };
201
202         job.setUser(true);
203         job.schedule();
204     }
205
206     protected final IPath[] generateLocations(IFile[] files, IProgressMonitor progressMonitor) {
207         progressMonitor.beginTask(TextEditorMessages.FileBufferOperationAction_collectionFiles_label, files.length);
208         try {
209             Set JavaDoc locations= new HashSet JavaDoc();
210             for (int i= 0; i < files.length; i++) {
211                 IPath fullPath= files[i].getFullPath();
212                 if (isAcceptableLocation(fullPath))
213                     locations.add(fullPath);
214                 progressMonitor.worked(1);
215             }
216             return (IPath[]) locations.toArray(new IPath[locations.size()]);
217
218         } finally {
219             progressMonitor.done();
220         }
221     }
222
223     protected boolean isAcceptableLocation(IPath location) {
224         return true;
225     }
226 }
227
Popular Tags