KickJava   Java API By Example, From Geeks To Geeks.

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


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.runtime.IPath;
17
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IResource;
20
21 import org.eclipse.core.filebuffers.FileBuffers;
22 import org.eclipse.core.filebuffers.ITextFileBufferManager;
23 import org.eclipse.core.filebuffers.manipulation.ConvertLineDelimitersOperation;
24
25 import org.eclipse.jface.action.Action;
26 import org.eclipse.jface.window.Window;
27
28 import org.eclipse.ui.editors.text.FileBufferOperationAction;
29
30 import org.eclipse.ui.internal.editors.text.SelectResourcesDialog.IFilter;
31
32
33 /**
34  * A file buffer operation action that changes the line delimiters to a specified
35  * line delimiter.
36  *
37  * @since 3.1
38  */

39 public class ConvertLineDelimitersAction extends FileBufferOperationAction {
40
41     private String JavaDoc fLabel;
42
43     protected ConvertLineDelimitersAction(String JavaDoc lineDelimiter, String JavaDoc label) {
44         super(new ConvertLineDelimitersOperation(lineDelimiter));
45         setText(constructLabel(label, lineDelimiter, System.getProperty("line.separator"))); //$NON-NLS-1$
46
fLabel= Action.removeMnemonics(label);
47     }
48
49     private static String JavaDoc constructLabel(String JavaDoc label, String JavaDoc lineDelimiter, String JavaDoc platformLineDelimiter) {
50         if (lineDelimiter.equals(platformLineDelimiter))
51             return label + TextEditorMessages.ConvertLineDelimitersAction_default_label;
52         return label;
53     }
54
55     /*
56      * @see org.eclipse.ui.internal.editors.text.FileBufferOperationAction#isAcceptableLocation(org.eclipse.core.runtime.IPath)
57      */

58     protected boolean isAcceptableLocation(IPath location) {
59         ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
60         return location != null && manager.isTextFileLocation(location, true);
61     }
62
63     /*
64      * @see org.eclipse.ui.internal.editors.text.FileBufferOperationAction#collectFiles(org.eclipse.core.resources.IResource[])
65      */

66     protected IFile[] collectFiles(IResource[] resources) {
67         IFile[] files= super.collectFiles(resources);
68         files= filterUnacceptableFiles(files);
69         if (containsOnlyFiles(resources))
70             return files;
71
72         final IFilter filter= new IFilter() {
73             public boolean accept(IResource resource) {
74                 return resource != null && isAcceptableLocation(resource.getFullPath());
75             }
76         };
77
78         SelectResourcesDialog dialog= new SelectResourcesDialog(getShell(), NLSUtility.format(TextEditorMessages.ConvertLineDelimitersAction_dialog_title, fLabel), TextEditorMessages.ConvertLineDelimitersAction_dialog_description, filter);
79         dialog.setInput(resources);
80         int result= dialog.open();
81         if (Window.OK == result) {
82             IResource[] selectedResources= dialog.getSelectedResources();
83             return super.collectFiles(selectedResources);
84         }
85         return null;
86     }
87
88     /**
89      * Checks whether the given resources array contains
90      * only files.
91      *
92      * @param resources the array with the resources
93      * @return <code>true</code> if there array only contains <code>IFiles</code>s
94      * @since 3.2
95      */

96     private boolean containsOnlyFiles(IResource[] resources) {
97         for (int i= 0; i < resources.length; i++) {
98             IResource resource= resources[i];
99             if ((IResource.FILE & resource.getType()) == 0)
100                 return false;
101         }
102         return true;
103     }
104     
105     /**
106      * Filters the unacceptable files.
107      *
108      * @param files the files to filter
109      * @return an array of files
110      * @since 3.2
111      */

112     private IFile[] filterUnacceptableFiles(IFile[] files) {
113         Set JavaDoc filtered= new HashSet JavaDoc();
114         for (int i= 0; i < files.length; i++) {
115             IFile file= files[i];
116             if (isAcceptableLocation(file.getFullPath()))
117                 filtered.add(file);
118         }
119         return (IFile[]) filtered.toArray(new IFile[filtered.size()]);
120     }
121
122 }
123
Popular Tags