changeset 85:9eb88ddd4e94

TabCalendarBoard: Populating columns based on selected date range
author Fox
date Mon, 30 Jan 2023 05:53:41 +0100
parents 0be27b125634
children 2a93d2a65b0f
files src/junotu/TabCalendarBoard.java
diffstat 1 files changed, 46 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/junotu/TabCalendarBoard.java	Wed Jan 25 05:29:33 2023 +0100
+++ b/src/junotu/TabCalendarBoard.java	Mon Jan 30 05:53:41 2023 +0100
@@ -3,6 +3,9 @@
 /* TODO: Clean-up imports. */
 
 import java.lang.RuntimeException;
+import java.util.Date;
+import java.util.Calendar;
+import java.text.DateFormat;
 
 import java.awt.Dimension;
 import java.awt.Font;
@@ -21,6 +24,8 @@
 import javax.swing.SwingUtilities;
 import javax.swing.event.DocumentListener;
 import javax.swing.event.DocumentEvent;
+import javax.swing.event.ChangeListener;
+import javax.swing.event.ChangeEvent;
 import javax.swing.SpinnerDateModel;
 
 import javax.swing.JPanel;
@@ -49,7 +54,7 @@
 import junotu.Window.Tab;
 import junotu.Card;
 
-public class TabCalendarBoard extends JPanel implements ActionListener, MouseListener {
+public class TabCalendarBoard extends JPanel implements ActionListener, MouseListener, ChangeListener {
 
     final static int COLUMN_CONTENT_WIDTH = 256;
     final static int COLUMN_WIDTH = COLUMN_CONTENT_WIDTH+16;
@@ -510,6 +515,9 @@
 	dateRangeBeginEditor.getFormat().applyPattern("yyyy-MM-dd EEEE");
 	dateRangeEndEditor.getFormat().applyPattern("yyyy-MM-dd EEEE");
 
+	dateRangeBegin.addChangeListener(this);
+	dateRangeEnd.addChangeListener(this);
+
 	back.addActionListener(this);
 
 	back.setToolTipText("Go back to where the calendar board was accessed from.");
@@ -555,13 +563,37 @@
 	columns.removeAll();
     }
 
-    public void insertColumn()
+    public void populateColumns()
+    {
+	columns.removeAll();
+	
+	JSpinner.DateEditor dateRangeBeginEditor = (JSpinner.DateEditor)dateRangeBegin.getEditor();
+	JSpinner.DateEditor dateRangeEndEditor = (JSpinner.DateEditor)dateRangeEnd.getEditor();
+	
+	Date begin = dateRangeBeginEditor.getModel().getDate();
+	Date end = dateRangeEndEditor.getModel().getDate();
+	
+	if( begin.after(end) ) {
+	    return;
+	}
+	
+	Calendar cur = Calendar.getInstance();
+	cur.setTime(begin);
+	while( cur.getTime().before(end) ) {
+	    boolean columnFilled = true;
+	    if( optionOnlyFilledColumns && !columnFilled ) {
+		continue;
+	    }
+	    insertColumn( cur.getTime() );
+	    cur.add( Calendar.DAY_OF_MONTH, 1 );
+	}
+    }
+
+    public void insertColumn( Date date )
     {
 	ColumnWidget column = new ColumnWidget(this, null);
-	column.titleSet("New column");
-	column.addMouseListener(this);
-	columns.add(column);
-	columns.revalidate();
+	column.titleSet( DateFormat.getDateInstance().format(date) );
+	insertColumnRaw(column);
     }
 
     public void insertColumnRaw( ColumnWidget column )
@@ -660,5 +692,13 @@
     public void mouseExited( MouseEvent e ) {}
     public void mousePressed( MouseEvent e ) {}
     public void mouseReleased( MouseEvent e ) {}
+
+    public void stateChanged( ChangeEvent e )
+    {
+	Object source = e.getSource();
+	if( source == dateRangeBegin || source == dateRangeEnd ) {
+	    populateColumns();
+	}
+    }
     
 }