Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2b6e563156aa050034ba21f73de0564a9f05ba57 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.jface.text;

import org.eclipse.core.runtime.Assert;


/**
 * Specification of changes applied to documents. All changes are represented as
 * replace commands, i.e. specifying a document range whose text gets replaced
 * with different text. In addition to this information, the event also contains
 * the changed document.
 *
 * @see org.eclipse.jface.text.IDocument
 */
public class DocumentEvent {

	/**
	 * Debug option for asserting that text is not null.
	 * If the <code>org.eclipse.text/debug/DocumentEvent/assertTextNotNull</code>
	 * system property is <code>true</code>
	 *
	 * @since 3.3
	 */
	private static final boolean ASSERT_TEXT_NOT_NULL= Boolean.getBoolean("org.eclipse.text/debug/DocumentEvent/assertTextNotNull"); //$NON-NLS-1$

	/** The changed document */
	public IDocument fDocument;
	/** The document offset */
	public int fOffset;
	/** Length of the replaced document text */
	public int fLength;
	/** Text inserted into the document */
	public String fText= ""; //$NON-NLS-1$
	/**
	 * The modification stamp of the document when firing this event.
	 * @since 3.1 and public since 3.3
	 */
	public long fModificationStamp;

	/**
	 * Creates a new document event.
	 *
	 * @param doc the changed document
	 * @param offset the offset of the replaced text
	 * @param length the length of the replaced text
	 * @param text the substitution text
	 */
	public DocumentEvent(IDocument doc, int offset, int length, String text) {

		Assert.isNotNull(doc);
		Assert.isTrue(offset >= 0);
		Assert.isTrue(length >= 0);

		if (ASSERT_TEXT_NOT_NULL)
			Assert.isNotNull(text);

		fDocument= doc;
		fOffset= offset;
		fLength= length;
		fText= text;

		if (fDocument instanceof IDocumentExtension4)
			fModificationStamp= ((IDocumentExtension4)fDocument).getModificationStamp();
		else
			fModificationStamp= IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP;
	}

	/**
	 * Creates a new, not initialized document event.
	 */
	public DocumentEvent() {
	}

	/**
	 * Returns the changed document.
	 *
	 * @return the changed document
	 */
	public IDocument getDocument() {
		return fDocument;
	}

	/**
	 * Returns the offset of the change.
	 *
	 * @return the offset of the change
	 */
	public int getOffset() {
		return fOffset;
	}

	/**
	 * Returns the length of the replaced text.
	 *
	 * @return the length of the replaced text
	 */
	public int getLength() {
		return fLength;
	}

	/**
	 * Returns the text that has been inserted.
	 *
	 * @return the text that has been inserted
	 */
	public String getText() {
		return fText;
	}

	/**
	 * Returns the document's modification stamp at the
	 * time when this event was sent.
	 *
	 * @return the modification stamp or {@link IDocumentExtension4#UNKNOWN_MODIFICATION_STAMP}.
	 * @see IDocumentExtension4#getModificationStamp()
	 * @since 3.1
	 */
	public long getModificationStamp() {
		return fModificationStamp;
	}

	/*
	 * @see java.lang.Object#toString()
	 * @since 3.4
	 */
	public String toString() {
		StringBuffer buffer= new StringBuffer();
		buffer.append("offset: " ); //$NON-NLS-1$
		buffer.append(fOffset);
		buffer.append(", length: " ); //$NON-NLS-1$
		buffer.append(fLength);
		buffer.append(", timestamp: " ); //$NON-NLS-1$
		buffer.append(fModificationStamp);
		buffer.append("\ntext:>" ); //$NON-NLS-1$
		buffer.append(fText);
		buffer.append("<\n" ); //$NON-NLS-1$
		return buffer.toString();
	}
}

Back to the top