Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1d804b088b7456c9e68e2f99d097aaa84d73c6c1 (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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*******************************************************************************
 * Copyright (c) 2016 BestSolution.at 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:
 * 	Tom Schindl<tom.schindl@bestsolution.at> - initial API and implementation
 *******************************************************************************/
package org.eclipse.fx.ui.controls.styledtext.internal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.eclipse.fx.core.Subscription;
import org.eclipse.fx.core.Util;
import org.eclipse.fx.ui.controls.styledtext.DecorationStrategyFactory;
import org.eclipse.fx.ui.controls.styledtext.model.DecorationStrategy;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.css.CssMetaData;
import javafx.css.ParsedValue;
import javafx.css.SimpleStyleableIntegerProperty;
import javafx.css.SimpleStyleableObjectProperty;
import javafx.css.StyleConverter;
import javafx.css.Styleable;
import javafx.css.StyleableProperty;
import javafx.geometry.Bounds;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextBoundsType;

@SuppressWarnings("javadoc")
public class TextNode extends HBox {

	@SuppressWarnings("null")
	@NonNull
	private static final CssMetaData<TextNode, @NonNull Paint> FILL = new CssMetaData<TextNode, @NonNull Paint>("-fx-fill", StyleConverter.getPaintConverter(), Color.BLACK) { //$NON-NLS-1$

		@Override
		public boolean isSettable(TextNode styleable) {
			return !styleable.fillProperty().isBound();
		}

		@SuppressWarnings("unchecked")
		@Override
		public StyleableProperty<@NonNull Paint> getStyleableProperty(TextNode styleable) {
			return (StyleableProperty<@NonNull Paint>) styleable.fill;
		}

	};
	@SuppressWarnings("null")
	@NonNull
	final ObjectProperty<@NonNull Paint> fill = new SimpleStyleableObjectProperty<>(FILL, this, "fill", Color.BLACK); //$NON-NLS-1$

	private static String[] BASIC_STRING_CACHE = new String[256];

	private static String toString(char c) {
		String rv = null;
		if( c < BASIC_STRING_CACHE.length ) {
			rv = BASIC_STRING_CACHE[c];
			if( rv == null ) {
				BASIC_STRING_CACHE[c] = String.valueOf(c);
			}
		}
		if( rv == null ) {
			rv = String.valueOf(c);
		}
		return rv;
	}

	/**
	 * The paint used to fill the text
	 *
	 * @return the property to observe
	 */
	public final @NonNull ObjectProperty<@NonNull Paint> fillProperty() {
		return (ObjectProperty<@NonNull Paint>) this.fill;
	}

	/**
	 * @return the paint used to fill the text
	 */
	public final @NonNull Paint getFill() {
		return this.fillProperty().get();
	}

	/**
	 * Set a new paint
	 *
	 * @param color
	 *            the paint used to fill the text
	 */
	public final void setFill(final @NonNull Paint color) {
		this.fillProperty().set(color);
	}

	static class DecorationStyleConverter extends StyleConverter<ParsedValue<?, DecorationStrategy>, DecorationStrategy> {
		private static Map<String, DecorationStrategyFactory> FACTORIES;

		@SuppressWarnings("null")
		@Override
		public DecorationStrategy convert(ParsedValue<ParsedValue<?, DecorationStrategy>, DecorationStrategy> value, Font font) {
			String definition = value.getValue() + ""; //$NON-NLS-1$

			if (FACTORIES == null) {
				FACTORIES = Util.lookupServiceList(getClass(), DecorationStrategyFactory.class).stream().sorted((f1, f2) -> -1 * Integer.compare(f1.getRanking(), f2.getRanking())).collect(Collectors.toMap(f -> f.getDecorationStrategyName(), f -> f));
			}

			String type;
			if (definition.contains("(")) { //$NON-NLS-1$
				type = definition.substring(0, definition.indexOf('('));
			} else {
				type = definition + ""; //$NON-NLS-1$
			}

			DecorationStrategyFactory strategy = FACTORIES.get(type);
			if (strategy != null) {
				return strategy.create(definition.contains("(") ? definition.substring(definition.indexOf('(') + 1, definition.lastIndexOf(')')) : null); //$NON-NLS-1$
			}

			return null;
		}
	}

	@NonNull
	private static final DecorationStyleConverter CONVERTER = new DecorationStyleConverter();

	@SuppressWarnings("null")
	@NonNull
	private static final CssMetaData<TextNode, @Nullable DecorationStrategy> DECORATIONSTRATEGY = new CssMetaData<TextNode, @Nullable DecorationStrategy>("-efx-decoration", CONVERTER, null) { //$NON-NLS-1$

		@Override
		public boolean isSettable(TextNode node) {
			return !node.decorationStrategyProperty().isBound();
		}

		@SuppressWarnings("unchecked")
		@Override
		public StyleableProperty<@Nullable DecorationStrategy> getStyleableProperty(TextNode node) {
			return (StyleableProperty<@Nullable DecorationStrategy>) node.decorationStrategyProperty();
		}

	};

	@SuppressWarnings("null")
	@NonNull
	private static final CssMetaData<TextNode, @NonNull Number> TABCHARADANCE = new CssMetaData<TextNode, @NonNull Number>("-efx-tab-char-advance", StyleConverter.getSizeConverter(), Integer.valueOf(4)) { //$NON-NLS-1$

		@Override
		public boolean isSettable(TextNode styleable) {
			return !styleable.tabCharAdvanceProperty().isBound();
		}

		@SuppressWarnings("unchecked")
		@Override
		public StyleableProperty<@NonNull Number> getStyleableProperty(TextNode styleable) {
			return (StyleableProperty<@NonNull Number>) styleable.tabCharAdvance;
		}

	};

	private static final List<CssMetaData<? extends Styleable, ?>> STYLEABLES;

	static {
		final List<CssMetaData<? extends Styleable, ?>> styleables = new ArrayList<CssMetaData<? extends Styleable, ?>>(Region.getClassCssMetaData());
		styleables.add(DECORATIONSTRATEGY);
		styleables.add(FILL);
		STYLEABLES = Collections.unmodifiableList(styleables);
	}

	public static List<CssMetaData<? extends Styleable, ?>> getClassCssMetaData() {
		return STYLEABLES;
	}

	@Override
	public List<CssMetaData<? extends Styleable, ?>> getCssMetaData() {
		return getClassCssMetaData();
	}

	private @NonNull ObjectProperty<@Nullable DecorationStrategy> decorationStrategy = new SimpleStyleableObjectProperty<@Nullable DecorationStrategy>(DECORATIONSTRATEGY, this, "decorationStrategy"); //$NON-NLS-1$

	/**
	 * The current strategy used for decoration
	 *
	 * @return the property to observe
	 */
	public final @NonNull ObjectProperty<@Nullable DecorationStrategy> decorationStrategyProperty() {
		return this.decorationStrategy;
	}

	/**
	 * @return strategy used for decoration
	 */
	public final @Nullable DecorationStrategy getDecorationStrategy() {
		return this.decorationStrategyProperty().get();
	}

	/**
	 * Set a new strategy used for decoration
	 *
	 * @param strategy
	 *            the strategy
	 */
	public final void setDecorationStrategy(final @Nullable DecorationStrategy strategy) {
		this.decorationStrategyProperty().set(strategy);
	}

	@NonNull
	IntegerProperty tabCharAdvance = new SimpleStyleableIntegerProperty(TABCHARADANCE, this, "tabCharAdvance", Integer.valueOf(4)); //$NON-NLS-1$

	/**
	 * Number of chars to use for tab advance (default is 4)
	 *
	 * @return the property to observe
	 */
	public final IntegerProperty tabCharAdvanceProperty() {
		return this.tabCharAdvance;
	}

	/**
	 * @return the number of chars to use for tab advance (default is 4)
	 */
	public final int getTabCharAdvance() {
		return this.tabCharAdvanceProperty().get();
	}

	/**
	 * Set a new number for chars to advance for a tab
	 *
	 * @param tabCharAdvance
	 *            the number of chars to use for tab advance (default is 4)
	 */
	public final void setTabCharAdvance(final int tabCharAdvance) {
		this.tabCharAdvanceProperty().set(tabCharAdvance);
	}


//	private ReuseCache<Text> letterCache = new ReuseCache<Text>(this::createText);

	private int startOffset;
	private List<Integer> tabPositions = new ArrayList<>();
	private String originalText;
//	private final HBox textNode;
//	private final Text textNode;

	private List<Text> activeLetters = new ArrayList<>();
	private ReuseCache<Text> cache;


	/**
	 * Create a new styled text node
	 *
	 * @param text
	 *            the text
	 */
	public TextNode(String text) {
		setMinWidth(Region.USE_COMPUTED_SIZE);
		this.cache = new ReuseCache<>(()->{
			Text letter = new Text();
			letter.setBoundsType(TextBoundsType.LOGICAL_VERTICAL_CENTER);
			return letter;
		});
		this.cache.addOnActivate(node->{
			getChildren().add(node);
		});
		this.cache.addOnRelease(node-> {
			getChildren().remove(node);
		});
//		cache.addOnClear(node->getChildren().remove(node));

		getStyleClass().add("styled-text-node"); //$NON-NLS-1$
		this.originalText = text;

		rebuildText(text);

		this.tabCharAdvance.addListener(o -> {
			rebuildText(text);
		});
		this.cache.addOnActivate((n)->{
			((Text)n).fillProperty().bind(fillProperty());

		});
		this.cache.addOnRelease(n->{
			((Text)n).fillProperty().unbind();
		});

//		setStyle("-fx-border-width: 0.1px; -fx-border-color: blue");


		decorationStrategy.addListener((x, o, n)->{
			System.err.println("deco start changed: " + n);
		});

		this.decorationStrategy.addListener(this::handleDecorationChange);
	}

	public Subscription setStyledRange(String clazz, int start, int end) {
		return null;
	}

	public void updateText(String text) {
		rebuildText(text);
		this.originalText = text;
	}

	private void rebuildText(String text) {
		for (Text t : this.activeLetters) {
			this.cache.releaseElement(t);
		}
		this.activeLetters.clear();
		for( char c : processText(text).toCharArray() ) {
			Text textNode = this.cache.getElement();
			textNode.setText(toString(c));
			this.activeLetters.add(textNode);
		}
	}

	private String processText(String text) {
		String tmp = text;
		StringBuilder b = new StringBuilder();
		for (int i = 0; i < this.tabCharAdvance.get(); i++) {
			b.append(" "); //$NON-NLS-1$
		}
		int position = -1;
		this.tabPositions.clear();
		while ((position = tmp.indexOf('\t')) != -1) {
			tmp = tmp.replaceFirst("\t", b.toString()); //$NON-NLS-1$
			this.tabPositions.add(Integer.valueOf(position));
		}
		return tmp;
	}

	private void handleDecorationChange(ObservableValue<? extends DecorationStrategy> observable, DecorationStrategy oldValue, DecorationStrategy newValue) {
		if (oldValue != null) {
			oldValue.unattach(this, this);
		}

		if (newValue != null) {
			newValue.attach(this, this);
		}
	}

	void setStartOffset(int startOffset) {
		this.startOffset = startOffset;
	}

	/**
	 * @return the start offset in the file
	 */
	@Deprecated
	public int getStartOffset() {
		return this.startOffset;
	}

	/**
	 * @return the end offset in the file
	 */
	@Deprecated
	public int getEndOffset() {
		return this.startOffset + getText().length();
	}

	/**
	 * Check if the text node intersects with the start and end locations
	 *
	 * @param start
	 *            the start
	 * @param end
	 *            the end
	 * @return <code>true</code> if intersects
	 */
	public boolean intersectOffset(int start, int end) {
		if (getStartOffset() > end) {
			return false;
		} else if (getEndOffset() < start) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return "'" + this.originalText + "'";  //$NON-NLS-1$//$NON-NLS-2$
	}

	@Override
	public ObservableList<Node> getChildren() {
		return super.getChildren();
	}

	/**
	 * @return the text
	 */
	public String getText() {
		return this.originalText;
	}

//	protected double getFixedLetterWidth() {
//		return 8;
//	}

	@Override
	protected void layoutChildren() {
		super.layoutChildren();

//		double off = 0;
//
//		for (Text letter : this.activeLetters) {
//			letter.setLayoutX(off);
//			off += getFixedLetterWidth();
//		}

//		this.textNode.relocate(0, 0);
		DecorationStrategy decorationStrategy2 = this.decorationStrategy.get();
		if (decorationStrategy2 != null) {
			decorationStrategy2.layout(this, this);
		}
	}

	/**
	 * Get the caret index at the given point
	 *
	 * @param point
	 *            the point relative to this node
	 * @return the index or <code>-1</code>
	 */
	public int getCaretIndexAtPoint(Point2D point) {
		Point2D local = this.sceneToLocal(localToScene(point));
//		System.err.println(local);

		Optional<Node> charNode = this.getChildren().stream().filter( n -> n.getBoundsInParent().contains(local)).findFirst();
		if( charNode.isPresent() ) {
			Node node = charNode.get();
			int idx = this.getChildren().indexOf(node);
			Bounds bounds = node.getBoundsInParent();

			// if it is the 2nd half of the character
			if( bounds.getMinX() + bounds.getWidth() / 2 < local.getX() ) {
				idx += 1;
			}

			int toRemove = 0;
			for (Integer i : this.tabPositions) {
				if (i.intValue() <= idx && idx < i.intValue() + this.tabCharAdvance.get()) {
					toRemove += idx - i.intValue();
					// If we are in the 2nd half of the tab we
					// simply move one past the value
					if ((idx - i.intValue()) % this.tabCharAdvance.get() >= this.tabCharAdvance.get() / 2) {
						idx += 1;
					}
					break;
				} else if (i.intValue() < idx) {
					toRemove += this.tabCharAdvance.get() - 1;
				}
			}
			idx -= toRemove;
//			System.err.println("==> (TextNode)" + idx);
			return idx;
		} else {
//			System.err.println("COULD NOT FIND NODE AT: " + local);
//			this.textNode.getChildren().stream().map( n -> n.getBoundsInParent()).forEach(System.err::println);
		}

//		@SuppressWarnings("deprecation")
//		HitInfo info = this.textNode.impl_hitTestChar(this.textNode.sceneToLocal(localToScene(point)));
//		if (info != null) {
//			int idx = info.getInsertionIndex();
//			int toRemove = 0;
//			for (Integer i : this.tabPositions) {
//				if (i.intValue() <= idx && idx < i.intValue() + this.tabCharAdvance.get()) {
//					toRemove += idx - i.intValue();
//					// If we are in the 2nd half of the tab we
//					// simply move one past the value
//					if ((idx - i.intValue()) % this.tabCharAdvance.get() >= this.tabCharAdvance.get() / 2) {
//						idx += 1;
//					}
//					break;
//				} else if (i.intValue() < idx) {
//					toRemove += this.tabCharAdvance.get() - 1;
//				}
//			}
//			idx -= toRemove;
//			return idx;
//		}
		return -1;
	}

	/**
	 * Find the x coordinate where we the character for the given index starts
	 *
	 * @param index
	 *            the index
	 * @return the location or <code>0</code> if not found
	 */
	public double getCharLocation(int index) {
		int realIndex = index;
		for (Integer i : this.tabPositions) {
			if (i.intValue() < realIndex) {
				realIndex += this.tabCharAdvance.get() - 1;
			}
		}

		if( realIndex >= 0 && realIndex < this.getChildren().size() ) {
			return this.localToParent(this.getChildren().get(realIndex).getBoundsInParent()).getMinX();
		} else if( ! this.getChildren().isEmpty() ) {
			return this.localToParent(this.getChildren().get(this.getChildren().size()-1).getBoundsInParent()).getMaxX();
		}

		return 0.0;

//		this.textNode.setImpl_caretPosition(realIndex);
//		PathElement[] pathElements = this.textNode.getImpl_caretShape();
//		for (PathElement e : pathElements) {
//			if (e instanceof MoveTo) {
//				return this.textNode.localToParent(((MoveTo) e).getX(), 0).getX();
//			}
//		}

	}
}

Back to the top