changeset 45:1cad42247892

TabEdit: Simplify shortcut logic
author Fox
date Sun, 30 Oct 2022 02:41:28 +0200
parents 5aa3c7e36ff1
children 487e560524c2
files src/junotu/TabEdit.java
diffstat 1 files changed, 26 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/src/junotu/TabEdit.java	Sun Oct 30 02:31:03 2022 +0200
+++ b/src/junotu/TabEdit.java	Sun Oct 30 02:41:28 2022 +0200
@@ -60,10 +60,14 @@
 
 	public void actionPerformed( ActionEvent e )
 	{
+	    /* Not exactly sure how this works, but it does. */
 	    tagEdit( this );
 	}
 	
     }
+
+    private final String KEY_ACTION_BACK = "back";
+    private final String KEY_ACTION_SAVE = "save";
     
     private Card card = null;
     private boolean newCard = true;
@@ -171,36 +175,8 @@
 						}
 						);
 
-	registerKeyboardAction(
-			       new ActionListener() {
-				   @Override
-				   public void actionPerformed( ActionEvent e )
-				   {
-				       buttonClickedBack();
-				   }
-			       },
-			       null,
-			       KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0 ),
-			       WHEN_IN_FOCUSED_WINDOW
-			       );
-	
-	registerKeyboardAction(
-			       new ActionListener() {
-				   @Override
-				   public void actionPerformed( ActionEvent e )
-				   {
-				       buttonClickedSave( true );
-				       try {
-					   Main.database.databaseCommit();
-				       } catch( Exception ex ) {
-					   System.out.print( "Failed to write database: "+ex.getMessage()+"\n" );
-				       }
-				   }
-			       },
-			       null,
-			       KeyStroke.getKeyStroke( KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK ),
-			       WHEN_IN_FOCUSED_WINDOW
-			       );
+	registerKeyboardAction( this, KEY_ACTION_BACK, KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0 ), WHEN_IN_FOCUSED_WINDOW );
+	registerKeyboardAction( this, KEY_ACTION_SAVE, KeyStroke.getKeyStroke( KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK ), WHEN_IN_FOCUSED_WINDOW );
 	
     }
 
@@ -499,7 +475,26 @@
     public void actionPerformed( ActionEvent e )
     {
 	Object source = e.getSource();
-	if( source == back ) {
+	if( source == this ) {
+	    switch( e.getActionCommand() ) {
+		
+	    case KEY_ACTION_BACK: {
+		buttonClickedBack();
+		break;
+	    }
+
+	    case KEY_ACTION_SAVE: {
+		buttonClickedSave( true );
+		try {
+		    Main.database.databaseCommit();
+		} catch( Exception ex ) {
+		    System.out.print( "Failed to write database: "+ex.getMessage()+"\n" );
+		}
+		break;
+	    }
+		
+	    }
+	} else if( source == back ) {
 	    buttonClickedBack();
         } else if( source == delete ) {
 	    buttonClickedDelete();