KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > ConvertLineDelimitersAction


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
12 package org.eclipse.ui.texteditor;
13
14
15 import java.lang.reflect.InvocationTargetException JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.MissingResourceException JavaDoc;
18 import java.util.ResourceBundle JavaDoc;
19
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.NullProgressMonitor;
22
23 import org.eclipse.swt.custom.BusyIndicator;
24 import org.eclipse.swt.widgets.Shell;
25
26 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
27 import org.eclipse.jface.operation.IRunnableWithProgress;
28 import org.eclipse.jface.text.BadLocationException;
29 import org.eclipse.jface.text.IDocument;
30 import org.eclipse.jface.text.IRegion;
31 import org.eclipse.jface.text.IRewriteTarget;
32 import org.eclipse.jface.text.TextUtilities;
33
34
35 /**
36  * An action to convert line delimiters of a text editor document to a
37  * particular line delimiter.
38  *
39  * @since 2.0
40  * @deprecated since 3.1. Line delimiter conversion has been modified to work on groups of files rather than being editor specific
41  */

42 public class ConvertLineDelimitersAction extends TextEditorAction {
43
44
45     /** The target line delimiter. */
46     private final String JavaDoc fLineDelimiter;
47
48     /**
49      * Creates a line delimiter conversion action.
50      *
51      * @param editor the editor
52      * @param lineDelimiter the target line delimiter to convert the editor's document to
53      */

54     public ConvertLineDelimitersAction(ITextEditor editor, String JavaDoc lineDelimiter) {
55         this(EditorMessages.getBundleForConstructedKeys(), "dummy", editor, lineDelimiter); //$NON-NLS-1$
56
}
57
58     /**
59      * Creates a line delimiter conversion action.
60      *
61      * @param bundle the resource bundle
62      * @param prefix the prefix for the resource bundle lookup
63      * @param editor the editor
64      * @param lineDelimiter the target line delimiter to convert the editor's document to
65      */

66     public ConvertLineDelimitersAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, ITextEditor editor, String JavaDoc lineDelimiter) {
67         super(bundle, prefix, editor);
68         fLineDelimiter= lineDelimiter;
69
70         String JavaDoc platformLineDelimiter= System.getProperty("line.separator"); //$NON-NLS-1$
71
setText(getString(getLabelKey(fLineDelimiter, platformLineDelimiter)));
72
73         update();
74     }
75
76     /*
77      * @see org.eclipse.jface.action.Action#run()
78      */

79     public void run() {
80
81         try {
82
83             ITextEditor editor= getTextEditor();
84             if (editor == null)
85                 return;
86
87             if (!validateEditorInputState())
88                 return;
89
90             Object JavaDoc adapter= editor.getAdapter(IRewriteTarget.class);
91             if (adapter instanceof IRewriteTarget) {
92
93                 IRewriteTarget target= (IRewriteTarget) adapter;
94                 IDocument document= target.getDocument();
95                 if (document != null) {
96                     Shell shell= getTextEditor().getSite().getShell();
97                     ConvertRunnable runnable= new ConvertRunnable(target, fLineDelimiter);
98
99                     if (document.getNumberOfLines() < 40) {
100                         BusyIndicator.showWhile(shell.getDisplay(), runnable);
101
102                     } else {
103                         ProgressMonitorDialog dialog= new ProgressMonitorDialog(shell);
104                         dialog.run(false, true, runnable);
105                     }
106                 }
107             }
108
109         } catch (InterruptedException JavaDoc e) {
110             // action canceled
111
} catch (InvocationTargetException JavaDoc e) {
112             // should not happen
113
}
114     }
115
116     /**
117      * A runnable that converts all line delimiters of a document to <code>lineDelimiter</code>.
118      */

119     private static class ConvertRunnable implements IRunnableWithProgress, Runnable JavaDoc {
120
121         /** The rewrite target */
122         private final IRewriteTarget fRewriteTarget;
123         /** The line delimiter to which to convert to */
124         private final String JavaDoc fLineDelimiter;
125
126         /**
127          * Returns a new runnable for converting all line delimiters in
128          * the <code>rewriteTarget</code> to <code>lineDelimter</code>.
129          * @param rewriteTarget
130          * @param lineDelimiter
131          */

132         public ConvertRunnable(IRewriteTarget rewriteTarget, String JavaDoc lineDelimiter) {
133             fRewriteTarget= rewriteTarget;
134             fLineDelimiter= lineDelimiter;
135         }
136
137         /*
138          * @see IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
139          */

140         public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
141
142             IDocument document= fRewriteTarget.getDocument();
143             final int lineCount= document.getNumberOfLines();
144             monitor.beginTask(EditorMessages.Editor_ConvertLineDelimiter_title, lineCount);
145
146             final boolean isLargeUpdate= lineCount > 50;
147             if (isLargeUpdate)
148                 fRewriteTarget.setRedraw(false);
149             fRewriteTarget.beginCompoundChange();
150
151             Map JavaDoc partitioners= TextUtilities.removeDocumentPartitioners(document);
152
153             try {
154                 for (int i= 0; i < lineCount; i++) {
155                     if (monitor.isCanceled())
156                         throw new InterruptedException JavaDoc();
157
158                     final String JavaDoc delimiter= document.getLineDelimiter(i);
159                     if (delimiter != null && delimiter.length() > 0 && !delimiter.equals(fLineDelimiter)) {
160                         IRegion region= document.getLineInformation(i);
161                         document.replace(region.getOffset() + region.getLength(), delimiter.length(), fLineDelimiter);
162                     }
163
164                     monitor.worked(1);
165                 }
166
167             } catch (BadLocationException e) {
168                 throw new InvocationTargetException JavaDoc(e);
169
170             } finally {
171
172                 if (partitioners != null)
173                     TextUtilities.addDocumentPartitioners(document, partitioners);
174
175                 fRewriteTarget.endCompoundChange();
176                 if (isLargeUpdate)
177                     fRewriteTarget.setRedraw(true);
178
179                 monitor.done();
180             }
181         }
182
183         /*
184          * @see Runnable#run()
185          */

186         public void run() {
187             try {
188                 run(new NullProgressMonitor());
189
190             } catch (InterruptedException JavaDoc e) {
191                 // should not happen
192

193             } catch (InvocationTargetException JavaDoc e) {
194                 // should not happen
195
}
196         }
197     }
198
199 // /**
200
// * Returns whether the given document uses only the given line delimiter.
201
// * @param document the document to check
202
// * @param lineDelimiter the line delimiter to check for
203
// */
204
// private static boolean usesLineDelimiterExclusively(IDocument document, String lineDelimiter) {
205
//
206
// try {
207
// final int lineCount= document.getNumberOfLines();
208
// for (int i= 0; i < lineCount; i++) {
209
// final String delimiter= document.getLineDelimiter(i);
210
// if (delimiter != null && delimiter.length() > 0 && !delimiter.equals(lineDelimiter))
211
// return false;
212
// }
213
//
214
// } catch (BadLocationException e) {
215
// return false;
216
// }
217
//
218
// return true;
219
// }
220

221     /**
222      * Computes and returns the key to be used to lookup the action's label in
223      * its resource bundle.
224      *
225      * @param lineDelimiter the line delimiter
226      * @param platformLineDelimiter the platform line delimiter
227      * @return the key used to lookup the action's label
228      */

229     private static String JavaDoc getLabelKey(String JavaDoc lineDelimiter, String JavaDoc platformLineDelimiter) {
230         if (lineDelimiter.equals(platformLineDelimiter)) {
231
232             if (lineDelimiter.equals("\r\n")) //$NON-NLS-1$
233
return "Editor.ConvertLineDelimiter.toWindows.default.label"; //$NON-NLS-1$
234

235             if (lineDelimiter.equals("\n")) //$NON-NLS-1$
236
return "Editor.ConvertLineDelimiter.toUNIX.default.label"; //$NON-NLS-1$
237

238             if (lineDelimiter.equals("\r")) //$NON-NLS-1$
239
return "Editor.ConvertLineDelimiter.toMac.default.label"; //$NON-NLS-1$
240

241         } else {
242
243             if (lineDelimiter.equals("\r\n")) //$NON-NLS-1$
244
return "Editor.ConvertLineDelimiter.toWindows.label"; //$NON-NLS-1$
245

246             if (lineDelimiter.equals("\n")) //$NON-NLS-1$
247
return "Editor.ConvertLineDelimiter.toUNIX.label"; //$NON-NLS-1$
248

249             if (lineDelimiter.equals("\r")) //$NON-NLS-1$
250
return "Editor.ConvertLineDelimiter.toMac.label"; //$NON-NLS-1$
251
}
252
253         return null;
254     }
255
256     /*
257      * @since 3.1
258      */

259     private static String JavaDoc getString(String JavaDoc key) {
260         try {
261             return EditorMessages.getBundleForConstructedKeys().getString(key);
262         } catch (MissingResourceException JavaDoc e) {
263             return "!" + key + "!";//$NON-NLS-2$ //$NON-NLS-1$
264
}
265     }
266
267     /*
268      * @see IUpdate#update()
269      */

270     public void update() {
271         super.update();
272         setEnabled(canModifyEditor());
273     }
274
275 }
276
Popular Tags