001: package wingset;
002:
003: import java.awt.event.ActionEvent;
004: import java.awt.event.ActionListener;
005: import java.text.DateFormat;
006: import java.text.ParseException;
007: import java.text.ParsePosition;
008: import java.text.SimpleDateFormat;
009: import java.util.Calendar;
010: import java.util.Date;
011: import java.util.GregorianCalendar;
012:
013: import javax.swing.*;
014:
015: import org.wings.*;
016: import org.wings.text.SDateFormatter;
017: import org.wingx.XCalendar;
018:
019: public class XCalendarExample extends WingSetPane {
020:
021: protected SComponent createControls() {
022: return null;
023: }
024:
025: public SComponent createExample() {
026: SGridLayout layout = new SGridLayout(3);
027: layout.setHgap(15);
028: SPanel panel = new SPanel(layout);
029:
030: //
031: // Regular nullable calendar
032: final DateFormat nullableDateFormat = new NullableDateFormatter();
033: panel.add(new SLabel("Calendar: ", SConstants.RIGHT_ALIGN));
034: final XCalendar nullablXCalendar = new XCalendar(
035: new SDateFormatter(nullableDateFormat));
036: nullablXCalendar.setNullable(true);
037: nullablXCalendar.setDate(null);
038: panel.add(nullablXCalendar);
039:
040: final SLabel valueLabel1 = new SLabel("< press submit >");
041: panel.add(valueLabel1);
042:
043: //
044: // Date spinner example
045: Calendar calendar = new GregorianCalendar();
046: Date initDate = calendar.getTime();
047:
048: calendar.add(Calendar.YEAR, -50);
049: Date earliestDate = calendar.getTime();
050:
051: calendar.add(Calendar.YEAR, 100);
052: Date latestDate = calendar.getTime();
053:
054: final SSpinner spinner = new SSpinner(new SpinnerDateModel(
055: initDate, earliestDate, latestDate, Calendar.MONTH));
056: final CalendarEditor calendarEditor = new CalendarEditor(
057: spinner, new SDateFormatter(DateFormat
058: .getDateInstance()));
059: spinner.setEditor(calendarEditor);
060:
061: panel.add(new SLabel("Spinner: ", SConstants.RIGHT_ALIGN));
062: panel.add(spinner);
063:
064: final SLabel valueLabel2 = new SLabel("< press submit >");
065: panel.add(valueLabel2);
066:
067: // For debugging purposes. Maybe ommit those labels
068: final SButton button = new SButton("submit input");
069: button.addActionListener(new ActionListener() {
070: public void actionPerformed(ActionEvent e) {
071: valueLabel1.setText("" + nullablXCalendar.getDate());
072: valueLabel2.setText(""
073: + calendarEditor.getCalendar().getDate());
074: }
075: });
076: panel.add(new SLabel());
077: panel.add(button);
078:
079: return panel;
080:
081: }
082:
083: public static class CalendarEditor extends SSpinner.DefaultEditor {
084: private XCalendar calendar = null;
085:
086: public CalendarEditor(SSpinner spinner, SDateFormatter formatter) {
087: super (spinner);
088:
089: removeAll();
090:
091: calendar = new XCalendar(formatter);
092: calendar.getFormattedTextField().setColumns(15);
093:
094: add(calendar);
095:
096: }
097:
098: public XCalendar getCalendar() {
099: return calendar;
100: }
101:
102: public SFormattedTextField getTextField() {
103: return calendar.getFormattedTextField();
104: }
105: }
106:
107: /**
108: * A simple date formatter that parses and allows empty strings as <code>null</code>.
109: */
110: private static class NullableDateFormatter extends SimpleDateFormat {
111: public NullableDateFormatter() {
112: super ("dd.MM.yyyy");
113: }
114:
115: public Object parseObject(String source) throws ParseException {
116: if (source == null || source.trim().length() == 0) {
117: return null;
118: }
119: return super .parseObject(source);
120: }
121:
122: public Date parse(String text, ParsePosition pos) {
123: if (text == null || text.trim().length() == 0) {
124: return null;
125: }
126: return super.parse(text, pos);
127: }
128: }
129: }
|