KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > editors > text > RemoveTrailingWhitespaceHandler


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.ui.internal.editors.text;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.IPath;
19
20 import org.eclipse.core.filebuffers.FileBuffers;
21 import org.eclipse.core.filebuffers.ITextFileBufferManager;
22 import org.eclipse.core.filebuffers.manipulation.RemoveTrailingWhitespaceOperation;
23
24 import org.eclipse.jface.window.Window;
25
26 import org.eclipse.ui.editors.text.FileBufferOperationHandler;
27
28 import org.eclipse.ui.internal.editors.text.SelectResourcesDialog.IFilter;
29
30
31 /**
32  * A file buffer operation action that removes trailing whitespace.
33  *
34  * @since 3.1
35  */

36 public class RemoveTrailingWhitespaceHandler extends FileBufferOperationHandler {
37
38     public RemoveTrailingWhitespaceHandler() {
39         super(new RemoveTrailingWhitespaceOperation());
40     }
41
42     /*
43      * @see org.eclipse.ui.editors.text.FileBufferOperationHandler#isAcceptableLocation(org.eclipse.core.runtime.IPath)
44      */

45     protected boolean isAcceptableLocation(IPath location) {
46         ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
47         return location != null && manager.isTextFileLocation(location, true);
48     }
49
50     /*
51      * @see org.eclipse.ui.editors.text.FileBufferOperationHandler#collectFiles(org.eclipse.core.resources.IResource[])
52      */

53     protected IFile[] collectFiles(IResource[] resources) {
54         IFile[] files= super.collectFiles(resources);
55         files= filterUnacceptableFiles(files);
56         if (containsOnlyFiles(resources))
57             return files;
58
59         final IFilter filter= new IFilter() {
60             public boolean accept(IResource resource) {
61                 return resource != null && isAcceptableLocation(resource.getFullPath());
62             }
63         };
64
65         SelectResourcesDialog dialog= new SelectResourcesDialog(getShell(), TextEditorMessages.RemoveTrailingWhitespaceHandler_dialog_title, TextEditorMessages.RemoveTrailingWhitespaceHandler_dialog_description, filter);
66         dialog.setInput(resources);
67         int result= dialog.open();
68         if (Window.OK == result) {
69             IResource[] selectedResources= dialog.getSelectedResources();
70             return super.collectFiles(selectedResources);
71         }
72         return null;
73     }
74     
75     /**
76      * Checks whether the given resources array contains
77      * only files.
78      *
79      * @param resources the array with the resources
80      * @return <code>true</code> if there array only contains <code>IFiles</code>s
81      * @since 3.2
82      */

83     private boolean containsOnlyFiles(IResource[] resources) {
84         for (int i= 0; i < resources.length; i++) {
85             IResource resource= resources[i];
86             if ((IResource.FILE & resource.getType()) == 0)
87                 return false;
88         }
89         return true;
90     }
91     
92     /**
93      * Filters the unacceptable files.
94      *
95      * @param files the files to filter
96      * @return an array of files
97      * @since 3.2
98      */

99     private IFile[] filterUnacceptableFiles(IFile[] files) {
100         Set JavaDoc filtered= new HashSet JavaDoc();
101         for (int i= 0; i < files.length; i++) {
102             IFile file= files[i];
103             if (isAcceptableLocation(file.getFullPath()))
104                 filtered.add(file);
105         }
106         return (IFile[]) filtered.toArray(new IFile[filtered.size()]);
107     }
108
109 }
110
Popular Tags