Skip to main content
summaryrefslogtreecommitdiffstats
blob: a79245254a6031c429258f85a49ad9c833b0ba9a (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
package org.eclipse.jface.text;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */


import org.eclipse.swt.events.VerifyEvent;


/**
 * Represents a text modification as a document replace command. The text modification is given
 * as a <code>VerifyEvent</code> and translated into a document replace command relative
 * to a given offset. A document command can also be used to initialize a given <code>VerifyEvent</code>.
 */
public final class DocumentCommand {
	
	/** Must the command be updated */
	public boolean doit= false;
	/** The offset of the command */
	public int offset;
	/** The length of the command */
	public int length;
	/** The text to be inserted */
	public String text;
	
	
	/**
	 * Creates a new document command.
	 */
	DocumentCommand() {
	}
	
	/**
	 * Translates a verify event into a document replace command using the given offset.
	 *
	 * @param event the event to be translated
	 * @param offset the offset used for the translation
	 */
	void setEvent(VerifyEvent event, int offset) {
		
		doit= true;
		
		text=  event.text;
		
		this.offset= event.start;
		length= event.end - event.start;
				
		if (length < 0) {
			this.offset += length;
			length= -length;
		}
		
		this.offset += offset;
	}
	
	/**
	 * Fills the given verify event with the replace text and the doit
	 * flag of this document command. Returns whether the document command
	 * covers the same range as the verify event considering the given offset.
	 *
	 * @param event the event to be changed
	 * @param offset to be considered for range comparison
	 * @return <code>true</code> if this command and the event cover the same range
	 */
	boolean fillEvent(VerifyEvent event, int offset) {
		
		int start= this.offset - offset;
		
		event.text= text;
		event.doit= (start == event.start && start + length == event.end) && doit;
		return event.doit;
	}
}


Back to the top