changeset 104:144cb44cd75e

TabEdit: Introduced dirty/unsaved marker '*' Also fixed card deletion in a case where it is open in multiple windows. Now it marks the card as new in all other windows.
author Fox
date Thu, 06 Apr 2023 09:59:12 +0200
parents 7a3fd865654a
children 33f090b497c8
files src/junotu/Main.java src/junotu/TabEdit.java
diffstat 2 files changed, 93 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/src/junotu/Main.java	Thu Apr 06 01:44:10 2023 +0200
+++ b/src/junotu/Main.java	Thu Apr 06 09:59:12 2023 +0200
@@ -113,6 +113,41 @@
 		System.out.print( "Opening edit tab to edit '"+card.titleGet()+"'.\n" );
 	}
 
+	public static void actionCardSaved( Window window, Card card )
+	{
+		for( int i = 0; i < windows.length; i++ ) {
+			if( windows[i] == window || windows[i] == null ) {
+				continue;
+			}
+			if(
+				windows[i].tabCurrent() == Tab.EDIT
+				&& windows[i].tabEdit.newCard == false
+				&& windows[i].tabEdit.card.identifierGet() == card.identifierGet()
+			) {
+				windows[i].tabEdit.markDirty();
+			}
+		}
+		refreshSearches();
+	}
+
+	public static void actionCardDeleted( Window window, Card card )
+	{
+		for( int i = 0; i < windows.length; i++ ) {
+			if( windows[i] == window || windows[i] == null ) {
+				continue;
+			}
+			if(
+				windows[i].tabCurrent() == Tab.EDIT
+				&& windows[i].tabEdit.newCard == false
+				&& windows[i].tabEdit.card.identifierGet() == card.identifierGet()
+			) {
+				windows[i].tabEdit.newCard = true;
+				windows[i].tabEdit.markDirty();
+			}
+		}
+		refreshSearches();
+	}
+
 	public static void actionOpenURI( String uri )
 	{
 		if( desktop == null ) {
--- a/src/junotu/TabEdit.java	Thu Apr 06 01:44:10 2023 +0200
+++ b/src/junotu/TabEdit.java	Thu Apr 06 09:59:12 2023 +0200
@@ -67,8 +67,9 @@
 	private final String KEY_ACTION_BACK = "back";
 	private final String KEY_ACTION_SAVE = "save";
 
-	private Card card = null;
-	private boolean newCard = true;
+	public Card card = null;
+	public boolean newCard = true;
+	public boolean dirty = true;
 
 	private TagWidget editedTag = null;
 	private JTextField editedTagField;
@@ -171,26 +172,28 @@
 			}
 		);
 
-		title.getDocument().addDocumentListener(
-			new DocumentListener()
+		DocumentListener documentListener = new DocumentListener()
+		{
+			@Override
+			public void changedUpdate( DocumentEvent e )
+			{
+				markDirty();
+			}
+			@Override
+			public void removeUpdate( DocumentEvent e )
 			{
-				@Override
-				public void changedUpdate( DocumentEvent e )
-				{
-					updateTitle();
-				}
-				@Override
-				public void removeUpdate( DocumentEvent e )
-				{
-					updateTitle();
-				}
-				@Override
-				public void insertUpdate( DocumentEvent e )
-				{
-					updateTitle();
-				}
+				markDirty();
 			}
-		);
+			@Override
+			public void insertUpdate( DocumentEvent e )
+			{
+				markDirty();
+			}
+		}
+		;
+
+		title.getDocument().addDocumentListener(documentListener);
+		content.getDocument().addDocumentListener(documentListener);
 
 		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 );
@@ -225,6 +228,7 @@
 	{
 		newCard = true;
 		card = new Card();
+		dirty = true;
 		updateTitle();
 		delete.setVisible(false);
 		updateTags();
@@ -236,6 +240,7 @@
 		this.card = card;
 		title.setText( card.titleGet() );
 		content.setText( card.contentGet() );
+		dirty = false;
 		updateTitle();
 		delete.setVisible(true);
 		updateTags();
@@ -251,6 +256,12 @@
 		);
 	}
 
+	public void markDirty()
+	{
+		dirty = true;
+		updateTitle();
+	}
+
 	private void reset()
 	{
 		title.setText("");
@@ -263,7 +274,15 @@
 		Window window = (Window)this.getTopLevelAncestor();
 
 		String text = title.getText();
-		String action = newCard ? "Create" : "Edit";
+		String action;
+		
+		if( newCard ) {
+			action = "Create";
+		} else if( dirty ) {
+			action = "Edit*";
+		} else {
+			action = "Edit";
+		}
 
 		if( text.length() > 0 ) {
 			window.setTitle( window.preferredTitle( action+": "+text ) );
@@ -309,6 +328,7 @@
 		System.out.print( "Removed tag '"+tagWidget.tag+"' with value '"+tagWidget.value+"'.\n" );
 		card.tagValueRemove( tagWidget.tag, tagWidget.value );
 		tags.remove( tagWidget );
+		markDirty();
 
 		tags.validate();
 		tags.repaint();
@@ -356,9 +376,15 @@
 			logTagChange( oldTag, oldValue, newTag, newValue );
 
 			if( oldTag.equals(newTag) ) {
-				card.tagValueReplace( oldTag, oldValue, newValue );
-				editedTag.value = newValue;
-				editedTag.update();
+				if(
+					( oldValue != null && !oldValue.equals(newValue) )
+					|| ( oldValue == null && oldValue != newValue )
+				) {
+					card.tagValueReplace( oldTag, oldValue, newValue );
+					editedTag.value = newValue;
+					editedTag.update();
+					markDirty();
+				}
 			} else { /* Replace tag with another one. */
 
 				card.tagValueRemove( oldTag, oldValue );
@@ -371,6 +397,7 @@
 				} else {
 					tags.remove( editedTag );
 				}
+				markDirty();
 
 			}
 
@@ -388,6 +415,7 @@
 				} else {
 					logTagChange( "", null, "", null );
 				}
+				markDirty();
 			} else {
 				logTagChange( "", null, "", null );
 			}
@@ -487,7 +515,7 @@
 		Main.database.cardDeleteByIdentifier( card.identifierGet() );
 
 		Window window = (Window)this.getTopLevelAncestor();
-		Main.refreshSearches();
+		Main.actionCardDeleted( window, card );
 		reset();
 		window.tabSwitch( Tab.SEARCH );
 
@@ -505,6 +533,7 @@
 
 	private void buttonClickedSave( boolean noSwitch )
 	{
+		Window window = (Window)this.getTopLevelAncestor();
 
 		card.titleSet( title.getText() );
 		card.contentSet( content.getText() );
@@ -515,14 +544,16 @@
 			Main.database.cardUpdate( card, true );
 		}
 
-		Main.refreshSearches();
+		dirty = false;
+		Main.actionCardSaved( window, card );
 
 		if( noSwitch ) {
 			if( newCard ) {
 				cardEdit( this.card );
+			} else {
+				updateTitle(); /* Dirty state might have changed. */
 			}
 		} else {
-			Window window = (Window)this.getTopLevelAncestor();
 			reset();
 			window.tabSwitch( Tab.SEARCH );
 		}