Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 15b5fa97f23abb017a79559e6bcf8cbb31f2fda5 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*******************************************************************************
 * Copyright (c) 2004, 2008 John Krasnay 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:
 *     John Krasnay - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.xml.vex.core.internal.layout;

import org.eclipse.wst.xml.vex.core.internal.core.ColorResource;
import org.eclipse.wst.xml.vex.core.internal.core.FontResource;
import org.eclipse.wst.xml.vex.core.internal.core.Graphics;
import org.eclipse.wst.xml.vex.core.internal.css.Styles;
import org.eclipse.wst.xml.vex.core.internal.dom.Document;
import org.eclipse.wst.xml.vex.core.internal.dom.Element;
import org.eclipse.wst.xml.vex.core.internal.dom.Text;

/**
 * A TextBox that gets its text from the document. Represents text which is
 * editable within the VexWidget.
 */
public class DocumentTextBox extends TextBox {

	private int startRelative;
	private int endRelative;

	/**
	 * Class constructor, accepting a Text object.
	 * 
	 * @param context
	 *            LayoutContext in use
	 * @param element
	 *            Element being used
	 * @param text
	 */
	public DocumentTextBox(LayoutContext context, Element element, Text text) {
		this(context, element, text.getStartOffset(), text.getEndOffset());
	}

	/**
	 * Class constructor.
	 * 
	 * @param context
	 *            LayoutContext used to calculate the box's size.
	 * @param element
	 *            Element that directly contains the text.
	 * @param startOffset
	 *            start offset of the text
	 * @param endOffset
	 *            end offset of the text
	 */
	public DocumentTextBox(LayoutContext context, Element element,
			int startOffset, int endOffset) {
		super(element);

		if (startOffset >= endOffset) {
			throw new IllegalStateException("DocumentTextBox: startOffset ("
					+ startOffset + ") >= endOffset (" + endOffset + ")");
		}

		this.startRelative = startOffset - element.getStartOffset();
		this.endRelative = endOffset - element.getStartOffset();
		this.calculateSize(context);

		if (this.getText().length() < (endOffset - startOffset)) {
			throw new IllegalStateException();
		}
	}

	/**
	 * @see org.eclipse.wst.xml.vex.core.internal.layout.Box#getEndOffset()
	 */
	public int getEndOffset() {
		if (this.endRelative == -1) {
			return -1;
		} else {
			return this.getElement().getStartOffset() + this.endRelative - 1;
		}
	}

	/**
	 * @see org.eclipse.wst.xml.vex.core.internal.layout.Box#getStartOffset()
	 */
	public int getStartOffset() {
		if (this.startRelative == -1) {
			return -1;
		} else {
			return this.getElement().getStartOffset() + this.startRelative;
		}
	}

	/**
	 * @see org.eclipse.wst.xml.vex.core.internal.layout.TextBox#getText()
	 */
	public String getText() {
		Document doc = this.getElement().getDocument();
		return doc.getText(this.getStartOffset(), this.getEndOffset() + 1);
	}

	/**
	 * @see org.eclipse.wst.xml.vex.core.internal.layout.Box#hasContent()
	 */
	public boolean hasContent() {
		return true;
	}

	/**
	 * @see org.eclipse.wst.xml.vex.core.internal.layout.Box#paint(org.eclipse.wst.xml.vex.core.internal.layout.LayoutContext,
	 *      int, int)
	 */
	public void paint(LayoutContext context, int x, int y) {

		Styles styles = context.getStyleSheet().getStyles(this.getElement());
		Graphics g = context.getGraphics();

		FontResource font = g.createFont(styles.getFont());
		FontResource oldFont = g.setFont(font);
		ColorResource foreground = g.createColor(styles.getColor());
		ColorResource oldForeground = g.setColor(foreground);
		// ColorResource background =
		// g.createColor(styles.getBackgroundColor());
		// ColorResource oldBackground = g.setBackgroundColor(background);

		char[] chars = this.getText().toCharArray();

		if (chars.length < this.getEndOffset() - this.getStartOffset()) {
			throw new IllegalStateException();
		}

		if (chars.length == 0) {
			throw new IllegalStateException();
		}

		int start = 0;
		int end = chars.length;
		if (chars[end - 1] == NEWLINE_CHAR) {
			end--;
		}
		int selStart = context.getSelectionStart() - this.getStartOffset();
		selStart = Math.min(Math.max(selStart, start), end);
		int selEnd = context.getSelectionEnd() - this.getStartOffset();
		selEnd = Math.min(Math.max(selEnd, start), end);

		// text before selection
		if (start < selStart) {
			g.drawChars(chars, start, selStart - start, x, y);
			String s = new String(chars, start, selStart - start);
			paintTextDecoration(context, styles, s, x, y);
		}

		// text after selection
		if (selEnd < end) {
			int x1 = x + g.charsWidth(chars, 0, selEnd);
			g.drawChars(chars, selEnd, end - selEnd, x1, y);
			String s = new String(chars, selEnd, end - selEnd);
			paintTextDecoration(context, styles, s, x1, y);
		}

		// text within selection
		if (selStart < selEnd) {
			String s = new String(chars, selStart, selEnd - selStart);
			int x1 = x + g.charsWidth(chars, 0, selStart);
			this.paintSelectedText(context, s, x1, y);
			paintTextDecoration(context, styles, s, x1, y);
		}

		g.setFont(oldFont);
		g.setColor(oldForeground);
		// g.setBackgroundColor(oldBackground);
		font.dispose();
		foreground.dispose();
		// background.dispose();
	}

	/**
	 * @see org.eclipse.wst.xml.vex.core.internal.layout.TextBox#splitAt(int)
	 */
	public Pair splitAt(LayoutContext context, int offset) {

		if (offset < 0 || offset > (this.endRelative - this.startRelative)) {
			throw new IllegalStateException();
		}

		int split = this.getStartOffset() + offset;

		DocumentTextBox left;
		if (offset == 0) {
			left = null;
		} else {
			left = new DocumentTextBox(context, this.getElement(), this
					.getStartOffset(), split);
		}

		InlineBox right;
		if (split == this.getEndOffset() + 1) {
			right = null;
		} else {
			right = new DocumentTextBox(context, this.getElement(), split, this
					.getEndOffset() + 1);
		}
		return new Pair(left, right);
	}

	/**
	 * @see org.eclipse.wst.xml.vex.core.internal.layout.Box#viewToModel(org.eclipse.wst.xml.vex.core.internal.layout.LayoutContext,
	 *      int, int)
	 */
	public int viewToModel(LayoutContext context, int x, int y) {

		Graphics g = context.getGraphics();
		Styles styles = context.getStyleSheet().getStyles(this.getElement());
		FontResource font = g.createFont(styles.getFont());
		FontResource oldFont = g.setFont(font);
		char[] chars = this.getText().toCharArray();

		if (this.getWidth() <= 0) {
			return this.getStartOffset();
		}

		// first, get an estimate based on x / width
		int offset = (x / this.getWidth()) * chars.length;
		offset = Math.max(0, offset);
		offset = Math.min(chars.length, offset);

		int delta = Math.abs(x - g.charsWidth(chars, 0, offset));

		// Search backwards
		while (offset > 0) {
			int newDelta = Math.abs(x - g.charsWidth(chars, 0, offset - 1));
			if (newDelta > delta) {
				break;
			}
			delta = newDelta;
			offset--;
		}

		// Search forwards
		while (offset < chars.length - 1) {
			int newDelta = Math.abs(x - g.charsWidth(chars, 0, offset + 1));
			if (newDelta > delta) {
				break;
			}
			delta = newDelta;
			offset++;
		}

		g.setFont(oldFont);
		font.dispose();
		return this.getStartOffset() + offset;
	}

}

Back to the top