1 11 12 package org.eclipse.ui.texteditor; 13 14 15 import java.lang.reflect.InvocationTargetException ; 16 import java.util.Map ; 17 import java.util.MissingResourceException ; 18 import java.util.ResourceBundle ; 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 42 public class ConvertLineDelimitersAction extends TextEditorAction { 43 44 45 46 private final String fLineDelimiter; 47 48 54 public ConvertLineDelimitersAction(ITextEditor editor, String lineDelimiter) { 55 this(EditorMessages.getBundleForConstructedKeys(), "dummy", editor, lineDelimiter); } 57 58 66 public ConvertLineDelimitersAction(ResourceBundle bundle, String prefix, ITextEditor editor, String lineDelimiter) { 67 super(bundle, prefix, editor); 68 fLineDelimiter= lineDelimiter; 69 70 String platformLineDelimiter= System.getProperty("line.separator"); setText(getString(getLabelKey(fLineDelimiter, platformLineDelimiter))); 72 73 update(); 74 } 75 76 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 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 e) { 110 } catch (InvocationTargetException e) { 112 } 114 } 115 116 119 private static class ConvertRunnable implements IRunnableWithProgress, Runnable { 120 121 122 private final IRewriteTarget fRewriteTarget; 123 124 private final String fLineDelimiter; 125 126 132 public ConvertRunnable(IRewriteTarget rewriteTarget, String lineDelimiter) { 133 fRewriteTarget= rewriteTarget; 134 fLineDelimiter= lineDelimiter; 135 } 136 137 140 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 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 partitioners= TextUtilities.removeDocumentPartitioners(document); 152 153 try { 154 for (int i= 0; i < lineCount; i++) { 155 if (monitor.isCanceled()) 156 throw new InterruptedException (); 157 158 final String 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 (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 186 public void run() { 187 try { 188 run(new NullProgressMonitor()); 189 190 } catch (InterruptedException e) { 191 193 } catch (InvocationTargetException e) { 194 } 196 } 197 } 198 199 221 229 private static String getLabelKey(String lineDelimiter, String platformLineDelimiter) { 230 if (lineDelimiter.equals(platformLineDelimiter)) { 231 232 if (lineDelimiter.equals("\r\n")) return "Editor.ConvertLineDelimiter.toWindows.default.label"; 235 if (lineDelimiter.equals("\n")) return "Editor.ConvertLineDelimiter.toUNIX.default.label"; 238 if (lineDelimiter.equals("\r")) return "Editor.ConvertLineDelimiter.toMac.default.label"; 241 } else { 242 243 if (lineDelimiter.equals("\r\n")) return "Editor.ConvertLineDelimiter.toWindows.label"; 246 if (lineDelimiter.equals("\n")) return "Editor.ConvertLineDelimiter.toUNIX.label"; 249 if (lineDelimiter.equals("\r")) return "Editor.ConvertLineDelimiter.toMac.label"; } 252 253 return null; 254 } 255 256 259 private static String getString(String key) { 260 try { 261 return EditorMessages.getBundleForConstructedKeys().getString(key); 262 } catch (MissingResourceException e) { 263 return "!" + key + "!"; } 265 } 266 267 270 public void update() { 271 super.update(); 272 setEnabled(canModifyEditor()); 273 } 274 275 } 276 | Popular Tags |