[Schmitzm-commits] r1679 - in trunk/schmitzm-core/src/main/java/de/schmitzm: lang swing

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Aug 18 21:25:04 CEST 2011


Author: mojays
Date: 2011-08-18 21:25:03 +0200 (Thu, 18 Aug 2011)
New Revision: 1679

Added:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/YearLessDateChooser.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/YearLessTextFieldDateEditor.java
Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
Log:
new YearLessDateChooser (JDateChooser to only choose day and month)
LangUtil: new methods to increase/decrease a date

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2011-08-17 15:20:23 UTC (rev 1678)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2011-08-18 19:25:03 UTC (rev 1679)
@@ -232,8 +232,40 @@
 
 		return combinedDateCal.getTime();
 	}
+	
+	/**
+	 * Increases a date by milliseconds.
+	 * @param date date to increase
+	 * @param millies milliseconds to increase the date with
+	 * @return a new {@link Date} instance
+	 */
+	public static Date incDate(Date date, long millies) {
+	  if ( date == null )
+	    return null;
+	  return new Date(date.getTime() + millies);
+	}
 
-	/**
+    /**
+     * Increases a date by days.
+     * @param date date to increase
+     * @param days days to increase the date with
+     * @return a new {@link Date} instance
+     */
+    public static Date incDays(Date date, int days) {
+      return incDate(date, days * DAY_MILLIS);
+    }
+
+    /**
+     * Decreases a date by days.
+     * @param date date to decrease
+     * @param days days to decrease the date with
+     * @return a new {@link Date} instance
+     */
+    public static Date decDays(Date date, int days) {
+      return incDays(date, -days);
+    }
+
+    /**
 	 * Tries several date formats to parse a date string.
 	 * 
 	 * @param dateStr

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/YearLessDateChooser.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/YearLessDateChooser.java	2011-08-17 15:20:23 UTC (rev 1678)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/YearLessDateChooser.java	2011-08-18 19:25:03 UTC (rev 1679)
@@ -0,0 +1,90 @@
+package de.schmitzm.swing;
+
+import java.awt.event.ActionEvent;
+
+import com.toedter.calendar.JDateChooser;
+
+/**
+ * {@link JDateChooser} without prompting a year, but only month and day of
+ * month. 
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ */
+public class YearLessDateChooser extends JDateChooser {
+  /**
+   * Creates a new year-less date chooser which accepts February 29th. 
+   * @param dateFormatPattern pattern which defines the input format (without year!)
+   * @param dateMaskPattern input mask for the format
+   * @param placeholder placeholder for empty positions
+   */
+  public YearLessDateChooser(String dateFormatPattern, String dateMaskPattern, char placeholder) {
+    this( dateFormatPattern, dateMaskPattern, placeholder,true );
+  }
+
+  public YearLessDateChooser(String dateFormatPattern, String dateMaskPattern, char placeholder, boolean inclFeb29) {
+    this( new YearLessTextFieldDateEditor(dateFormatPattern, dateMaskPattern, placeholder,inclFeb29) );
+  }
+
+  public YearLessDateChooser(YearLessTextFieldDateEditor editor) {
+    super(editor);
+    initGUI();
+  }
+  
+  /**
+   * Called by the constructor to initialize / modify the GUI.
+   */
+  protected void initGUI() {
+    // modify the popup calendar
+    // - hide the year chooser
+    // - hide the year week information
+    getJCalendar().getYearChooser().setMinimum(getFixedYear());
+    getJCalendar().getYearChooser().setMaximum(getFixedYear());
+    getJCalendar().getYearChooser().setVisible(false);
+    getJCalendar().getDayChooser().setWeekOfYearVisible(false);
+//    // Remove week day information
+//    for (int i=0; i<7; i++)
+//      getJCalendar().getDayChooser().getDayPanel().remove(0);
+  }
+  
+  
+  
+  /**
+   * After calling the super method a adequate year is set to
+   * the popup calendar (with or without February 29th).
+   */
+  @Override
+  public void actionPerformed(ActionEvent e) {
+    super.actionPerformed(e);
+    // set the calendar popup dialog to a year which provides
+    // the February 29th or not.
+    if ( isFebruary29Included() )
+      getJCalendar().getYearChooser().setYear( YearLessTextFieldDateEditor.LEAP_YEAR );
+    else
+      getJCalendar().getYearChooser().setYear( YearLessTextFieldDateEditor.NO_LEAP_YEAR );
+  }
+
+  /**
+   * Returns the date editor as {@link YearLessTextFieldDateEditor}.
+   */
+  @Override
+  public YearLessTextFieldDateEditor getDateEditor() {
+    return (YearLessTextFieldDateEditor)super.getDateEditor();
+  }
+  
+  /**
+   * Returns whether February 29th is accepted as valid by the editor. 
+   */
+  public boolean isFebruary29Included() {
+    return getDateEditor().isFebruary29Included();
+  }
+
+  /**
+   * Returns the year the calendar is fixed to, depending on
+   * whether or not February 29th included. 
+   * @see #LEAP_YEAR
+   * @see #NO_LEAP_YEAR
+   */
+  public int getFixedYear() {
+    return getDateEditor().getFixedYear();
+  }
+
+}

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/YearLessTextFieldDateEditor.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/YearLessTextFieldDateEditor.java	2011-08-17 15:20:23 UTC (rev 1678)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/YearLessTextFieldDateEditor.java	2011-08-18 19:25:03 UTC (rev 1679)
@@ -0,0 +1,100 @@
+package de.schmitzm.swing;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import com.toedter.calendar.JTextFieldDateEditor;
+
+/**
+ * {@link JTextFieldDateEditor} without prompting a year, but only month and day of
+ * month. 
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ */
+public class YearLessTextFieldDateEditor extends JTextFieldDateEditor {
+  /** Constant for a leap year (2012). */
+  public static final int LEAP_YEAR = 2012;
+  /** Constant for a non leap year (2011). */
+  public static final int NO_LEAP_YEAR = 2011;
+  
+  /** Indicates whether February 29th is accepted as input. */
+  protected boolean inclFeb29 = false;
+  /** Special date formatter to handle February 29th, although
+   *  the "normal" date format pattern does not include year
+   *  information. */
+  protected SimpleDateFormat leapYearDateFormatter;
+  
+  /**
+   * Creates a new editor. The format pattern should only include
+   * day and month definitions and NO YEAR PLACEHOLDER.
+   * @param inclFeb29 indicated whether February 29th is accepted as input 
+   */
+  public YearLessTextFieldDateEditor(String dateFormatPattern, String dateMaskPattern, char placeholder, boolean inclFeb29) {
+    super(dateFormatPattern,dateMaskPattern,placeholder);
+    this.inclFeb29 = inclFeb29;
+  }
+
+  /**
+   * Creates a new editor. The format pattern should only include
+   * day and month definitions and NO YEAR PLACEHOLDER.
+   * @param inclFeb29 indicated whether February 29th is accepted as input 
+   */
+  public YearLessTextFieldDateEditor(boolean inclFeb29, boolean showMask, String dateFormatPattern, String dateMaskPattern, char placeholder) {
+    super(showMask,dateFormatPattern,dateMaskPattern,placeholder);
+    this.inclFeb29 = inclFeb29;
+  }
+
+  /**
+   * Sets the date format sting for the editor. The format pattern 
+   * should only include day and month definitions and NO YEAR PLACEHOLDER.
+   */
+  @Override
+  public void setDateFormatString(String format) {
+    if ( leapYearDateFormatter == null ) {
+      // Create a SimpleDateFormat, which automatically parses
+      // a year-less date string to a corresponding date in the
+      // leap year 2012, so that the 29th of February is also
+      // parsed correctly
+      // Note: The normal SimpleDateFormat causes an exception when
+      //       trying to parse "29.02." for example because it
+      //       assumes the year 1970 which is not a leap year!
+      leapYearDateFormatter = new SimpleDateFormat(format) {
+        SimpleDateFormat stringToDateParser = null;
+        public Date parse(String txt) throws ParseException {
+          // if special parser for the direction "String -> Date" is
+          // not yet created, create it
+          if ( stringToDateParser == null ) {
+            String dateFormatPatternWithYear = getDateFormatString() + " yyyy";
+            stringToDateParser = new SimpleDateFormat(dateFormatPatternWithYear);
+          }
+          // add leap year information to date string and convert it
+          // with the special parser
+          txt += " "+getFixedYear();
+          
+          return stringToDateParser.parse(txt);
+        }
+      };
+    }  
+    // Instead of using the normal DateFormat, use our special
+    // formatter to also handle February 29th correctly
+    this.dateFormatter = leapYearDateFormatter;
+    super.setDateFormatString(format);
+  }
+  
+  /**
+   * Returns whether February 29th is accepted as valid by the editor. 
+   */
+  public boolean isFebruary29Included() {
+    return inclFeb29;
+  }
+
+  /**
+   * Returns the year the calendar is fixed to, depending on
+   * whether or not February 29th included. 
+   * @see #LEAP_YEAR
+   * @see #NO_LEAP_YEAR
+   */
+  public int getFixedYear() {
+    return isFebruary29Included() ? LEAP_YEAR : NO_LEAP_YEAR;
+  }
+}



More information about the Schmitzm-commits mailing list