changeset 60:cda50e8f5135

TabBoard: Rough saving and loading
author Fox
date Fri, 23 Dec 2022 17:10:09 +0100
parents a2696310fa8c
children 07a05c60abef
files src/junotu/Card.java src/junotu/TabBoard.java src/junotu/TabEdit.java
diffstat 3 files changed, 239 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/src/junotu/Card.java	Fri Dec 23 15:02:21 2022 +0100
+++ b/src/junotu/Card.java	Fri Dec 23 17:10:09 2022 +0100
@@ -11,11 +11,19 @@
 public class Card {
 
     public static final String TAG_CORE_PREFIX = "_junotu_";
-    public static final String TAG_IDENTIFIER = TAG_CORE_PREFIX+"identifier";
-    public static final String TAG_TITLE      = TAG_CORE_PREFIX+"title";
-    public static final String TAG_CONTENT    = TAG_CORE_PREFIX+"content";
-    public static final String TAG_SEARCH     = TAG_CORE_PREFIX+"search";
-    public static final String TAG_LAST_EDIT  = TAG_CORE_PREFIX+"timestamp_last_edit";
+    public static final String TAG_IDENTIFIER  = TAG_CORE_PREFIX+"identifier";
+    public static final String TAG_TITLE       = TAG_CORE_PREFIX+"title";
+    public static final String TAG_CONTENT     = TAG_CORE_PREFIX+"content";
+    public static final String TAG_SEARCH      = TAG_CORE_PREFIX+"search";
+    public static final String TAG_LAST_EDIT   = TAG_CORE_PREFIX+"timestamp_last_edit";
+    
+    public static final String TAG_BOARD              = TAG_CORE_PREFIX+"board";
+    public static final String TAG_BOARD_COLUMNS      = TAG_CORE_PREFIX+"board_columns";
+    public static final String TAG_BOARD_COLUMN_CARDS = TAG_CORE_PREFIX+"board_column_cards";
+
+    public static final String HIDE_TAGS[] = {
+	TAG_BOARD_COLUMN_CARDS,
+    };
 
     public SortedMap< String, Set<Object> > tags = new TreeMap< String, Set<Object> >();
 
@@ -149,5 +157,10 @@
 	T value = tagGetAs( tag );
 	return value != null ? value : or;
     }
+
+    public boolean isBoard()
+    {
+	return tags.containsKey(TAG_BOARD);
+    }
     
 }
--- a/src/junotu/TabBoard.java	Fri Dec 23 15:02:21 2022 +0100
+++ b/src/junotu/TabBoard.java	Fri Dec 23 17:10:09 2022 +0100
@@ -62,12 +62,16 @@
     public final String KEY_ACTION_CARD_FULL_RIGHT = "card_full_right";
     
     private class ColumnWidget extends JPanel implements ActionListener, MouseListener {
+
+	long identifier;
+	boolean newCard;
+	
 	JTextField titleEdit;
 	TitledBorder titledBorder;
 	Box cards;
 	JButton addCard;
 
-	public ColumnWidget( TabBoard parent )
+	public ColumnWidget( TabBoard parent, Card card )
 	{
 	    this.setLayout( new GridBagLayout() );
 	    
@@ -185,6 +189,37 @@
 					);
 
 	    addCard.setToolTipText("Add card.");
+
+	    newCard = card == null;
+	    
+	    if( newCard ) {
+		return;
+	    }
+
+	    identifier = card.identifierGet();
+	    titleSet(card.titleGet());
+	    
+	    String cardsString = card.<String>tagGetAsOr( Card.TAG_BOARD_COLUMN_CARDS, "" );
+	    String cardsSplit[] = cardsString.split(" ");
+	    System.out.print("Loading cards string: "+cardsString+"\n");
+	    
+	    for( int i = 0; i < cardsSplit.length; i++ ) {
+		try {
+		    long identifier = Long.parseLong(cardsSplit[i]);
+		    Card childCard = Main.database.cardGetByIdentifier(identifier);
+		    
+		    if( childCard == null ){
+			System.out.print("Column '"+card.titleGet()+"', identifier"+Long.toString(card.identifierGet())+": Failed to load card by identifier"+Long.toString(identifier)+"\n");
+		    }
+		
+		    insertCard( childCard, -1 );
+		} catch( NumberFormatException e ) {
+		    System.out.print("Column '"+card.titleGet()+"', identifier"+Long.toString(card.identifierGet())+": Failed to convert card identifier '"+cardsSplit[i]+"' to long, aborting cards loading. Full cards tag: '"+cardsString+"'\n");
+		    break;
+		} catch( Exception e ) {
+		    throw new RuntimeException(e);
+		}
+	    }
 	    
 	}
 
@@ -278,6 +313,52 @@
 	    return cards.getComponentCount();
 	}
 
+	public void save()
+	{
+	    Component[] cardList = cards.getComponents();
+	    String cardIdentifiers = "";
+	    for( int i = 0; i < cardList.length; i++ ) {
+		ColumnCardWidget cardWidget = (ColumnCardWidget)cardList[i];
+		cardWidget.save();
+		
+		if( cardIdentifiers.length() > 0 ) {
+		    cardIdentifiers += " ";
+		}
+		cardIdentifiers += Long.toString(cardWidget.identifier);
+	    }
+
+	    Card card;
+
+	    if( newCard ) {
+	        card = new Card();
+	    } else {
+		try {
+		    card = Main.database.cardGetByIdentifier(identifier);
+		} catch( Exception e ) {
+		    throw new RuntimeException(e);
+		}
+
+		if( card == null ) {
+		    throw new RuntimeException("Board column update: card not found.");
+		}
+	    }
+
+	    card.titleSet( titleGet() );
+	    card.tagValueSetOnly( Card.TAG_BOARD_COLUMN_CARDS, cardIdentifiers );
+
+	    try {
+		if( newCard ) {
+		    identifier = Main.database.cardAdd( card );
+		    newCard = false;
+		} else {
+		    Main.database.cardUpdate(card);
+		}
+	    } catch( Exception e ) {
+		throw new RuntimeException(e);
+	    }
+	    
+	}
+
 	public void actionPerformed( ActionEvent e )
 	{
 	    Object source = e.getSource();
@@ -313,9 +394,7 @@
 		    
 		}
 	    } else if( source == addCard ) {
-		Card newCard = new Card();
-		newCard.titleSet("New card");
-		insertCard( newCard, -1 );
+		insertCard( null, -1 );
 	    }
 	}
 
@@ -343,15 +422,15 @@
 
     private class ColumnCardWidget extends JPanel {
 
+	public boolean newCard;
 	public long identifier;
 	public JTextArea title;
 	
 	public ColumnCardWidget( Card card )
 	{
 	    this.setLayout( new BorderLayout() );
-	    
-	    identifier = card.identifierGet();
-	    title = new JTextArea( card.titleGet() );
+
+	    title = new JTextArea("");
 	    
 	    title.setFont( new Font( "Monospaced", Font.BOLD, 16 ) );
 		
@@ -380,9 +459,41 @@
 	    };
 
 	    title.addMouseListener( mouseListener );
+
+	    newCard = card == null;
+	    if( !newCard ) {
+		identifier = card.identifierGet();
+		title.setText(card.titleGet());
+	    }
 	    
 	}
 
+	public void save()
+	{
+	    try {
+		if( newCard ) {
+		    Card card = new Card();
+		    card.titleSet( title.getText() );
+		    
+		    identifier = Main.database.cardAdd(card);
+		    newCard = false;
+		    
+		} else {
+		    Card card = Main.database.cardGetByIdentifier(identifier);
+		    
+		    if( card == null ) {
+			throw new RuntimeException("Null card on update try.");
+		    }
+		    
+		    card.titleSet( title.getText() );
+		    Main.database.cardUpdate(card);
+		    
+		}
+	    } catch( Exception e ) {
+		throw new RuntimeException(e);
+	    }
+	}
+
 	public boolean isSelected()
 	{
 	    return title.isFocusOwner();
@@ -395,7 +506,7 @@
 	
     }
 
-    long columnsCard;
+    long identifier;
     JTextField title;
     Box columns;
     JScrollPane scroll;
@@ -427,7 +538,9 @@
 	this.add( scroll, BorderLayout.CENTER );
 	this.add( bottom, BorderLayout.SOUTH );
 
+	back.addActionListener(this);
 	addColumn.addActionListener(this);
+	editAsCard.addActionListener(this);
 
 	back.setToolTipText("Go back to where the card was accessed from.");
 	addColumn.setToolTipText("Add new column to the board.");
@@ -435,34 +548,99 @@
 	
     }
 
-    public void boardNew( String title_ )
+    public void boardEdit( Card card )
     {
-	title.setText(title_);
+	identifier = card.identifierGet();
+	
 	columns.removeAll();
-	/*for( int i = 0; i < 5; i++ ) {
-	    insertColumn();
-	}*/
+	title.setText(card.titleGet());
+	if( !card.isBoard() ) {
+	    return;
+	}
+	
+	String columnsString = card.<String>tagGetAsOr(Card.TAG_BOARD_COLUMNS, "");
+	String[] columnsSplit = columnsString.split(" ");
+	
+	for( int i = 0; i < columnsSplit.length; i++ ) {
+	    try {
+		long identifier = Long.parseLong(columnsSplit[i]);
+		Card columnCard = Main.database.cardGetByIdentifier(identifier);
+		
+		if( columnCard == null ){
+		    System.out.print("Board '"+card.titleGet()+"': Failed to load column by identifier"+Long.toString(identifier)+"\n");
+		}
+		
+		ColumnWidget column = new ColumnWidget(this, columnCard);
+		insertColumnRaw(column);
+		
+	    } catch( NumberFormatException e ) {
+		System.out.print("Board '"+card.titleGet()+"': Failed to convert column identifier '"+columnsSplit[i]+"' to long, aborting column loading. Full columns tag: '"+columnsString+"'\n");
+		break;
+	    } catch( Exception e ) {
+		throw new RuntimeException(e);
+	    }
+	}
     }
 
-    public void boardEdit( Card columnsCard )
+    public Card boardSave()
     {
+	Component[] columnsList = columns.getComponents();
+	String columnIdentifiers = "";
+	for( int i = 0; i < columnsList.length; i++ ) {
+	    ColumnWidget column = (ColumnWidget)columnsList[i];
+	    column.save();
+	    
+	    if( columnIdentifiers.length() > 0 ) {
+		columnIdentifiers += " ";
+	    }
+	    columnIdentifiers += Long.toString(column.identifier);
+	    
+	}
+	
+	Card card;
+
+	try {
+	    card = Main.database.cardGetByIdentifier(identifier);
+
+	    if( card == null ) {
+		throw new RuntimeException();
+	    }
+
+	    card.titleSet( title.getText() );
+	    card.tagValueSetOnly( Card.TAG_BOARD, null );
+	    card.tagValueSetOnly( Card.TAG_BOARD_COLUMNS, columnIdentifiers );
+
+	    Main.database.cardUpdate(card);
+	} catch( Exception e ) {
+	    throw new RuntimeException(e);
+	}
+	
+	return card;
 	
     }
 
-    public void boardSave()
+    public void boardReset()
     {
-	
+	title.setText("");
+	columns.removeAll();
     }
 
     public void insertColumn()
     {
-	ColumnWidget column = new ColumnWidget(this);
+	ColumnWidget column = new ColumnWidget(this, null);
 	column.titleSet("New column");
 	column.addMouseListener(this);
 	columns.add(column);
 	columns.revalidate();
     }
 
+    public void insertColumnRaw( ColumnWidget column )
+    {
+	column.addMouseListener(this);
+	columns.add(column);
+	columns.revalidate();
+    }
+
     public int findColumn( ColumnWidget columnWidget )
     {
 	Component[] columnsList = columns.getComponents();
@@ -486,12 +664,36 @@
 	cardWidget.select();
     }
 
+    public void buttonClickedAsCard()
+    {
+	Card card = boardSave();
+	Window window = (Window)this.getTopLevelAncestor();
+	window.tabEdit.cardEdit(card);
+	window.tabSwitch( Tab.EDIT );
+    }
+
+    public void buttonClickedBack()
+    {
+	boardSave();
+	boardReset();
+	Window window = (Window)this.getTopLevelAncestor();
+	window.tabSearch.search();
+	window.tabSwitch( Tab.SEARCH );
+    }
+
     public void actionPerformed( ActionEvent e )
     {
-	if( e.getSource() == addColumn ) {
+	if( e.getSource() == back ) {
+	    buttonClickedBack();
+	    return;
+	} else if( e.getSource() == addColumn ) {
 	    insertColumn();
 	    return;
+	} else if( e.getSource() == editAsCard ) {
+	    buttonClickedAsCard();
+	    return;
 	}
+
 	ColumnWidget sourceColumn = (ColumnWidget)e.getSource();
 	if( sourceColumn != null ) {
 	    int columnIndex = findColumn(sourceColumn);
--- a/src/junotu/TabEdit.java	Fri Dec 23 15:02:21 2022 +0100
+++ b/src/junotu/TabEdit.java	Fri Dec 23 17:10:09 2022 +0100
@@ -461,7 +461,7 @@
 	Window window = (Window)this.getTopLevelAncestor();
 	
 	buttonClickedSave(true);
-	window.tabBoard.boardNew(title.getText());
+	window.tabBoard.boardEdit(card);
 	reset();
 	window.tabSwitch( Tab.BOARD );
     }