-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathRTextMDIView.java
More file actions
688 lines (542 loc) · 17.2 KB
/
Copy pathRTextMDIView.java
File metadata and controls
688 lines (542 loc) · 17.2 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
/*
* 04/16/2004
*
* RTextMDIView.java - A multi-document interface implementation.
* 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.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javax.swing.*;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;
import org.fife.ui.rsyntaxtextarea.ErrorStrip;
import org.fife.ui.rtextarea.RTextScrollPane;
/**
* An implementation of a multi-document interface.
*
* @author Robert Futrell
* @version 1.0
*/
class RTextMDIView extends AbstractMainView implements InternalFrameListener {
private static final int CASCADE_X_INCREMENT = 30;
private static final int CASCADE_Y_INCREMENT = 30;
private static int openFrameCount;
private JDesktopPane desktopPane;
private java.util.List<InternalFrame> frames;
private JPopupMenu popupMenu;
// Whether documentList should be in the top, left, bottom, or right pane.
private int documentSelectionPlacement;
/**
* Creates a new <code>RTextMDIView</code>.
*
* @param owner The <code>RText</code> that this view sits in.
* @param filesToOpen Array of strings representing files to open. If
* this parameter is null, a single file with a default name is
* opened.
* @param properties A properties object used to initialize some fields on
* this view.
*/
RTextMDIView(RText owner, String[] filesToOpen,
RTextPrefs properties) {
frames = new ArrayList<>(5);
setLayout(new GridLayout(1,1));
desktopPane = new JDesktopPane();
desktopPane.setBackground(Color.GRAY);
desktopPane.addMouseListener(new MDIMouseListener());
add(desktopPane);
// Add transfer handler to listen for files being drag-and-dropped
// into this main view.
TransferHandler th = new MainPanelTransferHandler(this);
desktopPane.setTransferHandler(th);
// Set everything up.
initialize(owner, filesToOpen, properties);
}
/**
* Adds a new document to the desktop pane and places a number beside
* documents opened multiple times.
*
* @param title The "display name" for the document.
* @param component The scroll pane containing the text editor to add.
* @param fileFullPath The full path to the document being added.
*/
@Override
protected void addTextAreaImpl(String title, Component component,
String fileFullPath) {
JPanel temp = new JPanel(new BorderLayout());
temp.add(component);
RTextScrollPane sp = (RTextScrollPane)component;
RTextEditorPane textArea = (RTextEditorPane)sp.getTextArea();
ErrorStrip es = createErrorStrip(textArea);
temp.add(es, BorderLayout.LINE_END);
// "Physically" add the frame.
InternalFrame frame = new InternalFrame(title, temp);
frame.setVisible(true); // Necessary.
frame.addInternalFrameListener(this);
desktopPane.add(frame);
frames.add(frame);
// Loop through all tabs (documents) except the last (the one just added).
int numDocuments = getNumDocuments();
for (int i=0; i<numDocuments-1; i++) {
// If any of them is the same physical file as the just added one, do the numbering.
if (getRTextEditorPaneAt(i).getFileFullPath().equals(fileFullPath)) {
int count = 0;
for (int j=i; j<numDocuments; j++) {
RTextEditorPane pane = getRTextEditorPaneAt(j);
if (pane.getFileFullPath().equals(fileFullPath)) {
String newTitle = title + " (" + (++count) + ")";
if (pane.isDirty())
newTitle = newTitle + "*";
try {
setDocumentDisplayNameAt(j, newTitle);
} catch (Exception e) {
System.err.println("Exception: " + e);
}
}
}
break;
}
}
// Do any extra stuff.
// This updates currentTextArea and shifts focus too.
setSelectedIndex(numDocuments-1);
if (getCurrentTextArea().isDirty())
owner.setMessages(fileFullPath + "*", "Opened document '" + fileFullPath + "'");
else
owner.setMessages(fileFullPath, "Opened document '" + fileFullPath + "'");
// RText's listeners will be updated by stateChanged() for all
// addTextAreaImpl() calls.
}
/**
* Cascades the windows in this MDI view.
*/
public void cascadeWindows() {
int x = 0;
int y = 0;
int numRows = 0;
Dimension desktopSize = desktopPane.getSize();
int size = frames.size();
int selectedIndex = getSelectedIndex();
for (int i=0; i<size; i++) {
if (i==selectedIndex)
continue;
JInternalFrame frame = frames.get(i);
frame.setBounds(x,y, 300,300);
frame.toFront();
x += CASCADE_X_INCREMENT;
y += CASCADE_Y_INCREMENT;
if (y + 300 >= desktopSize.height) {
x = (CASCADE_X_INCREMENT*2) * ++numRows + CASCADE_X_INCREMENT/2;
y = 0;
}
}
// Make it so current text area is "last" to be cascaded by swapping
// its place with the last frame's place.
JInternalFrame currentFrame = frames.get(selectedIndex);
currentFrame.setBounds(x,y, 300,300);
currentFrame.toFront();
}
@Override
protected synchronized boolean closeCurrentDocumentImpl() {
ResourceBundle msg = owner.getResourceBundle();
// Return code for if the user is prompted to save; returns yes for
// closeAllDocuments().
int rc = promptToSaveBeforeClosingIfDirty();
if (rc==JOptionPane.CANCEL_OPTION) {
return false;
}
// Remove the document from this tabbed pane.
removeComponentAt(getSelectedIndex());
// If there are open documents, make sure any duplicates are numbered
// correctly. If there are no open documents, add a new empty one.
if (getNumDocuments()>0) {
renumberDisplayNames();
JInternalFrame frame = frames.getFirst();
desktopPane.setSelectedFrame(frame);
try {
frame.setSelected(true); // Updates currentTextArea.
} catch (PropertyVetoException e) {
e.printStackTrace(); // Never happens.
}
frame.toFront();
}
else
addNewEmptyUntitledFile();
// Update the RText's status bar.
updateStatusBar();
// Update RText's title and the status bar message.
RTextEditorPane editor = getCurrentTextArea();
if (editor.isDirty())
owner.setMessages(editor.getFileFullPath() + "*", msg.getString("Ready"));
else
owner.setMessages(editor.getFileFullPath(), msg.getString("Ready"));
return true;
}
/**
* Creates the popup menu for the desktop pane.
*/
protected void createPopupMenu() {
popupMenu = new JPopupMenu();
JMenuItem menuItem = new JMenuItem(new AbstractAction() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
tileWindowsVertically();
}
});
menuItem.setText("Tile Vertically");
popupMenu.add(menuItem);
menuItem = new JMenuItem(new AbstractAction() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
tileWindowsHorizontally();
}
});
menuItem.setText("Tile Horizontally");
popupMenu.add(menuItem);
menuItem = new JMenuItem(new AbstractAction() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
cascadeWindows();
}
});
menuItem.setText("Cascade");
popupMenu.add(menuItem);
}
/**
* Returns the name being displayed for the document. For example, in a tabbed
* pane, this could be the text on the tab for this document.
*
* @param index The index at which to find the name. If the index is invalid,
* <code>null</code> is returned.
* @return The name being displayed for this document.
*/
@Override
public String getDocumentDisplayNameAt(int index) {
if (index>=0 && index<getNumDocuments()) {
return (frames.get(index)).getTitle();
}
return null;
}
/**
* Returns the location of the document selection area of this component.
* Note that this value currently has no effect on an an instance of
* <code>RTextMDIView</code>.
*
* @return The location of the document selection area.
*/
@Override
public int getDocumentSelectionPlacement() {
return documentSelectionPlacement;
}
/**
* Returns the number of documents open in this container.
*
* @return The number of open documents.
*/
@Override
public int getNumDocuments() {
return frames.size();
}
/**
* Returns the preferred size of this MDI view.
*
* @return The preferred size of this view.
*/
@Override
public Dimension getPreferredSize() {
Dimension preferredSize = new Dimension(300,300); // Default value.
for (InternalFrame frame : frames) {
Dimension framePreferredSize = frame.getPreferredSize();
preferredSize.width = Math.max(framePreferredSize.width, preferredSize.width);
preferredSize.height = Math.max(framePreferredSize.height, preferredSize.height);
}
return preferredSize;
}
@Override
public RTextScrollPane getRTextScrollPaneAt(int index) {
if (index<0 || index>=getNumDocuments())
//throw new IndexOutOfBoundsException();
return null;
JPanel temp = (JPanel)(frames.get(index)).
getContentPane().getComponent(0);
return (RTextScrollPane)temp.getComponent(0);
}
/**
* Returns the currently active component.
*
* @return The component.
*/
@Override
public Component getSelectedComponent() {
return desktopPane.getSelectedFrame();
}
/**
* Returns the currently selected document's index.
*
* @return The index of the currently selected document.
*/
@Override
public int getSelectedIndex() {
return frames.indexOf(desktopPane.getSelectedFrame());
}
/**
* Called when an internal frame is activated.
*/
@Override
public void internalFrameActivated(InternalFrameEvent e) {
RTextEditorPane current = getRTextEditorPaneAt(getSelectedIndex());
setCurrentTextArea(current);
// Update RText's title bar and status bar.
String title = current.getFileFullPath()+(current.isDirty() ? "*" : "");
owner.setMessages(title, null);
updateStatusBar(); // Updates read-only indicator and line/column.
// currentTextArea.requestFocusInWindow();
current.addCaretListener(owner);
// Trick the parent RText into updating the row/column indicator.
// Null because caretUpdate doesn't actually use the caret event.
owner.caretUpdate(null);
// Let any listeners know that the current document changed.
firePropertyChange(CURRENT_DOCUMENT_PROPERTY, -1, getSelectedIndex());
fireCurrentTextAreaEvent(CurrentTextAreaEvent.TEXT_AREA_CHANGED,
null, current);
}
/**
* Called when an internal frame is closed.
*/
@Override
public void internalFrameClosed(InternalFrameEvent e) {
}
/**
* Called when an internal frame is closing.
*/
@Override
public void internalFrameClosing(InternalFrameEvent e) {
// We must ensure that the frame closing is the selected frame, because
// the user can click the "x" button of an internal frame that isn't the
// currently active frame.
setSelectedIndex(frames.indexOf(e.getInternalFrame()));
owner.getAction(RText.CLOSE_ACTION).actionPerformed(null);
}
/**
* Called when an internal frame is deactivated.
*/
@Override
public void internalFrameDeactivated(InternalFrameEvent e) {
getCurrentTextArea().removeCaretListener(owner);
}
/**
* Called when an internal frame is de-iconified.
*/
@Override
public void internalFrameDeiconified(InternalFrameEvent e) {
}
/**
* Called when an internal frame is iconified.
*/
@Override
public void internalFrameIconified(InternalFrameEvent e) {
}
/**
* Called when an internal frame is opened.
*/
@Override
public void internalFrameOpened(InternalFrameEvent e) {
}
/**
* Repaints the display names for open documents.
*/
@Override
public void refreshDisplayNames() {
// Can't change MDI window color in JDesktopPane?
}
@Override
public void refreshTabIcons() {
for (InternalFrame frame : frames) {
frame.refreshIcon();
}
}
/**
* Removes a component from this container. Note that this method does not
* update currentTextArea, you must do that yourself.
*/
@Override
protected void removeComponentAt(int index) {
if (index>=0 && index<getNumDocuments()) {
frames.get(index).dispose();
frames.remove(index);
//tabbedPane.removeTabAt(index);
}
}
/**
* Sets the name of the document displayed on the document's tab.
*
* @param index The index of the document whose name you want to change.
* If this value is invalid, this method does nothing.
* @param displayName The name to display.
* @see #getDocumentDisplayNameAt
*/
@Override
public void setDocumentDisplayNameAt(int index, String displayName) {
if (index>=0 && index<getNumDocuments()) {
frames.get(index).setTitle(displayName);
}
}
/**
* Changes the location of the document selection area of this component.
*
* @param location The location to use; (<code>TOP</code>,
* <code>LEFT</code>, <code>BOTTOM</code>, or <code>RIGHT</code>.
* If this value is invalid, nothing happens.
*/
@Override
public void setDocumentSelectionPlacement(int location) {
if (location==DOCUMENT_SELECT_TOP || location==DOCUMENT_SELECT_LEFT ||
location==DOCUMENT_SELECT_BOTTOM || location==DOCUMENT_SELECT_RIGHT)
documentSelectionPlacement = location;
}
/**
* Sets the currently active document. This updates currentTextArea.
*
* @param index The index of the document to make the active document. If this
* value is invalid, nothing happens.
*/
@Override
public void setSelectedIndex(int index) {
if (index>=0 && index<getNumDocuments()) {
JInternalFrame frame = frames.get(index);
try {
frame.setSelected(true); // Updates currentTextArea via internalFrameActivated.
} catch (PropertyVetoException ignored) { }
//frame.toFront();
desktopPane.setSelectedFrame(frame);
}
}
/**
* Tiles all open internal frames horizontally.
*/
public void tileWindowsHorizontally() {
int numToResize = 0;
for (JInternalFrame frame : frames) {
if (frame.isVisible() && !frame.isIcon()) {
// Make sure any frames that can't be resized don't
// cover all our stuff up.
if (!frame.isResizable()) {
try {
frame.setMaximum(false);
} catch (PropertyVetoException ignored) {
}
}
else
numToResize++;
} // End of if (frame.isVisible() && !frame.isIcon()).
} // End of for (int i=0; i<numFrames; i++).
if (numToResize>0) {
Rectangle desktopBounds = desktopPane.getBounds();
int desktopWidth = desktopBounds.width;
int desktopHeight = desktopBounds.height;
int fHeight = desktopHeight / numToResize;
int yPos = 0;
for (JInternalFrame frame : frames) {
if (frame.isVisible() && frame.isResizable() && !frame.isIcon()) {
frame.setSize(desktopWidth, fHeight);
frame.setLocation(0, yPos);
yPos += fHeight;
}
} // End of for (int i=0; i<numFrames; i++).
} // End of if (numToResize>0).
}
/**
* Tiles all open internal frames horizontally.
*/
public void tileWindowsVertically() {
int numToResize = 0;
for (JInternalFrame frame : frames) {
if (frame.isVisible() && !frame.isIcon()) {
// Make sure any frames that can't be resized don't
// cover all our stuff up.
if (!frame.isResizable()) {
try {
frame.setMaximum(false);
} catch (PropertyVetoException ignored) {
}
}
else
numToResize++;
} // End of if (frame.isVisible() && !frame.isIcon()).
} // End of for (int i=0; i<numFrames; i++).
if (numToResize>0) {
Rectangle desktopBounds = desktopPane.getBounds();
int desktopWidth = desktopBounds.width;
int desktopHeight = desktopBounds.height;
int fWidth = desktopWidth / numToResize;
int xPos = 0;
for (JInternalFrame frame : frames) {
if (frame.isVisible() && frame.isResizable() && !frame.isIcon()) {
frame.setBounds(xPos, 0, fWidth, desktopHeight);
xPos += fWidth;
}
} // End of for (int i=0; i<numFrames; i++).
} // End of if (numToResize>0).
}
/**
* Overridden so we can update the right-click popup menu.
*/
@Override
public void updateUI() {
super.updateUI();
if (popupMenu!=null)
SwingUtilities.updateComponentTreeUI(popupMenu);
}
/**
* The internal frames used.
*/
class InternalFrame extends JInternalFrame {
private static final int X_OFFSET = 30;
private static final int Y_OFFSET = 30;
private RTextScrollPane scrollPane;
InternalFrame(String title, Component component) {
super(title, true, true, true, true);
scrollPane = (RTextScrollPane)((JPanel)component).getComponent(0);
this.setFrameIcon(getIconFor(scrollPane));
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(1,1));
contentPane.add(component);
++openFrameCount;
if (openFrameCount>8)
openFrameCount = 0;
setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);
pack();
setLocation(X_OFFSET * openFrameCount, Y_OFFSET * openFrameCount);
}
void refreshIcon() {
setFrameIcon(getIconFor(scrollPane));
}
}
/**
* Listens for mouse events in the desktop pane.
*/
class MDIMouseListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
@Override
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
public void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
if (popupMenu==null)
createPopupMenu();
popupMenu.show(desktopPane, e.getX(), e.getY());
}
}
}
}