KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > core > refactoring > DocumentChange


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 package org.eclipse.ltk.core.refactoring;
12
13 import org.eclipse.text.edits.UndoEdit;
14
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18
19 import org.eclipse.jface.text.IDocument;
20
21 import org.eclipse.ltk.internal.core.refactoring.TextChanges;
22 import org.eclipse.ltk.internal.core.refactoring.UndoDocumentChange;
23
24 /**
25  * A text change that operates directly on instances of {@link IDocument}.
26  * The document change uses a simple length compare to check if it
27  * is still valid. So as long as its length hasn't changed the text edits
28  * managed have a valid range and can be applied to the document. The
29  * same applies to the undo change returned from the perform method.
30  *
31  * <p>
32  * Note: this class is not intended to be extended by clients.
33  * </p>
34  *
35  * @since 3.0
36  */

37 public class DocumentChange extends TextChange {
38
39     private IDocument fDocument;
40     private int fLength;
41     
42     /**
43      * Creates a new <code>DocumentChange</code> for the given
44      * {@link IDocument}.
45      *
46      * @param name the change's name. Has to be a human readable name.
47      * @param document the document this change is working on
48      */

49     public DocumentChange(String JavaDoc name, IDocument document) {
50         super(name);
51         Assert.isNotNull(document);
52         fDocument= document;
53     }
54     
55     /**
56      * {@inheritDoc}
57      */

58     public Object JavaDoc getModifiedElement(){
59         return fDocument;
60     }
61     
62     /**
63      * {@inheritDoc}
64      */

65     public void initializeValidationData(IProgressMonitor pm) {
66         // as long as we don't have modification stamps on documents
67
// we can only remember its length.
68
fLength= fDocument.getLength();
69     }
70     
71     /**
72      * {@inheritDoc}
73      */

74     public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
75         pm.beginTask("", 1); //$NON-NLS-1$
76
RefactoringStatus result= TextChanges.isValid(fDocument, fLength);
77         pm.worked(1);
78         return result;
79     }
80
81     /**
82      * {@inheritDoc}
83      */

84     protected IDocument acquireDocument(IProgressMonitor pm) throws CoreException {
85         return fDocument;
86     }
87     
88     /**
89      * {@inheritDoc}
90      */

91     protected void commit(IDocument document, IProgressMonitor pm) throws CoreException {
92         // do nothing
93
}
94     
95     /**
96      * {@inheritDoc}
97      */

98     protected void releaseDocument(IDocument document, IProgressMonitor pm) throws CoreException {
99         //do nothing
100
}
101     
102     /**
103      * {@inheritDoc}
104      */

105     protected Change createUndoChange(UndoEdit edit) {
106         return new UndoDocumentChange(getName(), fDocument, edit);
107     }
108 }
109
110
Popular Tags