changeset 88:a22a6d60a669

TabCalendarBoard: Functional options
author Fox
date Sun, 19 Feb 2023 00:15:34 +0100
parents caae98448674
children 355b3c6c6eb9
files src/junotu/TabCalendarBoard.java
diffstat 1 files changed, 58 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/junotu/TabCalendarBoard.java	Tue Feb 07 22:42:19 2023 +0100
+++ b/src/junotu/TabCalendarBoard.java	Sun Feb 19 00:15:34 2023 +0100
@@ -49,6 +49,9 @@
 import javax.swing.JTextField;
 import javax.swing.JScrollPane;
 import javax.swing.JScrollBar;
+import javax.swing.JPopupMenu;
+import javax.swing.JMenuItem;
+import javax.swing.JCheckBoxMenuItem;
 
 import javax.swing.BorderFactory;
 import javax.swing.border.TitledBorder;
@@ -81,13 +84,18 @@
 	long identifier;
 	boolean newCard;
 	Date date;
+	TabCalendarBoard parent;
 	
 	TitledBorder titledBorder;
 	Box cards;
 	JButton addCard;
 
-	public ColumnWidget( TabCalendarBoard parent, Card card, Date date_ )
+	public ColumnWidget( TabCalendarBoard parent_, Card card, Date date_ )
 	{
+	    newCard = card == null;
+	    date = date_;
+	    parent = parent_;
+	    
 	    this.setLayout( new GridBagLayout() );
 	    
 	    cards = Box.createVerticalBox();
@@ -185,9 +193,6 @@
 	    addCard.addActionListener(this);
 	    
 	    addCard.setToolTipText("Add card.");
-
-	    newCard = card == null;
-	    date = date_;
 	    
 	    if( newCard ) {
 		return;
@@ -263,6 +268,7 @@
 	    ColumnCardWidget cardWidget = (ColumnCardWidget)cards.getComponent(at);
 	    cards.remove(at);
 	    cardWidget.removeMouseListener(this);
+	    checkCardCount();
 	    return cardWidget;
 	}
 
@@ -300,6 +306,8 @@
 	    if( cardList.length == 0 ) {
 		if( !newCard ) {
 		    Main.database.cardDeleteByIdentifier(identifier);
+		    identifier = -1;
+		    newCard = true;
 		}
 		return;
 	    }
@@ -341,11 +349,19 @@
 	public void delete()
 	{
 	    save();
+	    parent = null;
 	    if( !newCard ) {
 		Main.database.cardDeleteByIdentifier(identifier);
 	    }
 	}
 
+	public void checkCardCount()
+	{
+	    if( cardCount() == 0 ) {
+	        columnIsEmpty(this);
+	    }
+	}
+
 	public void actionPerformed( ActionEvent e )
 	{
 	    Object source = e.getSource();
@@ -394,6 +410,7 @@
 		    cardWidget.delete();
 		    cards.remove(cardWidget);
 		    cards.revalidate();
+		    checkCardCount();
 		}
 	    }
 	}
@@ -508,18 +525,27 @@
     boolean optionOnlyFilledColumns;
 
     JButton back;
+    JButton options;
     JSpinner dateRangeBegin;
     JSpinner dateRangeEnd;
+    JPopupMenu menu;
+
+    JCheckBoxMenuItem menu_onlyFilled;
 
     public TabCalendarBoard()
     {
 	this.setLayout( new BorderLayout() );
 
 	back = new JButton("Back");
+	options = new JButton("=");
 	dateRangeBegin = new JSpinner( new SpinnerDateModel() );
 	dateRangeEnd = new JSpinner( new SpinnerDateModel() );
 	JSpinner.DateEditor dateRangeBeginEditor = (JSpinner.DateEditor)dateRangeBegin.getEditor();
 	JSpinner.DateEditor dateRangeEndEditor = (JSpinner.DateEditor)dateRangeEnd.getEditor();
+
+	menu = new JPopupMenu("Options");
+	menu_onlyFilled = new JCheckBoxMenuItem("Show only filled days");
+	menu.add(menu_onlyFilled);
 	    
 	Box bottom = Box.createHorizontalBox();
 	columns = Box.createHorizontalBox();
@@ -529,6 +555,7 @@
 	bottom.add( Box.createHorizontalGlue() );
 	bottom.add( dateRangeBegin );
 	bottom.add( dateRangeEnd );
+	bottom.add( options );
 	this.add( scroll, BorderLayout.CENTER );
 	this.add( bottom, BorderLayout.SOUTH );
 
@@ -539,8 +566,12 @@
 	dateRangeEnd.addChangeListener(this);
 
 	back.addActionListener(this);
+	options.addActionListener(this);
+	menu_onlyFilled.addActionListener(this);
 
 	back.setToolTipText("Go back to where the calendar board was accessed from.");
+	options.setToolTipText("Show calendar options.");
+	menu_onlyFilled.setToolTipText("If checked, days with no cards will be hidden.");
 	
     }
 
@@ -568,6 +599,7 @@
 	}
 	
 	optionOnlyFilledColumns = card.tagHas( Card.TAG_CALENDAR_BOARD_OPTION_ONLY_FILLED );
+	menu_onlyFilled.setSelected(optionOnlyFilledColumns);
 	
     }
 
@@ -628,7 +660,7 @@
 	
 	Calendar cur = Calendar.getInstance();
 	cur.setTime(begin);
-	while( cur.getTime().before(end) ) {
+	for( ; cur.getTime().before(end); cur.add( Calendar.DAY_OF_MONTH, 1 ) ) {
 	    Card card = null;
 	    
 	    term = term.createTerm( DATE_FORMAT.format(cur.getTime()) );
@@ -643,7 +675,6 @@
 	    }
 	    
 	    insertColumn( cur.getTime(), card );
-	    cur.add( Calendar.DAY_OF_MONTH, 1 );
 	}
     }
 
@@ -679,6 +710,12 @@
 	columns.repaint();
     }
 
+    public void columnIsEmpty( ColumnWidget columnWidget ) {
+	if( optionOnlyFilledColumns ) {
+	    removeColumn(columnWidget);
+	}
+    }
+
     public void moveCard( int from, int at, int to )
     {
 	ColumnWidget fromColumn = (ColumnWidget)columns.getComponent(from);
@@ -714,9 +751,18 @@
 
     public void actionPerformed( ActionEvent e )
     {
-	if( e.getSource() == back ) {
+	Object source = e.getSource();
+	if( source == back ) {
 	    buttonClickedBack();
 	    return;
+	} else if( source == options ) {
+	    menu.show( (Component)source, 0, 0 );
+	    return;
+	} else if( source == menu_onlyFilled ) {
+	    optionOnlyFilledColumns = menu_onlyFilled.isSelected();
+	    boardSave();
+	    populateColumns();
+	    return;
 	}
 
 	ColumnWidget sourceColumn = (ColumnWidget)e.getSource();
@@ -726,17 +772,21 @@
 
 	    case KEY_ACTION_CARD_LEFT: {
 		moveCard( columnIndex, sourceColumn.selectedCard(), max( columnIndex-1, 0) );
+		break;
 	    }
 	    case KEY_ACTION_CARD_RIGHT: {
 		moveCard( columnIndex, sourceColumn.selectedCard(), min( columnIndex+1, columns.getComponentCount()-1) );
+		break;
 	    }
 	    case KEY_ACTION_CARD_FULL_LEFT: {
 		moveCard( columnIndex, sourceColumn.selectedCard(), 0 );
+		break;
 	    }
 	    case KEY_ACTION_CARD_FULL_RIGHT: {
 		moveCard( columnIndex, sourceColumn.selectedCard(), columns.getComponentCount()-1 );
+		break;
 	    }
-		
+	    
 	    }
 	}
     }