diff --git a/.gitattributes b/.gitattributes index 1954181cc8c37f05bbede1a26d20401a9e36c78e..32af4b37ae386595b16a90a9335289bb25d5450c 100644 --- a/.gitattributes +++ b/.gitattributes @@ -219,6 +219,37 @@ JAVA/GUI/Plotter/src/gov/noaa/pmel/sgt/swing/plotmarkcodes.png -text JAVA/GUI/Plotter/src/gov/noaa/pmel/sgt/swing/sgt_logo.png -text JAVA/LofarUtils/.cvsignore -text JAVA/LofarUtils/nbproject/.cvsignore -text +JAVA/LofarUtils/src/com/toedter/calendar/demo/images/DemoTableColor16.gif -text +JAVA/LofarUtils/src/com/toedter/calendar/demo/images/JDateChooserColor16.gif -text +JAVA/LofarUtils/src/com/toedter/calendar/images/JCalendarColor16.gif -text +JAVA/LofarUtils/src/com/toedter/calendar/images/JCalendarColor32.gif -text +JAVA/LofarUtils/src/com/toedter/calendar/images/JCalendarMono16.gif -text +JAVA/LofarUtils/src/com/toedter/calendar/images/JCalendarMono32.gif -text +JAVA/LofarUtils/src/com/toedter/calendar/images/JDateChooserColor16.gif -text +JAVA/LofarUtils/src/com/toedter/calendar/images/JDateChooserColor32.gif -text +JAVA/LofarUtils/src/com/toedter/calendar/images/JDateChooserIcon.gif -text +JAVA/LofarUtils/src/com/toedter/calendar/images/JDateChooserMono16.gif -text +JAVA/LofarUtils/src/com/toedter/calendar/images/JDateChooserMono32.gif -text +JAVA/LofarUtils/src/com/toedter/calendar/images/JDayChooserColor16.gif -text +JAVA/LofarUtils/src/com/toedter/calendar/images/JDayChooserColor32.gif -text +JAVA/LofarUtils/src/com/toedter/calendar/images/JDayChooserMono16.gif -text +JAVA/LofarUtils/src/com/toedter/calendar/images/JDayChooserMono32.gif -text +JAVA/LofarUtils/src/com/toedter/calendar/images/JMonthChooserColor16.gif -text +JAVA/LofarUtils/src/com/toedter/calendar/images/JMonthChooserColor32.gif -text +JAVA/LofarUtils/src/com/toedter/calendar/images/JMonthChooserMono16.gif -text +JAVA/LofarUtils/src/com/toedter/calendar/images/JMonthChooserMono32.gif -text +JAVA/LofarUtils/src/com/toedter/calendar/images/JYearChooserColor16.gif -text +JAVA/LofarUtils/src/com/toedter/calendar/images/JYearChooserColor32.gif -text +JAVA/LofarUtils/src/com/toedter/calendar/images/JYearChooserMono16.gif -text +JAVA/LofarUtils/src/com/toedter/calendar/images/JYearChooserMono32.gif -text +JAVA/LofarUtils/src/com/toedter/components/images/JLocaleChooserColor16.gif -text +JAVA/LofarUtils/src/com/toedter/components/images/JLocaleChooserColor32.gif -text +JAVA/LofarUtils/src/com/toedter/components/images/JLocaleChooserMono16.gif -text +JAVA/LofarUtils/src/com/toedter/components/images/JLocaleChooserMono32.gif -text +JAVA/LofarUtils/src/com/toedter/components/images/JSpinFieldColor16.gif -text +JAVA/LofarUtils/src/com/toedter/components/images/JSpinFieldColor32.gif -text +JAVA/LofarUtils/src/com/toedter/components/images/JSpinFieldMono16.gif -text +JAVA/LofarUtils/src/com/toedter/components/images/JSpinFieldMono32.gif -text JAVA/MAC/jRSP/.cvsignore -text JAVA/MAC/jRSP/nbproject/.cvsignore -text LCS/AMC/amc_main_classes.png -text diff --git a/JAVA/LofarUtils/src/build.xml b/JAVA/LofarUtils/src/build.xml new file mode 100644 index 0000000000000000000000000000000000000000..647a710c3ad9cee517d66d692ce30f931a878334 --- /dev/null +++ b/JAVA/LofarUtils/src/build.xml @@ -0,0 +1,204 @@ +<?xml version="1.0"?> + +<!-- ======================================================================= --> +<!-- ANT-Makefile for JCalendar (C) 2006 Kai Toedter --> +<!-- ======================================================================= --> + +<project name="JCalendar" default="main" basedir=".."> + + <!-- ===================================================================== --> + <!-- PROPERTIES --> + <!-- ===================================================================== --> + + <property name="version" value="1.3.2" /> + <property name="modified" value="06/04/06" /> + <property name="copyright.year" value="1999 - 2006" /> + + <property name="name" value="jcalendar" /> + <property name="Name" value="JCalendar" /> + + <property name="classpath" value="" /> + + <property name="jcalendar.home" value=".." /> + + <property name="bin.dir" value="bin" /> + <property name="build.dir" value="build" /> + <property name="dist.dir" value="dist" /> + <property name="doc.dir" value="doc" /> + <property name="javadoc.dir" value="${doc.dir}/api" /> + <property name="lib.dir" value="lib" /> + <property name="src.dir" value="src" /> + + <!-- ===================================================================== --> + <!-- COMPILER: classic (JDK 1.1 & 1.2), modern or jikes --> + <!-- ===================================================================== --> + <property name="build.compiler" value="modern" /> + + <property name="manifest" value="src/jcalendar.manifest" /> + <property name="packages" + value="com.toedter.calendar.*,com.toedter.components.*, com.toedter.calendar.demo.*" + /> + + <!-- ===================================================================== --> + <!-- MAIN --> + <!-- ===================================================================== --> + + <target name="main" depends="compile"> + </target> + + <!-- ===================================================================== --> + <!-- PREPARE BUILD --> + <!-- ===================================================================== --> + + <target name="prepare" depends="clean"> + <mkdir dir="${build.dir}" /> + </target> + + <!-- ===================================================================== --> + <!-- COMPILE --> + <!-- ===================================================================== --> + + <target name="compile" depends="prepare"> + <copy todir="${build.dir}"> + <fileset dir="${src.dir}"> + <patternset> + <include name="**/*.gif" /> + <include name="**/classlist" /> + </patternset> + </fileset> + </copy> + <javac srcdir="${src.dir}" + destdir="${build.dir}" + classpath="${classpath};lib/looks-1.3.2.jar" + debug="off" + deprecation="on" + optimize="on" + source="1.4" + /> + </target> + + <!-- ===================================================================== --> + <!-- JAR FILE --> + <!-- ===================================================================== --> + + <target name="jar" depends="compile"> + <mkdir dir="${lib.dir}" /> + <jar jarfile="${lib.dir}/jcalendar-${version}.jar" + compress="no" + basedir="${build.dir}" + includes="com/**" + manifest="${manifest}" + /> + </target> + + <!-- ===================================================================== --> + <!-- JAVADOC --> + <!-- ===================================================================== --> + + <target name="javadocs" depends="prepare"> + <mkdir dir="${javadoc.dir}" /> + <javadoc packagenames="${packages}" + sourcepath="${basedir}/${src.dir}" + classpath=".;lib/looks-1.3.2.jar" + destdir="${javadoc.dir}" + author="true" + version="true" + windowtitle="${Name} API" + doctitle="${Name}" + bottom="Copyright © ${copyright.year} Kai Toedter." + /> + </target> + + <!-- ===================================================================== --> + <!-- DISTRIBUTION --> + <!-- ===================================================================== --> + + <target name="dist" depends="jar, javadocs"> + <filter token="VERSION" value="${version}" /> + <filter token="MODIFIED" value="${modified}" /> + <filter token="COPYRIGHTYEAR" value="${copyright.year}" /> + + <delete dir="${dist.dir}" /> + <mkdir dir="${dist.dir}" /> + <mkdir dir="${dist.dir}/${bin.dir}" /> + <mkdir dir="${dist.dir}/${lib.dir}" /> + <mkdir dir="${dist.dir}/${doc.dir}" /> + <mkdir dir="${dist.dir}/${src.dir}" /> + + <copy todir="${dist.dir}/${src.dir}"> + <fileset dir="${src.dir}" /> + </copy> + <copy todir="${dist.dir}/${lib.dir}"> + <fileset dir="${lib.dir}" /> + </copy> + <copy todir="${dist.dir}/${bin.dir}" filtering="yes"> + <fileset dir="${bin.dir}" /> + </copy> + <copy todir="${dist.dir}/${javadoc.dir}"> + <fileset dir="${javadoc.dir}" /> + </copy> + <copy todir="${dist.dir}/${doc.dir}/images"> + <fileset dir="${doc.dir}/images" /> + </copy> + + <copy todir="${dist.dir}" filtering="yes"> + <fileset dir="${basedir}"> + <include name="**/*.txt"/> + <exclude name="**/license.txt"/> + </fileset> + </copy> + <copy file="${doc.dir}/license.html" + todir="${dist.dir}/${doc.dir}/" + filtering="no" + /> + <copy file="${doc.dir}/index.html" + todir="${dist.dir}/${doc.dir}/" + filtering="yes" + /> + <copy file="${doc.dir}/demo.html" + todir="${dist.dir}/${doc.dir}/" + filtering="yes" + /> + <copy file="${doc.dir}/style.css" + todir="${dist.dir}/${doc.dir}/" + filtering="no" + /> + + <!-- create a zip file --> + <zip zipfile="jcalendar-${version}.zip"> + <zipfileset dir="${dist.dir}"> + <include name="**" /> + </zipfileset> + </zip> + </target> + + <!-- =================================================================== --> + <!-- Run Demo --> + <!-- =================================================================== --> + + <target name="runDemo" depends="jar"> + <echo message="Running JCalendar demo ..." /> + <java classpath="lib/jcalendar.jar;lib/looks-1.3.2.jar" + classname="com.toedter.calendar.demo.JCalendarDemo" + fork="yes" + > + </java> + </target> + + <!-- =================================================================== --> + <!-- CLEAN --> + <!-- =================================================================== --> + + <target name="clean"> + <delete> + <fileset dir="${lib.dir}" includes="jcalendar*"/> + </delete> + <delete dir="${build.dir}" /> + <delete dir="${dist.dir}" /> + <delete dir="${javadoc.dir}" /> + </target> + +</project> + +<!-- End of build.xml --> + diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/DateUtil.java b/JAVA/LofarUtils/src/com/toedter/calendar/DateUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..0366ba57cf9728c9841f9e62d62fa563a4d49aa3 --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/calendar/DateUtil.java @@ -0,0 +1,164 @@ +/* + * DateUtil.java - Date handling utilities + * Copyright (C) 2006 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +package com.toedter.calendar; + +import java.util.Calendar; +import java.util.Date; + +/** + * A utility class for some date operations. + * + * @author Kai Toedter + * @version $LastChangedRevision: 95 $ + * @version $LastChangedDate: 2006-05-05 18:43:15 +0200 (Fr, 05 Mai 2006) $ + */ +public class DateUtil { + protected Date minSelectableDate; + + protected Date maxSelectableDate; + + protected Date defaultMinSelectableDate; + + protected Date defaultMaxSelectableDate; + + public DateUtil() { + Calendar tmpCalendar = Calendar.getInstance(); + tmpCalendar.set(1, 0, 1, 1, 1); + defaultMinSelectableDate = tmpCalendar.getTime(); + minSelectableDate = defaultMinSelectableDate; + tmpCalendar.set(9999, 0, 1, 1, 1); + defaultMaxSelectableDate = tmpCalendar.getTime(); + maxSelectableDate = defaultMaxSelectableDate; + } + + /** + * Sets a valid date range for selectable dates. If max is before min, the + * default range with no limitation is set. + * + * @param min + * the minimum selectable date or null (then the minimum date is + * set to 01\01\0001) + * @param max + * the maximum selectable date or null (then the maximum date is + * set to 01\01\9999) + */ + public void setSelectableDateRange(Date min, Date max) { + if (min == null) { + minSelectableDate = defaultMinSelectableDate; + } else { + minSelectableDate = min; + } + if (max == null) { + maxSelectableDate = defaultMaxSelectableDate; + } else { + maxSelectableDate = max; + } + if (maxSelectableDate.before(minSelectableDate)) { + minSelectableDate = defaultMinSelectableDate; + maxSelectableDate = defaultMaxSelectableDate; + } + } + + /** + * Sets the maximum selectable date. If null, the date 01\01\9999 will be set instead. + * + * @param max the maximum selectable date + * + * @return the maximum selectable date + */ + public Date setMaxSelectableDate(Date max) { + if (max == null) { + maxSelectableDate = defaultMaxSelectableDate; + } else { + maxSelectableDate = max; + } + return maxSelectableDate; + } + + /** + * Sets the minimum selectable date. If null, the date 01\01\0001 will be set instead. + * + * @param min the minimum selectable date + * + * @return the minimum selectable date + */ + public Date setMinSelectableDate(Date min) { + if (min == null) { + minSelectableDate = defaultMinSelectableDate; + } else { + minSelectableDate = min; + } + return minSelectableDate; + } + + /** + * Gets the maximum selectable date. + * + * @return the maximum selectable date + */ + public Date getMaxSelectableDate() { + return maxSelectableDate; + } + + /** + * Gets the minimum selectable date. + * + * @return the minimum selectable date + */ + public Date getMinSelectableDate() { + return minSelectableDate; + } + + /** + * Checks a given date if it is in the formally specified date range. + * + * @param date + * the date to check + * @return true, if the date is within minSelectableDate and + * maxSelectableDate + */ + public boolean checkDate(Date date) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); + calendar.set(Calendar.HOUR_OF_DAY, 0); + calendar.set(Calendar.MINUTE, 0); + calendar.set(Calendar.SECOND, 0); + calendar.set(Calendar.MILLISECOND, 0); + + Calendar minCal = Calendar.getInstance(); + minCal.setTime(minSelectableDate); + minCal.set(Calendar.HOUR_OF_DAY, 0); + minCal.set(Calendar.MINUTE, 0); + minCal.set(Calendar.SECOND, 0); + minCal.set(Calendar.MILLISECOND, 0); + + Calendar maxCal = Calendar.getInstance(); + maxCal.setTime(maxSelectableDate); + maxCal.set(Calendar.HOUR_OF_DAY, 0); + maxCal.set(Calendar.MINUTE, 0); + maxCal.set(Calendar.SECOND, 0); + maxCal.set(Calendar.MILLISECOND, 0); + + return !(calendar.before(minCal) || calendar.after(maxCal)); + } + +} diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/IDateEditor.java b/JAVA/LofarUtils/src/com/toedter/calendar/IDateEditor.java new file mode 100644 index 0000000000000000000000000000000000000000..e876e5093b9390d91c9349d28c865be4416e1029 --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/calendar/IDateEditor.java @@ -0,0 +1,179 @@ +/* + * Copyright (C) 2006 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +package com.toedter.calendar; + +import java.beans.PropertyChangeListener; +import java.util.Date; +import java.util.Locale; + +import javax.swing.JComponent; + +/** + * All date editors that should be used by a JDateChooser have to implement this + * interface. + * + * @author Kai Toedter + * @version $LastChangedRevision: 96 $ + * @version $LastChangedDate: 2006-05-12 18:19:35 +0200 (Fr, 12 Mai 2006) $ + * + */ +public interface IDateEditor { + + /** + * + * Returns the date. + * + * @return the date + */ + public Date getDate(); + + /** + * Sets the date. This should be implemented as a bound property, firing the + * "date" property. + * + * @param date + * the date to set + */ + public void setDate(Date date); + + /** + * Sets the date format string, e.g. "MM/dd/yy". If the date format string + * is null or invalid, the date format string will be set to the MEDIUM + * Simple date format of the current locale. + * + * @param dateFormatString + * the date format string + */ + public void setDateFormatString(String dateFormatString); + + /** + * Returns tha date format string. + * + * @return the date format string + */ + public String getDateFormatString(); + + /** + * Sets a valid date range for selectable dates. If max is before + * min, the default range with no limitation is set. + * + * @param min + * the minimum selectable date or null (then the minimum date should be + * set to 01\01\0001) + * @param max + * the maximum selectable date or null (then the maximum date should be + * set to 01\01\9999) + */ + public void setSelectableDateRange(Date min, Date max) ; + + /** + * Gets the minimum selectable date. + * + * @return the minimum selectable date + */ + public Date getMaxSelectableDate(); + + /** + * Gets the maximum selectable date. + * + * @return the maximum selectable date + */ + public Date getMinSelectableDate(); + + /** + * Sets the maximum selectable date. + * + * @param max maximum selectable date + */ + public void setMaxSelectableDate(Date max); + + /** + * Sets the minimum selectable date. + * + * @param min minimum selectable date + */ + public void setMinSelectableDate(Date min); + + /** + * Returns the UI component, e.g. the actual JTextField implementing the + * editor. + * + * @return the UI component + */ + public JComponent getUiComponent(); + + /** + * Sets the locale. Usually this should have impact on the current date + * format string. + * + * @param locale + * the locale to set + */ + public void setLocale(Locale locale); + + /** + * Enables or disables the UI compoment. + * + * @param enabled + * true, if the UI component should be enabled. + */ + public void setEnabled(boolean enabled); + + /** + * Adds a property change listener that should be added to the implementing + * UI component. The UI component should fire a "date" property if the date + * changes. + * + * @param listener + * the property change listener. + */ + public void addPropertyChangeListener(PropertyChangeListener listener); + + /** + * Adds a property change listener that should be added to the implementing + * UI component. The UI component should fire a "date" property if the date + * changes. + * + * @param propertyName + * the property name, e.g. "date" + * @param listener + * the property change listener. + */ + public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener); + + /** + * Removes a property change listener. + * + * @param listener + * the property change listener. + */ + public void removePropertyChangeListener(PropertyChangeListener listener); + + /** + * Removes the listener from the date editor's property change listeners for the specific property. + * + * @param propertyName + * the property to listen for, e.g. "date" + * @param listener + * the listener + */ + public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener); +} diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/JCalendar.java b/JAVA/LofarUtils/src/com/toedter/calendar/JCalendar.java new file mode 100644 index 0000000000000000000000000000000000000000..dce15f22160b3615ddc57d80149275175c69cbad --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/calendar/JCalendar.java @@ -0,0 +1,660 @@ +/* + * JCalendar.java - A bean for choosing a date + * Copyright (C) 2004 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +package com.toedter.calendar; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Font; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; + +import javax.swing.BorderFactory; +import javax.swing.JFrame; +import javax.swing.JPanel; + +/** + * JCalendar is a bean for entering a date by choosing the year, month and day. + * + * @author Kai Toedter + * @version $LastChangedRevision: 95 $ + * @version $LastChangedDate: 2006-05-05 18:43:15 +0200 (Fr, 05 Mai 2006) $ + */ +public class JCalendar extends JPanel implements PropertyChangeListener { + private static final long serialVersionUID = 8913369762644440133L; + + private Calendar calendar; + + /** the day chooser */ + protected JDayChooser dayChooser; + private boolean initialized = false; + + /** indicates if weeks of year shall be visible */ + protected boolean weekOfYearVisible = true; + + /** the locale */ + protected Locale locale; + + /** the month chooser */ + protected JMonthChooser monthChooser; + + private JPanel monthYearPanel; + + /** the year chhoser */ + protected JYearChooser yearChooser; + + protected Date minSelectableDate; + + protected Date maxSelectableDate; + + /** + * Default JCalendar constructor. + */ + public JCalendar() { + this(null, null, true, true); + } + + /** + * JCalendar constructor which allows the initial date to be set. + * + * @param date + * the date + */ + public JCalendar(Date date) { + this(date, null, true, true); + } + + /** + * JCalendar constructor which allows the initial calendar to be set. + * + * @param calendar + * the calendar + */ + public JCalendar(Calendar calendar) { + this(null, null, true, true); + setCalendar(calendar); + } + + /** + * JCalendar constructor allowing the initial locale to be set. + * + * @param locale + * the new locale + */ + public JCalendar(Locale locale) { + this(null, locale, true, true); + } + + /** + * JCalendar constructor specifying both the initial date and locale. + * + * @param date + * the date + * @param locale + * the new locale + */ + public JCalendar(Date date, Locale locale) { + this(date, locale, true, true); + } + + /** + * JCalendar constructor specifying both the initial date and the month + * spinner type. + * + * @param date + * the date + * @param monthSpinner + * false, if no month spinner should be used + */ + public JCalendar(Date date, boolean monthSpinner) { + this(date, null, monthSpinner, true); + } + + /** + * JCalendar constructor specifying both the locale and the month spinner. + * + * @param locale + * the locale + * @param monthSpinner + * false, if no month spinner should be used + */ + public JCalendar(Locale locale, boolean monthSpinner) { + this(null, locale, monthSpinner, true); + } + + /** + * JCalendar constructor specifying the month spinner type. + * + * @param monthSpinner + * false, if no month spinner should be used + */ + public JCalendar(boolean monthSpinner) { + this(null, null, monthSpinner, true); + } + + /** + * JCalendar constructor with month spinner parameter. + * + * @param date + * the date + * @param locale + * the locale + * @param monthSpinner + * false, if no month spinner should be used + * @param weekOfYearVisible + * true, if weeks of year shall be visible + */ + public JCalendar(Date date, Locale locale, boolean monthSpinner, boolean weekOfYearVisible) { + + setName("JCalendar"); + + // needed for setFont() etc. + dayChooser = null; + monthChooser = null; + yearChooser = null; + this.weekOfYearVisible = weekOfYearVisible; + + this.locale = locale; + + if (locale == null) { + this.locale = Locale.getDefault(); + } + + calendar = Calendar.getInstance(); + + setLayout(new BorderLayout()); + + monthYearPanel = new JPanel(); + monthYearPanel.setLayout(new BorderLayout()); + + monthChooser = new JMonthChooser(monthSpinner); + yearChooser = new JYearChooser(); + monthChooser.setYearChooser(yearChooser); + monthYearPanel.add(monthChooser, BorderLayout.WEST); + monthYearPanel.add(yearChooser, BorderLayout.CENTER); + monthYearPanel.setBorder(BorderFactory.createEmptyBorder()); + + dayChooser = new JDayChooser(weekOfYearVisible); + dayChooser.addPropertyChangeListener(this); + monthChooser.setDayChooser(dayChooser); + monthChooser.addPropertyChangeListener(this); + yearChooser.setDayChooser(dayChooser); + yearChooser.addPropertyChangeListener(this); + add(monthYearPanel, BorderLayout.NORTH); + add(dayChooser, BorderLayout.CENTER); + + // Set the initialized flag before setting the calendar. This will + // cause the other components to be updated properly. + if (date != null) { + calendar.setTime(date); + } + + initialized = true; + + setCalendar(calendar); + } + + /** + * Creates a JFrame with a JCalendar inside and can be used for testing. + * + * @param s + * The command line arguments + */ + public static void main(String[] s) { + JFrame frame = new JFrame("JCalendar"); + + JCalendar jcalendar = new JCalendar(); + frame.getContentPane().add(jcalendar); + frame.pack(); + frame.setVisible(true); + } + + /** + * Returns the calendar property. + * + * @return the value of the calendar property. + */ + public Calendar getCalendar() { + return calendar; + } + + /** + * Gets the dayChooser attribute of the JCalendar object + * + * @return the dayChooser value + */ + public JDayChooser getDayChooser() { + return dayChooser; + } + + /** + * Returns the locale. + * + * @return the value of the locale property. + * + * @see #setLocale + */ + public Locale getLocale() { + return locale; + } + + /** + * Gets the monthChooser attribute of the JCalendar object + * + * @return the monthChooser value + */ + public JMonthChooser getMonthChooser() { + return monthChooser; + } + + /** + * Gets the yearChooser attribute of the JCalendar object + * + * @return the yearChooser value + */ + public JYearChooser getYearChooser() { + return yearChooser; + } + + /** + * Indicates if the weeks of year are visible.. + * + * @return boolean true, if weeks of year are visible + */ + public boolean isWeekOfYearVisible() { + return dayChooser.isWeekOfYearVisible(); + } + + /** + * JCalendar is a PropertyChangeListener, for its day, month and year + * chooser. + * + * @param evt + * the property change event + */ + public void propertyChange(PropertyChangeEvent evt) { + if (calendar != null) { + Calendar c = (Calendar) calendar.clone(); + + if (evt.getPropertyName().equals("day")) { + c.set(Calendar.DAY_OF_MONTH, ((Integer) evt.getNewValue()).intValue()); + setCalendar(c, false); + } else if (evt.getPropertyName().equals("month")) { + c.set(Calendar.MONTH, ((Integer) evt.getNewValue()).intValue()); + setCalendar(c, false); + } else if (evt.getPropertyName().equals("year")) { + c.set(Calendar.YEAR, ((Integer) evt.getNewValue()).intValue()); + setCalendar(c, false); + } else if (evt.getPropertyName().equals("date")) { + c.setTime((Date) evt.getNewValue()); + setCalendar(c, true); + } + } + } + + /** + * Sets the background color. + * + * @param bg + * the new background + */ + public void setBackground(Color bg) { + super.setBackground(bg); + + if (dayChooser != null) { + dayChooser.setBackground(bg); + } + } + + /** + * Sets the calendar property. This is a bound property. + * + * @param c + * the new calendar + * @throws NullPointerException - + * if c is null; + * @see #getCalendar + */ + public void setCalendar(Calendar c) { + setCalendar(c, true); + } + + /** + * Sets the calendar attribute of the JCalendar object + * + * @param c + * the new calendar value + * @param update + * the new calendar value + * @throws NullPointerException - + * if c is null; + */ + private void setCalendar(Calendar c, boolean update) { + if (c == null) { + setDate(null); + } + Calendar oldCalendar = calendar; + calendar = c; + + if (update) { + // Thanks to Jeff Ulmer for correcting a bug in the sequence :) + yearChooser.setYear(c.get(Calendar.YEAR)); + monthChooser.setMonth(c.get(Calendar.MONTH)); + dayChooser.setDay(c.get(Calendar.DATE)); + } + + firePropertyChange("calendar", oldCalendar, calendar); + } + + /** + * Enable or disable the JCalendar. + * + * @param enabled + * the new enabled value + */ + public void setEnabled(boolean enabled) { + super.setEnabled(enabled); + + if (dayChooser != null) { + dayChooser.setEnabled(enabled); + monthChooser.setEnabled(enabled); + yearChooser.setEnabled(enabled); + } + } + + /** + * Returns true, if enabled. + * + * @return true, if enabled. + */ + public boolean isEnabled() { + return super.isEnabled(); + } + + /** + * Sets the font property. + * + * @param font + * the new font + */ + public void setFont(Font font) { + super.setFont(font); + + if (dayChooser != null) { + dayChooser.setFont(font); + monthChooser.setFont(font); + yearChooser.setFont(font); + } + } + + /** + * Sets the foreground color. + * + * @param fg + * the new foreground + */ + public void setForeground(Color fg) { + super.setForeground(fg); + + if (dayChooser != null) { + dayChooser.setForeground(fg); + monthChooser.setForeground(fg); + yearChooser.setForeground(fg); + } + } + + /** + * Sets the locale property. This is a bound property. + * + * @param l + * the new locale value + * + * @see #getLocale + */ + public void setLocale(Locale l) { + if (!initialized) { + super.setLocale(l); + } else { + Locale oldLocale = locale; + locale = l; + dayChooser.setLocale(locale); + monthChooser.setLocale(locale); + firePropertyChange("locale", oldLocale, locale); + } + } + + /** + * Sets the week of year visible. + * + * @param weekOfYearVisible + * true, if weeks of year shall be visible + */ + public void setWeekOfYearVisible(boolean weekOfYearVisible) { + dayChooser.setWeekOfYearVisible(weekOfYearVisible); + setLocale(locale); // hack for doing complete new layout :) + } + + /** + * Gets the visibility of the decoration background. + * + * @return true, if the decoration background is visible. + */ + public boolean isDecorationBackgroundVisible() { + return dayChooser.isDecorationBackgroundVisible(); + } + + /** + * Sets the decoration background visible. + * + * @param decorationBackgroundVisible + * true, if the decoration background should be visible. + */ + public void setDecorationBackgroundVisible(boolean decorationBackgroundVisible) { + dayChooser.setDecorationBackgroundVisible(decorationBackgroundVisible); + setLocale(locale); // hack for doing complete new layout :) + } + + /** + * Gets the visibility of the decoration border. + * + * @return true, if the decoration border is visible. + */ + public boolean isDecorationBordersVisible() { + return dayChooser.isDecorationBordersVisible(); + } + + /** + * Sets the decoration borders visible. + * + * @param decorationBordersVisible + * true, if the decoration borders should be visible. + */ + public void setDecorationBordersVisible(boolean decorationBordersVisible) { + dayChooser.setDecorationBordersVisible(decorationBordersVisible); + setLocale(locale); // hack for doing complete new layout :) + } + + /** + * Returns the color of the decoration (day names and weeks). + * + * @return the color of the decoration (day names and weeks). + */ + public Color getDecorationBackgroundColor() { + return dayChooser.getDecorationBackgroundColor(); + } + + /** + * Sets the background of days and weeks of year buttons. + * + * @param decorationBackgroundColor + * the background color + */ + public void setDecorationBackgroundColor(Color decorationBackgroundColor) { + dayChooser.setDecorationBackgroundColor(decorationBackgroundColor); + } + + /** + * Returns the Sunday foreground. + * + * @return Color the Sunday foreground. + */ + public Color getSundayForeground() { + return dayChooser.getSundayForeground(); + } + + /** + * Returns the weekday foreground. + * + * @return Color the weekday foreground. + */ + public Color getWeekdayForeground() { + return dayChooser.getWeekdayForeground(); + } + + /** + * Sets the Sunday foreground. + * + * @param sundayForeground + * the sundayForeground to set + */ + public void setSundayForeground(Color sundayForeground) { + dayChooser.setSundayForeground(sundayForeground); + } + + /** + * Sets the weekday foreground. + * + * @param weekdayForeground + * the weekdayForeground to set + */ + public void setWeekdayForeground(Color weekdayForeground) { + dayChooser.setWeekdayForeground(weekdayForeground); + } + + /** + * Returns a Date object. + * + * @return a date object constructed from the calendar property. + */ + public Date getDate() { + return new Date(calendar.getTimeInMillis()); + } + + /** + * Sets the date. Fires the property change "date". + * + * @param date + * the new date. + * @throws NullPointerException - + * if tha date is null + */ + public void setDate(Date date) { + Date oldDate = calendar.getTime(); + calendar.setTime(date); + int year = calendar.get(Calendar.YEAR); + int month = calendar.get(Calendar.MONTH); + int day = calendar.get(Calendar.DAY_OF_MONTH); + + yearChooser.setYear(year); + monthChooser.setMonth(month); + dayChooser.setCalendar(calendar); + dayChooser.setDay(day); + + firePropertyChange("date", oldDate, date); + } + + /** + * Sets a valid date range for selectable dates. If max is before + * min, the default range with no limitation is set. + * + * @param min + * the minimum selectable date or null (then the minimum date is + * set to 01\01\0001) + * @param max + * the maximum selectable date or null (then the maximum date is + * set to 01\01\9999) + */ + public void setSelectableDateRange(Date min, Date max) { + dayChooser.setSelectableDateRange(min, max); + }; + + /** + * Gets the minimum selectable date. + * + * @return the minimum selectable date + */ + public Date getMaxSelectableDate() { + return dayChooser.getMaxSelectableDate(); + } + + /** + * Gets the maximum selectable date. + * + * @return the maximum selectable date + */ + public Date getMinSelectableDate() { + return dayChooser.getMinSelectableDate(); + } + + /** + * Sets the maximum selectable date. + * + * @param max maximum selectable date + */ + public void setMaxSelectableDate(Date max) { + dayChooser.setMaxSelectableDate(max); + } + + /** + * Sets the minimum selectable date. + * + * @param min minimum selectable date + */ + public void setMinSelectableDate(Date min) { + dayChooser.setMinSelectableDate(min); + } + + /** + * Gets the maximum number of characters of a day name or 0. If 0 is + * returned, dateFormatSymbols.getShortWeekdays() will be used. + * + * @return the maximum number of characters of a day name or 0. + */ + public int getMaxDayCharacters() { + return dayChooser.getMaxDayCharacters(); + } + + /** + * Sets the maximum number of characters per day in the day bar. Valid + * values are 0-4. If set to 0, dateFormatSymbols.getShortWeekdays() will be + * used, otherwise theses strings will be reduced to the maximum number of + * characters. + * + * @param maxDayCharacters + * the maximum number of characters of a day name. + */ + public void setMaxDayCharacters(int maxDayCharacters) { + dayChooser.setMaxDayCharacters(maxDayCharacters); + } +} diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/JCalendarBeanInfo.java b/JAVA/LofarUtils/src/com/toedter/calendar/JCalendarBeanInfo.java new file mode 100644 index 0000000000000000000000000000000000000000..a3f25cd5f3204b291d581ad0a20fc307de4292a4 --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/calendar/JCalendarBeanInfo.java @@ -0,0 +1,41 @@ +/* + * JcalendarBeanInfo.java - Bean Info for JCalendar Java Bean + * Copyright (C) 2004 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +package com.toedter.calendar; + +import com.toedter.components.GenericBeanInfo; + + +/** + * A BeanInfo class for JCalendar. + * + * @author Kai Toedter + * @version $LastChangedRevision: 85 $ + * @version $LastChangedDate: 2006-04-28 13:50:52 +0200 (Fr, 28 Apr 2006) $ + */ +public class JCalendarBeanInfo extends GenericBeanInfo { + + /** + * Constructs a new BeanInfo class for the JCalendar bean. + */ + public JCalendarBeanInfo() { + super("JCalendar", true); + } +} \ No newline at end of file diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/JDateChooser.java b/JAVA/LofarUtils/src/com/toedter/calendar/JDateChooser.java new file mode 100644 index 0000000000000000000000000000000000000000..ec31b827fa4b0128a6d0d9f85cbbfe8a288eebd2 --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/calendar/JDateChooser.java @@ -0,0 +1,577 @@ +/* + * JDateChooser.java - A bean for choosing a date + * Copyright (C) 2004 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +package com.toedter.calendar; + +import java.awt.BorderLayout; +import java.awt.Font; +import java.awt.Insets; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.net.URL; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; + +import javax.swing.ImageIcon; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JPopupMenu; +import javax.swing.MenuElement; +import javax.swing.MenuSelectionManager; +import javax.swing.SwingUtilities; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; + +/** + * A date chooser containig a date editor and a button, that makes a JCalendar + * visible for choosing a date. If no date editor is specified, a + * JTextFieldDateEditor is used as default. + * + * @author Kai Toedter + * @version $LastChangedRevision: 101 $ + * @version $LastChangedDate: 2006-06-04 14:42:29 +0200 (So, 04 Jun 2006) $ + */ +public class JDateChooser extends JPanel implements ActionListener, + PropertyChangeListener { + + private static final long serialVersionUID = -4306412745720670722L; + + protected IDateEditor dateEditor; + + protected JButton calendarButton; + + protected JCalendar jcalendar; + + protected JPopupMenu popup; + + protected boolean isInitialized; + + protected boolean dateSelected; + + protected Date lastSelectedDate; + + private ChangeListener changeListener; + + /** + * Creates a new JDateChooser. By default, no date is set and the textfield + * is empty. + */ + public JDateChooser() { + this(null, null, null, null); + } + + /** + * Creates a new JDateChooser with given IDateEditor. + * + * @param dateEditor + * the dateEditor to be used used to display the date. if null, a + * JTextFieldDateEditor is used. + */ + public JDateChooser(IDateEditor dateEditor) { + this(null, null, null, dateEditor); + } + + /** + * Creates a new JDateChooser. + * + * @param date + * the date or null + */ + public JDateChooser(Date date) { + this(date, null); + } + + /** + * Creates a new JDateChooser. + * + * @param date + * the date or null + * @param dateFormatString + * the date format string or null (then MEDIUM SimpleDateFormat + * format is used) + */ + public JDateChooser(Date date, String dateFormatString) { + this(date, dateFormatString, null); + } + + /** + * Creates a new JDateChooser. + * + * @param date + * the date or null + * @param dateFormatString + * the date format string or null (then MEDIUM SimpleDateFormat + * format is used) + * @param dateEditor + * the dateEditor to be used used to display the date. if null, a + * JTextFieldDateEditor is used. + */ + public JDateChooser(Date date, String dateFormatString, + IDateEditor dateEditor) { + this(null, date, dateFormatString, dateEditor); + } + + /** + * Creates a new JDateChooser. If the JDateChooser is created with this + * constructor, the mask will be always visible in the date editor. Please + * note that the date pattern and the mask will not be changed if the locale + * of the JDateChooser is changed. + * + * @param datePattern + * the date pattern, e.g. "MM/dd/yy" + * @param maskPattern + * the mask pattern, e.g. "##/##/##" + * @param placeholder + * the placeholer charachter, e.g. '_' + */ + public JDateChooser(String datePattern, String maskPattern, char placeholder) { + this(null, null, datePattern, new JTextFieldDateEditor(datePattern, + maskPattern, placeholder)); + } + + /** + * Creates a new JDateChooser. + * + * @param jcal + * the JCalendar to be used + * @param date + * the date or null + * @param dateFormatString + * the date format string or null (then MEDIUM Date format is + * used) + * @param dateEditor + * the dateEditor to be used used to display the date. if null, a + * JTextFieldDateEditor is used. + */ + public JDateChooser(JCalendar jcal, Date date, String dateFormatString, + IDateEditor dateEditor) { + setName("JDateChooser"); + + this.dateEditor = dateEditor; + if (this.dateEditor == null) { + this.dateEditor = new JTextFieldDateEditor(); + } + this.dateEditor.addPropertyChangeListener("date", this); + + if (jcal == null) { + jcalendar = new JCalendar(date); + } else { + jcalendar = jcal; + if (date != null) { + jcalendar.setDate(date); + } + } + + setLayout(new BorderLayout()); + + jcalendar.getDayChooser().addPropertyChangeListener("day", this); + // always fire"day" property even if the user selects + // the already selected day again + jcalendar.getDayChooser().setAlwaysFireDayProperty(true); + + setDateFormatString(dateFormatString); + setDate(date); + + // Display a calendar button with an icon + URL iconURL = getClass().getResource( + "/com/toedter/calendar/images/JDateChooserIcon.gif"); + ImageIcon icon = new ImageIcon(iconURL); + + calendarButton = new JButton(icon) { + private static final long serialVersionUID = -1913767779079949668L; + + public boolean isFocusable() { + return false; + } + }; + calendarButton.setMargin(new Insets(0, 0, 0, 0)); + calendarButton.addActionListener(this); + + // Alt + 'C' selects the calendar. + calendarButton.setMnemonic(KeyEvent.VK_C); + + add(calendarButton, BorderLayout.EAST); + add(this.dateEditor.getUiComponent(), BorderLayout.CENTER); + + calendarButton.setMargin(new Insets(0, 0, 0, 0)); + // calendarButton.addFocusListener(this); + + popup = new JPopupMenu() { + private static final long serialVersionUID = -6078272560337577761L; + + public void setVisible(boolean b) { + Boolean isCanceled = (Boolean) getClientProperty("JPopupMenu.firePopupMenuCanceled"); + if (b + || (!b && dateSelected) + || ((isCanceled != null) && !b && isCanceled + .booleanValue())) { + super.setVisible(b); + } + } + }; + + popup.setLightWeightPopupEnabled(true); + + popup.add(jcalendar); + + lastSelectedDate = date; + + // Corrects a problem that occured when the JMonthChooser's combobox is + // displayed, and a click outside the popup does not close it. + + // The following idea was originally provided by forum user + // podiatanapraia: + changeListener = new ChangeListener() { + boolean hasListened = false; + + public void stateChanged(ChangeEvent e) { + if (hasListened) { + hasListened = false; + return; + } + if (popup.isVisible() + && JDateChooser.this.jcalendar.monthChooser + .getComboBox().hasFocus()) { + MenuElement[] me = MenuSelectionManager.defaultManager() + .getSelectedPath(); + MenuElement[] newMe = new MenuElement[me.length + 1]; + newMe[0] = popup; + for (int i = 0; i < me.length; i++) { + newMe[i + 1] = me[i]; + } + hasListened = true; + MenuSelectionManager.defaultManager() + .setSelectedPath(newMe); + } + } + }; + MenuSelectionManager.defaultManager().addChangeListener(changeListener); + // end of code provided by forum user podiatanapraia + + isInitialized = true; + } + + /** + * Called when the jalendar button was pressed. + * + * @param e + * the action event + */ + public void actionPerformed(ActionEvent e) { + int x = calendarButton.getWidth() + - (int) popup.getPreferredSize().getWidth(); + int y = calendarButton.getY() + calendarButton.getHeight(); + + Calendar calendar = Calendar.getInstance(); + Date date = dateEditor.getDate(); + if (date != null) { + calendar.setTime(date); + } + jcalendar.setCalendar(calendar); + popup.show(calendarButton, x, y); + dateSelected = false; + } + + /** + * Listens for a "date" property change or a "day" property change event + * from the JCalendar. Updates the date editor and closes the popup. + * + * @param evt + * the event + */ + public void propertyChange(PropertyChangeEvent evt) { + if (evt.getPropertyName().equals("day")) { + if (popup.isVisible()) { + dateSelected = true; + popup.setVisible(false); + setDate(jcalendar.getCalendar().getTime()); + } + } else if (evt.getPropertyName().equals("date")) { + if (evt.getSource() == dateEditor) { + firePropertyChange("date", evt.getOldValue(), evt.getNewValue()); + } else { + setDate((Date) evt.getNewValue()); + } + } + } + + /** + * Updates the UI of itself and the popup. + */ + public void updateUI() { + super.updateUI(); + setEnabled(isEnabled()); + + if (jcalendar != null) { + SwingUtilities.updateComponentTreeUI(popup); + } + } + + /** + * Sets the locale. + * + * @param l + * The new locale value + */ + public void setLocale(Locale l) { + super.setLocale(l); + dateEditor.setLocale(l); + jcalendar.setLocale(l); + } + + /** + * Gets the date format string. + * + * @return Returns the dateFormatString. + */ + public String getDateFormatString() { + return dateEditor.getDateFormatString(); + } + + /** + * Sets the date format string. E.g "MMMMM d, yyyy" will result in "July 21, + * 2004" if this is the selected date and locale is English. + * + * @param dfString + * The dateFormatString to set. + */ + public void setDateFormatString(String dfString) { + dateEditor.setDateFormatString(dfString); + invalidate(); + } + + /** + * Returns the date. If the JDateChooser is started with a null date and no + * date was set by the user, null is returned. + * + * @return the current date + */ + public Date getDate() { + return dateEditor.getDate(); + } + + /** + * Sets the date. Fires the property change "date" if date != null. + * + * @param date + * the new date. + */ + public void setDate(Date date) { + dateEditor.setDate(date); + if (getParent() != null) { + getParent().invalidate(); + } + } + + /** + * Returns the calendar. If the JDateChooser is started with a null date (or + * null calendar) and no date was set by the user, null is returned. + * + * @return the current calendar + */ + public Calendar getCalendar() { + Date date = getDate(); + if (date == null) { + return null; + } + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); + return calendar; + } + + /** + * Sets the calendar. Value null will set the null date on the date editor. + * + * @param calendar + * the calendar. + */ + public void setCalendar(Calendar calendar) { + if (calendar == null) { + dateEditor.setDate(null); + } else { + dateEditor.setDate(calendar.getTime()); + } + } + + /** + * Enable or disable the JDateChooser. + * + * @param enabled + * the new enabled value + */ + public void setEnabled(boolean enabled) { + super.setEnabled(enabled); + if (dateEditor != null) { + dateEditor.setEnabled(enabled); + calendarButton.setEnabled(enabled); + } + } + + /** + * Returns true, if enabled. + * + * @return true, if enabled. + */ + public boolean isEnabled() { + return super.isEnabled(); + } + + /** + * Sets the icon of the buuton. + * + * @param icon + * The new icon + */ + public void setIcon(ImageIcon icon) { + calendarButton.setIcon(icon); + } + + /** + * Sets the font of all subcomponents. + * + * @param font + * the new font + */ + public void setFont(Font font) { + if (isInitialized) { + dateEditor.getUiComponent().setFont(font); + jcalendar.setFont(font); + } + super.setFont(font); + } + + /** + * Returns the JCalendar component. THis is usefull if you want to set some + * properties. + * + * @return the JCalendar + */ + public JCalendar getJCalendar() { + return jcalendar; + } + + /** + * Returns the calendar button. + * + * @return the calendar button + */ + public JButton getCalendarButton() { + return calendarButton; + } + + /** + * Returns the date editor. + * + * @return the date editor + */ + public IDateEditor getDateEditor() { + return dateEditor; + } + + /** + * Sets a valid date range for selectable dates. If max is before min, the + * default range with no limitation is set. + * + * @param min + * the minimum selectable date or null (then the minimum date is + * set to 01\01\0001) + * @param max + * the maximum selectable date or null (then the maximum date is + * set to 01\01\9999) + */ + public void setSelectableDateRange(Date min, Date max) { + jcalendar.setSelectableDateRange(min, max); + dateEditor.setSelectableDateRange(jcalendar.getMinSelectableDate(), + jcalendar.getMaxSelectableDate()); + } + + public void setMaxSelectableDate(Date max) { + jcalendar.setMaxSelectableDate(max); + dateEditor.setMaxSelectableDate(max); + } + + public void setMinSelectableDate(Date min) { + jcalendar.setMinSelectableDate(min); + dateEditor.setMinSelectableDate(min); + } + + /** + * Gets the maximum selectable date. + * + * @return the maximum selectable date + */ + public Date getMaxSelectableDate() { + return jcalendar.getMaxSelectableDate(); + } + + /** + * Gets the minimum selectable date. + * + * @return the minimum selectable date + */ + public Date getMinSelectableDate() { + return jcalendar.getMinSelectableDate(); + } + + /** + * Should only be invoked if the JDateChooser is not used anymore. Due to popup + * handling it had to register a change listener to the default menu + * selection manager which will be unregistered here. Use this method to + * cleanup possible memory leaks. + */ + public void cleanup() { + MenuSelectionManager.defaultManager().removeChangeListener(changeListener); + changeListener = null; + } + + /** + * Creates a JFrame with a JDateChooser inside and can be used for testing. + * + * @param s + * The command line arguments + */ + public static void main(String[] s) { + JFrame frame = new JFrame("JDateChooser"); + JDateChooser dateChooser = new JDateChooser(); + // JDateChooser dateChooser = new JDateChooser(null, new Date(), null, + // null); + // dateChooser.setLocale(new Locale("de")); + // dateChooser.setDateFormatString("dd. MMMM yyyy"); + + // dateChooser.setPreferredSize(new Dimension(130, 20)); + // dateChooser.setFont(new Font("Verdana", Font.PLAIN, 10)); + // dateChooser.setDateFormatString("yyyy-MM-dd HH:mm"); + + // URL iconURL = dateChooser.getClass().getResource( + // "/com/toedter/calendar/images/JMonthChooserColor32.gif"); + // ImageIcon icon = new ImageIcon(iconURL); + // dateChooser.setIcon(icon); + + frame.getContentPane().add(dateChooser); + frame.pack(); + frame.setVisible(true); + } + +} diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/JDateChooserBeanInfo.java b/JAVA/LofarUtils/src/com/toedter/calendar/JDateChooserBeanInfo.java new file mode 100644 index 0000000000000000000000000000000000000000..e6fd7db8434aae66a82d6a2598205f470d12c53b --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/calendar/JDateChooserBeanInfo.java @@ -0,0 +1,39 @@ +/* + * DateChooserPanelBeanInfo.java - Bean Info for JDateChooser + * Copyright (C) 2006 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +package com.toedter.calendar; + +import com.toedter.components.GenericBeanInfo; + +/** + * A BeanInfo class for the JDateChooser panel. + * + * @author Kai Toedter + * @version $LastChangedRevision: 85 $ + * @version $LastChangedDate: 2006-04-28 13:50:52 +0200 (Fr, 28 Apr 2006) $ + */ +public class JDateChooserBeanInfo extends GenericBeanInfo { + /** + * Constructs a new BeanInfo class for the JDateChooser bean. + */ + public JDateChooserBeanInfo() { + super("JDateChooser", true); + } +} \ No newline at end of file diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/JDateChooserCellEditor.java b/JAVA/LofarUtils/src/com/toedter/calendar/JDateChooserCellEditor.java new file mode 100644 index 0000000000000000000000000000000000000000..7303b93d739c65e585f82fa3d2bb08f779df50d8 --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/calendar/JDateChooserCellEditor.java @@ -0,0 +1,60 @@ +/* + * JDateChooserCellEditor.java - A CellEditor for tables, using a JDateChooser + * Copyright (C) 2005 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +package com.toedter.calendar; + +import java.awt.Component; +import java.util.Date; + +import javax.swing.AbstractCellEditor; +import javax.swing.JTable; +import javax.swing.table.TableCellEditor; + +/** + * A CellEditor for tables, using a JDateChooser. + * + * @author Kai Toedter + * @version $LastChangedRevision: 100 $ + * @version $LastChangedDate: 2006-06-04 14:36:06 +0200 (So, 04 Jun 2006) $ + */ +public class JDateChooserCellEditor extends AbstractCellEditor implements + TableCellEditor { + + private static final long serialVersionUID = 917881575221755609L; + + private JDateChooser dateChooser = new JDateChooser(); + + public Component getTableCellEditorComponent(JTable table, Object value, + boolean isSelected, int row, int column) { + + Date date = null; + if (value instanceof Date) + date = (Date) value; + + dateChooser.setDate(date); + + return dateChooser; + } + + public Object getCellEditorValue() { + return dateChooser.getDate(); + } +} \ No newline at end of file diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/JDayChooser.java b/JAVA/LofarUtils/src/com/toedter/calendar/JDayChooser.java new file mode 100644 index 0000000000000000000000000000000000000000..241f09a26b70707a985462fc9808e9cad577b30a --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/calendar/JDayChooser.java @@ -0,0 +1,1052 @@ +/* + * JDayChooser.java - A bean for choosing a day + * Copyright (C) 2004 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +package com.toedter.calendar; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Font; +import java.awt.Graphics; +import java.awt.GridLayout; +import java.awt.Insets; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.FocusEvent; +import java.awt.event.FocusListener; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.awt.event.MouseListener; + +import java.text.DateFormatSymbols; + +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.UIManager; + +/** + * JDayChooser is a bean for choosing a day. + * + * @author Kai Toedter + * @version $LastChangedRevision: 104 $ + * @version $LastChangedDate: 2006-06-04 15:20:45 +0200 (So, 04 Jun 2006) $ + */ +public class JDayChooser extends JPanel implements ActionListener, KeyListener, FocusListener { + private static final long serialVersionUID = 5876398337018781820L; + + protected JButton[] days; + + protected JButton[] weeks; + + protected JButton selectedDay; + + protected JPanel weekPanel; + + protected JPanel dayPanel; + + protected int day; + + protected Color oldDayBackgroundColor; + + protected Color selectedColor; + + protected Color sundayForeground; + + protected Color weekdayForeground; + + protected Color decorationBackgroundColor; + + protected String[] dayNames; + + protected Calendar calendar; + + protected Calendar today; + + protected Locale locale; + + protected boolean initialized; + + protected boolean weekOfYearVisible; + + protected boolean decorationBackgroundVisible = true; + + protected boolean decorationBordersVisible; + + protected boolean dayBordersVisible; + + private boolean alwaysFireDayProperty; + + protected Date minSelectableDate; + + protected Date maxSelectableDate; + + protected Date defaultMinSelectableDate; + + protected Date defaultMaxSelectableDate; + + protected int maxDayCharacters; + + /** + * Default JDayChooser constructor. + */ + public JDayChooser() { + this(false); + } + + /** + * JDayChooser constructor. + * + * @param weekOfYearVisible + * true, if the weeks of a year shall be shown + */ + public JDayChooser(boolean weekOfYearVisible) { + setName("JDayChooser"); + setBackground(Color.blue); + this.weekOfYearVisible = weekOfYearVisible; + locale = Locale.getDefault(); + days = new JButton[49]; + selectedDay = null; + calendar = Calendar.getInstance(locale); + today = (Calendar) calendar.clone(); + + setLayout(new BorderLayout()); + + dayPanel = new JPanel(); + dayPanel.setLayout(new GridLayout(7, 7)); + + sundayForeground = new Color(164, 0, 0); + weekdayForeground = new Color(0, 90, 164); + + // decorationBackgroundColor = new Color(194, 211, 252); + // decorationBackgroundColor = new Color(206, 219, 246); + decorationBackgroundColor = new Color(210, 228, 238); + + for (int y = 0; y < 7; y++) { + for (int x = 0; x < 7; x++) { + int index = x + (7 * y); + + if (y == 0) { + // Create a button that doesn't react on clicks or focus + // changes. + // Thanks to Thomas Schaefer for the focus hint :) + days[index] = new DecoratorButton(); + } else { + days[index] = new JButton("x") { + private static final long serialVersionUID = -7433645992591669725L; + + public void paint(Graphics g) { + if ("Windows".equals(UIManager.getLookAndFeel().getID())) { + // this is a hack to get the background painted + // when using Windows Look & Feel + if (selectedDay == this) { + g.setColor(selectedColor); + g.fillRect(0, 0, getWidth(), getHeight()); + } + } + super.paint(g); + } + + }; + days[index].addActionListener(this); + days[index].addKeyListener(this); + days[index].addFocusListener(this); + } + + days[index].setMargin(new Insets(0, 0, 0, 0)); + days[index].setFocusPainted(false); + dayPanel.add(days[index]); + } + } + + weekPanel = new JPanel(); + weekPanel.setLayout(new GridLayout(7, 1)); + weeks = new JButton[7]; + + for (int i = 0; i < 7; i++) { + weeks[i] = new DecoratorButton(); + weeks[i].setMargin(new Insets(0, 0, 0, 0)); + weeks[i].setFocusPainted(false); + weeks[i].setForeground(new Color(100, 100, 100)); + + if (i != 0) { + weeks[i].setText("0" + (i + 1)); + } + + weekPanel.add(weeks[i]); + } + + Calendar tmpCalendar = Calendar.getInstance(); + tmpCalendar.set(1, 0, 1, 1, 1); + defaultMinSelectableDate = tmpCalendar.getTime(); + minSelectableDate = defaultMinSelectableDate; + tmpCalendar.set(9999, 0, 1, 1, 1); + defaultMaxSelectableDate = tmpCalendar.getTime(); + maxSelectableDate = defaultMaxSelectableDate; + + init(); + + setDay(Calendar.getInstance().get(Calendar.DAY_OF_MONTH)); + add(dayPanel, BorderLayout.CENTER); + + if (weekOfYearVisible) { + add(weekPanel, BorderLayout.WEST); + } + initialized = true; + updateUI(); + } + + /** + * Initilizes the locale specific names for the days of the week. + */ + protected void init() { + JButton testButton = new JButton(); + oldDayBackgroundColor = testButton.getBackground(); + selectedColor = new Color(160, 160, 160); + + Date date = calendar.getTime(); + calendar = Calendar.getInstance(locale); + calendar.setTime(date); + + drawDayNames(); + drawDays(); + } + + /** + * Draws the day names of the day columnes. + */ + private void drawDayNames() { + int firstDayOfWeek = calendar.getFirstDayOfWeek(); + DateFormatSymbols dateFormatSymbols = new DateFormatSymbols(locale); + dayNames = dateFormatSymbols.getShortWeekdays(); + + int day = firstDayOfWeek; + + for (int i = 0; i < 7; i++) { + if (maxDayCharacters > 0 && maxDayCharacters < 5) { + if (dayNames[day].length() >= maxDayCharacters) { + dayNames[day] = dayNames[day].substring(0, maxDayCharacters); + } + } + + days[i].setText(dayNames[day]); + + if (day == 1) { + days[i].setForeground(sundayForeground); + } else { + days[i].setForeground(weekdayForeground); + } + + if (day < 7) { + day++; + } else { + day -= 6; + } + } + } + + /** + * Initializes both day names and weeks of the year. + */ + protected void initDecorations() { + for (int x = 0; x < 7; x++) { + days[x].setContentAreaFilled(decorationBackgroundVisible); + days[x].setBorderPainted(decorationBordersVisible); + days[x].invalidate(); + days[x].repaint(); + weeks[x].setContentAreaFilled(decorationBackgroundVisible); + weeks[x].setBorderPainted(decorationBordersVisible); + weeks[x].invalidate(); + weeks[x].repaint(); + } + } + + /** + * Hides and shows the week buttons. + */ + protected void drawWeeks() { + Calendar tmpCalendar = (Calendar) calendar.clone(); + + for (int i = 1; i < 7; i++) { + tmpCalendar.set(Calendar.DAY_OF_MONTH, (i * 7) - 6); + + int week = tmpCalendar.get(Calendar.WEEK_OF_YEAR); + String buttonText = Integer.toString(week); + + if (week < 10) { + buttonText = "0" + buttonText; + } + + weeks[i].setText(buttonText); + + if ((i == 5) || (i == 6)) { + weeks[i].setVisible(days[i * 7].isVisible()); + } + } + } + + /** + * Hides and shows the day buttons. + */ + protected void drawDays() { + Calendar tmpCalendar = (Calendar) calendar.clone(); + tmpCalendar.set(Calendar.HOUR_OF_DAY, 0); + tmpCalendar.set(Calendar.MINUTE, 0); + tmpCalendar.set(Calendar.SECOND, 0); + tmpCalendar.set(Calendar.MILLISECOND, 0); + + Calendar minCal = Calendar.getInstance(); + minCal.setTime(minSelectableDate); + minCal.set(Calendar.HOUR_OF_DAY, 0); + minCal.set(Calendar.MINUTE, 0); + minCal.set(Calendar.SECOND, 0); + minCal.set(Calendar.MILLISECOND, 0); + + Calendar maxCal = Calendar.getInstance(); + maxCal.setTime(maxSelectableDate); + maxCal.set(Calendar.HOUR_OF_DAY, 0); + maxCal.set(Calendar.MINUTE, 0); + maxCal.set(Calendar.SECOND, 0); + maxCal.set(Calendar.MILLISECOND, 0); + + int firstDayOfWeek = tmpCalendar.getFirstDayOfWeek(); + tmpCalendar.set(Calendar.DAY_OF_MONTH, 1); + + int firstDay = tmpCalendar.get(Calendar.DAY_OF_WEEK) - firstDayOfWeek; + + if (firstDay < 0) { + firstDay += 7; + } + + int i; + + for (i = 0; i < firstDay; i++) { + days[i + 7].setVisible(false); + days[i + 7].setText(""); + } + + tmpCalendar.add(Calendar.MONTH, 1); + + Date firstDayInNextMonth = tmpCalendar.getTime(); + tmpCalendar.add(Calendar.MONTH, -1); + + Date day = tmpCalendar.getTime(); + int n = 0; + Color foregroundColor = getForeground(); + + while (day.before(firstDayInNextMonth)) { + days[i + n + 7].setText(Integer.toString(n + 1)); + days[i + n + 7].setVisible(true); + + if ((tmpCalendar.get(Calendar.DAY_OF_YEAR) == today.get(Calendar.DAY_OF_YEAR)) + && (tmpCalendar.get(Calendar.YEAR) == today.get(Calendar.YEAR))) { + days[i + n + 7].setForeground(sundayForeground); + } else { + days[i + n + 7].setForeground(foregroundColor); + } + + if ((n + 1) == this.day) { + days[i + n + 7].setBackground(selectedColor); + selectedDay = days[i + n + 7]; + } else { + days[i + n + 7].setBackground(oldDayBackgroundColor); + } + + if (tmpCalendar.before(minCal) || tmpCalendar.after(maxCal)) { + days[i + n + 7].setEnabled(false); + } else { + days[i + n + 7].setEnabled(true); + } + + n++; + tmpCalendar.add(Calendar.DATE, 1); + day = tmpCalendar.getTime(); + } + + for (int k = n + i + 7; k < 49; k++) { + days[k].setVisible(false); + days[k].setText(""); + } + + drawWeeks(); + } + + /** + * Returns the locale. + * + * @return the locale value + * + * @see #setLocale + */ + public Locale getLocale() { + return locale; + } + + /** + * Sets the locale. + * + * @param locale + * the new locale value + * + * @see #getLocale + */ + public void setLocale(Locale locale) { + if (!initialized) { + super.setLocale(locale); + } else { + this.locale = locale; + super.setLocale(locale); + init(); + } + } + + /** + * Sets the day. This is a bound property. + * + * @param d + * the day + * + * @see #getDay + */ + public void setDay(int d) { + if (d < 1) { + d = 1; + } + Calendar tmpCalendar = (Calendar) calendar.clone(); + tmpCalendar.set(Calendar.DAY_OF_MONTH, 1); + tmpCalendar.add(Calendar.MONTH, 1); + tmpCalendar.add(Calendar.DATE, -1); + + int maxDaysInMonth = tmpCalendar.get(Calendar.DATE); + + if (d > maxDaysInMonth) { + d = maxDaysInMonth; + } + + int oldDay = day; + day = d; + + if (selectedDay != null) { + selectedDay.setBackground(oldDayBackgroundColor); + selectedDay.repaint(); + } + + for (int i = 7; i < 49; i++) { + if (days[i].getText().equals(Integer.toString(day))) { + selectedDay = days[i]; + selectedDay.setBackground(selectedColor); + break; + } + } + + if (alwaysFireDayProperty) { + firePropertyChange("day", 0, day); + } else { + firePropertyChange("day", oldDay, day); + } + } + + /** + * this is needed for JDateChooser. + * + * @param alwaysFire + * true, if day property shall be fired every time a day is + * chosen. + */ + public void setAlwaysFireDayProperty(boolean alwaysFire) { + alwaysFireDayProperty = alwaysFire; + } + + /** + * Returns the selected day. + * + * @return the day value + * + * @see #setDay + */ + public int getDay() { + return day; + } + + /** + * Sets a specific month. This is needed for correct graphical + * representation of the days. + * + * @param month + * the new month + */ + public void setMonth(int month) { + int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); + calendar.set(Calendar.MONTH, month); + if(maxDays == day) { + day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); + } + + // Since the day does not change, + // don't fire a day property change, even if alwaysFireDayProperty is + // true :) + boolean storedMode = alwaysFireDayProperty; + alwaysFireDayProperty = false; + setDay(day); + alwaysFireDayProperty = storedMode; + + drawDays(); + } + + /** + * Sets a specific year. This is needed for correct graphical representation + * of the days. + * + * @param year + * the new year + */ + public void setYear(int year) { + calendar.set(Calendar.YEAR, year); + drawDays(); + } + + /** + * Sets a specific calendar. This is needed for correct graphical + * representation of the days. + * + * @param calendar + * the new calendar + */ + public void setCalendar(Calendar calendar) { + this.calendar = calendar; + drawDays(); + } + + /** + * Sets the font property. + * + * @param font + * the new font + */ + public void setFont(Font font) { + if (days != null) { + for (int i = 0; i < 49; i++) { + days[i].setFont(font); + } + } + if (weeks != null) { + for (int i = 0; i < 7; i++) { + weeks[i].setFont(font); + } + } + } + + /** + * Sets the foregroundColor color. + * + * @param foreground + * the new foregroundColor + */ + public void setForeground(Color foreground) { + super.setForeground(foreground); + + if (days != null) { + for (int i = 7; i < 49; i++) { + days[i].setForeground(foreground); + } + + drawDays(); + } + } + + /** + * JDayChooser is the ActionListener for all day buttons. + * + * @param e + * the ActionEvent + */ + public void actionPerformed(ActionEvent e) { + JButton button = (JButton) e.getSource(); + String buttonText = button.getText(); + int day = new Integer(buttonText).intValue(); + setDay(day); + } + + /** + * JDayChooser is the FocusListener for all day buttons. (Added by Thomas + * Schaefer) + * + * @param e + * the FocusEvent + */ + /* + * Code below commented out by Mark Brown on 24 Aug 2004. This code breaks + * the JDateChooser code by triggering the actionPerformed method on the + * next day button. This causes the date chosen to always be incremented by + * one day. + */ + public void focusGained(FocusEvent e) { + // JButton button = (JButton) e.getSource(); + // String buttonText = button.getText(); + // + // if ((buttonText != null) && !buttonText.equals("") && + // !e.isTemporary()) { + // actionPerformed(new ActionEvent(e.getSource(), 0, null)); + // } + } + + /** + * Does nothing. + * + * @param e + * the FocusEvent + */ + public void focusLost(FocusEvent e) { + } + + /** + * JDayChooser is the KeyListener for all day buttons. (Added by Thomas + * Schaefer and modified by Austin Moore) + * + * @param e + * the KeyEvent + */ + public void keyPressed(KeyEvent e) { + int offset = (e.getKeyCode() == KeyEvent.VK_UP) ? (-7) + : ((e.getKeyCode() == KeyEvent.VK_DOWN) ? (+7) + : ((e.getKeyCode() == KeyEvent.VK_LEFT) ? (-1) + : ((e.getKeyCode() == KeyEvent.VK_RIGHT) ? (+1) : 0))); + + int newDay = getDay() + offset; + + if ((newDay >= 1) && (newDay <= calendar.getMaximum(Calendar.DAY_OF_MONTH))) { + setDay(newDay); + } + } + + /** + * Does nothing. + * + * @param e + * the KeyEvent + */ + public void keyTyped(KeyEvent e) { + } + + /** + * Does nothing. + * + * @param e + * the KeyEvent + */ + public void keyReleased(KeyEvent e) { + } + + /** + * Enable or disable the JDayChooser. + * + * @param enabled + * The new enabled value + */ + public void setEnabled(boolean enabled) { + super.setEnabled(enabled); + + for (short i = 0; i < days.length; i++) { + if (days[i] != null) { + days[i].setEnabled(enabled); + } + } + + for (short i = 0; i < weeks.length; i++) { + if (weeks[i] != null) { + weeks[i].setEnabled(enabled); + } + } + } + + /** + * In some Countries it is often usefull to know in which week of the year a + * date is. + * + * @return boolean true, if the weeks of the year is shown + */ + public boolean isWeekOfYearVisible() { + return weekOfYearVisible; + } + + /** + * In some Countries it is often usefull to know in which week of the year a + * date is. + * + * @param weekOfYearVisible + * true, if the weeks of the year shall be shown + */ + public void setWeekOfYearVisible(boolean weekOfYearVisible) { + if (weekOfYearVisible == this.weekOfYearVisible) { + return; + } else if (weekOfYearVisible) { + add(weekPanel, BorderLayout.WEST); + } else { + remove(weekPanel); + } + + this.weekOfYearVisible = weekOfYearVisible; + validate(); + dayPanel.validate(); + } + + /** + * Returns the day panel. + * + * @return the day panel + */ + public JPanel getDayPanel() { + return dayPanel; + } + + /** + * Returns the color of the decoration (day names and weeks). + * + * @return the color of the decoration (day names and weeks). + */ + public Color getDecorationBackgroundColor() { + return decorationBackgroundColor; + } + + /** + * Sets the background of days and weeks of year buttons. + * + * @param decorationBackgroundColor + * The background to set + */ + public void setDecorationBackgroundColor(Color decorationBackgroundColor) { + this.decorationBackgroundColor = decorationBackgroundColor; + + if (days != null) { + for (int i = 0; i < 7; i++) { + days[i].setBackground(decorationBackgroundColor); + } + } + + if (weeks != null) { + for (int i = 0; i < 7; i++) { + weeks[i].setBackground(decorationBackgroundColor); + } + } + } + + /** + * Returns the Sunday foreground. + * + * @return Color the Sunday foreground. + */ + public Color getSundayForeground() { + return sundayForeground; + } + + /** + * Returns the weekday foreground. + * + * @return Color the weekday foreground. + */ + public Color getWeekdayForeground() { + return weekdayForeground; + } + + /** + * Sets the Sunday foreground. + * + * @param sundayForeground + * The sundayForeground to set + */ + public void setSundayForeground(Color sundayForeground) { + this.sundayForeground = sundayForeground; + drawDayNames(); + drawDays(); + } + + /** + * Sets the weekday foreground. + * + * @param weekdayForeground + * The weekdayForeground to set + */ + public void setWeekdayForeground(Color weekdayForeground) { + this.weekdayForeground = weekdayForeground; + drawDayNames(); + drawDays(); + } + + /** + * Requests that the selected day also have the focus. + */ + public void setFocus() { + if (selectedDay != null) { + this.selectedDay.requestFocus(); + } + } + + /** + * The decoration background is the background color of the day titles and + * the weeks of the year. + * + * @return Returns true, if the decoration background is painted. + */ + public boolean isDecorationBackgroundVisible() { + return decorationBackgroundVisible; + } + + /** + * The decoration background is the background color of the day titles and + * the weeks of the year. + * + * @param decorationBackgroundVisible + * true, if the decoration background shall be painted. + */ + public void setDecorationBackgroundVisible(boolean decorationBackgroundVisible) { + this.decorationBackgroundVisible = decorationBackgroundVisible; + initDecorations(); + } + + /** + * The decoration border is the button border of the day titles and the + * weeks of the year. + * + * @return Returns true, if the decoration border is painted. + */ + public boolean isDecorationBordersVisible() { + return decorationBordersVisible; + } + + public boolean isDayBordersVisible() { + return dayBordersVisible; + } + + /** + * The decoration border is the button border of the day titles and the + * weeks of the year. + * + * @param decorationBordersVisible + * true, if the decoration border shall be painted. + */ + public void setDecorationBordersVisible(boolean decorationBordersVisible) { + this.decorationBordersVisible = decorationBordersVisible; + initDecorations(); + } + + public void setDayBordersVisible(boolean dayBordersVisible) { + this.dayBordersVisible = dayBordersVisible; + if (initialized) { + for (int x = 7; x < 49; x++) { + if ("Windows".equals(UIManager.getLookAndFeel().getID())) { + days[x].setContentAreaFilled(dayBordersVisible); + } else { + days[x].setContentAreaFilled(true); + } + days[x].setBorderPainted(dayBordersVisible); + } + } + } + + /** + * Updates the UI and sets the day button preferences. + */ + public void updateUI() { + super.updateUI(); + setFont(Font.decode("Dialog Plain 11")); + + if (weekPanel != null) { + weekPanel.updateUI(); + } + if (initialized) { + if ("Windows".equals(UIManager.getLookAndFeel().getID())) { + setDayBordersVisible(false); + setDecorationBackgroundVisible(true); + setDecorationBordersVisible(false); + } else { + setDayBordersVisible(true); + setDecorationBackgroundVisible(decorationBackgroundVisible); + setDecorationBordersVisible(decorationBordersVisible); + } + } + } + + /** + * Sets a valid date range for selectable dates. If max is before min, the + * default range with no limitation is set. + * + * @param min + * the minimum selectable date or null (then the minimum date is + * set to 01\01\0001) + * @param max + * the maximum selectable date or null (then the maximum date is + * set to 01\01\9999) + */ + public void setSelectableDateRange(Date min, Date max) { + if (min == null) { + minSelectableDate = defaultMinSelectableDate; + } else { + minSelectableDate = min; + } + if (max == null) { + maxSelectableDate = defaultMaxSelectableDate; + } else { + maxSelectableDate = max; + } + if (maxSelectableDate.before(minSelectableDate)) { + minSelectableDate = defaultMinSelectableDate; + maxSelectableDate = defaultMaxSelectableDate; + } + drawDays(); + } + + /** + * Sets the maximum selectable date. If null, the date 01\01\9999 will be set instead. + * + * @param max the maximum selectable date + * + * @return the maximum selectable date + */ + public Date setMaxSelectableDate(Date max) { + if (max == null) { + maxSelectableDate = defaultMaxSelectableDate; + } else { + maxSelectableDate = max; + } + drawDays(); + return maxSelectableDate; + } + + /** + * Sets the minimum selectable date. If null, the date 01\01\0001 will be set instead. + * + * @param min the minimum selectable date + * + * @return the minimum selectable date + */ + public Date setMinSelectableDate(Date min) { + if (min == null) { + minSelectableDate = defaultMinSelectableDate; + } else { + minSelectableDate = min; + } + drawDays(); + return minSelectableDate; + } + + /** + * Gets the maximum selectable date. + * + * @return the maximum selectable date + */ + public Date getMaxSelectableDate() { + return maxSelectableDate; + } + + /** + * Gets the minimum selectable date. + * + * @return the minimum selectable date + */ + public Date getMinSelectableDate() { + return minSelectableDate; + } + + /** + * Gets the maximum number of characters of a day name or 0. If 0 is + * returned, dateFormatSymbols.getShortWeekdays() will be used. + * + * @return the maximum number of characters of a day name or 0. + */ + public int getMaxDayCharacters() { + return maxDayCharacters; + } + + /** + * Sets the maximum number of characters per day in the day bar. Valid + * values are 0-4. If set to 0, dateFormatSymbols.getShortWeekdays() will be + * used, otherwise theses strings will be reduced to the maximum number of + * characters. + * + * @param maxDayCharacters + * the maximum number of characters of a day name. + */ + public void setMaxDayCharacters(int maxDayCharacters) { + if (maxDayCharacters == this.maxDayCharacters) { + return; + } + + if (maxDayCharacters < 0 || maxDayCharacters > 4) { + this.maxDayCharacters = 0; + } else { + this.maxDayCharacters = maxDayCharacters; + } + drawDayNames(); + drawDays(); + invalidate(); + } + + /** + * Creates a JFrame with a JDayChooser inside and can be used for testing. + * + * @param s + * The command line arguments + */ + public static void main(String[] s) { + JFrame frame = new JFrame("JDayChooser"); + frame.getContentPane().add(new JDayChooser()); + frame.pack(); + frame.setVisible(true); + } + + class DecoratorButton extends JButton { + private static final long serialVersionUID = -5306477668406547496L; + + public DecoratorButton() { + setBackground(decorationBackgroundColor); + setContentAreaFilled(decorationBackgroundVisible); + setBorderPainted(decorationBordersVisible); + } + + public void addMouseListener(MouseListener l) { + } + + public boolean isFocusable() { + return false; + } + + public void paint(Graphics g) { + if ("Windows".equals(UIManager.getLookAndFeel().getID())) { + // this is a hack to get the background painted + // when using Windows Look & Feel + if (decorationBackgroundVisible) { + g.setColor(decorationBackgroundColor); + } else { + g.setColor(days[7].getBackground()); + } + g.fillRect(0, 0, getWidth(), getHeight()); + if (isBorderPainted()) { + setContentAreaFilled(true); + } else { + setContentAreaFilled(false); + } + } + super.paint(g); + } + }; +} diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/JDayChooserBeanInfo.java b/JAVA/LofarUtils/src/com/toedter/calendar/JDayChooserBeanInfo.java new file mode 100644 index 0000000000000000000000000000000000000000..4c459307b0997c5f7ca4e5d7af96a1b63ef9389c --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/calendar/JDayChooserBeanInfo.java @@ -0,0 +1,39 @@ +/* + * JDayChooserBeanInfo.java - Bean Info for JDayChooser + * Copyright (C) 2004 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +package com.toedter.calendar; + +import com.toedter.components.GenericBeanInfo; + +/** + * A BeanInfo class for the JDayChooser bean. + * + * @author Kai Toedter + * @version $LastChangedRevision: 85 $ + * @version $LastChangedDate: 2006-04-28 13:50:52 +0200 (Fr, 28 Apr 2006) $ + */ +public class JDayChooserBeanInfo extends GenericBeanInfo { + /** + * Constructs a new BeanInfo class for the JDayChooser bean. + */ + public JDayChooserBeanInfo() { + super("JDayChooser", true); + } +} \ No newline at end of file diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/JMonthChooser.java b/JAVA/LofarUtils/src/com/toedter/calendar/JMonthChooser.java new file mode 100644 index 0000000000000000000000000000000000000000..bb23009182c40dff23a69be528546a5bb220a103 --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/calendar/JMonthChooser.java @@ -0,0 +1,403 @@ +/* + * JMonthChooser.java - A bean for choosing a month + * Copyright (C) 2004 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +package com.toedter.calendar; + +import java.awt.BorderLayout; +import java.awt.Component; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; +import java.text.DateFormatSymbols; +import java.util.Calendar; +import java.util.Locale; + +import javax.swing.JComboBox; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JSpinner; +import javax.swing.JTextField; +import javax.swing.SpinnerNumberModel; +import javax.swing.UIManager; +import javax.swing.border.EmptyBorder; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; + +/** + * JMonthChooser is a bean for choosing a month. + * + * @author Kai Toedter + * @version $LastChangedRevision: 100 $ + * @version $LastChangedDate: 2006-06-04 14:36:06 +0200 (So, 04 Jun 2006) $ + */ +public class JMonthChooser extends JPanel implements ItemListener, + ChangeListener { + private static final long serialVersionUID = -2028361332231218527L; + + /** true, if the month chooser has a spinner component */ + protected boolean hasSpinner; + + private Locale locale; + + private int month; + + private int oldSpinnerValue = 0; + + // needed for comparison + private JDayChooser dayChooser; + + private JYearChooser yearChooser; + + private JComboBox comboBox; + + private JSpinner spinner; + + private boolean initialized; + + private boolean localInitialize; + + /** + * Default JMonthChooser constructor. + */ + public JMonthChooser() { + this(true); + } + + /** + * JMonthChooser constructor with month spinner parameter. + * + * @param hasSpinner + * true, if the month chooser should have a spinner component + */ + public JMonthChooser(boolean hasSpinner) { + super(); + setName("JMonthChooser"); + this.hasSpinner = hasSpinner; + + setLayout(new BorderLayout()); + + comboBox = new JComboBox(); + comboBox.addItemListener(this); + + // comboBox.addPopupMenuListener(this); + locale = Locale.getDefault(); + initNames(); + + if (hasSpinner) { + spinner = new JSpinner() { + private static final long serialVersionUID = 1L; + + private JTextField textField = new JTextField(); + + public Dimension getPreferredSize() { + Dimension size = super.getPreferredSize(); + return new Dimension(size.width, textField + .getPreferredSize().height); + } + }; + spinner.addChangeListener(this); + spinner.setEditor(comboBox); + comboBox.setBorder(new EmptyBorder(0, 0, 0, 0)); + updateUI(); + + add(spinner, BorderLayout.WEST); + } else { + add(comboBox, BorderLayout.WEST); + } + + initialized = true; + setMonth(Calendar.getInstance().get(Calendar.MONTH)); + } + + /** + * Initializes the locale specific month names. + */ + public void initNames() { + localInitialize = true; + + DateFormatSymbols dateFormatSymbols = new DateFormatSymbols(locale); + String[] monthNames = dateFormatSymbols.getMonths(); + + if (comboBox.getItemCount() == 12) { + comboBox.removeAllItems(); + } + + for (int i = 0; i < 12; i++) { + comboBox.addItem(monthNames[i]); + } + + localInitialize = false; + comboBox.setSelectedIndex(month); + } + + /** + * Is invoked if the state of the spnner changes. + * + * @param e + * the change event. + */ + public void stateChanged(ChangeEvent e) { + SpinnerNumberModel model = (SpinnerNumberModel) ((JSpinner) e + .getSource()).getModel(); + int value = model.getNumber().intValue(); + boolean increase = (value > oldSpinnerValue) ? true : false; + oldSpinnerValue = value; + + int month = getMonth(); + + if (increase) { + month += 1; + + if (month == 12) { + month = 0; + + if (yearChooser != null) { + int year = yearChooser.getYear(); + year += 1; + yearChooser.setYear(year); + } + } + } else { + month -= 1; + + if (month == -1) { + month = 11; + + if (yearChooser != null) { + int year = yearChooser.getYear(); + year -= 1; + yearChooser.setYear(year); + } + } + } + + setMonth(month); + } + + /** + * The ItemListener for the months. + * + * @param e + * the item event + */ + public void itemStateChanged(ItemEvent e) { + if (e.getStateChange() == ItemEvent.SELECTED) { + int index = comboBox.getSelectedIndex(); + + if ((index >= 0) && (index != month)) { + setMonth(index, false); + } + } + } + + /** + * Sets the month attribute of the JMonthChooser object. Fires a property + * change "month". + * + * @param newMonth + * the new month value + * @param select + * true, if the month should be selcted in the combo box. + */ + private void setMonth(int newMonth, boolean select) { + if (!initialized || localInitialize) { + return; + } + + int oldMonth = month; + month = newMonth; + + if (select) { + comboBox.setSelectedIndex(month); + } + + if (dayChooser != null) { + dayChooser.setMonth(month); + } + + firePropertyChange("month", oldMonth, month); + } + + /** + * Sets the month. This is a bound property. Valuse are valid between 0 + * (January) and 11 (December). A value < 0 will be treated as 0, a value > + * 11 will be treated as 11. + * + * @param newMonth + * the new month value + * + * @see #getMonth + */ + public void setMonth(int newMonth) { + if (newMonth < 0 || newMonth == Integer.MIN_VALUE) { + setMonth(0, true); + } else if (newMonth > 11) { + setMonth(11, true); + } else { + setMonth(newMonth, true); + } + } + + /** + * Returns the month. + * + * @return the month value + */ + public int getMonth() { + return month; + } + + /** + * Convenience method set a day chooser. + * + * @param dayChooser + * the day chooser + */ + public void setDayChooser(JDayChooser dayChooser) { + this.dayChooser = dayChooser; + } + + /** + * Convenience method set a year chooser. If set, the spin for the month + * buttons will spin the year as well + * + * @param yearChooser + * the new yearChooser value + */ + public void setYearChooser(JYearChooser yearChooser) { + this.yearChooser = yearChooser; + } + + /** + * Returns the locale. + * + * @return the locale value + * + * @see #setLocale + */ + public Locale getLocale() { + return locale; + } + + /** + * Set the locale and initializes the new month names. + * + * @param l + * the new locale value + * + * @see #getLocale + */ + public void setLocale(Locale l) { + if (!initialized) { + super.setLocale(l); + } else { + locale = l; + initNames(); + } + } + + /** + * Enable or disable the JMonthChooser. + * + * @param enabled + * the new enabled value + */ + public void setEnabled(boolean enabled) { + super.setEnabled(enabled); + comboBox.setEnabled(enabled); + + if (spinner != null) { + spinner.setEnabled(enabled); + } + } + + /** + * Returns the month chooser's comboBox text area (which allow the focus to + * be set to it). + * + * @return the combo box + */ + public Component getComboBox() { + return this.comboBox; + } + + /** + * Returns the month chooser's comboBox bar (which allow the focus to be set + * to it). + * + * @return Component the spinner or null, if the month chooser has no + * spinner + */ + public Component getSpinner() { + // Returns <null> if there is no spinner. + return spinner; + } + + /** + * Returns the type of spinner the month chooser is using. + * + * @return true, if the month chooser has a spinner + */ + public boolean hasSpinner() { + return hasSpinner; + } + + /** + * Sets the font for this component. + * + * @param font the desired <code>Font</code> for this component + */ + public void setFont(Font font) { + if (comboBox != null) { + comboBox.setFont(font); + } + super.setFont(font); + } + + /** + * Updates the UI. + * + * @see javax.swing.JPanel#updateUI() + */ + public void updateUI() { + final JSpinner testSpinner = new JSpinner(); + if (spinner != null) { + if ("Windows".equals(UIManager.getLookAndFeel().getID())) { + spinner.setBorder(testSpinner.getBorder()); + } else { + spinner.setBorder(new EmptyBorder(0, 0, 0, 0)); + } + } + } + + /** + * Creates a JFrame with a JMonthChooser inside and can be used for testing. + * + * @param s + * The command line arguments + */ + public static void main(String[] s) { + JFrame frame = new JFrame("MonthChooser"); + frame.getContentPane().add(new JMonthChooser()); + frame.pack(); + frame.setVisible(true); + } +} \ No newline at end of file diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/JMonthChooserBeanInfo.java b/JAVA/LofarUtils/src/com/toedter/calendar/JMonthChooserBeanInfo.java new file mode 100644 index 0000000000000000000000000000000000000000..715c8d7f03f83fbabcc63fa4c9d5aee82fb3636d --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/calendar/JMonthChooserBeanInfo.java @@ -0,0 +1,39 @@ +/* + * JMonthChooserBeanInfo.java - Bean Info for JMonthChooser + * Copyright (C) 2004 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +package com.toedter.calendar; + +import com.toedter.components.GenericBeanInfo; + +/** + * A BeanInfo class for the JMonthChooser bean. + * + * @author Kai Toedter + * @version $LastChangedRevision: 85 $ + * @version $LastChangedDate: 2006-04-28 13:50:52 +0200 (Fr, 28 Apr 2006) $ + */ +public class JMonthChooserBeanInfo extends GenericBeanInfo { + /** + * Constructs a new BeanInfo class for the JMonthChooser bean. + */ + public JMonthChooserBeanInfo() { + super("JMonthChooser", true); + } +} \ No newline at end of file diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/JSpinnerDateEditor.java b/JAVA/LofarUtils/src/com/toedter/calendar/JSpinnerDateEditor.java new file mode 100644 index 0000000000000000000000000000000000000000..c99a09fc1304a2e9f5d6703915b59475766a665a --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/calendar/JSpinnerDateEditor.java @@ -0,0 +1,190 @@ +package com.toedter.calendar; + +import java.awt.event.FocusEvent; +import java.awt.event.FocusListener; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + +import javax.swing.JComponent; +import javax.swing.JSpinner; +import javax.swing.SpinnerDateModel; +import javax.swing.UIManager; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; + +/** + * JSpinnerDateEditor is a date editor based on a JSpinner. + * + * @author Kai Toedter + * @version $LastChangedRevision: 100 $ + * @version $LastChangedDate: 2006-06-04 14:36:06 +0200 (So, 04 Jun 2006) $ + */ +public class JSpinnerDateEditor extends JSpinner implements IDateEditor, + FocusListener, ChangeListener { + + private static final long serialVersionUID = 5692204052306085316L; + + protected Date date; + + protected String dateFormatString; + + protected SimpleDateFormat dateFormatter; + + public JSpinnerDateEditor() { + super(new SpinnerDateModel()); + dateFormatter = (SimpleDateFormat) DateFormat + .getDateInstance(DateFormat.MEDIUM); + ((JSpinner.DateEditor) getEditor()).getTextField().addFocusListener( + this); + DateUtil dateUtil = new DateUtil(); + setMinSelectableDate(dateUtil.getMinSelectableDate()); + setMaxSelectableDate(dateUtil.getMaxSelectableDate()); + addChangeListener(this); + } + + public Date getDate() { + if (date == null) { + return null; + } + return ((SpinnerDateModel) getModel()).getDate(); + } + + public void setDate(Date date) { + setDate(date, true); + } + + public void setDate(Date date, boolean updateModel) { + Date oldDate = this.date; + this.date = date; + if (date == null) { + ((JSpinner.DateEditor) getEditor()).getFormat().applyPattern(""); + ((JSpinner.DateEditor) getEditor()).getTextField().setText(""); + } else if (updateModel) { + if (dateFormatString != null) { + ((JSpinner.DateEditor) getEditor()).getFormat().applyPattern( + dateFormatString); + } + ((SpinnerDateModel) getModel()).setValue(date); + } + firePropertyChange("date", oldDate, date); + } + + public void setDateFormatString(String dateFormatString) { + try { + dateFormatter.applyPattern(dateFormatString); + } catch (RuntimeException e) { + dateFormatter = (SimpleDateFormat) DateFormat + .getDateInstance(DateFormat.MEDIUM); + dateFormatter.setLenient(false); + } + this.dateFormatString = dateFormatter.toPattern(); + setToolTipText(this.dateFormatString); + + if (date != null) { + ((JSpinner.DateEditor) getEditor()).getFormat().applyPattern( + this.dateFormatString); + } else { + ((JSpinner.DateEditor) getEditor()).getFormat().applyPattern(""); + } + + if (date != null) { + String text = dateFormatter.format(date); + ((JSpinner.DateEditor) getEditor()).getTextField().setText(text); + } + } + + public String getDateFormatString() { + return dateFormatString; + } + + public JComponent getUiComponent() { + return this; + } + + public void setLocale(Locale locale) { + super.setLocale(locale); + dateFormatter = (SimpleDateFormat) DateFormat.getDateInstance( + DateFormat.MEDIUM, locale); + setDateFormatString(dateFormatter.toPattern()); + } + + /* + * (non-Javadoc) + * + * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent) + */ + public void focusLost(FocusEvent focusEvent) { + String text = ((JSpinner.DateEditor) getEditor()).getTextField() + .getText(); + if (text.length() == 0) { + setDate(null); + } + } + + /* + * (non-Javadoc) + * + * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent) + */ + public void focusGained(FocusEvent e) { + } + + /** + * Enables and disabled the compoment. It also fixes the background bug + * 4991597 and sets the background explicitely to a + * TextField.inactiveBackground. + */ + public void setEnabled(boolean b) { + super.setEnabled(b); + if (!b) { + ((JSpinner.DateEditor) getEditor()).getTextField().setBackground( + UIManager.getColor("TextField.inactiveBackground")); + } + } + + /* (non-Javadoc) + * @see com.toedter.calendar.IDateEditor#getMaxSelectableDate() + */ + public Date getMaxSelectableDate() { + return (Date) ((SpinnerDateModel) getModel()).getEnd(); + } + + /** + * @see com.toedter.calendar.IDateEditor#getMinSelectableDate() + */ + public Date getMinSelectableDate() { + return (Date) ((SpinnerDateModel) getModel()).getStart(); + } + + /** + * @see com.toedter.calendar.IDateEditor#setMaxSelectableDate(java.util.Date) + */ + public void setMaxSelectableDate(Date max) { + ((SpinnerDateModel) getModel()).setEnd(max); + } + + /** + * @see com.toedter.calendar.IDateEditor#setMinSelectableDate(java.util.Date) + */ + public void setMinSelectableDate(Date min) { + ((SpinnerDateModel) getModel()).setStart(min); + } + + /** + * @see com.toedter.calendar.IDateEditor#setSelectableDateRange(java.util.Date, java.util.Date) + */ + public void setSelectableDateRange(Date min, Date max) { + setMaxSelectableDate(max); + setMinSelectableDate(min); + } + + /** + * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent) + */ + public void stateChanged(ChangeEvent e) { + setDate(((SpinnerDateModel) getModel()).getDate(), false); + } + +} diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/JTextFieldDateEditor.java b/JAVA/LofarUtils/src/com/toedter/calendar/JTextFieldDateEditor.java new file mode 100644 index 0000000000000000000000000000000000000000..160cd4b8580e8b7105699b1a14524ffbfe49cbaa --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/calendar/JTextFieldDateEditor.java @@ -0,0 +1,460 @@ +/* + * Copyright (C) 2006 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +package com.toedter.calendar; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.FocusEvent; +import java.awt.event.FocusListener; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; + +import javax.swing.JComponent; +import javax.swing.JFormattedTextField; +import javax.swing.JFrame; +import javax.swing.JTextField; +import javax.swing.UIManager; +import javax.swing.event.CaretEvent; +import javax.swing.event.CaretListener; +import javax.swing.text.MaskFormatter; + +/** + * JTextFieldDateEditor is the default editor used by JDateChooser. It is a + * formatted text field, that colores valid dates green/black and invalid dates + * red. The date format patten and mask can be set manually. If not set, the + * MEDIUM pattern of a SimpleDateFormat with regards to the actual locale is + * used. + * + * @author Kai Toedter + * @version $LastChangedRevision: 97 $ + * @version $LastChangedDate: 2006-05-24 17:30:41 +0200 (Mi, 24 Mai 2006) $ + */ +public class JTextFieldDateEditor extends JFormattedTextField implements IDateEditor, + CaretListener, FocusListener, ActionListener { + + private static final long serialVersionUID = -8901842591101625304L; + + protected Date date; + + protected SimpleDateFormat dateFormatter; + + protected MaskFormatter maskFormatter; + + protected String datePattern; + + protected String maskPattern; + + protected char placeholder; + + protected Color darkGreen; + + protected DateUtil dateUtil; + + private boolean isMaskVisible; + + private boolean ignoreDatePatternChange; + + private int hours; + + private int minutes; + + private int seconds; + + private int millis; + + private Calendar calendar; + + public JTextFieldDateEditor() { + this(false, null, null, ' '); + } + + public JTextFieldDateEditor(String datePattern, String maskPattern, char placeholder) { + this(true, datePattern, maskPattern, placeholder); + } + + public JTextFieldDateEditor(boolean showMask, String datePattern, String maskPattern, + char placeholder) { + dateFormatter = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.MEDIUM); + dateFormatter.setLenient(false); + + setDateFormatString(datePattern); + if (datePattern != null) { + ignoreDatePatternChange = true; + } + + this.placeholder = placeholder; + + if (maskPattern == null) { + this.maskPattern = createMaskFromDatePattern(this.datePattern); + } else { + this.maskPattern = maskPattern; + } + + setToolTipText(this.datePattern); + setMaskVisible(showMask); + + addCaretListener(this); + addFocusListener(this); + addActionListener(this); + darkGreen = new Color(0, 150, 0); + + calendar = Calendar.getInstance(); + + dateUtil = new DateUtil(); + } + + /* + * (non-Javadoc) + * + * @see com.toedter.calendar.IDateEditor#getDate() + */ + public Date getDate() { + try { + calendar.setTime(dateFormatter.parse(getText())); + calendar.set(Calendar.HOUR_OF_DAY, hours); + calendar.set(Calendar.MINUTE, minutes); + calendar.set(Calendar.SECOND, seconds); + calendar.set(Calendar.MILLISECOND, millis); + date = calendar.getTime(); + } catch (ParseException e) { + date = null; + } + return date; + } + + /* + * (non-Javadoc) + * + * @see com.toedter.calendar.IDateEditor#setDate(java.util.Date) + */ + public void setDate(Date date) { + setDate(date, true); + } + + /** + * Sets the date. + * + * @param date + * the date + * @param firePropertyChange + * true, if the date property should be fired. + */ + protected void setDate(Date date, boolean firePropertyChange) { + Date oldDate = this.date; + this.date = date; + + if (date == null) { + setText(""); + } else { + calendar.setTime(date); + hours = calendar.get(Calendar.HOUR_OF_DAY); + minutes = calendar.get(Calendar.MINUTE); + seconds = calendar.get(Calendar.SECOND); + millis = calendar.get(Calendar.MILLISECOND); + + String formattedDate = dateFormatter.format(date); + try { + setText(formattedDate); + } catch (RuntimeException e) { + e.printStackTrace(); + } + } + if (date != null && dateUtil.checkDate(date)) { + setForeground(Color.BLACK); + + } + + if (firePropertyChange) { + firePropertyChange("date", oldDate, date); + } + } + + /* + * (non-Javadoc) + * + * @see com.toedter.calendar.IDateEditor#setDateFormatString(java.lang.String) + */ + public void setDateFormatString(String dateFormatString) { + if (ignoreDatePatternChange) { + return; + } + + try { + dateFormatter.applyPattern(dateFormatString); + } catch (RuntimeException e) { + dateFormatter = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.MEDIUM); + dateFormatter.setLenient(false); + } + this.datePattern = dateFormatter.toPattern(); + setToolTipText(this.datePattern); + setDate(date, false); + } + + /* + * (non-Javadoc) + * + * @see com.toedter.calendar.IDateEditor#getDateFormatString() + */ + public String getDateFormatString() { + return datePattern; + } + + /* + * (non-Javadoc) + * + * @see com.toedter.calendar.IDateEditor#getUiComponent() + */ + public JComponent getUiComponent() { + return this; + } + + /** + * After any user input, the value of the textfield is proofed. Depending on + * being a valid date, the value is colored green or red. + * + * @param event + * the caret event + */ + public void caretUpdate(CaretEvent event) { + String text = getText().trim(); + String emptyMask = maskPattern.replace('#', placeholder); + + if (text.length() == 0 || text.equals(emptyMask)) { + setForeground(Color.BLACK); + return; + } + + try { + Date date = dateFormatter.parse(getText()); + if (dateUtil.checkDate(date)) { + setForeground(darkGreen); + } else { + setForeground(Color.RED); + } + } catch (Exception e) { + setForeground(Color.RED); + } + } + + /* + * (non-Javadoc) + * + * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent) + */ + public void focusLost(FocusEvent focusEvent) { + checkText(); + } + + private void checkText() { + try { + Date date = dateFormatter.parse(getText()); + setDate(date, true); + } catch (Exception e) { + // ignore + } + } + + /* + * (non-Javadoc) + * + * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent) + */ + public void focusGained(FocusEvent e) { + } + + /* + * (non-Javadoc) + * + * @see java.awt.Component#setLocale(java.util.Locale) + */ + public void setLocale(Locale locale) { + if (locale == getLocale() || ignoreDatePatternChange) { + return; + } + + super.setLocale(locale); + dateFormatter = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.MEDIUM, locale); + setToolTipText(dateFormatter.toPattern()); + + setDate(date, false); + doLayout(); + } + + /** + * Creates a mask from a date pattern. This is a very simple (and + * incomplete) implementation thet works only with numbers. A date pattern + * of "MM/dd/yy" will result in the mask "##/##/##". Probably you want to + * override this method if it does not fit your needs. + * + * @param datePattern + * the date pattern + * @return the mask + */ + public String createMaskFromDatePattern(String datePattern) { + String symbols = "GyMdkHmsSEDFwWahKzZ"; + String mask = ""; + for (int i = 0; i < datePattern.length(); i++) { + char ch = datePattern.charAt(i); + boolean symbolFound = false; + for (int n = 0; n < symbols.length(); n++) { + if (symbols.charAt(n) == ch) { + mask += "#"; + symbolFound = true; + break; + } + } + if (!symbolFound) { + mask += ch; + } + } + return mask; + } + + /** + * Returns true, if the mask is visible. + * + * @return true, if the mask is visible + */ + public boolean isMaskVisible() { + return isMaskVisible; + } + + /** + * Sets the mask visible. + * + * @param isMaskVisible + * true, if the mask should be visible + */ + public void setMaskVisible(boolean isMaskVisible) { + this.isMaskVisible = isMaskVisible; + if (isMaskVisible) { + if (maskFormatter == null) { + try { + maskFormatter = new MaskFormatter(createMaskFromDatePattern(datePattern)); + maskFormatter.setPlaceholderCharacter(this.placeholder); + maskFormatter.install(this); + } catch (ParseException e) { + e.printStackTrace(); + } + } + } + } + + /** + * Returns the preferred size. If a date pattern is set, it is the size the + * date pattern would take. + */ + public Dimension getPreferredSize() { + if (datePattern != null) { + return new JTextField(datePattern).getPreferredSize(); + } + return super.getPreferredSize(); + } + + /** + * Validates the typed date and sets it (only if it is valid). + */ + public void actionPerformed(ActionEvent e) { + checkText(); + } + + /** + * Enables and disabled the compoment. It also fixes the background bug + * 4991597 and sets the background explicitely to a + * TextField.inactiveBackground. + */ + public void setEnabled(boolean b) { + super.setEnabled(b); + if (!b) { + super.setBackground(UIManager.getColor("TextField.inactiveBackground")); + } + } + + /* + * (non-Javadoc) + * + * @see com.toedter.calendar.IDateEditor#getMaxSelectableDate() + */ + public Date getMaxSelectableDate() { + return dateUtil.getMaxSelectableDate(); + } + + /* + * (non-Javadoc) + * + * @see com.toedter.calendar.IDateEditor#getMinSelectableDate() + */ + public Date getMinSelectableDate() { + return dateUtil.getMinSelectableDate(); + } + + /* + * (non-Javadoc) + * + * @see com.toedter.calendar.IDateEditor#setMaxSelectableDate(java.util.Date) + */ + public void setMaxSelectableDate(Date max) { + dateUtil.setMaxSelectableDate(max); + checkText(); + } + + /* + * (non-Javadoc) + * + * @see com.toedter.calendar.IDateEditor#setMinSelectableDate(java.util.Date) + */ + public void setMinSelectableDate(Date min) { + dateUtil.setMinSelectableDate(min); + checkText(); + } + + /* + * (non-Javadoc) + * + * @see com.toedter.calendar.IDateEditor#setSelectableDateRange(java.util.Date, + * java.util.Date) + */ + public void setSelectableDateRange(Date min, Date max) { + dateUtil.setSelectableDateRange(min, max); + checkText(); + } + + /** + * Creates a JFrame with a JCalendar inside and can be used for testing. + * + * @param s + * The command line arguments + */ + public static void main(String[] s) { + JFrame frame = new JFrame("JTextFieldDateEditor"); + JTextFieldDateEditor jTextFieldDateEditor = new JTextFieldDateEditor(); + jTextFieldDateEditor.setDate(new Date()); + frame.getContentPane().add(jTextFieldDateEditor); + frame.pack(); + frame.setVisible(true); + } +} diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/JYearChooser.java b/JAVA/LofarUtils/src/com/toedter/calendar/JYearChooser.java new file mode 100644 index 0000000000000000000000000000000000000000..89e8fe8ea30aacfae5c091d112fbe4603b0fe1dd --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/calendar/JYearChooser.java @@ -0,0 +1,148 @@ +/* + * JYearChooser.java - A bean for choosing a year + * Copyright (C) 2004 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +package com.toedter.calendar; + +import com.toedter.components.JSpinField; + +import java.util.Calendar; + +import javax.swing.JFrame; + + +/** + * JYearChooser is a bean for choosing a year. + * + * @version $LastChangedRevision: 85 $ + * @version $LastChangedDate: 2006-04-28 13:50:52 +0200 (Fr, 28 Apr 2006) $ + */ +public class JYearChooser extends JSpinField { + private static final long serialVersionUID = 2648810220491090064L; + protected JDayChooser dayChooser; + protected int oldYear; + protected int startYear; + protected int endYear; + + /** + * Default JCalendar constructor. + */ + public JYearChooser() { + setName("JYearChooser"); + Calendar calendar = Calendar.getInstance(); + dayChooser = null; + setMinimum(calendar.getMinimum(Calendar.YEAR)); + setMaximum(calendar.getMaximum(Calendar.YEAR)); + setValue(calendar.get(Calendar.YEAR)); + } + + /** + * Sets the year. This is a bound property. + * + * @param y the new year + * + * @see #getYear + */ + public void setYear(int y) { + super.setValue(y, true, false); + + if (dayChooser != null) { + dayChooser.setYear(value); + } + + spinner.setValue(new Integer(value)); + firePropertyChange("year", oldYear, value); + oldYear = value; + } + + /** + * Sets the year value. + * + * @param value the year value + */ + public void setValue(int value) { + setYear(value); + } + + /** + * Returns the year. + * + * @return the year + */ + public int getYear() { + return super.getValue(); + } + + /** + * Convenience method set a day chooser that might be updated directly. + * + * @param dayChooser the day chooser + */ + public void setDayChooser(JDayChooser dayChooser) { + this.dayChooser = dayChooser; + } + + /** + * Returns the endy ear. + * + * @return the end year + */ + public int getEndYear() { + return getMaximum(); + } + + /** + * Sets the end ear. + * + * @param endYear the end year + */ + public void setEndYear(int endYear) { + setMaximum(endYear); + } + + /** + * Returns the start year. + * + * @return the start year. + */ + public int getStartYear() { + return getMinimum(); + } + + /** + * Sets the start year. + * + * @param startYear the start year + */ + public void setStartYear(int startYear) { + setMinimum(startYear); + } + + /** + * Creates a JFrame with a JYearChooser inside and can be used for testing. + * + * @param s command line arguments + */ + static public void main(String[] s) { + JFrame frame = new JFrame("JYearChooser"); + frame.getContentPane().add(new JYearChooser()); + frame.pack(); + frame.setVisible(true); + } +} diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/JYearChooserBeanInfo.java b/JAVA/LofarUtils/src/com/toedter/calendar/JYearChooserBeanInfo.java new file mode 100644 index 0000000000000000000000000000000000000000..0643cf32b9b89322518bcf0565ecfe161aa13f8a --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/calendar/JYearChooserBeanInfo.java @@ -0,0 +1,39 @@ +/* + * JYearChooserBeanInfo.java - Bean Info for JYearChooser + * Copyright (C) 2004 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +package com.toedter.calendar; + +import com.toedter.components.GenericBeanInfo; + +/** + * A BeanInfo class for the JYearChooser bean. + * + * @author Kai Toedter + * @version $LastChangedRevision: 85 $ + * @version $LastChangedDate: 2006-04-28 13:50:52 +0200 (Fr, 28 Apr 2006) $ + */ +public class JYearChooserBeanInfo extends GenericBeanInfo { + /** + * Constructs a new BeanInfo class for the JYearChooser bean. + */ + public JYearChooserBeanInfo() { + super("JYearChooser", false); + } +} \ No newline at end of file diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/demo/DateChooserPanel.java b/JAVA/LofarUtils/src/com/toedter/calendar/demo/DateChooserPanel.java new file mode 100644 index 0000000000000000000000000000000000000000..91f97357fa20191515d34f137409f37dee36fd4f --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/calendar/demo/DateChooserPanel.java @@ -0,0 +1,196 @@ +/* + * DateChooserPanel.java - A panel to demo several JDateChooser configurations. + * Copyright (C) 2006 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +package com.toedter.calendar.demo; + +import java.awt.Dimension; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.util.Date; +import java.util.Locale; + +import javax.swing.JComponent; +import javax.swing.JLabel; +import javax.swing.JPanel; + +import com.toedter.calendar.JDateChooser; +import com.toedter.calendar.JSpinnerDateEditor; + +/** + * A demonstration panel including several JDateChoosers. + * + * @author Kai Toedter + * @version $LastChangedRevision: 96 $ + * @version $LastChangedDate: 2006-05-12 18:19:35 +0200 (Fr, 12 Mai 2006) $ + */ +public class DateChooserPanel extends JPanel implements PropertyChangeListener { + private static final long serialVersionUID = -1282280858252793253L; + private JComponent[] components; + + public DateChooserPanel() { + setName("JDateChooser"); + + GridBagLayout gridbag = new GridBagLayout(); + GridBagConstraints c = new GridBagConstraints(); + c.fill = GridBagConstraints.BOTH; + + setLayout(gridbag); + + components = new JComponent[5]; + components[0] = new JDateChooser(); + components[1] = new JDateChooser(new Date()); + components[2] = new JDateChooser(null, null, null, + new JSpinnerDateEditor()); + components[3] = new JDateChooser("yyyy/MM/dd", "####/##/##", '_'); + components[4] = new DemoTable(); + + addEntry("Default", components[0], gridbag); + addEntry("Default with date set", components[1], gridbag); + addEntry("Spinner Editor", components[2], gridbag); + addEntry("Explicite date pattern and mask", components[3], gridbag); + addEntry("Table with date editors", components[4], gridbag); + } + + private void addEntry(String text, JComponent component, GridBagLayout grid) { + JLabel label = new JLabel(text + ": ", null, JLabel.RIGHT); + GridBagConstraints c = new GridBagConstraints(); + c.weightx = 1.0; + c.fill = GridBagConstraints.BOTH; + grid.setConstraints(label, c); + add(label); + c.gridwidth = GridBagConstraints.REMAINDER; + grid.setConstraints(component, c); + add(component); + JPanel blankLine = new JPanel() { + private static final long serialVersionUID = 4514530330521503732L; + + public Dimension getPreferredSize() { + return new Dimension(10,3); + } + }; + grid.setConstraints(blankLine, c); + add(blankLine); + } + + /** + * Gets the date format string. + * + * @return Returns the dateFormatString. + */ + public String getDateFormatString() { + return ((JDateChooser) components[1]).getDateFormatString(); + } + + /** + * Sets the date format string. E.g "MMMMM d, yyyy" will result in "July 21, + * 2004" if this is the selected date and locale is English. + * + * @param dfString + * The dateFormatString to set. + */ + public void setDateFormatString(String dfString) { + for (int i = 0; i < 4; i++) { + ((JDateChooser) components[i]).setDateFormatString(dfString); + } + } + + /** + * Returns the date. If the JDateChooser is started with an empty date and + * no date is set by the user, null is returned. + * + * @return the current date + */ + public Date getDate() { + return ((JDateChooser) components[1]).getDate(); + } + + /** + * Sets the date. Fires the property change "date" if date != null. + * + * @param date + * the new date. + */ + public void setDate(Date date) { + for (int i = 0; i < 4; i++) { + ((JDateChooser) components[i]).setDate(date); + } + } + + public void propertyChange(PropertyChangeEvent evt) { + if (evt.getPropertyName().equals("date")) { + setDate((Date) evt.getNewValue()); + } + } + + /** + * Returns the locale of the first JDateChooser. + */ + public Locale getLocale() { + return ((JDateChooser) components[0]).getLocale(); + } + + /** + * Sets the locale of the first 4 JDateChoosers. + */ + public void setLocale(Locale locale) { + for (int i = 0; i < 5; i++) { + components[i].setLocale(locale); + } + } + + /* (non-Javadoc) + * @see java.awt.Component#isEnabled() + */ + public boolean isEnabled() { + return ((JDateChooser) components[0]).isEnabled(); + } + + /* (non-Javadoc) + * @see javax.swing.JComponent#setEnabled(boolean) + */ + public void setEnabled(boolean enabled) { + for (int i = 0; i < 5; i++) { + components[i].setEnabled(enabled); + } + } + + public Date getMinSelectableDate() { + return ((JDateChooser) components[0]).getMinSelectableDate(); + } + + public void setMinSelectableDate(Date date) { + for (int i = 0; i < 4; i++) { + ((JDateChooser) components[i]).setMinSelectableDate(date); + } + } + + public Date getMaxSelectableDate() { + return ((JDateChooser) components[0]).getMaxSelectableDate(); + } + + public void setMaxSelectableDate(Date date) { + for (int i = 0; i < 4; i++) { + ((JDateChooser) components[i]).setMaxSelectableDate(date); + } + } +} diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/demo/DateChooserPanelBeanInfo.java b/JAVA/LofarUtils/src/com/toedter/calendar/demo/DateChooserPanelBeanInfo.java new file mode 100644 index 0000000000000000000000000000000000000000..da180211fdf64e8267862f6601c151145eed83e7 --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/calendar/demo/DateChooserPanelBeanInfo.java @@ -0,0 +1,39 @@ +/* + * JDateChooserBeanInfo.java - Bean Info for JDateChooser + * Copyright (C) 2004 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +package com.toedter.calendar.demo; + +import com.toedter.components.GenericBeanInfo; + +/** + * A BeanInfo class for the JDateChooser bean. + * + * @author Kai Toedter + * @version $LastChangedRevision: 85 $ + * @version $LastChangedDate: 2006-04-28 13:50:52 +0200 (Fr, 28 Apr 2006) $ + */ +public class DateChooserPanelBeanInfo extends GenericBeanInfo { + /** + * Constructs a new BeanInfo class for the JDateChooser bean. + */ + public DateChooserPanelBeanInfo() { + super("JDateChooser", true); + } +} \ No newline at end of file diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/demo/DemoTable.java b/JAVA/LofarUtils/src/com/toedter/calendar/demo/DemoTable.java new file mode 100644 index 0000000000000000000000000000000000000000..c46a1be9dea3689361ecb7aa9fc09989635a4507 --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/calendar/demo/DemoTable.java @@ -0,0 +1,112 @@ +/* + * DemoTable.java - A table to demo JDateChooser cell editors. + * Copyright (C) 2006 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +package com.toedter.calendar.demo; + +import java.awt.Dimension; +import java.awt.GridLayout; +import java.util.Date; + +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTable; +import javax.swing.table.AbstractTableModel; + +import com.toedter.calendar.JDateChooserCellEditor; + +/** + * A demonstration table with JDateChooserCellEditors. + * + * @author Kai Toedter + * @version $LastChangedRevision: 85 $ + * @version $LastChangedDate: 2006-04-28 13:50:52 +0200 (Fr, 28 Apr 2006) $ + */ +public class DemoTable extends JPanel { + private static final long serialVersionUID = -2823838920746867592L; + + public DemoTable() { + super(new GridLayout(1, 0)); + + setName("DemoTable"); + + JTable table = new JTable(new DemoTableModel()); + table.setPreferredScrollableViewportSize(new Dimension(180, 32)); + table.setDefaultEditor(Date.class, new JDateChooserCellEditor()); + + // Create the scroll pane and add the table to it. + JScrollPane scrollPane = new JScrollPane(table); + + // Add the scroll pane to this panel. + add(scrollPane); + } + + class DemoTableModel extends AbstractTableModel { + private static final long serialVersionUID = 3283465559187131559L; + + private String[] columnNames = { "Empty Date", "Date set" }; + + private Object[][] data = { + { null, new Date() }, + { null, new Date() } + }; + + public int getColumnCount() { + return columnNames.length; + } + + public int getRowCount() { + return data.length; + } + + public String getColumnName(int col) { + return columnNames[col]; + } + + public Object getValueAt(int row, int col) { + return data[row][col]; + } + + /* + * JTable uses this method to determine the default renderer/ editor for + * each cell. If we didn't implement this method, then the last column + * would contain text ("true"/"false"), rather than a check box. + */ + public Class getColumnClass(int c) { + return getValueAt(0, 1).getClass(); + } + + /* + * Don't need to implement this method unless your table's editable. + */ + public boolean isCellEditable(int row, int col) { + return true; + } + + /* + * Don't need to implement this method unless your table's data can + * change. + */ + public void setValueAt(Object value, int row, int col) { + data[row][col] = value; + fireTableCellUpdated(row, col); + } + } +} \ No newline at end of file diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/demo/JCalendarDemo.java b/JAVA/LofarUtils/src/com/toedter/calendar/demo/JCalendarDemo.java new file mode 100644 index 0000000000000000000000000000000000000000..cab8ab6917ea86b2ec7be71d0d74dea58ea8ce1b --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/calendar/demo/JCalendarDemo.java @@ -0,0 +1,658 @@ +/* + * JCalendarDemo.java - Demonstration of JCalendar Java Bean + * Copyright (C) 2004 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +package com.toedter.calendar.demo; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.awt.event.WindowListener; +import java.beans.BeanInfo; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.beans.PropertyDescriptor; +import java.lang.reflect.Method; +import java.net.URL; +import java.util.Date; + +import javax.swing.AbstractAction; +import javax.swing.BorderFactory; +import javax.swing.ButtonGroup; +import javax.swing.Icon; +import javax.swing.ImageIcon; +import javax.swing.JApplet; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JColorChooser; +import javax.swing.JComponent; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JMenu; +import javax.swing.JMenuBar; +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JRadioButtonMenuItem; +import javax.swing.JSplitPane; +import javax.swing.JTextField; +import javax.swing.JToolBar; +import javax.swing.KeyStroke; +import javax.swing.SwingUtilities; +import javax.swing.UIManager; +import javax.swing.UIManager.LookAndFeelInfo; +import javax.swing.plaf.basic.BasicSplitPaneDivider; +import javax.swing.plaf.basic.BasicSplitPaneUI; + +import com.toedter.calendar.JCalendar; +import com.toedter.calendar.JDateChooser; +import com.toedter.calendar.JDayChooser; +import com.toedter.calendar.JMonthChooser; +import com.toedter.calendar.JYearChooser; +import com.toedter.components.JLocaleChooser; +import com.toedter.components.JSpinField; +import com.toedter.components.JTitlePanel; + +/** + * A demonstration Applet for the JCalendar bean. The demo can also be started + * as Java application. + * + * @author Kai Toedter + * @version $LastChangedRevision: 103 $ + * @version $LastChangedDate: 2006-06-04 14:57:02 +0200 (So, 04 Jun 2006) $ + */ +public class JCalendarDemo extends JApplet implements PropertyChangeListener { + private static final long serialVersionUID = 6739986412544494316L; + private JSplitPane splitPane; + private JPanel calendarPanel; + private JComponent[] beans; + private JPanel propertyPanel; + private JTitlePanel propertyTitlePanel; + private JTitlePanel componentTitlePanel; + private JPanel componentPanel; + private JToolBar toolBar; + + /** + * Initializes the applet. + */ + public void init() { + // Set the JGoodies Plastic 3D look and feel + initializeLookAndFeels(); + + // initialize all beans to demo + beans = new JComponent[6]; + beans[0] = new DateChooserPanel(); + beans[1] = new JCalendar(); + beans[2] = new JDayChooser(); + beans[3] = new JMonthChooser(); + beans[4] = new JYearChooser(); + beans[5] = new JSpinField(); + + ((JSpinField) beans[5]).adjustWidthToMaximumValue(); + ((JYearChooser) beans[4]).setMaximum(((JSpinField) beans[5]).getMaximum()); + ((JYearChooser) beans[4]).adjustWidthToMaximumValue(); + + getContentPane().setLayout(new BorderLayout()); + setJMenuBar(createMenuBar()); + + toolBar = createToolBar(); + getContentPane().add(toolBar, BorderLayout.NORTH); + + splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); + splitPane.setBorder(BorderFactory.createLineBorder(Color.GRAY)); + splitPane.setDividerSize(4); + splitPane.setDividerLocation(190); + + BasicSplitPaneDivider divider = ((BasicSplitPaneUI) splitPane.getUI()).getDivider(); + + if (divider != null) { + divider.setBorder(null); + } + + propertyPanel = new JPanel(); + componentPanel = new JPanel(); + + URL iconURL = beans[0].getClass().getResource( + "images/" + beans[0].getName() + "Color16.gif"); + ImageIcon icon = new ImageIcon(iconURL); + + propertyTitlePanel = new JTitlePanel("Properties", null, propertyPanel, BorderFactory + .createEmptyBorder(4, 4, 4, 4)); + + componentTitlePanel = new JTitlePanel("Component", icon, componentPanel, BorderFactory + .createEmptyBorder(4, 4, 0, 4)); + + splitPane.setBottomComponent(propertyTitlePanel); + splitPane.setTopComponent(componentTitlePanel); + installBean(beans[0]); + + getContentPane().add(splitPane, BorderLayout.CENTER); + } + + /** + * Installs the JGoodies Look & Feels, if available, in classpath. + */ + public final void initializeLookAndFeels() { + // if in classpath thry to load JGoodies Plastic Look & Feel + try { + LookAndFeelInfo[] lnfs = UIManager.getInstalledLookAndFeels(); + boolean found = false; + for (int i = 0; i < lnfs.length; i++) { + if (lnfs[i].getName().equals("JGoodies Plastic 3D")) { + found = true; + } + } + if (!found) { + UIManager.installLookAndFeel("JGoodies Plastic 3D", + "com.jgoodies.looks.plastic.Plastic3DLookAndFeel"); + } + UIManager.setLookAndFeel("com.jgoodies.looks.plastic.Plastic3DLookAndFeel"); + } catch (Throwable t) { + try { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + /** + * Creates the menu bar + * + * @return Description of the Return Value + */ + public JToolBar createToolBar() { + // Create the tool bar + toolBar = new JToolBar(); + toolBar.putClientProperty("jgoodies.headerStyle", "Both"); + toolBar.setRollover(true); + toolBar.setFloatable(false); + + for (int i = 0; i < beans.length; i++) { + Icon icon; + JButton button; + + try { + final JComponent bean = beans[i]; + URL iconURL = bean.getClass().getResource( + "images/" + bean.getName() + "Color16.gif"); + icon = new ImageIcon(iconURL); + + button = new JButton(icon); + + ActionListener actionListener = new ActionListener() { + public void actionPerformed(ActionEvent e) { + installBean(bean); + } + }; + + button.addActionListener(actionListener); + } catch (Exception e) { + System.out.println("JCalendarDemo.createToolBar(): " + e); + button = new JButton(beans[i].getName()); + } + + button.setFocusPainted(false); + toolBar.add(button); + } + + return toolBar; + } + + /** + * Creates the menu bar + * + * @return Description of the Return Value + */ + public JMenuBar createMenuBar() { + // Create the menu bar + final JMenuBar menuBar = new JMenuBar(); + + // Menu for all beans to demo + JMenu componentsMenu = new JMenu("Components"); + componentsMenu.setMnemonic('C'); + + menuBar.add(componentsMenu); + + for (int i = 0; i < beans.length; i++) { + Icon icon; + JMenuItem menuItem; + + try { + URL iconURL = beans[i].getClass().getResource( + "images/" + beans[i].getName() + "Color16.gif"); + icon = new ImageIcon(iconURL); + menuItem = new JMenuItem(beans[i].getName(), icon); + } catch (Exception e) { + System.out.println("JCalendarDemo.createMenuBar(): " + e + " for URL: " + "images/" + + beans[i].getName() + "Color16.gif"); + menuItem = new JMenuItem(beans[i].getName()); + } + + componentsMenu.add(menuItem); + + final JComponent bean = beans[i]; + ActionListener actionListener = new ActionListener() { + public void actionPerformed(ActionEvent e) { + installBean(bean); + } + }; + + menuItem.addActionListener(actionListener); + } + + // Menu for the look and feels (lnfs). + UIManager.LookAndFeelInfo[] lnfs = UIManager.getInstalledLookAndFeels(); + + ButtonGroup lnfGroup = new ButtonGroup(); + JMenu lnfMenu = new JMenu("Look&Feel"); + lnfMenu.setMnemonic('L'); + + menuBar.add(lnfMenu); + + for (int i = 0; i < lnfs.length; i++) { + if (!lnfs[i].getName().equals("CDE/Motif")) { + JRadioButtonMenuItem rbmi = new JRadioButtonMenuItem(lnfs[i].getName()); + lnfMenu.add(rbmi); + + // preselect the current Look & feel + rbmi.setSelected(UIManager.getLookAndFeel().getName().equals(lnfs[i].getName())); + + // store lool & feel info as client property + rbmi.putClientProperty("lnf name", lnfs[i]); + + // create and add the item listener + rbmi.addItemListener( + // inlining + new ItemListener() { + public void itemStateChanged(ItemEvent ie) { + JRadioButtonMenuItem rbmi2 = (JRadioButtonMenuItem) ie.getSource(); + + if (rbmi2.isSelected()) { + // get the stored look & feel info + UIManager.LookAndFeelInfo info = (UIManager.LookAndFeelInfo) rbmi2 + .getClientProperty("lnf name"); + + try { + menuBar.putClientProperty("jgoodies.headerStyle", "Both"); + UIManager.setLookAndFeel(info.getClassName()); + + // update the complete application's + // look & feel + SwingUtilities.updateComponentTreeUI(JCalendarDemo.this); + for (int i = 0; i < beans.length; i++) { + SwingUtilities.updateComponentTreeUI(beans[i]); + } + // set the split pane devider border to + // null + BasicSplitPaneDivider divider = ((BasicSplitPaneUI) splitPane + .getUI()).getDivider(); + + if (divider != null) { + divider.setBorder(null); + } + } catch (Exception e) { + e.printStackTrace(); + + System.err.println("Unable to set UI " + e.getMessage()); + } + } + } + }); + lnfGroup.add(rbmi); + } + } + + // the help menu + JMenu helpMenu = new JMenu("Help"); + helpMenu.setMnemonic('H'); + + JMenuItem aboutItem = helpMenu.add(new AboutAction(this)); + aboutItem.setMnemonic('A'); + aboutItem.setAccelerator(KeyStroke.getKeyStroke('A', java.awt.Event.CTRL_MASK)); + + menuBar.add(helpMenu); + + return menuBar; + } + + /** + * The applet is a PropertyChangeListener for "locale" and "calendar". + * + * @param evt + * Description of the Parameter + */ + public void propertyChange(PropertyChangeEvent evt) { + if (calendarPanel != null) { + if (evt.getPropertyName().equals("calendar")) { + // calendar = (Calendar) evt.getNewValue(); + // DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, + // jcalendar.getLocale()); + // dateField.setText(df.format(calendar.getTime())); + } + } + } + + /** + * Creates a JFrame with a JCalendarDemo inside and can be used for testing. + * + * @param s + * The command line arguments + */ + public static void main(String[] s) { + WindowListener l = new WindowAdapter() { + public void windowClosing(WindowEvent e) { + System.exit(0); + } + }; + + JFrame frame = new JFrame("JCalendar Demo"); + frame.addWindowListener(l); + + JCalendarDemo demo = new JCalendarDemo(); + demo.init(); + frame.getContentPane().add(demo); + frame.pack(); + frame.setBounds(200, 200, (int) frame.getPreferredSize().getWidth() + 20, (int) frame + .getPreferredSize().getHeight() + 180); + frame.setVisible(true); + } + + /** + * Installes a demo bean. + * + * @param bean + * the demo bean + */ + private void installBean(JComponent bean) { + try { + componentPanel.removeAll(); + componentPanel.add(bean); + + BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass(), bean.getClass() + .getSuperclass()); + PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); + + propertyPanel.removeAll(); + + GridBagLayout gridbag = new GridBagLayout(); + GridBagConstraints c = new GridBagConstraints(); + c.fill = GridBagConstraints.BOTH; + + propertyPanel.setLayout(gridbag); + + int count = 0; + + String[] types = new String[] { "class java.util.Locale", "boolean", "int", + "class java.awt.Color", "class java.util.Date", "class java.lang.String" }; + + for (int t = 0; t < types.length; t++) { + for (int i = 0; i < propertyDescriptors.length; i++) { + if (propertyDescriptors[i].getWriteMethod() != null) { + String type = propertyDescriptors[i].getPropertyType().toString(); + + final PropertyDescriptor propertyDescriptor = propertyDescriptors[i]; + final JComponent currentBean = bean; + final Method readMethod = propertyDescriptor.getReadMethod(); + final Method writeMethod = propertyDescriptor.getWriteMethod(); + + if (type.equals(types[t]) + && (((readMethod != null) && (writeMethod != null)) || ("class java.util.Locale" + .equals(type)))) { + if ("boolean".equals(type)) { + boolean isSelected = false; + + try { + Boolean booleanObj = ((Boolean) readMethod.invoke(bean, null)); + isSelected = booleanObj.booleanValue(); + } catch (Exception e) { + e.printStackTrace(); + } + + final JCheckBox checkBox = new JCheckBox("", isSelected); + checkBox.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent event) { + try { + if (checkBox.isSelected()) { + writeMethod.invoke(currentBean, + new Object[] { new Boolean(true) }); + } else { + writeMethod.invoke(currentBean, + new Object[] { new Boolean(false) }); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + addProperty(propertyDescriptors[i], checkBox, gridbag); + count += 1; + } else if ("int".equals(type)) { + JSpinField spinField = new JSpinField(); + spinField.addPropertyChangeListener(new PropertyChangeListener() { + public void propertyChange(PropertyChangeEvent evt) { + try { + if (evt.getPropertyName().equals("value")) { + writeMethod.invoke(currentBean, new Object[] { evt + .getNewValue() }); + } + } catch (Exception e) { + } + } + }); + + try { + Integer integerObj = ((Integer) readMethod.invoke(bean, null)); + spinField.setValue(integerObj.intValue()); + } catch (Exception e) { + e.printStackTrace(); + } + + addProperty(propertyDescriptors[i], spinField, gridbag); + count += 1; + } else if ("class java.lang.String".equals(type)) { + String string = ""; + + try { + string = ((String) readMethod.invoke(bean, null)); + } catch (Exception e) { + e.printStackTrace(); + } + + JTextField textField = new JTextField(string); + ActionListener actionListener = new ActionListener() { + public void actionPerformed(ActionEvent e) { + try { + writeMethod.invoke(currentBean, new Object[] { e + .getActionCommand() }); + } catch (Exception ex) { + } + } + }; + + textField.addActionListener(actionListener); + + addProperty(propertyDescriptors[i], textField, gridbag); + count += 1; + } else if ("class java.util.Locale".equals(type)) { + JLocaleChooser localeChooser = new JLocaleChooser(bean); + localeChooser.setPreferredSize(new Dimension(200, localeChooser + .getPreferredSize().height)); + addProperty(propertyDescriptors[i], localeChooser, gridbag); + count += 1; + } else if ("class java.util.Date".equals(type)) { + Date date = null; + + try { + date = ((Date) readMethod.invoke(bean, null)); + } catch (Exception e) { + e.printStackTrace(); + } + + JDateChooser dateChooser = new JDateChooser(date); + + dateChooser.addPropertyChangeListener(new PropertyChangeListener() { + public void propertyChange(PropertyChangeEvent evt) { + try { + if (evt.getPropertyName().equals("date")) { + writeMethod.invoke(currentBean, new Object[] { evt + .getNewValue() }); + } + } catch (Exception e) { + } + } + }); + + addProperty(propertyDescriptors[i], dateChooser, gridbag); + count += 1; + } else if ("class java.awt.Color".equals(type)) { + final JButton button = new JButton(); + + try { + final Color colorObj = ((Color) readMethod.invoke(bean, null)); + button.setText("..."); + button.setBackground(colorObj); + + ActionListener actionListener = new ActionListener() { + public void actionPerformed(ActionEvent e) { + Color newColor = JColorChooser.showDialog( + JCalendarDemo.this, "Choose Color", colorObj); + button.setBackground(newColor); + + try { + writeMethod.invoke(currentBean, + new Object[] { newColor }); + } catch (Exception e1) { + e1.printStackTrace(); + } + } + }; + + button.addActionListener(actionListener); + } catch (Exception e) { + e.printStackTrace(); + } + + addProperty(propertyDescriptors[i], button, gridbag); + count += 1; + } + } + } + } + } + + URL iconURL = bean.getClass().getResource("images/" + bean.getName() + "Color16.gif"); + ImageIcon icon = new ImageIcon(iconURL); + + componentTitlePanel.setTitle(bean.getName(), icon); + bean.invalidate(); + propertyPanel.invalidate(); + componentPanel.invalidate(); + componentPanel.repaint(); + } catch (IntrospectionException e) { + e.printStackTrace(); + } + } + + private void addProperty(PropertyDescriptor propertyDescriptor, JComponent editor, + GridBagLayout grid) { + String text = propertyDescriptor.getDisplayName(); + String newText = ""; + + for (int i = 0; i < text.length(); i++) { + char c = text.charAt(i); + + if (((c >= 'A') && (c <= 'Z')) || (i == 0)) { + if (i == 0) { + c += ('A' - 'a'); + } + + newText += (" " + c); + } else { + newText += c; + } + } + + JLabel label = new JLabel(newText + ": ", null, JLabel.RIGHT); + GridBagConstraints c = new GridBagConstraints(); + c.weightx = 1.0; + c.fill = GridBagConstraints.BOTH; + grid.setConstraints(label, c); + propertyPanel.add(label); + c.gridwidth = GridBagConstraints.REMAINDER; + grid.setConstraints(editor, c); + propertyPanel.add(editor); + + JPanel blankLine = new JPanel() { + private static final long serialVersionUID = 4514530330521503732L; + + public Dimension getPreferredSize() { + return new Dimension(10, 2); + } + }; + grid.setConstraints(blankLine, c); + propertyPanel.add(blankLine); + } + + /** + * Action to show the About dialog + * + * @author toedter_k + */ + class AboutAction extends AbstractAction { + private static final long serialVersionUID = -5204865941545323214L; + private JCalendarDemo demo; + + /** + * Constructor for the AboutAction object + * + * @param demo + * Description of the Parameter + */ + AboutAction(JCalendarDemo demo) { + super("About..."); + this.demo = demo; + } + + /** + * Description of the Method + * + * @param event + * Description of the Parameter + */ + public void actionPerformed(ActionEvent event) { + JOptionPane + .showMessageDialog( + demo, + "JCalendar Demo\nVersion 1.3.2\n\nKai Toedter\nkai@toedter.com\nwww.toedter.com", + "About...", JOptionPane.INFORMATION_MESSAGE); + } + } +} diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/demo/images/DemoTableColor16.gif b/JAVA/LofarUtils/src/com/toedter/calendar/demo/images/DemoTableColor16.gif new file mode 100644 index 0000000000000000000000000000000000000000..1d214bfc9cd47c162367e77d9758c0f8442ccac8 Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/calendar/demo/images/DemoTableColor16.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/demo/images/JDateChooserColor16.gif b/JAVA/LofarUtils/src/com/toedter/calendar/demo/images/JDateChooserColor16.gif new file mode 100644 index 0000000000000000000000000000000000000000..e41899be08c1df0bb82dca3075d6b8bc0f4860ce Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/calendar/demo/images/JDateChooserColor16.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/images/JCalendarColor16.gif b/JAVA/LofarUtils/src/com/toedter/calendar/images/JCalendarColor16.gif new file mode 100644 index 0000000000000000000000000000000000000000..16fb4170bbbf969221ebd74568642a01397999a5 Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/calendar/images/JCalendarColor16.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/images/JCalendarColor32.gif b/JAVA/LofarUtils/src/com/toedter/calendar/images/JCalendarColor32.gif new file mode 100644 index 0000000000000000000000000000000000000000..4d72dc0b34568047e9f7b23262644d02c706ed12 Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/calendar/images/JCalendarColor32.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/images/JCalendarMono16.gif b/JAVA/LofarUtils/src/com/toedter/calendar/images/JCalendarMono16.gif new file mode 100644 index 0000000000000000000000000000000000000000..edd34c9aa2470a40413243f1b1e2e63c9d2ad7fb Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/calendar/images/JCalendarMono16.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/images/JCalendarMono32.gif b/JAVA/LofarUtils/src/com/toedter/calendar/images/JCalendarMono32.gif new file mode 100644 index 0000000000000000000000000000000000000000..cb57af26ed50b1177a0c70aed6762e486c7f8c7e Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/calendar/images/JCalendarMono32.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/images/JDateChooserColor16.gif b/JAVA/LofarUtils/src/com/toedter/calendar/images/JDateChooserColor16.gif new file mode 100644 index 0000000000000000000000000000000000000000..e41899be08c1df0bb82dca3075d6b8bc0f4860ce Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/calendar/images/JDateChooserColor16.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/images/JDateChooserColor32.gif b/JAVA/LofarUtils/src/com/toedter/calendar/images/JDateChooserColor32.gif new file mode 100644 index 0000000000000000000000000000000000000000..6d3d9ca0960a0e2f0a72c6ee13d4e5bfe97996e3 Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/calendar/images/JDateChooserColor32.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/images/JDateChooserIcon.gif b/JAVA/LofarUtils/src/com/toedter/calendar/images/JDateChooserIcon.gif new file mode 100644 index 0000000000000000000000000000000000000000..7fca2fbcdb02b12b9b5101ef8a033c8720831878 Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/calendar/images/JDateChooserIcon.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/images/JDateChooserMono16.gif b/JAVA/LofarUtils/src/com/toedter/calendar/images/JDateChooserMono16.gif new file mode 100644 index 0000000000000000000000000000000000000000..b38aa913518f829e4812954312aa95df2d6eb3a6 Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/calendar/images/JDateChooserMono16.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/images/JDateChooserMono32.gif b/JAVA/LofarUtils/src/com/toedter/calendar/images/JDateChooserMono32.gif new file mode 100644 index 0000000000000000000000000000000000000000..9785dce7fcf2f98288182e19e6df7ceb7f534ec3 Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/calendar/images/JDateChooserMono32.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/images/JDayChooserColor16.gif b/JAVA/LofarUtils/src/com/toedter/calendar/images/JDayChooserColor16.gif new file mode 100644 index 0000000000000000000000000000000000000000..4bd2f0ec1e578f49465d3fd14c3f40d26d751768 Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/calendar/images/JDayChooserColor16.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/images/JDayChooserColor32.gif b/JAVA/LofarUtils/src/com/toedter/calendar/images/JDayChooserColor32.gif new file mode 100644 index 0000000000000000000000000000000000000000..7e8ecce87a5ffff5290b5c5c1ab48d28319423da Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/calendar/images/JDayChooserColor32.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/images/JDayChooserMono16.gif b/JAVA/LofarUtils/src/com/toedter/calendar/images/JDayChooserMono16.gif new file mode 100644 index 0000000000000000000000000000000000000000..94cbb054ab889bebcdf801dbda74b75d8a0ef82a Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/calendar/images/JDayChooserMono16.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/images/JDayChooserMono32.gif b/JAVA/LofarUtils/src/com/toedter/calendar/images/JDayChooserMono32.gif new file mode 100644 index 0000000000000000000000000000000000000000..d99a7985738bb56c315711773b4670b1bf76e5ef Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/calendar/images/JDayChooserMono32.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/images/JMonthChooserColor16.gif b/JAVA/LofarUtils/src/com/toedter/calendar/images/JMonthChooserColor16.gif new file mode 100644 index 0000000000000000000000000000000000000000..9ad61b0dcfd5d806b04bf70054589814774759f3 Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/calendar/images/JMonthChooserColor16.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/images/JMonthChooserColor32.gif b/JAVA/LofarUtils/src/com/toedter/calendar/images/JMonthChooserColor32.gif new file mode 100644 index 0000000000000000000000000000000000000000..c3123594ebc27f5359b4ab3a3fce65ba51a87ef9 Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/calendar/images/JMonthChooserColor32.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/images/JMonthChooserMono16.gif b/JAVA/LofarUtils/src/com/toedter/calendar/images/JMonthChooserMono16.gif new file mode 100644 index 0000000000000000000000000000000000000000..ea649a7e2c7ef66659f2741371fb14f354956611 Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/calendar/images/JMonthChooserMono16.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/images/JMonthChooserMono32.gif b/JAVA/LofarUtils/src/com/toedter/calendar/images/JMonthChooserMono32.gif new file mode 100644 index 0000000000000000000000000000000000000000..b649592bbd3ffc8946acac63bc0910364c0c1ce4 Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/calendar/images/JMonthChooserMono32.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/images/JYearChooserColor16.gif b/JAVA/LofarUtils/src/com/toedter/calendar/images/JYearChooserColor16.gif new file mode 100644 index 0000000000000000000000000000000000000000..eba37f7baf73a73ddd3931d9dc47177e768476f1 Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/calendar/images/JYearChooserColor16.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/images/JYearChooserColor32.gif b/JAVA/LofarUtils/src/com/toedter/calendar/images/JYearChooserColor32.gif new file mode 100644 index 0000000000000000000000000000000000000000..68f292ff5028c57ee85c1bda02611105717b0059 Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/calendar/images/JYearChooserColor32.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/images/JYearChooserMono16.gif b/JAVA/LofarUtils/src/com/toedter/calendar/images/JYearChooserMono16.gif new file mode 100644 index 0000000000000000000000000000000000000000..f525f8c4d205117f231c66f261f2a215602bf813 Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/calendar/images/JYearChooserMono16.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/calendar/images/JYearChooserMono32.gif b/JAVA/LofarUtils/src/com/toedter/calendar/images/JYearChooserMono32.gif new file mode 100644 index 0000000000000000000000000000000000000000..8c43cc9e305e59e0403fdab02a936e683acc5490 Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/calendar/images/JYearChooserMono32.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/components/GenericBeanInfo.java b/JAVA/LofarUtils/src/com/toedter/components/GenericBeanInfo.java new file mode 100644 index 0000000000000000000000000000000000000000..e2ca936c695b498c7d2dd439d2d31f907f28f06c --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/components/GenericBeanInfo.java @@ -0,0 +1,92 @@ +/* + * GenericBeanInfo.java - Bean Info base for all toedter.com beans + * Copyright (C) 2004 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +package com.toedter.components; + +import java.awt.Image; +import java.beans.PropertyEditorManager; +import java.beans.SimpleBeanInfo; +import java.util.Locale; + +/** + * A generic bean info class use by all bean infos of the JCalendar bean suite. + * + * @author Kai Toedter + * @version $LastChangedRevision: 85 $ + * @version $LastChangedDate: 2006-04-28 13:50:52 +0200 (Fr, 28 Apr 2006) $ + */ +public class GenericBeanInfo extends SimpleBeanInfo { + /** 16x16 color icon. */ + protected Image iconColor16; + + /** 32x32 color icon. */ + protected Image iconColor32; + + /** 16x16 mono icon. */ + protected Image iconMono16; + + /** 32x32 mono icon. */ + protected Image iconMono32; + + /** + * Constructs a new BeanInfo. + */ + public GenericBeanInfo(String bean, boolean registerLocaleEditor) { + try { + iconColor16 = loadImage("images/" + bean + "Color16.gif"); + iconColor32 = loadImage("images/" + bean + "Color32.gif"); + iconMono16 = loadImage("images/" + bean + "Mono16.gif"); + iconMono32 = loadImage("images/" + bean + "Mono32.gif"); + } catch (RuntimeException e) { + System.out.println("GenericBeanInfo.GenericBeanInfo(): " + e); + } + + if(registerLocaleEditor) { + PropertyEditorManager.registerEditor(Locale.class,LocaleEditor.class); + } + } + + /** + * This method returns an image object that can be used to represent the + * bean in toolboxes, toolbars, etc. + * + * @param iconKind + * the kind of requested icon + * + * @return the icon image + */ + public Image getIcon(int iconKind) { + switch (iconKind) { + case ICON_COLOR_16x16 : + return iconColor16; + + case ICON_COLOR_32x32 : + return iconColor32; + + case ICON_MONO_16x16 : + return iconMono16; + + case ICON_MONO_32x32 : + return iconMono32; + } + + return null; + } +} \ No newline at end of file diff --git a/JAVA/LofarUtils/src/com/toedter/components/JLocaleChooser.java b/JAVA/LofarUtils/src/com/toedter/components/JLocaleChooser.java new file mode 100644 index 0000000000000000000000000000000000000000..07f8326a65e26beb42d3ef684a52502d4036fb64 --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/components/JLocaleChooser.java @@ -0,0 +1,150 @@ +/* + * JLocaleChooser.java - A bean for choosing locales + * Copyright (C) 2004 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +package com.toedter.components; + +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; +import java.util.Calendar; +import java.util.Locale; + +import javax.swing.JComboBox; +import javax.swing.JComponent; +import javax.swing.JFrame; + +/** + * JLocaleChooser is a bean for choosing locales. + * + * @author Kai Toedter + * @version $LastChangedRevision: 85 $ + * @version $LastChangedDate: 2006-04-28 13:50:52 +0200 (Fr, 28 Apr 2006) $ + */ +public class JLocaleChooser extends JComboBox implements ItemListener { + private static final long serialVersionUID = 8152430789764877431L; + protected JComponent component; + + /** + * Default JLocaleChooser constructor. + */ + public JLocaleChooser() { + this(null); + } + + /** + * Returns "JLocaleChoose". + * + * @return the name value + */ + public String getName() { + return "JLocaleChoose"; + } + + /** + * Default JLocaleChooser constructor. + */ + public JLocaleChooser(JComponent component) { + super(); + this.component = component; + addItemListener(this); + locales = Calendar.getAvailableLocales(); + localeCount = locales.length; + + for (int i = 0; i < localeCount; i++) { + if (locales[i].getCountry().length() > 0) { + addItem(locales[i].getDisplayName()); + } + } + + setLocale(Locale.getDefault()); + } + + /** + * The ItemListener for the locales. + */ + public void itemStateChanged(ItemEvent iEvt) { + String item = (String) iEvt.getItem(); + int i; + + for (i = 0; i < localeCount; i++) { + if (locales[i].getDisplayName().equals(item)) + break; + } + setLocale(locales[i], false); + } + + /** + * Sets the locale. + * + * @see #getLocale + */ + private void setLocale(Locale l, boolean select) { + Locale oldLocale = locale; + locale = l; + int n = 0; + + if (select) { + for (int i = 0; i < localeCount; i++) { + if (locales[i].getCountry().length() > 0) { + if (locales[i].equals(locale)) + setSelectedIndex(n); + n += 1; + } + } + } + + firePropertyChange("locale", oldLocale, locale); + if(component != null) { + component.setLocale(l); + } + } + + /** + * Sets the locale. This is a bound property. + * + * @see #getLocale + */ + public void setLocale(Locale l) { + setLocale(l, true); + } + + /** + * Returns the locale. + */ + public Locale getLocale() { + return locale; + } + + /** + * Creates a JFrame with a JLocaleChooser inside and can be used for + * testing. + */ + static public void main(String[] s) { + JFrame frame = new JFrame("LocaleChooser"); + frame.getContentPane().add(new JLocaleChooser()); + frame.pack(); + frame.setVisible(true); + } + + private Locale[] locales; + private Locale locale; + private int localeCount; +} + diff --git a/JAVA/LofarUtils/src/com/toedter/components/JLocaleChooserBeanInfo.java b/JAVA/LofarUtils/src/com/toedter/components/JLocaleChooserBeanInfo.java new file mode 100644 index 0000000000000000000000000000000000000000..e2920435013e70e1a1bfe326b6ba4caf1e177bd8 --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/components/JLocaleChooserBeanInfo.java @@ -0,0 +1,38 @@ +/* + * JLocaleChooserBeanInfo.java - Bean Info for JLocaleChooser + * Copyright (C) 2004 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +package com.toedter.components; + + +/** + * A BeanInfo class for the JLocaleChooser bean. + * + * @author Kai Toedter + * @version $LastChangedRevision: 85 $ + * @version $LastChangedDate: 2006-04-28 13:50:52 +0200 (Fr, 28 Apr 2006) $ + */ +public class JLocaleChooserBeanInfo extends GenericBeanInfo { + /** + * Constructs a new BeanInfo class for the JLocaleChooser bean. + */ + public JLocaleChooserBeanInfo() { + super("JLocaleChooser", true); + } +} \ No newline at end of file diff --git a/JAVA/LofarUtils/src/com/toedter/components/JSpinField.java b/JAVA/LofarUtils/src/com/toedter/components/JSpinField.java new file mode 100644 index 0000000000000000000000000000000000000000..34f71f89c91e0ab0b0b0720361e793c8d0942879 --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/components/JSpinField.java @@ -0,0 +1,366 @@ +/* + * JSpinField.java - A spin field using a JSpinner (JDK 1.4) + * Copyright (C) 2004 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +package com.toedter.components; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Component; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.FocusEvent; +import java.awt.event.FocusListener; + +import javax.swing.BorderFactory; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JSpinner; +import javax.swing.JTextField; +import javax.swing.SpinnerNumberModel; +import javax.swing.SwingConstants; +import javax.swing.UIManager; +import javax.swing.event.CaretEvent; +import javax.swing.event.CaretListener; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; + +/** + * JSpinField is a numeric field with 2 spin buttons to increase or decrease the + * value. It has the same interface as the "old" JSpinField but uses a JSpinner + * internally (since J2SE SDK 1.4) rather than a scrollbar for emulating the + * spin buttons. + * + * @author Kai Toedter + * @version $LastChangedRevision: 85 $ + * @version $LastChangedDate: 2006-04-28 13:50:52 +0200 (Fr, 28 Apr 2006) $ + */ +public class JSpinField extends JPanel implements ChangeListener, CaretListener, ActionListener, + FocusListener { + private static final long serialVersionUID = 1694904792717740650L; + + protected JSpinner spinner; + + /** the text (number) field */ + protected JTextField textField; + protected int min; + protected int max; + protected int value; + protected Color darkGreen; + + /** + * Default JSpinField constructor. The valid value range is between + * Integer.MIN_VALUE and Integer.MAX_VALUE. The initial value is 0. + */ + public JSpinField() { + this(Integer.MIN_VALUE, Integer.MAX_VALUE); + } + + /** + * JSpinField constructor with given minimum and maximum vaues and initial + * value 0. + */ + public JSpinField(int min, int max) { + super(); + setName("JSpinField"); + this.min = min; + if (max < min) + max = min; + this.max = max; + value = 0; + if (value < min) + value = min; + if (value > max) + value = max; + + darkGreen = new Color(0, 150, 0); + setLayout(new BorderLayout()); + textField = new JTextField(); + textField.addCaretListener(this); + textField.addActionListener(this); + textField.setHorizontalAlignment(SwingConstants.RIGHT); + textField.setBorder(BorderFactory.createEmptyBorder()); + textField.setText(Integer.toString(value)); + textField.addFocusListener(this); + spinner = new JSpinner() { + private static final long serialVersionUID = -6287709243342021172L; + private JTextField textField = new JTextField(); + + public Dimension getPreferredSize() { + Dimension size = super.getPreferredSize(); + return new Dimension(size.width, textField.getPreferredSize().height); + } + }; + spinner.setEditor(textField); + spinner.addChangeListener(this); + // spinner.setSize(spinner.getWidth(), textField.getHeight()); + add(spinner, BorderLayout.CENTER); + } + + public void adjustWidthToMaximumValue() { + JTextField testTextField = new JTextField(Integer.toString(max)); + int width = testTextField.getPreferredSize().width; + int height = testTextField.getPreferredSize().height; + textField.setPreferredSize(new Dimension(width, height)); + textField.revalidate(); + } + + /** + * Is invoked when the spinner model changes + * + * @param e + * the ChangeEvent + */ + public void stateChanged(ChangeEvent e) { + SpinnerNumberModel model = (SpinnerNumberModel) spinner.getModel(); + int value = model.getNumber().intValue(); + setValue(value); + } + + /** + * Sets the value attribute of the JSpinField object. + * + * @param newValue + * The new value + * @param updateTextField + * true if text field should be updated + */ + protected void setValue(int newValue, boolean updateTextField, boolean firePropertyChange) { + int oldValue = value; + if (newValue < min) { + value = min; + } else if (newValue > max) { + value = max; + } else { + value = newValue; + } + + if (updateTextField) { + textField.setText(Integer.toString(value)); + textField.setForeground(Color.black); + } + + if (firePropertyChange) { + firePropertyChange("value", oldValue, value); + } + } + + /** + * Sets the value. This is a bound property. + * + * @param newValue + * the new value + * + * @see #getValue + */ + public void setValue(int newValue) { + setValue(newValue, true, true); + spinner.setValue(new Integer(value)); + } + + /** + * Returns the value. + * + * @return the value value + */ + public int getValue() { + return value; + } + + /** + * Sets the minimum value. + * + * @param newMinimum + * the new minimum value + * + * @see #getMinimum + */ + public void setMinimum(int newMinimum) { + min = newMinimum; + } + + /** + * Returns the minimum value. + * + * @return the minimum value + */ + public int getMinimum() { + return min; + } + + /** + * Sets the maximum value and adjusts the preferred width. + * + * @param newMaximum + * the new maximum value + * + * @see #getMaximum + */ + public void setMaximum(int newMaximum) { + max = newMaximum; + } + + /** + * Sets the horizontal alignment of the displayed value. + * + * @param alignment + * the horizontal alignment + */ + public void setHorizontalAlignment(int alignment) { + textField.setHorizontalAlignment(alignment); + } + + /** + * Returns the maximum value. + * + * @return the maximum value + */ + public int getMaximum() { + return max; + } + + /** + * Sets the font property. + * + * @param font + * the new font + */ + public void setFont(Font font) { + if (textField != null) { + textField.setFont(font); + } + } + + /** + * Sets the foreground + * + * @param fg + * the foreground + */ + public void setForeground(Color fg) { + if (textField != null) { + textField.setForeground(fg); + } + } + + /** + * After any user input, the value of the textfield is proofed. Depending on + * being an integer, the value is colored green or red. + * + * @param e + * the caret event + */ + public void caretUpdate(CaretEvent e) { + try { + int testValue = Integer.valueOf(textField.getText()).intValue(); + + if ((testValue >= min) && (testValue <= max)) { + textField.setForeground(darkGreen); + setValue(testValue, false, true); + } else { + textField.setForeground(Color.red); + } + } catch (Exception ex) { + if (ex instanceof NumberFormatException) { + textField.setForeground(Color.red); + } + + // Ignore all other exceptions, e.g. illegal state exception + } + + textField.repaint(); + } + + /** + * After any user input, the value of the textfield is proofed. Depending on + * being an integer, the value is colored green or red. If the textfield is + * green, the enter key is accepted and the new value is set. + * + * @param e + * Description of the Parameter + */ + public void actionPerformed(ActionEvent e) { + if (textField.getForeground().equals(darkGreen)) { + setValue(Integer.valueOf(textField.getText()).intValue()); + } + } + + /** + * Enable or disable the JSpinField. + * + * @param enabled + * The new enabled value + */ + public void setEnabled(boolean enabled) { + super.setEnabled(enabled); + spinner.setEnabled(enabled); + textField.setEnabled(enabled); + /* + * Fixes the background bug + * 4991597 and sets the background explicitely to a + * TextField.inactiveBackground. + */ + if (!enabled) { + textField.setBackground(UIManager.getColor("TextField.inactiveBackground")); + } + } + + /** + * Returns the year chooser's spinner (which allow the focus to be set to + * it). + * + * @return Component the spinner or null, if the month chooser has no + * spinner + */ + public Component getSpinner() { + return spinner; + } + + /** + * Creates a JFrame with a JSpinField inside and can be used for testing. + * + * @param s + * The command line arguments + */ + public static void main(String[] s) { + JFrame frame = new JFrame("JSpinField"); + frame.getContentPane().add(new JSpinField()); + frame.pack(); + frame.setVisible(true); + } + + /* + * (non-Javadoc) + * + * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent) + */ + public void focusGained(FocusEvent e) { + } + + /** + * The value of the text field is checked against a valid (green) value. If + * valid, the value is set and a property change is fired. + */ + public void focusLost(FocusEvent e) { + actionPerformed(null); + } +} \ No newline at end of file diff --git a/JAVA/LofarUtils/src/com/toedter/components/JSpinFieldBeanInfo.java b/JAVA/LofarUtils/src/com/toedter/components/JSpinFieldBeanInfo.java new file mode 100644 index 0000000000000000000000000000000000000000..bfcae071fe83384cf0892845ef64c29dd3ba4a64 --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/components/JSpinFieldBeanInfo.java @@ -0,0 +1,38 @@ +/* + * JSpinFieldBeanInfo.java - Bean Info for JSpinField + * Copyright (C) 2004 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +package com.toedter.components; + + +/** + * A BeanInfo class for the JLocaleChooser bean. + * + * @author Kai Toedter + * @version $LastChangedRevision: 85 $ + * @version $LastChangedDate: 2006-04-28 13:50:52 +0200 (Fr, 28 Apr 2006) $ + */ +public class JSpinFieldBeanInfo extends GenericBeanInfo { + /** + * Constructs a new BeanInfo class for the JLocaleChooser bean. + */ + public JSpinFieldBeanInfo() { + super("JSpinField", false); + } +} \ No newline at end of file diff --git a/JAVA/LofarUtils/src/com/toedter/components/JTitlePanel.java b/JAVA/LofarUtils/src/com/toedter/components/JTitlePanel.java new file mode 100644 index 0000000000000000000000000000000000000000..d725ccef1d3b8dbc2426002b6735e3b2817280ba --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/components/JTitlePanel.java @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2004 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +package com.toedter.components; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.GradientPaint; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Paint; + +import javax.swing.BorderFactory; +import javax.swing.Icon; +import javax.swing.JComponent; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.border.Border; + + +/** + * A simple JPanel with a border and a title + * + * @author Kai Toedter + * @version $LastChangedRevision: 85 $ + * @version $LastChangedDate: 2006-04-28 13:50:52 +0200 (Fr, 28 Apr 2006) $ + */ +public class JTitlePanel extends JPanel { + private static final long serialVersionUID = 9104873267039717087L; + protected JPanel northPanel; + protected JLabel label; + + /** + * Constructs a titled panel. + * + * @param title the title + * @param content the JComponent that contains the content + * @param outerBorder the outer border + */ + public JTitlePanel(String title, Icon icon, JComponent content, Border outerBorder) { + setLayout(new BorderLayout()); + + label = new JLabel(title, icon, JLabel.LEADING); + label.setForeground(Color.WHITE); + + GradientPanel titlePanel = new GradientPanel(Color.BLACK); + titlePanel.setLayout(new BorderLayout()); + titlePanel.add(label, BorderLayout.WEST); + int borderOffset = 2; + if(icon == null) { + borderOffset += 1; + } + titlePanel.setBorder(BorderFactory.createEmptyBorder(borderOffset, 4, borderOffset, 1)); + add(titlePanel, BorderLayout.NORTH); + + JPanel northPanel = new JPanel(); + northPanel.setLayout(new BorderLayout()); + northPanel.add(content,BorderLayout.NORTH); + northPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); + add(northPanel, BorderLayout.CENTER); + + if (outerBorder == null) { + setBorder(BorderFactory.createLineBorder(Color.GRAY)); + } else { + setBorder(BorderFactory.createCompoundBorder(outerBorder, + BorderFactory.createLineBorder(Color.GRAY))); + } + } + + public void setTitle(String label, Icon icon) { + this.label.setText(label); + this.label.setIcon(icon); + } + + private static class GradientPanel extends JPanel { + private static final long serialVersionUID = -6385751027379193053L; + + private GradientPanel(Color background) { + setBackground(background); + } + + public void paintComponent(Graphics g) { + super.paintComponent(g); + + if (isOpaque()) { + // Color controlColor = UIManager.getColor("control"); + // Color controlColor = new Color(252, 198, 82); + Color controlColor = new Color(99, 153, 255); + int width = getWidth(); + int height = getHeight(); + + Graphics2D g2 = (Graphics2D) g; + Paint oldPaint = g2.getPaint(); + g2.setPaint(new GradientPaint(0, 0, getBackground(), width, 0, + controlColor)); + g2.fillRect(0, 0, width, height); + g2.setPaint(oldPaint); + } + } + } +} diff --git a/JAVA/LofarUtils/src/com/toedter/components/LocaleEditor.java b/JAVA/LofarUtils/src/com/toedter/components/LocaleEditor.java new file mode 100644 index 0000000000000000000000000000000000000000..0f39204b877b06dd17f5169f9444e7a6d4add648 --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/components/LocaleEditor.java @@ -0,0 +1,88 @@ +/* + * LocaleEditor.java - a locale editor for JavaBeans + * Copyright (C) 2004 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +package com.toedter.components; + +import java.util.Calendar; +import java.util.Locale; + +/** + * Property editor for locales. + * + * @author Kai Toedter + * @version $LastChangedRevision: 85 $ + * @version $LastChangedDate: 2006-04-28 13:50:52 +0200 (Fr, 28 Apr 2006) $ + */ +public class LocaleEditor extends java.beans.PropertyEditorSupport { + private Locale[] locales; + private String[] localeStrings; + private Locale locale; + private int length; + + /** + * Default LocaleEditor constructor. + */ + public LocaleEditor() { + locale = Locale.getDefault(); + locales = Calendar.getAvailableLocales(); + length = locales.length; + localeStrings = new String[length]; + } + + /** + * Returns the locale strings. + * + * @return the locale strings + */ + public String[] getTags() { + for (int i = 0; i < length; i++) + localeStrings[i] = locales[i].getDisplayName(); + + return localeStrings; + } + + /** + * Sets the locale strings as text and invokes setValue( locale ). + * + * @param text + * the locale string text + * + * @throws IllegalArgumentException + * not used + */ + public void setAsText(String text) throws IllegalArgumentException { + for (int i = 0; i < length; i++) + if (text.equals(locales[i].getDisplayName())) { + locale = locales[i]; + setValue(locale); + + break; + } + } + + /** + * Returns the locale string as text. + * + * @return the locale string + */ + public String getAsText() { + return locale.getDisplayName(); + } +} \ No newline at end of file diff --git a/JAVA/LofarUtils/src/com/toedter/components/images/JLocaleChooserColor16.gif b/JAVA/LofarUtils/src/com/toedter/components/images/JLocaleChooserColor16.gif new file mode 100644 index 0000000000000000000000000000000000000000..7a983e3e0160c42e4bf2050e9ffc28f6ea01f3f7 Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/components/images/JLocaleChooserColor16.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/components/images/JLocaleChooserColor32.gif b/JAVA/LofarUtils/src/com/toedter/components/images/JLocaleChooserColor32.gif new file mode 100644 index 0000000000000000000000000000000000000000..9a0a467e471d963a8b2057f3a4940f36cf44956b Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/components/images/JLocaleChooserColor32.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/components/images/JLocaleChooserMono16.gif b/JAVA/LofarUtils/src/com/toedter/components/images/JLocaleChooserMono16.gif new file mode 100644 index 0000000000000000000000000000000000000000..4e99135db872f3d922959f14c55140c468ba1fe7 Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/components/images/JLocaleChooserMono16.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/components/images/JLocaleChooserMono32.gif b/JAVA/LofarUtils/src/com/toedter/components/images/JLocaleChooserMono32.gif new file mode 100644 index 0000000000000000000000000000000000000000..063817428989fb0904d03cc19556048ab2c2b2a6 Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/components/images/JLocaleChooserMono32.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/components/images/JSpinFieldColor16.gif b/JAVA/LofarUtils/src/com/toedter/components/images/JSpinFieldColor16.gif new file mode 100644 index 0000000000000000000000000000000000000000..0f2285f6d8b92de7027f688967e95480d08efd54 Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/components/images/JSpinFieldColor16.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/components/images/JSpinFieldColor32.gif b/JAVA/LofarUtils/src/com/toedter/components/images/JSpinFieldColor32.gif new file mode 100644 index 0000000000000000000000000000000000000000..f530f017b145a77401509b4f971b1359a6c468e9 Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/components/images/JSpinFieldColor32.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/components/images/JSpinFieldMono16.gif b/JAVA/LofarUtils/src/com/toedter/components/images/JSpinFieldMono16.gif new file mode 100644 index 0000000000000000000000000000000000000000..084d96dd071a264037449ab2cf2a077750b80151 Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/components/images/JSpinFieldMono16.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/components/images/JSpinFieldMono32.gif b/JAVA/LofarUtils/src/com/toedter/components/images/JSpinFieldMono32.gif new file mode 100644 index 0000000000000000000000000000000000000000..e38060a453867740ad082bf34abbbe6154de457f Binary files /dev/null and b/JAVA/LofarUtils/src/com/toedter/components/images/JSpinFieldMono32.gif differ diff --git a/JAVA/LofarUtils/src/com/toedter/plaf/JCalendarTheme.java b/JAVA/LofarUtils/src/com/toedter/plaf/JCalendarTheme.java new file mode 100644 index 0000000000000000000000000000000000000000..9302a09cc6a34783b367df15298174e9194c78d0 --- /dev/null +++ b/JAVA/LofarUtils/src/com/toedter/plaf/JCalendarTheme.java @@ -0,0 +1,128 @@ +/* + * Copyright (C) 2004 Kai Toedter + * kai@toedter.com + * www.toedter.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +package com.toedter.plaf; + +import java.awt.Font; + +import javax.swing.plaf.ColorUIResource; +import javax.swing.plaf.FontUIResource; +import javax.swing.plaf.metal.DefaultMetalTheme; + +/** + * The JCalendar theme. The colors are based on the theme "Experience Bue" of + * the great Plastic3D Look and feel by JGoddies (www.jgoodies.com) + * + * @author Kai Toedter + * @version $LastChangedRevision: 85 $ + * @version $LastChangedDate: 2006-04-28 13:50:52 +0200 (Fr, 28 Apr 2006) $ + */ +public class JCalendarTheme extends DefaultMetalTheme { + static final ColorUIResource secondary1 = new ColorUIResource(128, 128, 128); + static final ColorUIResource secondary2 = new ColorUIResource(189, 190, 176); + static final ColorUIResource secondary3 = new ColorUIResource(236, 233, 216); + static final ColorUIResource BLUE_LOW_MEDIUM = new ColorUIResource(166, 202, 240); + static final ColorUIResource BLUE_LOW_LIGHTEST = new ColorUIResource(195, 212, 232); + static final ColorUIResource BLUE_MEDIUM_DARKEST = new ColorUIResource(44, 73, 135); + static final ColorUIResource BLUE_MEDIUM_DARK = new ColorUIResource(49, 106, 196); + static final ColorUIResource BLUE_MEDIUM_MEDIUM = new ColorUIResource(85, 115, 170); + static final ColorUIResource ORANGE_FOCUS = new ColorUIResource(245, 165, 16); + static final ColorUIResource GREEN_CHECK = new ColorUIResource(33, 161, 33); + static final ColorUIResource WHITE = new ColorUIResource(255, 255, 255); + static final FontUIResource standardFont = new FontUIResource( + new Font("Tahoma", Font.PLAIN, 11)); + + /** + * Creates a new JCalendarTheme object. + */ + public JCalendarTheme() { + } + + public String getName() { + return "JCalendar Theme"; + } + + protected Font getFont() { + return new Font("Dialog", Font.PLAIN, 12); + } + + public FontUIResource getSystemTextFont() { + return standardFont; + } + + public FontUIResource getUserTextFont() { + return standardFont; + } + + public FontUIResource getControlTextFont() { + return standardFont; + } + + public FontUIResource getMenuTextFont() { + return standardFont; + } + + protected ColorUIResource getPrimary1() { + return BLUE_MEDIUM_DARK; + } + + protected ColorUIResource getPrimary2() { + return BLUE_LOW_MEDIUM; + } + + protected ColorUIResource getPrimary3() { + return BLUE_LOW_LIGHTEST; + } + + protected ColorUIResource getSecondary1() { + return secondary1; + } + + protected ColorUIResource getSecondary2() { + return secondary2; + } + + protected ColorUIResource getSecondary3() { + return secondary3; + } + + public ColorUIResource getFocusColor() { + return ORANGE_FOCUS; + } + + public ColorUIResource getPrimaryControlShadow() { + return getPrimary3(); + } + + public ColorUIResource getMenuSelectedBackground() { + return getPrimary1(); + } + + public ColorUIResource getMenuSelectedForeground() { + return WHITE; + } + + public ColorUIResource getMenuItemBackground() { + return WHITE; + } + + public ColorUIResource getToggleButtonCheckColor() { + return GREEN_CHECK; + } +} \ No newline at end of file diff --git a/JAVA/LofarUtils/src/jcalendar.manifest b/JAVA/LofarUtils/src/jcalendar.manifest new file mode 100644 index 0000000000000000000000000000000000000000..3c2b819478bc20d2023b1958d013242d520b4298 --- /dev/null +++ b/JAVA/LofarUtils/src/jcalendar.manifest @@ -0,0 +1,23 @@ +Manifest-Version: 1.0 +Main-Class: com.toedter.calendar.demo.JCalendarDemo + +Name: com/toedter/components/JLocaleChooser.class +Java-Bean: True + +Name: com/toedter/calendar/JCalendar.class +Java-Bean: True + +Name: com/toedter/calendar/JYearChooser.class +Java-Bean: True + +Name: com/toedter/calendar/JMonthChooser.class +Java-Bean: True + +Name: com/toedter/calendar/JDayChooser.class +Java-Bean: True + +Name: com/toedter/calendar/JDateChooser.class +Java-Bean: True + +Name: com/toedter/components/JSpinField.class +Java-Bean: True diff --git a/JAVA/LofarUtils/src/nl/astron/lofar/lofarutils/DateTimeChooser.form b/JAVA/LofarUtils/src/nl/astron/lofar/lofarutils/DateTimeChooser.form new file mode 100644 index 0000000000000000000000000000000000000000..50fdfdc516edf1be7de827d0314ea9306b076da9 --- /dev/null +++ b/JAVA/LofarUtils/src/nl/astron/lofar/lofarutils/DateTimeChooser.form @@ -0,0 +1,135 @@ +<?xml version="1.0" encoding="UTF-8" ?> + +<Form version="1.3" type="org.netbeans.modules.form.forminfo.JPanelFormInfo"> + <AuxValues> + <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/> + <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/> + <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/> + </AuxValues> + + <Layout> + <DimensionLayout dim="0"> + <Group type="103" groupAlignment="0" attributes="0"> + <Group type="102" alignment="0" attributes="0"> + <EmptySpace max="-2" attributes="0"/> + <Group type="103" groupAlignment="1" attributes="0"> + <Component id="calendar" alignment="0" pref="176" max="32767" attributes="1"/> + <Group type="102" alignment="1" attributes="0"> + <Group type="103" groupAlignment="1" attributes="0"> + <Group type="102" alignment="0" attributes="0"> + <Component id="now" min="-2" max="-2" attributes="0"/> + <EmptySpace max="-2" attributes="0"/> + <Component id="zero" min="-2" max="-2" attributes="0"/> + </Group> + <Group type="102" alignment="1" attributes="0"> + <Group type="103" groupAlignment="0" attributes="0"> + <Component id="jLabel1" alignment="0" min="-2" max="-2" attributes="0"/> + <Component id="hours" alignment="0" min="-2" pref="37" max="-2" attributes="0"/> + </Group> + <EmptySpace pref="26" max="32767" attributes="0"/> + <Group type="103" groupAlignment="0" attributes="0"> + <Group type="102" attributes="0"> + <Component id="mins" alignment="0" pref="50" max="32767" attributes="1"/> + <EmptySpace min="-2" max="-2" attributes="0"/> + </Group> + <Component id="jLabel2" min="-2" max="-2" attributes="1"/> + </Group> + <EmptySpace min="-2" pref="19" max="-2" attributes="0"/> + <Group type="103" groupAlignment="1" max="-2" attributes="0"> + <Component id="secs" alignment="0" max="32767" attributes="1"/> + <Component id="jLabel3" alignment="0" max="32767" attributes="1"/> + </Group> + </Group> + </Group> + <EmptySpace min="-2" max="-2" attributes="0"/> + </Group> + </Group> + <EmptySpace min="-2" max="-2" attributes="0"/> + </Group> + </Group> + </DimensionLayout> + <DimensionLayout dim="1"> + <Group type="103" groupAlignment="0" attributes="0"> + <Group type="102" alignment="1" attributes="0"> + <EmptySpace max="32767" attributes="0"/> + <Component id="calendar" min="-2" max="-2" attributes="0"/> + <EmptySpace max="-2" attributes="0"/> + <Group type="103" groupAlignment="3" attributes="0"> + <Component id="jLabel1" alignment="3" min="-2" max="-2" attributes="0"/> + <Component id="jLabel2" alignment="3" min="-2" max="-2" attributes="0"/> + <Component id="jLabel3" alignment="3" min="-2" max="-2" attributes="0"/> + </Group> + <EmptySpace max="-2" attributes="0"/> + <Group type="103" groupAlignment="1" attributes="0"> + <Component id="hours" min="-2" max="-2" attributes="0"/> + <Component id="mins" min="-2" max="-2" attributes="0"/> + <Component id="secs" min="-2" max="-2" attributes="0"/> + </Group> + <EmptySpace min="-2" pref="20" max="-2" attributes="0"/> + <Group type="103" groupAlignment="3" attributes="0"> + <Component id="now" alignment="3" min="-2" max="-2" attributes="0"/> + <Component id="zero" alignment="3" min="-2" max="-2" attributes="0"/> + </Group> + <EmptySpace min="-2" pref="24" max="-2" attributes="0"/> + </Group> + </Group> + </DimensionLayout> + </Layout> + <SubComponents> + <Component class="com.toedter.calendar.JCalendar" name="calendar"> + </Component> + <Component class="com.toedter.components.JSpinField" name="hours"> + <AuxValues> + <AuxValue name="JavaCodeGenerator_CreateCodeCustom" type="java.lang.String" value="new JSpinField(0,23);"/> + <AuxValue name="JavaCodeGenerator_CreateCodePost" type="java.lang.String" value="hours.adjustWidthToMaximumValue();"/> + </AuxValues> + </Component> + <Component class="javax.swing.JLabel" name="jLabel1"> + <Properties> + <Property name="text" type="java.lang.String" value="Hours:"/> + </Properties> + </Component> + <Component class="javax.swing.JLabel" name="jLabel2"> + <Properties> + <Property name="text" type="java.lang.String" value="Minutes:"/> + </Properties> + </Component> + <Component class="javax.swing.JLabel" name="jLabel3"> + <Properties> + <Property name="text" type="java.lang.String" value="Seconds:"/> + </Properties> + </Component> + <Component class="com.toedter.components.JSpinField" name="mins"> + <AuxValues> + <AuxValue name="JavaCodeGenerator_CreateCodeCustom" type="java.lang.String" value="new JSpinField(0,59);"/> + <AuxValue name="JavaCodeGenerator_CreateCodePost" type="java.lang.String" value="mins.adjustWidthToMaximumValue();"/> + </AuxValues> + </Component> + <Component class="com.toedter.components.JSpinField" name="secs"> + <AuxValues> + <AuxValue name="JavaCodeGenerator_CreateCodeCustom" type="java.lang.String" value="new JSpinField(0,59);"/> + <AuxValue name="JavaCodeGenerator_CreateCodePost" type="java.lang.String" value="secs.adjustWidthToMaximumValue();"/> + </AuxValues> + </Component> + <Component class="javax.swing.JButton" name="now"> + <Properties> + <Property name="text" type="java.lang.String" value="Set to now"/> + <Property name="toolTipText" type="java.lang.String" value="Set Date & Time to now"/> + </Properties> + <Events> + <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="nowActionPerformed"/> + </Events> + </Component> + <Component class="javax.swing.JButton" name="zero"> + <Properties> + <Property name="text" type="java.lang.String" value="Clear time"/> + <Property name="toolTipText" type="java.lang.String" value="Empty settings"/> + </Properties> + <Events> + <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="zeroActionPerformed"/> + </Events> + </Component> + </SubComponents> +</Form> diff --git a/JAVA/LofarUtils/src/nl/astron/lofar/lofarutils/DateTimeChooser.java b/JAVA/LofarUtils/src/nl/astron/lofar/lofarutils/DateTimeChooser.java new file mode 100644 index 0000000000000000000000000000000000000000..8227b5ad472aa7fad465ca7abc1500d59c50fb85 --- /dev/null +++ b/JAVA/LofarUtils/src/nl/astron/lofar/lofarutils/DateTimeChooser.java @@ -0,0 +1,366 @@ +/* + * DateTimeChooser.java + * + * Created on 28 juni 2007, 10:58 + */ + +package nl.astron.lofar.lofarutils; + +import com.toedter.components.JSpinField; +import java.awt.Component; +import java.beans.PropertyChangeEvent; +import java.util.Calendar; +import java.util.Date; +import javax.swing.JOptionPane; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; +import javax.swing.event.EventListenerList; + +/** + * A Swing component that lets choose date and time. It is just a thin + * wrapper around an open-source package <em>jcalendar</em>, written + * by <a href="http://www.toedter.com">Kai Toedter</a>, and released + * under GNU Lesser General Public License. <p> + * + * Regarding functionality, the wrapper is similar to the standard + * Java <tt>JColorChooser</tt>. Which means that a date-time chooser + * can be either instantiate as a standalone component, or as part of + * a modal dialog. In former case, the user can listen to the + * date-time changes in the chooser by implementing a standard + * <tt>ChangeListener</tt>, in the later case the returned value of + * {@link #showDialog showDialog} contains the selected date-time. <p> + * + * It also allows to add a user-defined <em>preview panel</em> that + * can indicate (in any user-defined format) directly in the chooser + * window what date is selected. <p> + * + * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A> + * @version $Id$ + * + * Minor changes to adepts to LOFAR + */ +public class DateTimeChooser + extends javax.swing.JPanel + implements java.beans.PropertyChangeListener { + + + + protected Date initialDate = null; + protected transient ChangeEvent changeEvent = null; + protected EventListenerList lList = new EventListenerList(); + + /** Creates a DateTimeChooser with the current date as an initial date */ + public DateTimeChooser() { + this(null); + initComponents(); + } + + /** Creates a DateTimeChooser with the given date as an initial date */ + public DateTimeChooser(Date aDate) { + this.initialDate = aDate; + initComponents(); + createItself(); + } + + /** Creates this component */ + protected void createItself() { + hours.addPropertyChangeListener(this); + mins.addPropertyChangeListener(this); + secs.addPropertyChangeListener(this); + + calendar.setWeekOfYearVisible(false); + calendar.addPropertyChangeListener(this); + calendar.getYearChooser().addPropertyChangeListener(this); + + setDate (initialDate == null ? new Date() : initialDate); + } + + /** gets the current date from the date-time chooser */ + public Date getDate() { + Calendar cal = Calendar.getInstance(); + cal.setTime(calendar.getDate()); + cal.set(Calendar.HOUR_OF_DAY,hours.getValue()); + cal.set(Calendar.MINUTE,mins.getValue()); + cal.set(Calendar.SECOND,secs.getValue()); + + return cal.getTime(); + } + + /** sets the given date as a new value for the date-time chooser <p> + * + *@param newDate to be set into the chooser + */ + public void setDate(Date newDate) { + Calendar cal = Calendar.getInstance(); + cal.setTime(newDate); + calendar.setDate(newDate); + hours.setValue(cal.get(Calendar.HOUR_OF_DAY)); + mins.setValue(cal.get(Calendar.MINUTE)); + secs.setValue(cal.get(Calendar.SECOND)); + } + + /** Returns a date that was used to initiate this date-time chooser instance. + * It can be null. + */ + public Date getInitialDate() { + return initialDate; + } + + /************************************************************************* + * Shows a modal date-time chooser dialog and blocks until the + * dialog is hidden. The dialog has three buttons: OK, Empty, + * Cancel. <p> + * + * If the user presses the "OK" button, then this method + * hides/disposes the dialog and returns the selected date. <p> + * + * If the user presses the "Cancel" button or closes the dialog + * without pressing "OK", then this method hides/disposes the + * dialog and returns the initial date (which could have been + * null). <p> + * + * If the user presses the "Empty" button, then this method + * hides/disposes the dialog and returns null. It indicates that + * no date is selected (even though that might have been a date as + * an initial value). <p> + * + * @param parent is the parent Component for the dialog + * @param title contains the dialog's title + * @param initialDate is shown when the dialog starts; if this is + * null the current date is shown + * + * @return the selected date (if OK pressed), the initial date (if + * Cancel presed), or null (if Empty pressed) + * + *************************************************************************/ + public static Date showDialog (Component parent, + String title, + Date initialDate) { + return showDialog (parent, title, new DateTimeChooser (initialDate)); + } + + /************************************************************************* + * Shows a modal date-time chooser dialog and blocks until the + * dialog is hidden. The dialog has three buttons: OK, Empty, + * Cancel. See details how the buttons are dealt with in {@link + * #showDialog(Component,String,Date) showDialog}. <p> + * + * This method allows to create an instance of a date-time chooser + * separately, and perhaps to customize it (e.g. by calling + * <tt>chooser.setPreviewPanel (myPreviewPanel)</tt>) before it is + * used in a modal dialog. <p> + * + * @param parent is the parent Component for the dialog + * @param title contains the dialog's title + * @param chooser is the chooser instance that was created + * separately and will be used in this dialog + * + * @return the selected date (if OK pressed), the initial date (if + * Cancel presed), or null (if Empty pressed) + * + *************************************************************************/ + public static Date showDialog (Component parent, + String title, + DateTimeChooser chooser) { + String[] buttons = new String[] { "OK", "Empty", "Cancel"}; + int selected = + JOptionPane.showOptionDialog (parent, + chooser, + title, + JOptionPane.YES_NO_OPTION, + JOptionPane.PLAIN_MESSAGE, + null, + buttons, + null); + if (selected == 0) + return chooser.getDate(); // 'ok' selected + else if (selected == 1) + return null; // 'empty' selected + else + return chooser.getInitialDate(); // cancelled + } + + + + /** This method is called from within the constructor to + * initialize the form. + * WARNING: Do NOT modify this code. The content of this method is + * always regenerated by the Form Editor. + */ + // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents + private void initComponents() { + calendar = new com.toedter.calendar.JCalendar(); + hours = new JSpinField(0,23); + hours.adjustWidthToMaximumValue(); + jLabel1 = new javax.swing.JLabel(); + jLabel2 = new javax.swing.JLabel(); + jLabel3 = new javax.swing.JLabel(); + mins = new JSpinField(0,59); + mins.adjustWidthToMaximumValue(); + secs = new JSpinField(0,59); + secs.adjustWidthToMaximumValue(); + now = new javax.swing.JButton(); + zero = new javax.swing.JButton(); + + jLabel1.setText("Hours:"); + + jLabel2.setText("Minutes:"); + + jLabel3.setText("Seconds:"); + + now.setText("Set to now"); + now.setToolTipText("Set Date & Time to now"); + now.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + nowActionPerformed(evt); + } + }); + + zero.setText("Clear time"); + zero.setToolTipText("Empty settings"); + zero.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + zeroActionPerformed(evt); + } + }); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); + this.setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) + .addComponent(calendar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) + .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() + .addComponent(now) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(zero)) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(jLabel1) + .addComponent(hours, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addComponent(mins, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)) + .addComponent(jLabel2)) + .addGap(19, 19, 19) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) + .addComponent(secs, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))) + .addContainerGap()) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(calendar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jLabel1) + .addComponent(jLabel2) + .addComponent(jLabel3)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) + .addComponent(hours, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(mins, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(secs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGap(20, 20, 20) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(now) + .addComponent(zero)) + .addGap(24, 24, 24)) + ); + }// </editor-fold>//GEN-END:initComponents + + private void zeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_zeroActionPerformed + hours.setValue(0); + mins.setValue(0); + secs.setValue(0); + }//GEN-LAST:event_zeroActionPerformed + + private void nowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nowActionPerformed + DateTimeChooser.this.setDate(new Date()); + }//GEN-LAST:event_nowActionPerformed + + + + // Variables declaration - do not modify//GEN-BEGIN:variables + private com.toedter.calendar.JCalendar calendar; + private com.toedter.components.JSpinField hours; + private javax.swing.JLabel jLabel1; + private javax.swing.JLabel jLabel2; + private javax.swing.JLabel jLabel3; + private com.toedter.components.JSpinField mins; + private javax.swing.JButton now; + private com.toedter.components.JSpinField secs; + private javax.swing.JButton zero; + // End of variables declaration//GEN-END:variables + + /** + * Utility field holding the ChangeListener. + */ + private transient javax.swing.event.ChangeListener changeListener = null; + + /** + * Registers ChangeListener to receive events. + * @param listener The listener to register. + */ + public synchronized void addChangeListener(javax.swing.event.ChangeListener listener) throws java.util.TooManyListenersException { + if (changeListener != null) { + throw new java.util.TooManyListenersException (); + } + lList.add(ChangeListener.class,listener); + } + + /** + * Removes ChangeListener from the list of listeners. + * @param listener The listener to remove. + */ + public synchronized void removeChangeListener(javax.swing.event.ChangeListener listener) { + lList.remove (ChangeListener.class, listener); + } + + /************************************************************************* + * Returns an array of all the <code>ChangeListener</code>s. <p> + * + * @return all of the <code>ChangeListener</code>s added, or an empty + * array if no listeners have been added + *************************************************************************/ + public ChangeListener[] getChangeListeners() { + return (ChangeListener[])lList + .getListeners (ChangeListener.class); + } + + /** + * Notifies the registered listener about the event. + * + * @param event The event to be fired + */ + private void fireStateChanged() { + Object[] listeners = lList.getListenerList(); + for (int i = listeners.length - 2; i >= 0; i -=2 ) { + if (listeners[i] == ChangeListener.class) { + if (changeEvent == null) { + changeEvent = new ChangeEvent (this); + } + ((ChangeListener)listeners [i+1]).stateChanged(changeEvent); + } + } + } + + + /************************************************************************* + * Implementing PropertyChangeListener. This implementation only + * propagates all property change events as a ChangeEvent. + *************************************************************************/ + public void propertyChange (PropertyChangeEvent evt) { + fireStateChanged(); + } + +}