1 11 package org.eclipse.jdt.internal.corext.textmanipulation; 12 13 import org.eclipse.text.edits.MalformedTreeException; 14 import org.eclipse.text.edits.MultiTextEdit; 15 import org.eclipse.text.edits.TextEdit; 16 import org.eclipse.text.edits.TextEditProcessor; 17 import org.eclipse.text.edits.UndoEdit; 18 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IProgressMonitor; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.Status; 23 24 import org.eclipse.jface.text.BadLocationException; 25 26 import org.eclipse.jdt.internal.corext.Assert; 27 28 import org.eclipse.jdt.internal.ui.JavaPlugin; 29 30 33 public class TextBufferEditor extends TextEditProcessor { 34 35 private TextBuffer fBuffer; 36 private TextEditProcessor fUndoProcessor; 37 38 44 public TextBufferEditor(TextBuffer buffer) { 45 super(buffer.getDocument(), new MultiTextEdit(0, buffer.getDocument().getLength()), 46 TextEdit.CREATE_UNDO | TextEdit.UPDATE_REGIONS); 47 fBuffer= buffer; 48 } 49 50 55 public TextBuffer getTextBuffer() { 56 return fBuffer; 57 } 58 59 71 public void add(TextEdit edit) throws MalformedTreeException { 72 getRoot().addChild(edit); 73 } 74 75 83 public void add(UndoEdit undo) { 84 Assert.isTrue(!getRoot().hasChildren()); 85 fUndoProcessor= new TextEditProcessor(getDocument(), undo, TextEdit.CREATE_UNDO | TextEdit.UPDATE_REGIONS); 86 } 87 88 91 public boolean canPerformEdits() { 92 if (fUndoProcessor != null) 93 return fUndoProcessor.canPerformEdits(); 94 return super.canPerformEdits(); 95 } 96 97 106 public UndoEdit performEdits(IProgressMonitor pm) throws CoreException { 107 try { 108 if (fUndoProcessor != null) { 109 return fUndoProcessor.performEdits(); 110 } else { 111 return super.performEdits(); 112 } 113 } catch (BadLocationException e) { 114 String message= (e != null ? e.getMessage() : ""); throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 116 IStatus.ERROR, message, e)); 117 } catch (MalformedTreeException e) { 118 String message= (e != null ? e.getMessage() : ""); throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 120 IStatus.ERROR, message, e)); 121 } 122 } 123 } 124 125 | Popular Tags |