3333import jssc .SerialPortException ;
3434import processing .app .debug .RunnerException ;
3535import processing .app .forms .PasswordAuthorizationDialog ;
36+ import processing .app .helpers .DocumentTextChangeListener ;
3637import processing .app .helpers .Keys ;
3738import processing .app .helpers .OSUtils ;
3839import processing .app .helpers .PreferencesMapException ;
3940import processing .app .legacy .PApplet ;
4041import processing .app .syntax .PdeKeywords ;
42+ import processing .app .syntax .SketchTextArea ;
4143import processing .app .tools .MenuScroller ;
4244import processing .app .tools .Tool ;
4345
4446import javax .swing .*;
4547import javax .swing .event .*;
4648import javax .swing .text .BadLocationException ;
47- import javax .swing .undo .CannotRedoException ;
48- import javax .swing .undo .CannotUndoException ;
49- import javax .swing .undo .UndoManager ;
5049import java .awt .*;
5150import java .awt .datatransfer .DataFlavor ;
5251import java .awt .datatransfer .Transferable ;
@@ -185,8 +184,6 @@ public boolean test(SketchController sketch) {
185184 // undo fellers
186185 private JMenuItem undoItem ;
187186 private JMenuItem redoItem ;
188- protected UndoAction undoAction ;
189- protected RedoAction redoAction ;
190187
191188 private FindReplace find ;
192189
@@ -1273,7 +1270,7 @@ private JMenu buildEditMenu() {
12731270
12741271 undoItem = newJMenuItem (tr ("Undo" ), 'Z' );
12751272 undoItem .setName ("menuEditUndo" );
1276- undoItem .addActionListener (undoAction = new UndoAction ());
1273+ undoItem .addActionListener (e -> getCurrentTab (). handleUndo ());
12771274 menu .add (undoItem );
12781275
12791276 if (!OSUtils .isMacOS ()) {
@@ -1282,7 +1279,7 @@ private JMenu buildEditMenu() {
12821279 redoItem = newJMenuItemShift (tr ("Redo" ), 'Z' );
12831280 }
12841281 redoItem .setName ("menuEditRedo" );
1285- redoItem .addActionListener (redoAction = new RedoAction ());
1282+ redoItem .addActionListener (e -> getCurrentTab (). handleRedo ());
12861283 menu .add (redoItem );
12871284
12881285 menu .addSeparator ();
@@ -1478,68 +1475,10 @@ private static JMenuItem newJMenuItemAlt(String title, int what) {
14781475 // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
14791476
14801477
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 ());
15431482 }
15441483
15451484
@@ -1610,8 +1549,7 @@ public List<EditorTab> getTabs() {
16101549 */
16111550 public void selectTab (final int index ) {
16121551 currentTabIndex = index ;
1613- undoAction .updateUndoState ();
1614- redoAction .updateRedoState ();
1552+ updateUndoRedoState ();
16151553 updateTitle ();
16161554 header .rebuild ();
16171555 getCurrentTab ().activated ();
@@ -1710,6 +1648,9 @@ public void reorderTabs() {
17101648 */
17111649 protected void addTab (SketchFile file , String contents ) throws IOException {
17121650 EditorTab tab = new EditorTab (this , file , contents );
1651+ tab .getTextArea ().getDocument ()
1652+ .addDocumentListener (new DocumentTextChangeListener (
1653+ () -> updateUndoRedoState ()));
17131654 tabs .add (tab );
17141655 reorderTabs ();
17151656 }
0 commit comments