-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathAbstractMainView.java
More file actions
4424 lines (3735 loc) · 130 KB
/
Copy pathAbstractMainView.java
File metadata and controls
4424 lines (3735 loc) · 130 KB
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
/*
* 03/19/2004
*
* AbstractMainView.java - Abstract class representing a collection of
* RTextEditorPanes. This class contains all logic that would be common to
* different implementations (i.e., everything except the view parts).
* Copyright (C) 2004 Robert Futrell
* https://bobbylight.github.io/RText/
* Licensed under a modified BSD license.
* See the included license file for details.
*/
package org.fife.rtext;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.*;
import java.net.URL;
import java.nio.file.FileSystems;
import java.util.*;
import java.util.Timer;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.EventListenerList;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Caret;
import org.fife.io.UnicodeWriter;
import org.fife.rsta.ui.GoToDialog;
import org.fife.rsta.ui.search.*;
import org.fife.rtext.SearchManager.SearchingMode;
import org.fife.rtext.actions.AbstractSearchAction;
import org.fife.rtext.actions.CapsLockAction;
import org.fife.rtext.actions.ToggleTextModeAction;
import org.fife.ui.UIUtil;
import org.fife.ui.app.AbstractGUIApplication;
import org.fife.ui.app.AppTheme;
import org.fife.ui.autocomplete.Util;
import org.fife.ui.rsyntaxtextarea.*;
import org.fife.ui.rsyntaxtextarea.parser.ParserNotice;
import org.fife.ui.rtextarea.*;
import org.fife.ui.rtextfilechooser.RTextFileChooser;
import org.fife.ui.search.*;
import org.fife.util.MacOSUtil;
/**
* Abstract class representing a collection of RTextEditorPanes. This class
* contains all logic that would be common to different implementations (i.e.,
* everything except the "view" parts).<p>
*
* An implementation of this class must fire a property change event of type
* {@link #CURRENT_DOCUMENT_PROPERTY} whenever the currently-active document
* changes so that other pieces of RText can function properly.<p>
*
* RText plugins may wish to register to be
* <code>CurrentTextAreaListener</code>s if they want to be notified whenever
* a property of the currently-active text area (or the text area itself)
* changes.
*
* @author Robert Futrell
* @version 0.5
*/
@SuppressWarnings("checkstyle:VisibilityModifier")
public abstract class AbstractMainView extends JPanel
implements PropertyChangeListener, ActionListener, SearchListener,
FindInFilesListener, HyperlinkListener {
public static final int DOCUMENT_SELECT_TOP = JTabbedPane.TOP;
public static final int DOCUMENT_SELECT_LEFT = JTabbedPane.LEFT;
public static final int DOCUMENT_SELECT_BOTTOM = JTabbedPane.BOTTOM;
public static final int DOCUMENT_SELECT_RIGHT = JTabbedPane.RIGHT;
public static final String AUTO_INSERT_CLOSING_CURLYS = "MainView.autoInsertClosingCurlys";
public static final String CURRENT_DOCUMENT_PROPERTY = "MainView.currentDocument";
public static final String DEFAULT_ENCODING_PROPERTY = "MainView.defaultEncoding";
public static final String FILE_SIZE_CHECK_PROPERTY = "MainView.fileSizeCheck";
public static final String FRACTIONAL_METRICS_PROPERTY = "MainView.fractionalMetrics";
public static final String MARK_ALL_COLOR_PROPERTY = "MainView.markAllColor";
public static final String MARK_OCCURRENCES_COLOR_PROPERTY = "MainView.markOccurrencesColor";
public static final String MARK_OCCURRENCES_PROPERTY = "MainView.markOccurrences";
public static final String MAX_FILE_SIZE_PROPERTY = "MainView.maxFileSize";
public static final String MAX_FILE_SIZE_FOR_CODE_FOLDING_PROPERTY = "MainView.maxFileSizeForCodeFolding";
public static final String REMEMBER_WS_LINES_PROPERTY = "MainView.rememberWhitespaceLines";
public static final String ROUNDED_SELECTION_PROPERTY = "MainView.roundedSelection";
public static final String SMOOTH_TEXT_PROPERTY = "MainView.smoothText";
public static final String TEXT_AREA_ADDED_PROPERTY = "MainView.textAreaAdded";
public static final String TEXT_AREA_REMOVED_PROPERTY = "MainView.textAreaRemoved";
private RTextEditorPane currentTextArea; // Currently active text area.
public FindInFilesSearchContext searchContext;
private SearchManager searchManager;
private boolean lineNumbersEnabled; // If true, line numbers are visible on the documents.
private boolean lineWrapEnabled; // If true, word wrap is enabled for all documents.
private String defaultLineTerminator; // Line terminator of new text files.
private String defaultEncoding; // Encoding of new text files.
private boolean guessFileContentType;
public FindInFilesDialog findInFilesDialog; // Dialog for searching for text in files.
public ReplaceInFilesDialog replaceInFilesDialog;
public GoToDialog goToDialog; // Dialog that lets you go to a certain line number.
private int textMode; // Either INSERT_MODE or OVERWRITE_MODE.
private int tabSize; // The size (in spaces) tabs are.
private boolean emulateTabsWithWhitespace; // If true, tabs are emulated with spaces.
private Font printFont; // The font to use when printing a document.
private Color caretColor; // The color used for carets.
private Color selectionColor; // The color used for selections.
private Color selectedTextColor;
private boolean useSelectedTextColor;
private Color background; // Text area background color
private float imageAlpha; // Alpha value used to make the bg image translucent.
protected RText owner;
private SyntaxFilters syntaxFilters; // Used to decide how to syntax highlight a file.
private boolean highlightCurrentLine; // whether the current line is highlighted.
private Color currentLineColor; // The color with which to highlight the current line.
private boolean highlightModifiedDocDisplayNames; // Color display names of modified files differently?
private Color modifiedDocumentDisplayNameColor; // Color to color display names of modified editors.
private boolean checkForModification; // Check for files being changed outside of RText?
private long modificationCheckDelay = 10000; // Delay in milliseconds.
private boolean overrideEditorStyles;
private boolean bracketMatchingEnabled;
private boolean matchBothBrackets;
private Color matchedBracketBGColor;
private Color matchedBracketBorderColor;
private boolean marginLineEnabled;
private int marginLinePosition;
private Color marginLineColor;
private boolean highlightSecondaryLanguages;
private Color[] secondaryLanguageColors;
private Color hyperlinkColor;
private boolean whitespaceVisible;
private boolean showEOLMarkers;
private boolean showTabLines;
private Color tabLinesColor;
private boolean rememberWhitespaceLines;
private boolean autoInsertClosingCurlys;
private boolean aaEnabled; // Whether text is anti-aliased.
private boolean fractionalMetricsEnabled; // Whether fractional fontmetrics are used.
private Color markAllHighlightColor;
private boolean markOccurrences;
private Color markOccurrencesColor;
private boolean roundedSelectionEdges;
private int caretBlinkRate;
private CaretStyle[] carets; // index 0=>insert, 1=>overwrite.
private boolean doFileSizeCheck;
private float maxFileSize; // In MB.
private int maxFileSizeForCodeFolding;
private boolean ignoreBackupExtensions;
private Font textAreaFont;
private boolean textAreaUnderline;
private Color textAreaForeground;
private ComponentOrientation textAreaOrientation;
private FoldIndicatorStyle foldIndicatorStyle;
private Color foldForeground;
private Color armedFoldForeground;
private Color foldBackground;
private Color armedFoldBackground;
private EventListenerList listenerList;
private Map<String, Boolean> codeFoldingEnabledStates;
private Icon bookmarkIcon;
private Font lineNumberFont;
private Color lineNumberColor;
private Color gutterBorderColor;
private SpellingSupport spellingSupport;
private ToggleTextModeAction toggleTextModeAction;
private CapsLockAction capsLockAction;
/**
* The cursor used when recording a macro.
*/
private static Cursor macroCursor;
/**
* Constructor.<p>
* You should call {@link #initialize} right after this.
*/
public AbstractMainView() {
listenerList = new EventListenerList();
checkForModification = true;
Timer t = new Timer();
// Check for files modified outside the editor
// every 30 seconds
t.schedule(new TimerTask() {
@Override
public void run() {
checkFilesForOutsideModification();
}
},
modificationCheckDelay,
modificationCheckDelay);
}
// Callback for various actions.
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
// If a file was found to be modified outside of the editor...
if (command.startsWith("FileModified. ")) {
handleFileModifiedEvent(command);
}
}
/**
* Adds a current text area listener.
*
* @param l The listener to add.
* @see #removeCurrentTextAreaListener
*/
public void addCurrentTextAreaListener(CurrentTextAreaListener l) {
listenerList.add(CurrentTextAreaListener.class, l);
}
/**
* Adds an empty text file to this tabbed pane. This method is
* synchronized so it doesn't interfere with the thread checking for
* files being modified outside of the editor.
*
* @param fileNameAndPath The full path and name of the file to add.
* @param encoding The encoding in which the file is to be saved. If
* an invalid value is passed in, the system default encoding
* is used.
*/
private synchronized void addNewEmptyFile(String fileNameAndPath,
String encoding) {
// Ensure the encoding is a proper value.
if (encoding==null) {
encoding = RTextFileChooser.getDefaultEncoding();
}
// Actually create the file on disk.
if (!fileNameAndPath.equals(getDefaultFileName())) {
try {
new File(fileNameAndPath).createNewFile();
} catch (IOException ioe) {
String text = owner.getString("ErrorWritingFile",
fileNameAndPath, ioe.getMessage());
JOptionPane.showMessageDialog(this, text,
owner.getString("ErrorDialogTitle"),
JOptionPane.ERROR_MESSAGE);
}
}
// Set pointers for easy reference to new document.
try {
currentTextArea = createRTextEditorPane(fileNameAndPath, encoding);
} catch (IOException ioe) {
owner.displayException(ioe);
ensureFilesAreOpened();
return;
}
// Add new text file to tabbed pane.
RTextScrollPane scrollPane = createScrollPane(currentTextArea);
currentTextArea.applyComponentOrientation(getTextAreaOrientation());
addTextAreaImpl(currentTextArea.getFileName(), scrollPane,
currentTextArea.getFileFullPath());
// Let anybody who cares know we've opened this file.
firePropertyChange(TEXT_AREA_ADDED_PROPERTY, null, currentTextArea);
}
/**
* Adds an empty text file with a default name to this panel. This method
* is synchronized so it doesn't interfere with the thread checking for
* files being modified outside of the editor.
*/
public synchronized void addNewEmptyUntitledFile() {
addNewEmptyFile(getDefaultFileName(), getDefaultEncoding());
}
/**
* Adds a text area to this view. This method fires a property change
* event of type {@link #TEXT_AREA_ADDED_PROPERTY}.
*
* @param textArea The text area to add.
* @see #addTextAreaImpl(String, Component, String)
*/
private void addTextArea(RTextEditorPane textArea) {
// This is needed because the text area's undoManager picked up
// the read() call above and added it as an insertion edit. We
// don't want the user to be able to undo this, however.
textArea.discardAllEdits();
// Add the new document into our tabbed pane.
// This sets currentTextArea==tempTextArea.
RTextScrollPane scrollPane = createScrollPane(textArea);
textArea.applyComponentOrientation(getTextAreaOrientation());
addTextAreaImpl(textArea.getFileName(), scrollPane,
textArea.getFileFullPath());
// REMEMBER: currentTextArea has just been updated by
// addTextAreaImpl() above!!
// Let anybody who cares know we've opened this file.
firePropertyChange(TEXT_AREA_ADDED_PROPERTY, null, currentTextArea);
moveToTopOfCurrentDocument();
}
/**
* Adds a text area visually to this panel.
*
* @param title The name of the document to display.
* @param component The component to add (usually an RTextScrollPane).
* @param fileFullPath The full path to the file being displayed by the
* component.
*/
protected abstract void addTextAreaImpl(String title,
Component component, String fileFullPath);
/**
* Overridden so we ensure text areas keep their special LTR or RTL
* orientations.
*
* @param o The new component orientation.
*/
@Override
public void applyComponentOrientation(ComponentOrientation o) {
super.applyComponentOrientation(o);
// Force a reset of textAreaOrientation since the
// applyComponentOrientation() above will trickle down to the
// text areas and override their special orientations.
ComponentOrientation temp = getTextAreaOrientation();
textAreaOrientation = null;
setTextAreaOrientation(temp);
}
/**
* Returns whether tabs are emulated with spaces.
*
* @return <code>true</code> iff tabs are emulated with spaces.
*/
public boolean areTabsEmulated() {
return emulateTabsWithWhitespace;
}
/**
* Checks the "modified" timestamps for open files against the last known
* "modified" timestamps to see if any files have been modified outside of
* this RText instance. This method is synchronized so that it isn't
* called while the user is loading or saving a file.
*/
public synchronized void checkFilesForOutsideModification() {
// If we're currently not waiting on the user to decide about a
// previous "another program modified..." message...
if (checkForModification) {
// Flag so that if the user takes to long deciding, messages
// don't pile up about the same file being modified.
// NOTE: This is theoretically not thread-safe, but the
// delay is set at 10 seconds, so it should be more than
// enough to get to and complete this line).
checkForModification = false;
StringBuilder sb = new StringBuilder();
for (int i=0; i<getNumDocuments(); i++) {
RTextEditorPane textArea = getRTextEditorPaneAt(i);
if (textArea.isModifiedOutsideEditor()) {
sb.append(' ').append(i);
}
}
// If no documents were modified outside the editor, allow the
// thread to check again; otherwise, remember to prompt the user
// about all of the documents that changed outside of the editor.
if (sb.length()==0) {
checkForModification = true;
}
else {
final String actionCommand = "FileModified." + sb;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
actionPerformed(new ActionEvent(this,
ActionEvent.ACTION_PERFORMED, actionCommand));
}
});
}
} // End of if (checkForModification==true).
}
/**
* Attempts to close all currently active documents.
*
* @return <code>true</code> if all active documents were closed, and
* <code>false</code> if they weren't (i.e., the user hit cancel).
*/
public boolean closeAllDocuments() {
return closeAllDocumentsExcept(-1);
}
/**
* Attempts to close all currently active documents except the one
* specified.
*
* @param except The document to not close.
* @return <code>true</code> if the documents were all closed, and
* <code>false</code> if they weren't (i.e., the user hit cancel).
*/
public boolean closeAllDocumentsExcept(int except) {
int numDocuments = getNumDocuments();
setSelectedIndex(numDocuments-1); // Start at the back.
// Cycle through each document, one by one.
for (int i=numDocuments-1; i>=0; i--) {
if (i==except) {
// Instead of removing this document, set focus to the
// "first" document, and continue closing documents with the
// next iteration. Since we're only keeping around 1
// document, this keeps it open.
if (i>0) {
setSelectedIndex(0);
}
}
else {
// Try to close the document.
boolean closed = closeCurrentDocument();
// If the user cancels out of it, quit the whole shebang.
if (!closed) {
// If the newly-active file is read-only, say so in the status bar.
owner.setStatusBarReadOnlyIndicatorEnabled(
currentTextArea != null && currentTextArea.isReadOnly());
return false;
}
}
} // End of for (int i=tabCount-1; i>=0; i--).
// If we got this far, then all documents were closed.
// We'll just have an empty default-named file out there.
return true;
}
/**
* Attempts to close the current document.
*
* @return Whether the file was closed (e.g. the user didn't cancel the
* operation). This will also return <code>false</code> if an
* IO error occurs saving the file, if the user chooses to do so.
*/
public final boolean closeCurrentDocument() {
RTextEditorPane old = currentTextArea;
boolean closed = closeCurrentDocumentImpl();
if (closed) {
old.clearParsers();
firePropertyChange(TEXT_AREA_REMOVED_PROPERTY, null, old);
}
return closed;
}
/**
* Attempts to close the current document. Any implementation of this
* method <i>must be synchronized</i> so it doesn't interfere with the
* thread checking for files being modified outside of the editor.
*
* @return Whether the document was closed (e.g. the user didn't cancel the
* operation).
*/
protected abstract boolean closeCurrentDocumentImpl();
/**
* Converts all instances of a number of spaces equal to a tab in all open
* documents into tabs.
*
* @see #convertOpenFilesTabsToSpaces
*/
public void convertOpenFilesSpacesToTabs() {
for (int i=0; i<getNumDocuments(); i++)
getRTextEditorPaneAt(i).convertSpacesToTabs();
}
/**
* Converts all tabs in all open documents into an equivalent number of
* spaces.
*
* @see #convertOpenFilesSpacesToTabs
*/
public void convertOpenFilesTabsToSpaces() {
for (int i=0; i<getNumDocuments(); i++)
getRTextEditorPaneAt(i).convertTabsToSpaces();
}
/**
* Copies data from another view into this one. Useful when
* changing from a tabbed to a list view, for example.
*
* @param fromPanel The panel to copy data from.
*/
public void copyData(AbstractMainView fromPanel) {
currentTextArea = fromPanel.currentTextArea;
searchManager = fromPanel.searchManager;
searchContext = fromPanel.searchContext;
lineNumbersEnabled = fromPanel.lineNumbersEnabled;
lineWrapEnabled = fromPanel.lineWrapEnabled;
findInFilesDialog = fromPanel.findInFilesDialog;
if (findInFilesDialog!=null) {
findInFilesDialog.removeFindInFilesListener(fromPanel);
findInFilesDialog.addFindInFilesListener(this);
}
replaceInFilesDialog = fromPanel.replaceInFilesDialog;
if (replaceInFilesDialog!=null) {
replaceInFilesDialog.removeFindInFilesListener(fromPanel);
replaceInFilesDialog.addFindInFilesListener(this);
}
goToDialog = fromPanel.goToDialog;
overrideEditorStyles = fromPanel.overrideEditorStyles;
textMode = fromPanel.textMode;
tabSize = fromPanel.tabSize;
emulateTabsWithWhitespace = fromPanel.emulateTabsWithWhitespace;
printFont = fromPanel.printFont;
caretColor = fromPanel.caretColor;
selectionColor = fromPanel.selectionColor;
selectedTextColor = fromPanel.selectedTextColor;
useSelectedTextColor = fromPanel.useSelectedTextColor;
background = fromPanel.background;
imageAlpha = fromPanel.imageAlpha;
owner = fromPanel.owner;
syntaxFilters = fromPanel.syntaxFilters;
highlightCurrentLine = fromPanel.highlightCurrentLine;
currentLineColor = fromPanel.currentLineColor;
highlightModifiedDocDisplayNames = fromPanel.highlightModifiedDocDisplayNames;
modifiedDocumentDisplayNameColor = fromPanel.modifiedDocumentDisplayNameColor;
checkForModification = fromPanel.checkForModification;
modificationCheckDelay = fromPanel.modificationCheckDelay;
bracketMatchingEnabled = fromPanel.bracketMatchingEnabled;
matchBothBrackets = fromPanel.matchBothBrackets;
matchedBracketBGColor = fromPanel.matchedBracketBGColor;
matchedBracketBorderColor = fromPanel.matchedBracketBorderColor;
marginLineEnabled = fromPanel.marginLineEnabled;
marginLinePosition = fromPanel.marginLinePosition;
marginLineColor = fromPanel.marginLineColor;
highlightSecondaryLanguages = fromPanel.highlightSecondaryLanguages;
System.arraycopy(fromPanel.secondaryLanguageColors, 0, secondaryLanguageColors, 0,
secondaryLanguageColors.length);
whitespaceVisible = fromPanel.whitespaceVisible;
showEOLMarkers = fromPanel.showEOLMarkers;
showTabLines = fromPanel.showTabLines;
tabLinesColor = fromPanel.tabLinesColor;
rememberWhitespaceLines = fromPanel.rememberWhitespaceLines;
autoInsertClosingCurlys = fromPanel.autoInsertClosingCurlys;
aaEnabled = fromPanel.aaEnabled;
fractionalMetricsEnabled = fromPanel.fractionalMetricsEnabled;
markAllHighlightColor = fromPanel.markAllHighlightColor;
markOccurrences = fromPanel.markOccurrences;
markOccurrencesColor = fromPanel.markOccurrencesColor;
roundedSelectionEdges = fromPanel.roundedSelectionEdges;
caretBlinkRate = fromPanel.caretBlinkRate;
carets = fromPanel.carets.clone();
doFileSizeCheck = fromPanel.doFileSizeCheck;
maxFileSize = fromPanel.maxFileSize;
maxFileSizeForCodeFolding = fromPanel.maxFileSizeForCodeFolding;
ignoreBackupExtensions = fromPanel.ignoreBackupExtensions;
textAreaFont = fromPanel.textAreaFont;
textAreaUnderline = fromPanel.textAreaUnderline;
textAreaForeground = fromPanel.textAreaForeground;
textAreaOrientation = fromPanel.textAreaOrientation;
foldIndicatorStyle = fromPanel.foldIndicatorStyle;
foldForeground = fromPanel.foldForeground;
armedFoldForeground = fromPanel.armedFoldForeground;
foldBackground = fromPanel.foldBackground;
armedFoldBackground = fromPanel.armedFoldBackground;
// "Move over" all current text area listeners.
// Remember "listeners" is guaranteed to be non-null.
Object[] listeners = fromPanel.listenerList.getListenerList();
Class<CurrentTextAreaListener> ctalClass = CurrentTextAreaListener.class;
for (int i=0; i<listeners.length; i+=2) {
if (listeners[i]==ctalClass) {
CurrentTextAreaListener l =
(CurrentTextAreaListener)listeners[i+1];
fromPanel.listenerList.remove(ctalClass, l);
listenerList.add(ctalClass, l);
}
}
bookmarkIcon = fromPanel.bookmarkIcon;
lineNumberFont = fromPanel.lineNumberFont;
lineNumberColor = fromPanel.lineNumberColor;
gutterBorderColor = fromPanel.gutterBorderColor;
setPreferredSize(fromPanel.getPreferredSize());
int numDocuments = fromPanel.getNumDocuments();
int fromSelectedIndex = fromPanel.getSelectedIndex();
ArrayList<RTextScrollPane> scrollPanes =
new ArrayList<>(numDocuments);
for (int i=0; i<numDocuments; i++) {
scrollPanes.add(fromPanel.getRTextScrollPaneAt(0));
fromPanel.removeComponentAt(0);
}
for (int i=0; i<numDocuments; i++) {
RTextScrollPane scrollPane = scrollPanes.get(i);
RTextEditorPane editorPane = (RTextEditorPane)scrollPane.getTextArea();
addTextAreaImpl(editorPane.getFileName(), scrollPane,
editorPane.getFileFullPath());
editorPane.removePropertyChangeListener(fromPanel);
editorPane.removeHyperlinkListener(fromPanel);
editorPane.addPropertyChangeListener(this);
editorPane.addHyperlinkListener(this);
}
removeComponentAt(0); // Remove the default-named file.
renumberDisplayNames(); // In case the same document is opened multiple times.
setSelectedIndex(fromSelectedIndex);
spellingSupport = fromPanel.spellingSupport;
}
protected ErrorStrip createErrorStrip(RTextEditorPane textArea) {
ErrorStrip strip = new ErrorStrip(textArea);
strip.setLevelThreshold(ParserNotice.Level.WARNING);
return strip;
}
/**
* Returns an editor pane to add to this main view.
*
* @param fileName The name of the file to add.
* @param encoding The encoding of the file.
* @return An editor pane.
* @throws IOException If an IO error occurs reading the file to load.
*/
private RTextEditorPane createRTextEditorPane(String fileName,
String encoding) throws IOException {
return createRTextEditorPane(FileLocation.create(fileName), encoding);
}
/**
* Returns an editor pane to add to this main view.
*
* @param loc The location of the file to add.
* @param encoding The encoding of the file.
* @return An editor pane.
* @throws IOException If an IO error occurs reading the file to load.
*/
private RTextEditorPane createRTextEditorPane(FileLocation loc,
String encoding) throws IOException {
String style = getSyntaxStyleForFile(loc.getFileName());
RTextEditorPane pane = new RTextEditorPane(owner, lineWrapEnabled,
textMode, loc, encoding);
// Set some properties.
pane.setFont(getTextAreaFont());
//pane.setUnderline(textAreaUnderline);
pane.setForeground(getTextAreaForeground());
pane.setBackgroundObject(getTextAreaBackgroundColor());
pane.setTabSize(getTabSize());
pane.setHighlightCurrentLine(highlightCurrentLine);
if (currentLineColor != null) {
pane.setCurrentLineHighlightColor(currentLineColor);
}
pane.setMarginLineEnabled(marginLineEnabled);
pane.setMarginLinePosition(getMarginLinePosition());
pane.setMarginLineColor(getMarginLineColor());
pane.setHighlightSecondaryLanguages(getHighlightSecondaryLanguages());
for (int i=0; i<secondaryLanguageColors.length; i++) {
pane.setSecondaryLanguageBackground(i+1, getSecondaryLanguageColor(i));
}
pane.setMarkAllHighlightColor(getMarkAllHighlightColor());
pane.setMarkOccurrences(getMarkOccurrences());
pane.setMarkOccurrencesColor(getMarkOccurrencesColor());
setSyntaxStyle(pane, style);
pane.setBracketMatchingEnabled(isBracketMatchingEnabled());
pane.setPaintMatchedBracketPair(getMatchBothBrackets());
pane.setMatchedBracketBGColor(getMatchedBracketBGColor());
pane.setMatchedBracketBorderColor(getMatchedBracketBorderColor());
if (defaultLineTerminator!=null &&
pane.getDocument().getLength()==0) {
// Empty (or new) file => use default line terminator.
pane.setLineSeparator(defaultLineTerminator, false);
}
pane.setWhitespaceVisible(isWhitespaceVisible());
pane.setPaintTabLines(getShowTabLines());
pane.setTabLineColor(getTabLinesColor());
pane.setEOLMarkersVisible(getShowEOLMarkers());
pane.setClearWhitespaceLinesEnabled(!rememberWhitespaceLines);
pane.setCloseCurlyBraces(autoInsertClosingCurlys);
pane.setCaretColor(getCaretColor());
pane.setSelectionColor(getSelectionColor());
pane.setSelectedTextColor(getSelectedTextColor());
pane.setUseSelectedTextColor(getUseSelectedTextColor());
pane.setSyntaxScheme(owner.getSyntaxScheme());
if (hyperlinkColor != null) {
pane.setHyperlinkForeground(hyperlinkColor);
}
pane.setRoundedSelectionEdges(getRoundedSelectionEdges());
pane.setCaretStyle(RTextEditorPane.INSERT_MODE,
carets[RTextEditorPane.INSERT_MODE]);
pane.setCaretStyle(RTextEditorPane.OVERWRITE_MODE,
carets[RTextEditorPane.OVERWRITE_MODE]);
pane.getCaret().setBlinkRate(getCaretBlinkRate());
//pane.setFadeCurrentLineHighlight(fadeCurrentLineHighlight);
// If we're in the middle of recording a macro, make the cursor
// appropriate on this guy.
if (RTextEditorPane.isRecordingMacro()) {
pane.setCursor(getMacroCursor());
}
// Other properties.
pane.setTabsEmulated(emulateTabsWithWhitespace);
pane.setAntiAliasingEnabled(aaEnabled);
pane.setFractionalFontMetricsEnabled(isFractionalFontMetricsEnabled());
// orientation is done later to override scroll pane's
// applyComponentOrientation(...).
//pane.applyComponentOrientation(getTextAreaOrientation());
setCodeFoldingEnabledForTextArea(pane, isCodeFoldingEnabledFor(style));
// Listeners.
pane.addPropertyChangeListener(owner);
pane.addPropertyChangeListener((StatusBar)owner.getStatusBar());
pane.addPropertyChangeListener(this);
pane.addHyperlinkListener(this);
// Add any parsers.
if (spellingSupport.isSpellCheckingEnabled()) {
pane.addParser(spellingSupport.getSpellingParser());
}
// Override the default Insert key action to one that toggles the text
// mode for all text editors.
InputMap im = pane.getInputMap();
ActionMap am = pane.getActionMap();
am.put(RTextAreaEditorKit.rtaToggleTextModeAction, toggleTextModeAction);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_CAPS_LOCK, 0), "OnCapsLock");
am.put("OnCapsLock", capsLockAction);
return pane;
}
/**
* Creates and returns a scroll pane containing a text area.
*
* @param textArea The text area.
* @return The scroll pane.
*/
private RTextScrollPane createScrollPane(RTextEditorPane textArea) {
RTextScrollPane scrollPane = new RTextScrollPane(textArea,
lineNumbersEnabled, null);
scrollPane.applyComponentOrientation(getComponentOrientation());
Gutter gutter = scrollPane.getGutter();
gutter.setBookmarkIcon(bookmarkIcon);
gutter.setBookmarkingEnabled(true);
gutter.setLineNumberFont(lineNumberFont);
gutter.setLineNumberColor(lineNumberColor);
gutter.setBorderColor(gutterBorderColor);
// Always visible, makes life easier
scrollPane.setIconRowHeaderEnabled(true);
Color activeLineRangeColor = getAppropriateActiveLineRangeColor();
gutter.setActiveLineRangeColor(activeLineRangeColor);
gutter.setFoldIndicatorStyle(foldIndicatorStyle);
gutter.setFoldIndicatorForeground(foldForeground);
gutter.setFoldIndicatorArmedForeground(armedFoldForeground);
gutter.setFoldBackground(foldBackground);
gutter.setArmedFoldBackground(armedFoldBackground);
UIUtil.removeTabbedPaneFocusTraversalKeyBindings(scrollPane);
return scrollPane;
}
/**
* Disposes of this view. This is called when the user changes the main
* view style. The default implementation does nothing; subclasses can
* override to dispose of anything they want.
*/
public void dispose() {
}
/**
* Ensures at least 1 file is open.
*/
private void ensureFilesAreOpened() {
if (getNumDocuments()==0) {
addNewEmptyUntitledFile();
}
}
/**
* Called when the user selects a file in a listened-to find-in-files
* dialog.
*
* @param e The event received from the <code>FindInFilesDialog</code>.
*/
@Override
public void findInFilesFileSelected(FindInFilesEvent e) {
String fileName = e.getFileName();
// "null" encoding means check for Unicode before using default.
// "true" means reuse an already-opened copy of the file if
// one exists.
if (!openFile(fileName, null, true)) {
JOptionPane.showMessageDialog(findInFilesDialog,
owner.getString("ErrorReloadFNF"),
owner.getString("ErrorDialogTitle"),
JOptionPane.ERROR_MESSAGE);
return;
}
FindInFilesDialog fnfd = (FindInFilesDialog)e.getSource();
String desc = owner.getString("FileOpened", fileName);
fnfd.setStatusText(desc);
int line = e.getLine();
if (line!=-1) {
try {
// currentTextArea is updated here. Highlight the searched-for
// text.
int start = currentTextArea.getLineStartOffset(line-1);
int end = currentTextArea.getLineEndOffset(line-1) - 1;
currentTextArea.setCaretPosition(end);
currentTextArea.moveCaretPosition(start);
currentTextArea.getCaret().setSelectionVisible(true);
// The editor isn't visible initially, must wait to do this
SwingUtilities.invokeLater(() -> RTextUtilities.centerSelectionVertically(currentTextArea));
} catch (Exception exc) {
owner.displayException(exc);
moveToTopOfCurrentDocument();
}
}
else
moveToTopOfCurrentDocument();
}
/**
* Notifies all registered <code>CurrentTextAreaListener</code>s of a
* change in the current text area.
*
* @param type The type of event to fire.
* @param oldValue The old value.
* @param newValue The new value.
*/
protected void fireCurrentTextAreaEvent(int type, Object oldValue,
Object newValue) {
// Guaranteed to return a non-null array.
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event.
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==CurrentTextAreaListener.class) {
((CurrentTextAreaListener)listeners[i+1]).
currentTextAreaPropertyChanged(
new CurrentTextAreaEvent(this, type,
oldValue, newValue));
}
}
}
/**
* Returns the color to use for the "active line range" of editors. The
* user currently cannot set this, but we try to be smart and pick a good
* color based on the foreground/background colors of the current Look and
* Feel.
*
* @return The color. <code>null</code> means to use the default.
*/
private Color getAppropriateActiveLineRangeColor() {
Component c = owner.getJMenuBar()!=null ?
owner.getJMenuBar().getMenu(0) : new JLabel();
Color fg = c.getForeground();
return Util.isLightForeground(fg) ? fg : null;
}
/**
* Returns the color to use for the background of armed fold icons.
*
* @return The color.
* @see #setArmedFoldBackground(Color)
* @see #getFoldBackground()
*/
public Color getArmedFoldBackground() {
return armedFoldBackground;
}
/**
* Returns the color to use for the foreground of armed fold icons.
*
* @return The color.
* @see #setArmedFoldForeground(Color)
* @see #getFoldForeground()
*/
public Color getArmedFoldForeground() {
return armedFoldForeground;
}
/**
* Returns whether closing curly braces are auto-inserted in languages
* where it is appropriate.
*
* @return Whether closing curly braces are auto-inserted.
* @see #setAutoInsertClosingCurlys(boolean)