changeset 87:caae98448674

TabCalendarBoard: Working saving/loading of columns
author Fox
date Tue, 07 Feb 2023 22:42:19 +0100
parents 2a93d2a65b0f
children a22a6d60a669
files src/junotu/Card.java src/junotu/TabCalendarBoard.java
diffstat 2 files changed, 47 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/junotu/Card.java	Mon Feb 06 23:58:57 2023 +0100
+++ b/src/junotu/Card.java	Tue Feb 07 22:42:19 2023 +0100
@@ -25,6 +25,7 @@
 
     public static final String TAG_CALENDAR_BOARD                    = TAG_CORE_PREFIX+"calendar_board";
     public static final String TAG_CALENDAR_BOARD_COLUMN             = TAG_CORE_PREFIX+"calendar_board_column";
+    public static final String TAG_CALENDAR_BOARD_COLUMN_DATE        = TAG_CORE_PREFIX+"calendar_board_column_date";
     public static final String TAG_CALENDAR_BOARD_COLUMN_CARDS       = TAG_CORE_PREFIX+"calendar_board_column_cards";
     public static final String TAG_CALENDAR_BOARD_OPTION_ONLY_FILLED = TAG_CORE_PREFIX+"calendar_board_hide_empty_days";
 
@@ -32,6 +33,8 @@
 
     public static final String HIDE_TAGS[] = {
 	TAG_BOARD_COLUMN,
+	TAG_CALENDAR_BOARD,
+	TAG_CALENDAR_BOARD_COLUMN,
     };
 
     public static final String HIDE_TAG_VALUES[] = {
--- a/src/junotu/TabCalendarBoard.java	Mon Feb 06 23:58:57 2023 +0100
+++ b/src/junotu/TabCalendarBoard.java	Tue Feb 07 22:42:19 2023 +0100
@@ -6,9 +6,11 @@
 import java.util.Date;
 import java.util.Calendar;
 import java.text.DateFormat;
+import java.text.SimpleDateFormat;
 
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.WildcardQuery;
+import org.apache.lucene.search.TermQuery;
 import org.apache.lucene.index.Term;
 
 import java.awt.Dimension;
@@ -63,6 +65,7 @@
 
     final static int COLUMN_CONTENT_WIDTH = 256;
     final static int COLUMN_WIDTH = COLUMN_CONTENT_WIDTH+16;
+    final static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
 
     public final String KEY_ACTION_CARD_UP         = "card_up";
     public final String KEY_ACTION_CARD_DOWN       = "card_down";
@@ -77,12 +80,13 @@
 
 	long identifier;
 	boolean newCard;
+	Date date;
 	
 	TitledBorder titledBorder;
 	Box cards;
 	JButton addCard;
 
-	public ColumnWidget( TabCalendarBoard parent, Card card )
+	public ColumnWidget( TabCalendarBoard parent, Card card, Date date_ )
 	{
 	    this.setLayout( new GridBagLayout() );
 	    
@@ -183,6 +187,7 @@
 	    addCard.setToolTipText("Add card.");
 
 	    newCard = card == null;
+	    date = date_;
 	    
 	    if( newCard ) {
 		return;
@@ -191,7 +196,7 @@
 	    identifier = card.identifierGet();
 	    titleSet(card.titleGet());
 	    
-	    String cardsString = card.<String>tagGetAsOr( Card.TAG_BOARD_COLUMN_CARDS, "" );
+	    String cardsString = card.<String>tagGetAsOr( Card.TAG_CALENDAR_BOARD_COLUMN_CARDS, "" );
 	    Card[] cardsSplit = TagUtility.parseCardList(cardsString);
 	    
 	    for( int i = 0; i < cardsSplit.length; i++ ) {
@@ -291,6 +296,13 @@
 		}
 		cardIdentifiers += Long.toString(cardWidget.identifier);
 	    }
+	    
+	    if( cardList.length == 0 ) {
+		if( !newCard ) {
+		    Main.database.cardDeleteByIdentifier(identifier);
+		}
+		return;
+	    }
 
 	    Card card;
 
@@ -310,6 +322,7 @@
 
 	    card.titleSet( titleGet() );
 	    card.tagValueSetOnly( Card.TAG_CALENDAR_BOARD_COLUMN, null );
+	    card.tagValueSetOnly( Card.TAG_CALENDAR_BOARD_COLUMN_DATE, DATE_FORMAT.format(date) );
 	    card.tagValueSetOnly( Card.TAG_CALENDAR_BOARD_COLUMN_CARDS, cardIdentifiers );
 
 	    try {
@@ -328,7 +341,9 @@
 	public void delete()
 	{
 	    save();
-	    Main.database.cardDeleteByIdentifier(identifier);
+	    if( !newCard ) {
+		Main.database.cardDeleteByIdentifier(identifier);
+	    }
 	}
 
 	public void actionPerformed( ActionEvent e )
@@ -532,6 +547,7 @@
     public void boardEdit()
     {
 	if( identifier != -1 ) {
+	    populateColumns();
 	    return;
 	}
 	
@@ -545,7 +561,8 @@
 	    identifier = Main.database.cardAdd(card);
 	    boardSave();
 	} else if( cards.length == 1 ) {
-	    card = cards[0];	    
+	    card = cards[0];
+	    identifier = card.identifierGet();
 	} else {
 	    throw new RuntimeException();
 	}
@@ -574,7 +591,11 @@
 	    card.tagRemove( Card.TAG_CALENDAR_BOARD_OPTION_ONLY_FILLED );
 	}
 
-	// TODO: Save columns here.
+	Component[] columnsList = columns.getComponents();
+	for( int i = 0; i < columnsList.length; i++ ) {
+	    ColumnWidget column = (ColumnWidget)columnsList[i];
+	    column.save();
+	}
 
 	Main.database.cardUpdate(card);
 	Main.refreshSearches();
@@ -590,6 +611,8 @@
     public void populateColumns()
     {
 	columns.removeAll();
+	columns.validate();
+	columns.repaint();
 	
 	JSpinner.DateEditor dateRangeBeginEditor = (JSpinner.DateEditor)dateRangeBegin.getEditor();
 	JSpinner.DateEditor dateRangeEndEditor = (JSpinner.DateEditor)dateRangeEnd.getEditor();
@@ -600,22 +623,33 @@
 	if( begin.after(end) ) {
 	    return;
 	}
+
+	Term term = new Term(Card.TAG_CALENDAR_BOARD_COLUMN_DATE);
 	
 	Calendar cur = Calendar.getInstance();
 	cur.setTime(begin);
 	while( cur.getTime().before(end) ) {
-	    boolean columnFilled = true;
-	    if( optionOnlyFilledColumns && !columnFilled ) {
+	    Card card = null;
+	    
+	    term = term.createTerm( DATE_FORMAT.format(cur.getTime()) );
+	    Card cards[] = Main.database.searchCustom( new TermQuery(term), 1, false );
+
+	    if( cards.length != 0 ) {
+		card = cards[0];
+	    }
+	    
+	    if( optionOnlyFilledColumns && card == null ) {
 		continue;
 	    }
-	    insertColumn( cur.getTime() );
+	    
+	    insertColumn( cur.getTime(), card );
 	    cur.add( Calendar.DAY_OF_MONTH, 1 );
 	}
     }
 
-    public void insertColumn( Date date )
+    public void insertColumn( Date date, Card card )
     {
-	ColumnWidget column = new ColumnWidget(this, null);
+	ColumnWidget column = new ColumnWidget(this, card, date);
 	column.titleSet( DateFormat.getDateInstance().format(date) );
 	insertColumnRaw(column);
     }