KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > textmanipulation > TextBufferEditor


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.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 /**
31  * @deprecated Use file buffers instead
32  */

33 public class TextBufferEditor extends TextEditProcessor {
34
35     private TextBuffer fBuffer;
36     private TextEditProcessor fUndoProcessor;
37         
38     /**
39      * Creates a new <code>TextBufferEditor</code> for the given
40      * <code>TextBuffer</code>.
41      *
42      * @param buffer the text buffer this editor is working on.
43      */

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     /**
51      * Returns the text buffer this editor is working on.
52      *
53      * @return the text buffer this editor is working on
54      */

55     public TextBuffer getTextBuffer() {
56         return fBuffer;
57     }
58     
59     /**
60      * Adds an <code>Edit</code> to this edit processor. Adding an edit
61      * to an edit processor transfers ownership of the edit to the
62      * processor. So after an edit has been added to a processor the
63      * creator of the edit <b>must</b> not continue modifying the edit.
64      *
65      * @param edit the edit to add
66      * @exception MalformedTreeException if the text edit can not be
67      * added to this edit processor.
68      *
69      * @see TextEdit#addChild(TextEdit)
70      */

71     public void add(TextEdit edit) throws MalformedTreeException {
72         getRoot().addChild(edit);
73     }
74         
75     /**
76      * Adds an undo memento to this edit processor. Adding an undo memento
77      * transfers ownership of the memento to the processor. So after a memento
78      * has been added the creator of that memento <b>must</b> not continue
79      * modifying it.
80      *
81      * @param undo the undo memento to add
82      */

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     /* (non-Javadoc)
89      * @see org.eclipse.text.edits.TextEditProcessor#canPerformEdits()
90      */

91     public boolean canPerformEdits() {
92         if (fUndoProcessor != null)
93             return fUndoProcessor.canPerformEdits();
94         return super.canPerformEdits();
95     }
96     
97     /**
98      * Executes the text edits added to this text buffer editor and clears all added
99      * text edits.
100      *
101      * @param pm a progress monitor to report progress or <code>null</code> if
102      * no progress is desired.
103      * @return an object representing the undo of the executed <code>TextEdit</code>s
104      * @exception CoreException if the edits cannot be executed
105      */

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 JavaDoc message= (e != null ? e.getMessage() : ""); //$NON-NLS-1$
115
throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(),
116                 IStatus.ERROR, message, e));
117         } catch (MalformedTreeException e) {
118             String JavaDoc message= (e != null ? e.getMessage() : ""); //$NON-NLS-1$
119
throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(),
120                 IStatus.ERROR, message, e));
121         }
122     }
123 }
124
125
Popular Tags