/
TextViewer.java
1251 lines (1067 loc) · 36.7 KB
/
TextViewer.java
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2000, 2014 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v.2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
* Tom Eicher (Avaloq Evolution AG) - block selection mode
* Markus Schorn <markus.schorn@windriver.com> - shift with trailing empty line - https://bugs.eclipse.org/325438
*******************************************************************************/
package org.eclipse.fx.text.ui;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.eclipse.core.runtime.Assert;
import org.eclipse.fx.core.Subscription;
import org.eclipse.fx.core.SystemUtils;
import org.eclipse.fx.core.text.TextEditAction;
import org.eclipse.fx.text.hover.HoverInfo;
import org.eclipse.fx.ui.controls.styledtext.StyleRange;
import org.eclipse.fx.ui.controls.styledtext.StyledTextArea;
import org.eclipse.fx.ui.controls.styledtext.TriggerActionMapping;
import org.eclipse.fx.ui.controls.styledtext.TriggerActionMapping.Context;
import org.eclipse.fx.ui.controls.styledtext.VerifyEvent;
import org.eclipse.fx.ui.controls.styledtext.events.UndoHintEvent;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.BadPositionCategoryException;
import org.eclipse.jface.text.DefaultPositionUpdater;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.DocumentRewriteSessionEvent;
import org.eclipse.jface.text.DocumentRewriteSessionType;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentExtension4;
import org.eclipse.jface.text.IDocumentInformationMapping;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.text.IDocumentRewriteSessionListener;
import org.eclipse.jface.text.IPositionUpdater;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ISlaveDocumentManager;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.SlaveDocumentEvent;
import org.eclipse.jface.text.TextUtilities;
import org.eclipse.jface.text.projection.ChildDocument;
import org.eclipse.jface.text.projection.ChildDocumentManager;
import org.eclipse.jface.text.source.Annotation;
import javafx.beans.property.SetProperty;
import javafx.beans.property.SimpleSetProperty;
import javafx.collections.FXCollections;
import javafx.collections.SetChangeListener;
import javafx.geometry.Point2D;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.AnchorPane;
/**
* Text viewer with support for auto-complete, annotations, ...
*/
public class TextViewer extends AnchorPane implements ITextViewer, ITextViewerExtension, ITextViewerExtension2, ITextViewerExtension4, ITextViewerExtension6, ITextViewerExtension7, ITextViewerExtension8
/*
* , IEditingSupportRegistry, ITextOperationTarget,
* ITextOperationTargetExtension, IWidgetTokenOwner, IWidgetTokenOwnerExtension,
* IPostSelectionProvider
*/ {
private StyledTextArea fTextWidget;
private IDocument fVisibleDocument;
private VisibleDocumentListener fVisibleDocumentListener = new VisibleDocumentListener();
private ISlaveDocumentManager fSlaveDocumentManager;
private IDocumentInformationMapping fInformationMapping;
private IDocumentAdapter fDocumentAdapter;
private WidgetCommand fWidgetCommand = new WidgetCommand();
private List<ITextListener> fTextListeners;
private int fRedrawCounter = 0;
IRegion fLastSentSelectionChange;
private String fPartitioning;
private IDocument fDocument;
private boolean fReplaceTextPresentation;
private DocumentRewriteSessionListener fDocumentRewriteSessionListener = new DocumentRewriteSessionListener();
private IRewriteTarget fRewriteTarget;
private ViewerState fViewerState;
private List<ITextInputListener> fTextInputListeners;
private Position fMarkPosition;
private final String MARK_POSITION_CATEGORY = "__mark_category_" + hashCode(); //$NON-NLS-1$
private final IPositionUpdater fMarkPositionUpdater = new DefaultPositionUpdater(this.MARK_POSITION_CATEGORY);
private List<ITextPresentationListener> fTextPresentationListeners;
private Map<TextHoverKey, ITextHover> fTextHovers;
private TextViewerHoverManager fTextHoverManager;
private IUndoManager fUndoManager;
private SetProperty<Feature> features = new SimpleSetProperty<>(this, "features", FXCollections.observableSet()); //$NON-NLS-1$
public SetProperty<Feature> getFeatures() {
return this.features;
}
private final TriggerActionMapping actionMapping = new TriggerActionMapping();
/**
* Create a new text viewer
*/
public TextViewer() {
// default features
this(Feature.SHOW_LINE_NUMBERS);
}
public TextViewer(Feature... features) {
createControl();
bindFeatures();
this.features.addAll(Arrays.asList(features));
initActionMapping();
}
public void dispose() {
if (this.fUndoManager != null) {
this.fUndoManager.disconnect();
}
}
protected TriggerActionMapping getActionMapping() {
return actionMapping;
}
private void initActionMapping() {
// TODO add undo/redo stuff to action mapping
getTextWidget().setOverrideActionMapping(actionMapping);
}
@Override
public Subscription subscribeAction(BiFunction<TextEditAction, Context, Boolean> handler) {
return actionMapping.subscribe(handler);
}
private Map<Feature, Subscription> activeFeatures = new HashMap<>();
private void bindFeatures() {
this.features.addListener((SetChangeListener<Feature>)change->{
if (change.wasAdded()) {
@NonNull Feature feature = change.getElementAdded();
IFeature iFeature = feature.getFeatureFactory().get();
Subscription put = this.activeFeatures.put(feature, iFeature.install(getTextWidget()));
if (put != null) {
// we need to dispose overwritten installations
put.dispose();
}
}
if (change.wasRemoved()) {
Feature feature = change.getElementRemoved();
Subscription remove = this.activeFeatures.remove(feature);
if (remove != null) {
remove.dispose();
}
}
});
}
@Override
public void setUndoManager(IUndoManager undoManager) {
if (this.fUndoManager != null) {
this.fUndoManager.disconnect();
}
this.fUndoManager = undoManager;
this.fUndoManager.connect(this);
}
@Override
public IUndoManager getUndoManager() {
return this.fUndoManager;
}
public Set<HoverInfo> getHoverInfo(int offset) {
return Collections.emptySet();
}
public Set<HoverInfo> getHoverInfo(Annotation annotation) {
return Collections.emptySet();
}
/**
* Create the text control
*/
protected void createControl() {
this.fTextWidget = createTextWidget();
AnchorPane.setLeftAnchor(this.fTextWidget, Double.valueOf(0.0));
AnchorPane.setRightAnchor(this.fTextWidget, Double.valueOf(0.0));
AnchorPane.setTopAnchor(this.fTextWidget, Double.valueOf(0.0));
AnchorPane.setBottomAnchor(this.fTextWidget, Double.valueOf(0.0));
getChildren().add(this.fTextWidget);
this.fTextWidget.addEventHandler(VerifyEvent.VERIFY, this::onVerify);
this.fTextWidget.addEventHandler(UndoHintEvent.UNDO_HINT_BEGIN_COMPOUND_CHANGE, e->getUndoManager().beginCompoundChange());
this.fTextWidget.addEventHandler(UndoHintEvent.UNDO_HINT_END_COMPOUND_CHANGE, e->getUndoManager().endCompoundChange());
// new LineNumberSupport(this.fTextWidget).install();
// new InvisibleCharSupport(this.fTextWidget).install();
}
private static KeyCode getKeyCode(VerifyEvent event) {
// JDK-8150709
KeyCode kc = event.getCode();
if (SystemUtils.isMacOS()) {
if (kc == KeyCode.Z || kc == KeyCode.Y) {
if (event.getText().toUpperCase().equals("Z")) { //$NON-NLS-1$
return KeyCode.Z;
} else if (event.getText().toUpperCase().equals("Y")) { //$NON-NLS-1$
return KeyCode.Y;
}
}
}
return kc;
}
// TODO get rid of onVerify event
protected void onVerify(VerifyEvent event) {
// TODO add undo support to textViewer
if (getUndoManager() != null) {
if (SystemUtils.isWindows()) {
if (event.isControlDown() && !event.isShiftDown() && getKeyCode(event) == KeyCode.Z) {
if (getUndoManager().undoable()) {
getUndoManager().undo();
event.consume();
}
} else if (event.isControlDown() && !event.isShiftDown() && getKeyCode(event) == KeyCode.Y) {
if (getUndoManager().redoable()) {
getUndoManager().redo();
event.consume();
}
}
} else if (SystemUtils.isMacOS()) {
if (event.isMetaDown() && !event.isShiftDown() && getKeyCode(event) == KeyCode.Z) {
if (getUndoManager().undoable()) {
getUndoManager().undo();
event.consume();
}
} else if (event.isMetaDown() && event.isShiftDown() && getKeyCode(event) == KeyCode.Z) {
if (getUndoManager().redoable()) {
getUndoManager().redo();
event.consume();
}
}
} else {
if (event.isControlDown() && !event.isShiftDown() && getKeyCode(event) == KeyCode.Z) {
if (getUndoManager().undoable()) {
getUndoManager().undo();
event.consume();
}
} else if (event.isControlDown() && event.isShiftDown() && getKeyCode(event) == KeyCode.Z) {
if (getUndoManager().redoable()) {
getUndoManager().redo();
event.consume();
}
}
}
}
}
@Override
public StyledTextArea getTextWidget() {
return this.fTextWidget;
}
/**
* @return create the text widget
*/
@SuppressWarnings("static-method")
protected StyledTextArea createTextWidget() {
StyledTextArea styledText = new StyledTextArea();
styledText.setLineRulerVisible(true);
// styledText.setLeftMargin(Math.max(styledText.getLeftMargin(), 2));
return styledText;
}
/**
* Set the partitioning
*
* @param partitioning
* the partitioning
*/
public void setDocumentPartitioning(String partitioning) {
this.fPartitioning = partitioning;
}
/**
* @return the partitioning
*/
protected String getDocumentPartitioning() {
return this.fPartitioning;
}
/**
* Sets this viewer's visible document. The visible document represents the
* visible region of the viewer's input document.
*
* @param document
* the visible document
*/
protected void setVisibleDocument(IDocument document) {
if (this.fVisibleDocument == document && this.fVisibleDocument instanceof ChildDocument) {
// optimization for new child documents
return;
}
if (this.fVisibleDocument != null) {
if (this.fVisibleDocumentListener != null)
this.fVisibleDocument.removeDocumentListener(this.fVisibleDocumentListener);
if (this.fVisibleDocument != document)
freeSlaveDocument(this.fVisibleDocument);
}
this.fVisibleDocument = document;
initializeDocumentInformationMapping(this.fVisibleDocument);
initializeWidgetContents();
// TODO needs porting
// fFindReplaceDocumentAdapter= null;
if (this.fVisibleDocument != null && this.fVisibleDocumentListener != null)
this.fVisibleDocument.addDocumentListener(this.fVisibleDocumentListener);
}
@Override
public IDocument getDocument() {
return this.fDocument;
}
@Override
public void setDocument(IDocument document) {
this.fReplaceTextPresentation = true;
fireInputDocumentAboutToBeChanged(this.fDocument, document);
IDocument oldDocument = this.fDocument;
this.fDocument = document;
setVisibleDocument(this.fDocument);
// TODO needs porting
// resetPlugins();
inputChanged(this.fDocument, oldDocument);
fireInputDocumentChanged(oldDocument, this.fDocument);
this.fLastSentSelectionChange = null;
this.fReplaceTextPresentation = false;
}
@Override
public void setDocument(IDocument document, int modelRangeOffset, int modelRangeLength) {
this.fReplaceTextPresentation = true;
fireInputDocumentAboutToBeChanged(this.fDocument, document);
IDocument oldDocument = this.fDocument;
this.fDocument = document;
try {
IDocument slaveDocument = createSlaveDocument(document);
updateSlaveDocument(slaveDocument, modelRangeOffset, modelRangeLength);
setVisibleDocument(slaveDocument);
} catch (BadLocationException x) {
// throw new
// IllegalArgumentException(JFaceTextMessages.getString("TextViewer.error.invalid_visible_region_1"));
// //$NON-NLS-1$
throw new IllegalArgumentException();
}
// TODO needs porting
// resetPlugins();
inputChanged(this.fDocument, oldDocument);
fireInputDocumentChanged(oldDocument, this.fDocument);
this.fLastSentSelectionChange = null;
this.fReplaceTextPresentation = false;
}
/**
* Creates a slave document for the given document if there is a slave
* document manager associated with this viewer.
*
* @param document
* the master document
* @return the newly created slave document
* @since 2.1
*/
protected IDocument createSlaveDocument(IDocument document) {
ISlaveDocumentManager manager = getSlaveDocumentManager();
if (manager != null) {
if (manager.isSlaveDocument(document))
return document;
return manager.createSlaveDocument(document);
}
return document;
}
/**
* Updates the given slave document to show the specified range of its
* master document.
*
* @param slaveDocument
* the slave document
* @param modelRangeOffset
* the offset of the master document range
* @param modelRangeLength
* the length of the master document range
* @return <code>true</code> if the slave has been adapted successfully
* @throws BadLocationException
* in case the specified range is not valid in the master
* document
* @since 3.0
*/
protected boolean updateSlaveDocument(IDocument slaveDocument, int modelRangeOffset, int modelRangeLength) throws BadLocationException {
return updateVisibleDocument(slaveDocument, modelRangeOffset, modelRangeLength);
}
/**
* Sets the given slave document to the specified range of its master
* document.
*
* @param visibleDocument
* the slave document
* @param visibleRegionOffset
* the offset of the master document range
* @param visibleRegionLength
* the length of the master document range
* @return <code>true</code> if the slave has been adapted successfully
* @throws BadLocationException
* in case the specified range is not valid in the master
* document
* @since 2.1
* @deprecated use <code>updateSlaveDocument</code> instead
*/
protected boolean updateVisibleDocument(IDocument visibleDocument, int visibleRegionOffset, int visibleRegionLength) throws BadLocationException {
if (visibleDocument instanceof ChildDocument) {
ChildDocument childDocument = (ChildDocument) visibleDocument;
IDocument document = childDocument.getParentDocument();
int line = document.getLineOfOffset(visibleRegionOffset);
int offset = document.getLineOffset(line);
int length = (visibleRegionOffset - offset) + visibleRegionLength;
Position parentRange = childDocument.getParentDocumentRange();
if (offset != parentRange.getOffset() || length != parentRange.getLength()) {
childDocument.setParentDocumentRange(offset, length);
return true;
}
}
return false;
}
@Override
public IRegion getVisibleRegion() {
IDocument document = getVisibleDocument();
if (document instanceof ChildDocument) {
Position p = ((ChildDocument) document).getParentDocumentRange();
return new Region(p.getOffset(), p.getLength());
}
return new Region(0, document == null ? 0 : document.getLength());
}
/**
* Fire about to be changed event
*
* @param oldInput
* the old document
* @param newInput
* the new document
*/
protected void fireInputDocumentAboutToBeChanged(IDocument oldInput, IDocument newInput) {
List<ITextInputListener> listener = this.fTextInputListeners;
if (listener != null) {
for (int i = 0; i < listener.size(); i++) {
ITextInputListener l = (ITextInputListener) listener.get(i);
l.inputDocumentAboutToBeChanged(oldInput, newInput);
}
}
}
/**
* Fire changed event
*
* @param oldInput
* the old input
* @param newInput
* the new input
*/
protected void fireInputDocumentChanged(IDocument oldInput, IDocument newInput) {
List<ITextInputListener> listener = this.fTextInputListeners;
if (listener != null) {
for (int i = 0; i < listener.size(); i++) {
ITextInputListener l = (ITextInputListener) listener.get(i);
l.inputDocumentChanged(oldInput, newInput);
}
}
}
/**
* @return the visible document
*/
protected IDocument getVisibleDocument() {
return this.fVisibleDocument;
}
/**
* Free the slave document
*
* @param slave
* the slave
*/
protected void freeSlaveDocument(IDocument slave) {
ISlaveDocumentManager manager = getSlaveDocumentManager();
if (manager != null && manager.isSlaveDocument(slave))
manager.freeSlaveDocument(slave);
}
/**
* @return the slave document manager
*/
protected ISlaveDocumentManager getSlaveDocumentManager() {
if (this.fSlaveDocumentManager == null)
this.fSlaveDocumentManager = createSlaveDocumentManager();
return this.fSlaveDocumentManager;
}
/**
* @return create the slave document manager
*/
@SuppressWarnings("static-method")
protected ISlaveDocumentManager createSlaveDocumentManager() {
return new ChildDocumentManager();
}
/**
* Initialize the information mapping
*
* @param visibleDocument
* the visible document
*/
protected void initializeDocumentInformationMapping(IDocument visibleDocument) {
ISlaveDocumentManager manager = getSlaveDocumentManager();
this.fInformationMapping = manager == null ? null : manager.createMasterSlaveMapping(visibleDocument);
}
/**
* Report about the input change
*
* @param newInput
* the new input
* @param oldInput
* the old input
*/
protected void inputChanged(Object newInput, Object oldInput) {
IDocument oldDocument = (IDocument) oldInput;
if (oldDocument != null) {
if (this.fMarkPosition != null && !this.fMarkPosition.isDeleted())
oldDocument.removePosition(this.fMarkPosition);
try {
oldDocument.removePositionUpdater(this.fMarkPositionUpdater);
oldDocument.removePositionCategory(this.MARK_POSITION_CATEGORY);
} catch (BadPositionCategoryException e) {
// TODO Should we log that??
}
}
this.fMarkPosition = null;
if (oldDocument instanceof IDocumentExtension4) {
IDocumentExtension4 document = (IDocumentExtension4) oldDocument;
document.removeDocumentRewriteSessionListener(this.fDocumentRewriteSessionListener);
}
// super.inputChanged(newInput, oldInput);
if (newInput instanceof IDocumentExtension4) {
IDocumentExtension4 document = (IDocumentExtension4) newInput;
document.addDocumentRewriteSessionListener(this.fDocumentRewriteSessionListener);
}
IDocument newDocument = (IDocument) newInput;
if (newDocument != null) {
newDocument.addPositionCategory(this.MARK_POSITION_CATEGORY);
newDocument.addPositionUpdater(this.fMarkPositionUpdater);
}
}
private void initializeWidgetContents() {
if (this.fTextWidget != null && this.fVisibleDocument != null) {
IDocumentAdapter adapter = this.fDocumentAdapter;
// set widget content
if (adapter == null) {
this.fDocumentAdapter = adapter = createDocumentAdapter();
}
this.fDocumentAdapter.setDocument(this.fVisibleDocument);
this.fTextWidget.setContent(adapter);
// invalidate presentation
invalidateTextPresentation();
}
}
/**
* @return the document adapter
*/
@SuppressWarnings("static-method")
protected @NonNull IDocumentAdapter createDocumentAdapter() {
return new DefaultDocumentAdapter();
}
@Override
public final void invalidateTextPresentation() {
if (this.fVisibleDocument != null) {
this.fWidgetCommand.event = null;
this.fWidgetCommand.start = 0;
this.fWidgetCommand.length = this.fVisibleDocument.getLength();
this.fWidgetCommand.text = this.fVisibleDocument.get();
updateTextListeners(this.fWidgetCommand);
}
}
/**
* Update all text listeners
*
* @param cmd
* the command
*/
protected void updateTextListeners(WidgetCommand cmd) {
List<ITextListener> textListeners = this.fTextListeners;
if (textListeners != null) {
textListeners = new ArrayList<>(textListeners);
DocumentEvent event = cmd.event;
if (event instanceof SlaveDocumentEvent)
event = ((SlaveDocumentEvent) event).getMasterEvent();
TextEvent e = new TextEvent(cmd.start, cmd.length, cmd.text, cmd.preservedText, event, redraws());
for (int i = 0; i < textListeners.size(); i++) {
ITextListener l = (ITextListener) textListeners.get(i);
l.textChanged(e);
}
}
}
/**
* @return check if redraws are active
*/
protected final boolean redraws() {
return this.fRedrawCounter <= 0;
}
/**
* Handle the event
* @param event the event
*/
protected void handleVisibleDocumentAboutToBeChanged(DocumentEvent event) {
//nothing to be done
}
/**
* Handle the event
* @param event the event
*/
protected void handleVisibleDocumentChanged(DocumentEvent event) {
// nothing to be done
}
public void addTextListener(ITextListener listener) {
Assert.isNotNull(listener);
if (this.fTextListeners == null)
this.fTextListeners = new ArrayList();
if (!this.fTextListeners.contains(listener))
this.fTextListeners.add(listener);
}
public void removeTextListener(ITextListener listener) {
Assert.isNotNull(listener);
if (this.fTextListeners != null) {
this.fTextListeners.remove(listener);
if (this.fTextListeners.size() == 0)
this.fTextListeners = null;
}
}
public void addTextInputListener(ITextInputListener listener) {
Assert.isNotNull(listener);
if (this.fTextInputListeners == null)
this.fTextInputListeners = new ArrayList();
if (!this.fTextInputListeners.contains(listener))
this.fTextInputListeners.add(listener);
}
public void removeTextInputListener(ITextInputListener listener) {
Assert.isNotNull(listener);
if (this.fTextInputListeners != null) {
this.fTextInputListeners.remove(listener);
if (this.fTextInputListeners.size() == 0)
this.fTextInputListeners = null;
}
}
public void changeTextPresentation(TextPresentation presentation, boolean controlRedraw) {
if (presentation == null || !redraws())
return;
if (this.fTextWidget == null)
return;
/*
* Call registered text presentation listeners and let them apply their
* presentation.
*/
if (this.fTextPresentationListeners != null) {
ArrayList listeners = new ArrayList(this.fTextPresentationListeners);
for (int i = 0, size = listeners.size(); i < size; i++) {
ITextPresentationListener listener = (ITextPresentationListener) listeners.get(i);
listener.applyTextPresentation(presentation);
}
}
if (presentation.isEmpty())
return;
// if (controlRedraw)
// fTextWidget.setRedraw(false);
if (this.fReplaceTextPresentation)
applyTextPresentation(presentation);
else
addPresentation(presentation);
// if (controlRedraw)
// fTextWidget.setRedraw(true);
}
private void addPresentation(TextPresentation presentation) {
StyleRange range = presentation.getDefaultStyleRange();
if (range != null) {
range = modelStyleRange2WidgetStyleRange(range);
if (range != null)
this.fTextWidget.setStyleRange(range);
ArrayList ranges = new ArrayList(presentation.getDenumerableRanges());
Iterator e = presentation.getNonDefaultStyleRangeIterator();
while (e.hasNext()) {
range = (StyleRange) e.next();
range = modelStyleRange2WidgetStyleRange(range);
if (range != null)
ranges.add(range);
}
if (!ranges.isEmpty())
this.fTextWidget.replaceStyleRanges(0, 0, (StyleRange[]) ranges.toArray(new StyleRange[ranges.size()]));
} else {
IRegion region = modelRange2WidgetRange(presentation.getCoverage());
if (region == null)
return;
List list = new ArrayList(presentation.getDenumerableRanges());
Iterator e = presentation.getAllStyleRangeIterator();
while (e.hasNext()) {
range = (StyleRange) e.next();
range = modelStyleRange2WidgetStyleRange(range);
if (range != null)
list.add(range);
}
if (!list.isEmpty()) {
StyleRange[] ranges = new StyleRange[list.size()];
list.toArray(ranges);
this.fTextWidget.replaceStyleRanges(region.getOffset(), region.getLength(), ranges);
}
}
}
private void applyTextPresentation(TextPresentation presentation) {
List list = new ArrayList(presentation.getDenumerableRanges());
Iterator e = presentation.getAllStyleRangeIterator();
while (e.hasNext()) {
StyleRange range = (StyleRange) e.next();
range = modelStyleRange2WidgetStyleRange(range);
if (range != null)
list.add(range);
}
if (!list.isEmpty()) {
StyleRange[] ranges = new StyleRange[list.size()];
list.toArray(ranges);
this.fTextWidget.setStyleRanges(ranges);
}
}
protected StyleRange modelStyleRange2WidgetStyleRange(StyleRange range) {
IRegion region = modelRange2WidgetRange(new Region(range.start, range.length));
if (region != null) {
StyleRange result = (StyleRange) range.clone();
result.start = region.getOffset();
result.length = region.getLength();
return result;
}
return null;
}
public IRegion modelRange2WidgetRange(IRegion modelRange) {
if (this.fInformationMapping == null)
return modelRange;
try {
if (modelRange.getLength() < 0) {
Region reversed = new Region(modelRange.getOffset() + modelRange.getLength(), -modelRange.getLength());
IRegion result = this.fInformationMapping.toImageRegion(reversed);
if (result != null)
return new Region(result.getOffset() + result.getLength(), -result.getLength());
}
return this.fInformationMapping.toImageRegion(modelRange);
} catch (BadLocationException x) {
}
return null;
}
public IRewriteTarget getRewriteTarget() {
if (this.fRewriteTarget == null)
this.fRewriteTarget = new RewriteTarget();
return this.fRewriteTarget;
}
public final void setRedraw(boolean redraw) {
// TODO needs porting
// setRedraw(redraw, -1);
}
public void setVisibleRegion(int start, int length) {
IRegion region = getVisibleRegion();
if (start == region.getOffset() && length == region.getLength()) {
// nothing to change
return;
}
setRedraw(false);
try {
IDocument slaveDocument = createSlaveDocument(getVisibleDocument());
if (updateSlaveDocument(slaveDocument, start, length))
setVisibleDocument(slaveDocument);
} catch (BadLocationException x) {
throw new IllegalArgumentException("TextViewer.error.invalid_visible_region_2"); //$NON-NLS-1$
} finally {
setRedraw(true);
}
}
public void resetVisibleRegion() {
ISlaveDocumentManager manager = getSlaveDocumentManager();
if (manager != null) {
IDocument slave = getVisibleDocument();
IDocument master = manager.getMasterDocument(slave);
if (master != null) {
setVisibleDocument(master);
manager.freeSlaveDocument(slave);
}
}
}
class VisibleDocumentListener implements IDocumentListener {
public void documentAboutToBeChanged(DocumentEvent e) {
if (e.getDocument() == getVisibleDocument())
TextViewer.this.fWidgetCommand.setEvent(e);
handleVisibleDocumentAboutToBeChanged(e);
}
public void documentChanged(DocumentEvent e) {
if (TextViewer.this.fWidgetCommand.event == e)
updateTextListeners(TextViewer.this.fWidgetCommand);
fLastSentSelectionChange = null;
handleVisibleDocumentChanged(e);
}
}
/**
* Represents a replace command that brings the text viewer's text widget
* back in synchronization with text viewer's document after the document
* has been changed.
*/
protected class WidgetCommand {
/** The document event encapsulated by this command. */
public DocumentEvent event;
/** The start of the event. */
public int start;
/** The length of the event. */
public int length;
/** The inserted and replaced text segments of <code>event</code>. */
public String text;
/** The replaced text segments of <code>event</code>. */
public String preservedText;
/**
* Translates a document event into the presentation coordinates of this
* text viewer.
*
* @param e
* the event to be translated
*/
public void setEvent(DocumentEvent e) {
this.event = e;
this.start = e.getOffset();
this.length = e.getLength();
this.text = e.getText();
if (this.length != 0) {
try {
if (e instanceof SlaveDocumentEvent) {
SlaveDocumentEvent slave = (SlaveDocumentEvent) e;
DocumentEvent master = slave.getMasterEvent();
if (master != null)
this.preservedText = master.getDocument().get(master.getOffset(), master.getLength());
} else {
this.preservedText = e.getDocument().get(e.getOffset(), e.getLength());
}
} catch (BadLocationException x) {
this.preservedText = null;
// if (TRACE_ERRORS)
// System.out.println(JFaceTextMessages.getString("TextViewer.error.bad_location.WidgetCommand.setEvent"));
// //$NON-NLS-1$
}
} else
this.preservedText = null;
}
}
class DocumentRewriteSessionListener implements IDocumentRewriteSessionListener {
/*
* @see org.eclipse.jface.text.IDocumentRewriteSessionListener#
* documentRewriteSessionChanged(org.eclipse.jface.text.
* DocumentRewriteSessionEvent)
*/
public void documentRewriteSessionChanged(DocumentRewriteSessionEvent event) {
IRewriteTarget target = TextViewer.this.getRewriteTarget();
final boolean toggleRedraw;
// if (REDRAW_BUG_158746)
// toggleRedraw= true;
// else
toggleRedraw = event.getSession().getSessionType() != DocumentRewriteSessionType.UNRESTRICTED_SMALL;
final boolean viewportStabilize = !toggleRedraw;
if (DocumentRewriteSessionEvent.SESSION_START == event.getChangeType()) {
if (toggleRedraw)
target.setRedraw(false);
target.beginCompoundChange();
if (viewportStabilize && fViewerState == null)
fViewerState = new ViewerState();
} else if (DocumentRewriteSessionEvent.SESSION_STOP == event.getChangeType()) {
if (viewportStabilize && fViewerState != null) {