33
33
import jssc .SerialPortException ;
34
34
import processing .app .debug .RunnerException ;
35
35
import processing .app .forms .PasswordAuthorizationDialog ;
36
+ import processing .app .helpers .DocumentTextChangeListener ;
36
37
import processing .app .helpers .Keys ;
37
38
import processing .app .helpers .OSUtils ;
38
39
import processing .app .helpers .PreferencesMapException ;
39
40
import processing .app .legacy .PApplet ;
40
41
import processing .app .syntax .PdeKeywords ;
42
+ import processing .app .syntax .SketchTextArea ;
41
43
import processing .app .tools .MenuScroller ;
42
44
import processing .app .tools .Tool ;
43
45
44
46
import javax .swing .*;
45
47
import javax .swing .event .*;
46
48
import javax .swing .text .BadLocationException ;
47
- import javax .swing .undo .CannotRedoException ;
48
- import javax .swing .undo .CannotUndoException ;
49
- import javax .swing .undo .UndoManager ;
50
49
import java .awt .*;
51
50
import java .awt .datatransfer .DataFlavor ;
52
51
import java .awt .datatransfer .Transferable ;
@@ -185,8 +184,6 @@ public boolean test(SketchController sketch) {
185
184
// undo fellers
186
185
private JMenuItem undoItem ;
187
186
private JMenuItem redoItem ;
188
- protected UndoAction undoAction ;
189
- protected RedoAction redoAction ;
190
187
191
188
private FindReplace find ;
192
189
@@ -1273,7 +1270,7 @@ private JMenu buildEditMenu() {
1273
1270
1274
1271
undoItem = newJMenuItem (tr ("Undo" ), 'Z' );
1275
1272
undoItem .setName ("menuEditUndo" );
1276
- undoItem .addActionListener (undoAction = new UndoAction ());
1273
+ undoItem .addActionListener (e -> getCurrentTab (). handleUndo ());
1277
1274
menu .add (undoItem );
1278
1275
1279
1276
if (!OSUtils .isMacOS ()) {
@@ -1282,7 +1279,7 @@ private JMenu buildEditMenu() {
1282
1279
redoItem = newJMenuItemShift (tr ("Redo" ), 'Z' );
1283
1280
}
1284
1281
redoItem .setName ("menuEditRedo" );
1285
- redoItem .addActionListener (redoAction = new RedoAction ());
1282
+ redoItem .addActionListener (e -> getCurrentTab (). handleRedo ());
1286
1283
menu .add (redoItem );
1287
1284
1288
1285
menu .addSeparator ();
@@ -1478,68 +1475,10 @@ private static JMenuItem newJMenuItemAlt(String title, int what) {
1478
1475
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1479
1476
1480
1477
1481
- class UndoAction extends AbstractAction {
1482
- public UndoAction () {
1483
- super ("Undo" );
1484
- this .setEnabled (false );
1485
- }
1486
-
1487
- public void actionPerformed (ActionEvent e ) {
1488
- try {
1489
- getCurrentTab ().handleUndo ();
1490
- } catch (CannotUndoException ex ) {
1491
- //System.out.println("Unable to undo: " + ex);
1492
- //ex.printStackTrace();
1493
- }
1494
- }
1495
-
1496
- protected void updateUndoState () {
1497
- UndoManager undo = getCurrentTab ().getUndoManager ();
1498
-
1499
- if (undo .canUndo ()) {
1500
- this .setEnabled (true );
1501
- undoItem .setEnabled (true );
1502
- undoItem .setText (undo .getUndoPresentationName ());
1503
- putValue (Action .NAME , undo .getUndoPresentationName ());
1504
- } else {
1505
- this .setEnabled (false );
1506
- undoItem .setEnabled (false );
1507
- undoItem .setText (tr ("Undo" ));
1508
- putValue (Action .NAME , "Undo" );
1509
- }
1510
- }
1511
- }
1512
-
1513
-
1514
- class RedoAction extends AbstractAction {
1515
- public RedoAction () {
1516
- super ("Redo" );
1517
- this .setEnabled (false );
1518
- }
1519
-
1520
- public void actionPerformed (ActionEvent e ) {
1521
- try {
1522
- getCurrentTab ().handleRedo ();
1523
- } catch (CannotRedoException ex ) {
1524
- //System.out.println("Unable to redo: " + ex);
1525
- //ex.printStackTrace();
1526
- }
1527
- }
1528
-
1529
- protected void updateRedoState () {
1530
- UndoManager undo = getCurrentTab ().getUndoManager ();
1531
-
1532
- if (undo .canRedo ()) {
1533
- redoItem .setEnabled (true );
1534
- redoItem .setText (undo .getRedoPresentationName ());
1535
- putValue (Action .NAME , undo .getRedoPresentationName ());
1536
- } else {
1537
- this .setEnabled (false );
1538
- redoItem .setEnabled (false );
1539
- redoItem .setText (tr ("Redo" ));
1540
- putValue (Action .NAME , "Redo" );
1541
- }
1542
- }
1478
+ protected void updateUndoRedoState () {
1479
+ SketchTextArea textArea = getCurrentTab ().getTextArea ();
1480
+ undoItem .setEnabled (textArea .canUndo ());
1481
+ redoItem .setEnabled (textArea .canRedo ());
1543
1482
}
1544
1483
1545
1484
@@ -1610,8 +1549,7 @@ public List<EditorTab> getTabs() {
1610
1549
*/
1611
1550
public void selectTab (final int index ) {
1612
1551
currentTabIndex = index ;
1613
- undoAction .updateUndoState ();
1614
- redoAction .updateRedoState ();
1552
+ updateUndoRedoState ();
1615
1553
updateTitle ();
1616
1554
header .rebuild ();
1617
1555
getCurrentTab ().activated ();
@@ -1710,6 +1648,9 @@ public void reorderTabs() {
1710
1648
*/
1711
1649
protected void addTab (SketchFile file , String contents ) throws IOException {
1712
1650
EditorTab tab = new EditorTab (this , file , contents );
1651
+ tab .getTextArea ().getDocument ()
1652
+ .addDocumentListener (new DocumentTextChangeListener (
1653
+ () -> updateUndoRedoState ()));
1713
1654
tabs .add (tab );
1714
1655
reorderTabs ();
1715
1656
}
0 commit comments