2
* Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
3
* California 95054, U.S.A. All rights reserved.
5
* This library is free software; you can redistribute it and/or modify it under
6
* the terms of the GNU Lesser General Public License as published by the Free
7
* Software Foundation; either version 2.1 of the License, or (at your option)
10
* This library is distributed in the hope that it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15
* You should have received a copy of the GNU Lesser General Public License
16
* along with this library; if not, write to the Free Software Foundation, Inc.,
17
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
package org.jdesktop.swingx;
21
import java.awt.Component;
22
import java.awt.Container;
23
import java.awt.LayoutManager;
24
import java.awt.Window;
25
import java.awt.event.ActionEvent;
26
import java.awt.event.ActionListener;
27
import java.awt.event.FocusEvent;
28
import java.awt.event.FocusListener;
29
import java.awt.event.InputEvent;
30
import java.awt.event.MouseEvent;
31
import java.beans.PropertyChangeEvent;
32
import java.beans.PropertyChangeListener;
33
import java.text.DateFormat;
34
import java.text.SimpleDateFormat;
35
import java.util.Calendar;
36
import java.util.Date;
37
import java.util.Locale;
38
import java.util.TimeZone;
39
import java.util.logging.Logger;
41
import javax.swing.AbstractAction;
42
import javax.swing.Action;
43
import javax.swing.Box;
44
import javax.swing.JCheckBox;
45
import javax.swing.JComboBox;
46
import javax.swing.JComponent;
47
import javax.swing.JFormattedTextField;
48
import javax.swing.JLabel;
49
import javax.swing.JMenu;
50
import javax.swing.JMenuBar;
51
import javax.swing.JPanel;
52
import javax.swing.JPopupMenu;
53
import javax.swing.JSpinner;
54
import javax.swing.JTabbedPane;
55
import javax.swing.JTextArea;
56
import javax.swing.JTextField;
57
import javax.swing.SpinnerDateModel;
58
import javax.swing.SwingUtilities;
59
import javax.swing.UIManager;
60
import javax.swing.UnsupportedLookAndFeelException;
61
import javax.swing.JSpinner.DefaultEditor;
62
import javax.swing.UIManager.LookAndFeelInfo;
63
import javax.swing.event.DocumentEvent;
64
import javax.swing.event.DocumentListener;
65
import javax.swing.event.PopupMenuEvent;
66
import javax.swing.event.PopupMenuListener;
67
import javax.swing.text.DateFormatter;
68
import javax.swing.text.DefaultFormatterFactory;
69
import javax.swing.text.JTextComponent;
71
import org.jdesktop.swingx.action.AbstractActionExt;
72
import org.jdesktop.swingx.calendar.DateSelectionModel;
73
import org.jdesktop.swingx.calendar.DaySelectionModel;
74
import org.jdesktop.swingx.calendar.SingleDaySelectionModel;
75
import org.jdesktop.swingx.calendar.DateSelectionModel.SelectionMode;
76
import org.jdesktop.test.VerticalLayoutPref;
79
* Simple tests to ensure that the {@code JXDatePicker} can be instantiated and
82
* JW: being lazy - added visuals for <code>JXMonthView</code> as well.
84
* @author Karl Schaefer
86
public class JXDatePickerVisualCheck extends InteractiveTestCase {
87
@SuppressWarnings("all")
88
private static final Logger LOG = Logger
89
.getLogger(JXDatePickerVisualCheck.class.getName());
90
@SuppressWarnings("unused")
91
private Calendar calendar;
92
/** flag to decide if the menubar should be created */
93
private boolean showMenu;
95
public JXDatePickerVisualCheck() {
96
super("JXDatePicker Test");
99
public static void main(String[] args) throws Exception {
100
UIManager.put("JXDatePicker.forceZoomable", Boolean.TRUE);
101
// setSystemLF(true);
102
JXDatePickerVisualCheck test = new JXDatePickerVisualCheck();
105
// test.runInteractiveTests();
106
// test.runInteractiveTests("interactive.*PrefSize.*");
107
// test.runInteractiveTests("interactive.*Keep.*");
108
// test.runInteractiveTests("interactive.*Multiple.*");
109
// test.runInteractiveTests("interactive.*Editable.*");
110
// test.runInteractiveTests("interactive.*Enable.*");
111
test.runInteractiveTests("interactive.*Popup.*");
112
// test.runInteractiveTests("interactive.*Event.*");
113
} catch (Exception e) {
114
System.err.println("exception when executing interactive tests:");
119
public void interactivePopupMenuListener() {
120
JXDatePicker picker = new JXDatePicker();
121
PopupMenuListener l = new PopupMenuListener() {
124
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
129
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
130
LOG.info("invisible");
134
public void popupMenuCanceled(PopupMenuEvent e) {
135
LOG.info("canceled");
138
picker.addPopupMenuListener(l);
139
showInFrame(picker, "picker: notification?");
142
public void interactivePopupMenuListenerCompare() {
143
JTextArea textArea = new JTextArea(10, 20);
144
JPopupMenu picker = new JPopupMenu();
145
picker.add("just something");
146
PopupMenuListener l = new PopupMenuListener() {
149
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
154
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
155
LOG.info("invisible");
159
public void popupMenuCanceled(PopupMenuEvent e) {
160
LOG.info("canceled");
163
textArea.setComponentPopupMenu(picker);
164
picker.addPopupMenuListener(l);
165
showInFrame(textArea, "plain Popup: notification");
168
* Issue #940-swingx: support multiple selection in picker.
169
* Nothing out-off-the-box, trying to manipulate the mouseEvent.
171
* No success: no way to unselect
173
public void interactiveMultipleSelectionWithoutCtrl() {
174
JXMonthView monthView = new JXMonthView(new Date()) {
177
protected void processMouseEvent(MouseEvent e) {
178
// change the modifiers to believe that control key is down
179
// int modifiers = e.getModifiers() | InputEvent.CTRL_DOWN_MASK;
180
int modifiers = e.getModifiers() | InputEvent.CTRL_MASK;
182
MouseEvent myME = new MouseEvent(
183
(Component) e.getSource(),
186
modifiers, // my changed modifier
194
super.processMouseEvent(myME);
195
// super.processMouseEvent(e);
199
monthView.setSelectionMode(SelectionMode.MULTIPLE_INTERVAL_SELECTION);
200
JXDatePicker picker = new JXDatePicker();
201
picker.setMonthView(monthView);
202
JXFrame frame = showInFrame(picker, "multiple selection without ctrl");
207
* Issue #940-swingx: support multiple selection in picker.
208
* Nothing out-off-the-box, trying to implement custom
209
* dateSelectionModel with overridden setSelectionInterval.
210
* Looks halfway okay for mouse-interaction, completely
211
* unusable with keyboard interaction.
213
public void interactiveMultipleSelectionTogglingModel() {
214
DateSelectionModel model = new ToggleSelectionModel();
215
model.setSelectionMode(SelectionMode.MULTIPLE_INTERVAL_SELECTION);
216
JXMonthView monthView = new JXMonthView(new Date(), model);
217
JXDatePicker picker = new JXDatePicker();
218
picker.setMonthView(monthView);
219
JXFrame frame = showInFrame(picker, "multiple selection - toggle");
224
* Trying to interfere in setSelectionInterval. Idea from Sunacle dev forum:
225
* <a href=http://forums.sun.com/thread.jspa?threadID=5431797&tstart=0>
226
* MultiSelect JList without ctrl
229
public static class ToggleSelectionModel extends DaySelectionModel {
232
public void setSelectionInterval(Date startDate, Date endDate) {
233
if (!isSameDay(startDate, endDate)) {
234
super.setSelectionInterval(startDate, endDate);
237
if (isSelected(startDate)) {
238
removeSelectionInterval(startDate, startDate);
240
addSelectionInterval(startDate, startDate);
248
* Issue #940-swingx: support multiple selection in picker.
249
* Nothing out-off-the-box, trying to implement custom
250
* dateSelectionModel. No success.
252
public void interactiveMultipleSelectionAddingModel() {
253
DateSelectionModel model = new AddingDaySelectionModel();
254
model.setSelectionMode(SelectionMode.MULTIPLE_INTERVAL_SELECTION);
255
JXMonthView monthView = new JXMonthView(new Date(), model) {
258
public Date getSelectionDate() {
259
if (getSelectionModel() instanceof AddingDaySelectionModel) {
260
return ((AddingDaySelectionModel) getSelectionModel()).getLastAddedDate();
262
return super.getSelectionDate();
266
JXDatePicker picker = new JXDatePicker();
267
picker.setMonthView(monthView);
268
JXFrame frame = showInFrame(picker, "multiple selection");
273
* Custom model which always adds selection dates.
275
public static class AddingDaySelectionModel extends DaySelectionModel {
280
public void setSelectionInterval(Date startDate, Date endDate) {
281
addSelectionInterval(startDate, endDate);
285
public void addSelectionInterval(Date startDate, Date endDate) {
286
if (endDate.before(startDate)) return;
287
super.addSelectionInterval(startDate, endDate);
288
// PENDING: need to do better to cope with unselectables
289
if (isSelected(endDate)) {
291
} else if (isSelected(startDate)) {
292
lastAdded = startDate;
300
public void clearSelection() {
302
super.clearSelection();
305
public Date getLastAddedDate() {
310
* Issue #1196-swingx: must not allow edits if disabled.
313
public void interactiveNotEnabled() {
314
final JXDatePicker picker = new JXDatePicker();
315
Calendar cal = picker.getMonthView().getCalendar();
316
cal.add(Calendar.MONTH, 5);
317
picker.setDate(cal.getTime());
318
JXFrame frame = wrapInFrame(picker, "enabled?");
319
Action enabled = new AbstractAction("toggle enabled") {
322
public void actionPerformed(ActionEvent e) {
323
picker.setEnabled(!picker.isEnabled());
327
addAction(frame, enabled);
332
* Issue #910-swingx: commitToday must not be allowed if field not editable.
335
public void interactiveNotEditableCommitToday() {
336
JXDatePicker picker = new JXDatePicker();
337
Calendar cal = picker.getMonthView().getCalendar();
338
cal.add(Calendar.MONTH, 5);
339
picker.setDate(cal.getTime());
340
picker.setEditable(false);
341
showInFrame(picker, "not editable ");
345
* Issue #910-swingx: commitToday must not be allowed if field not editable.
348
public void interactiveNotEditableCompareTextField() {
349
final JXDatePicker picker = new JXDatePicker(new Date());
350
final JFormattedTextField field = new JFormattedTextField(picker.getDate());
351
field.setEditable(false);
352
PropertyChangeListener l = new PropertyChangeListener() {
354
public void propertyChange(PropertyChangeEvent evt) {
355
if ("date".equals(evt.getPropertyName())) {
356
if (picker.getDate() != null)
357
field.setValue(picker.getDate());
362
picker.addPropertyChangeListener(l);
363
JXPanel panel = new JXPanel();
366
showInFrame(panel, "not editable field ");
369
* From forum: have spinner with the same timezone.
371
public void interactiveTimeZoneFormat() {
372
final JSpinner spinner = new JSpinner(new SpinnerDateModel());
373
final JXDatePicker picker = new JXDatePicker();
374
picker.setTimeZone(TimeZone.getTimeZone("GMT-10"));
375
picker.setDate(new Date());
376
spinner.setValue(picker.getDate());
377
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss", picker.getLocale());
378
format.setTimeZone(picker.getTimeZone());
379
JFormattedTextField textField = ((DefaultEditor) spinner.getEditor()).getTextField();
380
textField.setFormatterFactory(
381
new DefaultFormatterFactory(new DateFormatter(format)));
382
PropertyChangeListener l = new PropertyChangeListener() {
384
public void propertyChange(PropertyChangeEvent evt) {
385
if ("date".equals(evt.getPropertyName())) {
386
if (picker.getDate() != null)
387
spinner.setValue(picker.getDate());
392
picker.addPropertyChangeListener(l);
393
JXPanel panel = new JXPanel();
396
showInFrame(panel, "formats in picker and spinner");
399
* Issue #565-swingx: popup not closed if open and
400
* clicking into other focus-tricksing component (like
403
* Issue #1011-swingx: popup of first picker opened on click on second
406
* And: Compare picker and combo behaviour on toggle lf.
408
* Not really comparable: the combo has complete control over the popup, re-creates
409
* both popup and content on install.
412
public void interactivePopupPickerCompareCombo() {
413
// Trace14.keyboardFocusManager(true);
414
final JXDatePicker picker = new JXDatePicker();
415
picker.getEditor().setName("first DateField");
416
JComboBox box = new JComboBox(new Object[] {"one", "twooooooo", "threeeeeeeeeeee", "GOOO!"});
417
box.setEditable(true);
418
JComponent comp = new JPanel();
420
comp.add(new JXDatePicker(new Date()));
422
JXFrame frame = wrapInFrame(comp, "compare combo <-> picker, picker <-> picker", true);
423
addMessage(frame, "open popup, click in other picker/combo - popup must close and other focused");
428
* Issue #1011-swingx: popup of first picker opened on click on second
432
public void interactivePopupTwoPickers() {
433
// Trace14.keyboardFocusManager(true);
434
final JXDatePicker picker = new JXDatePicker();
435
picker.getEditor().setName("first DateField");
436
JComponent comp = new JPanel();
438
JXDatePicker other = new JXDatePicker(new Date());
440
// other.setFocusable(false);
441
// picker.setFocusable(false);
442
JXFrame frame = wrapInFrame(comp, "popup: picker <-> picker", true);
443
addMessage(frame, "click on second opens popup of first (mac only?)");
444
show(frame, 400, 400);
449
* Visually characterize focus behaviour.
451
* Issue #577-swingx: JXDatePicker focus cleanup.
452
* After commit/cancel in popup: picker's editor should be focused.
455
* Issue #757-swingx: JXDatePicker inconsistent focusLost firing.
457
* JXDatePicker must not fire focusLost, the picker's editor should.
459
* New (?) problem: after closing focused popup by clicking into
460
* another the focus is in the picker's editor and can't be moved
463
* - focus popup (by clicking next month, no keyboard, nor commit/cancel)
464
* - click into textfield: popup closed, picker editor has focus
466
* Independent of forcing focus into picker itself or its editor on open.
467
* Looks dependent on heavyweight popup: okay on resizing the frame so
468
* that the popup fits in.
472
public void interactiveFocusEventOnTogglePopup() {
473
JXDatePicker picker = new JXDatePicker();
474
final Action togglePopup = picker.getActionMap().get("TOGGLE_POPUP");
475
JComboBox box = new JComboBox(new String[] {"one", "twos"});
476
box.setEditable(true);
477
FocusListener l = new FocusListener() {
479
public void focusGained(FocusEvent e) {
480
if (e.isTemporary()) return;
481
String source = e.getSource().getClass().getSimpleName();
482
LOG.info("focus gained from: " + source);
485
public void focusLost(FocusEvent e) {
486
if (e.isTemporary()) return;
487
String source = e.getSource().getClass().getSimpleName();
488
LOG.info("focus lost from: " + source);
490
picker.getEditor().addFocusListener(l);
491
picker.addFocusListener(l);
492
box.addFocusListener(l);
493
box.getEditor().getEditorComponent().addFocusListener(l);
494
JComponent panel = new JPanel();
497
panel.add(new JTextField("something to focus"));
498
JXFrame frame = showInFrame(panel, "E: FocusEvents on editor");
499
addAction(frame, togglePopup);
506
* Issue #568-swingx: DatePicker must not reset time fields.
508
* Behaviour defined by selection model of monthView. While the default
509
* (DaySelectionModel) normalizes the dates to the start of the day in the
510
* model's calendar coordinates, a SingleDaySelectionModel keeps the date as-is.
511
* For now, need to explicitly set. <p>
513
* Note: picking a date in the monthView still resets. And may lead to
514
* inconsistent fields (picker.date different from editor/model selected) the
515
* latter being normalized? Not reproducible here - in the reported example
516
* the time-part was hard-set when getting an actionEvent from the monthView.
518
public void interactiveKeepTimeFields() {
519
final JXDatePicker picker = new JXDatePicker();
520
DateSelectionModel selectionModel = new SingleDaySelectionModel();
521
picker.getMonthView().setSelectionModel(selectionModel);
522
picker.setDate(new Date());
523
DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL);
524
picker.setFormats(format);
525
final JFormattedTextField field = new JFormattedTextField(format);
526
field.setValue(picker.getDate());
527
PropertyChangeListener l = new PropertyChangeListener() {
529
public void propertyChange(PropertyChangeEvent evt) {
530
if ("date".equals(evt.getPropertyName())) {
531
field.setValue(evt.getNewValue());
532
LOG.info("picker/editor/monthView: " + picker.getDate() + "/"
533
+ picker.getEditor().getValue() + "/"
534
+ picker.getMonthView().getSelectionDate());
540
picker.addPropertyChangeListener(l);
541
Action setDate = new AbstractActionExt("set date") {
543
public void actionPerformed(ActionEvent e) {
544
picker.setDate(new Date());
549
JComponent box = Box.createHorizontalBox();
552
JXFrame frame = wrapInFrame(box, "time fields");
553
addAction(frame, setDate);
555
frame.setVisible(true);
560
* Issue #568-swingx: DatePicker must not reset time fields.
562
* Behaviour defined by selection model of monthView. While the default
563
* (DaySelectionModel) normalizes the dates to the start of the day in the
564
* model's calendar coordinates, a SingleDaySelectionModel keeps the date as-is.
565
* For now, need to explicitly set. <p>
567
* Note: picking a date in the monthView still resets. And may lead to
568
* inconsistent fields (picker.date different from editor/model selected) the
569
* latter being normalized? Not reproducible here - in the reported example
570
* the time-part was hard-set when getting an actionEvent from the monthView.
572
public void interactiveKeepEndOfDay() {
573
final JXDatePicker picker = new JXDatePicker();
574
SingleDaySelectionModel selectionModel = new SingleDaySelectionModel() {
577
public Date getNormalizedDate(Date date) {
578
return endOfDay(date);
582
picker.getMonthView().setSelectionModel(selectionModel);
583
picker.setDate(new Date());
584
DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL);
585
picker.setFormats(format);
586
final JFormattedTextField field = new JFormattedTextField(format);
587
field.setValue(picker.getDate());
588
PropertyChangeListener l = new PropertyChangeListener() {
590
public void propertyChange(PropertyChangeEvent evt) {
591
if ("date".equals(evt.getPropertyName())) {
592
field.setValue(evt.getNewValue());
593
LOG.info("picker/editor/monthView: " + picker.getDate() + "/"
594
+ picker.getEditor().getValue() + "/"
595
+ picker.getMonthView().getSelectionDate());
601
picker.addPropertyChangeListener(l);
602
Action setDate = new AbstractActionExt("set date") {
604
public void actionPerformed(ActionEvent e) {
605
picker.setDate(new Date());
610
JComponent box = Box.createHorizontalBox();
613
JXFrame frame = wrapInFrame(box, "end of day");
614
addAction(frame, setDate);
616
frame.setVisible(true);
620
* Issue #706-swingx: picker doesn't update monthView.
623
public void interactiveUpdateUIPickerMonthView() {
624
final JXDatePicker picker = new JXDatePicker();
625
JXFrame frame = showInFrame(picker, "picker update ui");
626
Action action = new AbstractActionExt("toggleUI") {
627
public void actionPerformed(ActionEvent e) {
628
String uiClass = (String) UIManager.get(JXMonthView.uiClassID);
629
boolean custom = uiClass.indexOf("Custom") > 0;
631
UIManager.put(JXMonthView.uiClassID, "org.jdesktop.swingx.test.CustomMonthViewUI");
633
UIManager.put(JXMonthView.uiClassID, null);
640
addAction(frame, action);
646
* Issue #1292-swingx: prefsize growing on inserting text into empty editor.
648
* PrefSize should be independent of empty/filled picker.
649
* If not, the initial size might appear kind of collapsed.
651
* adapted code example from bug report: revalidate on text insert/remove
652
* Note: this simulation doesn't survive a LAF change - picker's editor is recreated
653
* and listener not re-wired.
655
public void interactivePrefSizeOnInsert() {
656
final JPanel panel = new JPanel();
657
final JXDatePicker picker = new JXDatePicker();
658
final JFormattedTextField field = new JFormattedTextField();
659
DocumentListener l = new DocumentListener() {
662
public void insertUpdate(DocumentEvent e) {
669
private void revalidate() {
674
public void removeUpdate(DocumentEvent e) {
680
public void changedUpdate(DocumentEvent e) {
683
picker.getEditor().getDocument().addDocumentListener(l);
684
field.getDocument().addDocumentListener(l);
686
JComboBox box = new JComboBox(new Object[] {});
687
box.setEditable(true);
688
((JTextComponent) box.getEditor().getEditorComponent()).getDocument().addDocumentListener(l);
692
JXFrame frame = wrapInFrame(panel, "compare prefSize on insert");
693
addMessage(frame, "type text and compare formatted/picker/combo");
698
* Issue #764-swingx: JXDatePicker sizing.
700
* Compare pref size with/-out date initially.
701
* - null date is slightly narrower than not null
702
* - formats using the day of week are cut a bit (for "long" day names like wed)
703
* - a formatted text field is slightly off, by the width of the caret
705
public void interactiveLocalePrefSize() {
706
// wednesday - has width problems
707
calendar.set(2008, Calendar.FEBRUARY, 20);
708
Date date = calendar.getTime();
709
String formatString = "EEE MM/dd/yyyy";
710
LayoutManager layout = new VerticalLayoutPref();
711
JComponent fieldsNull = new JPanel(layout);
712
addFormattedTextField(fieldsNull, Locale.US, null, formatString);
713
addFormattedTextField(fieldsNull, Locale.UK, null, formatString);
714
addFormattedTextField(fieldsNull, Locale.GERMAN, null, formatString);
715
addFormattedTextField(fieldsNull, Locale.ITALIAN, null, formatString);
716
JComponent fields = new JPanel(layout);
717
addFormattedTextField(fields, Locale.US, date, formatString);
718
addFormattedTextField(fields, Locale.UK, date, formatString);
719
addFormattedTextField(fields, Locale.GERMAN, date, formatString);
720
addFormattedTextField(fields, Locale.ITALIAN, date, formatString);
721
JComponent other = new JPanel(layout);
722
addDatePickerWithLocaleSet(other, Locale.US, date, formatString);
723
addDatePickerWithLocaleSet(other, Locale.UK, date, formatString);
724
addDatePickerWithLocaleSet(other, Locale.GERMAN, date, formatString);
725
addDatePickerWithLocaleSet(other, Locale.ITALIAN, date, formatString);
726
JComponent comp = new JPanel(layout);
727
addDatePickerWithLocaleSet(comp, Locale.US, null, formatString);
728
addDatePickerWithLocaleSet(comp, Locale.UK, null, formatString);
729
addDatePickerWithLocaleSet(comp, Locale.GERMAN, null, formatString);
730
addDatePickerWithLocaleSet(comp, Locale.ITALIAN, null, formatString);
731
JComponent outer = Box.createHorizontalBox();
735
outer.add(fieldsNull);
736
JXFrame frame = wrapInFrame(outer, "Sizing DatePicker");
737
addMessage(frame, "rows: locales, columns: picker/formatted field");
742
* Issue #764-swingx: JXDatePicker sizing.
744
* Compare pref size with/-out date initially.
745
* - null date is slightly narrower than not null
746
* - formats using the day of week are cut a bit (for "long" day names like wed)
747
* - a formatted text field is slightly off, by the width of the caret
749
public void interactiveLocalePrefSize2() {
750
// wednesday - has width problems
751
calendar.set(2008, Calendar.FEBRUARY, 20);
752
Date date = calendar.getTime();
753
String formatString = "EEE MM/dd/yyyy";
754
LayoutManager layout = new VerticalLayoutPref();
756
final JComponent german = new JPanel(layout);
757
addFormattedTextField(german, Locale.GERMAN, date, formatString);
758
addDatePickerWithLocaleSet(german, Locale.GERMAN, date, formatString);
759
addDatePickerWithLocaleSet(german, Locale.GERMAN, null, formatString);
760
addFormattedTextField(german, Locale.GERMAN, null, formatString);
762
JComponent italian = new JPanel(layout);
763
addFormattedTextField(italian, Locale.ITALIAN, date, formatString);
764
addDatePickerWithLocaleSet(italian, Locale.ITALIAN, date, formatString);
765
addDatePickerWithLocaleSet(italian, Locale.ITALIAN, null, formatString);
766
addFormattedTextField(italian, Locale.ITALIAN, null, formatString);
768
JComponent uk = new JPanel(layout);
769
addFormattedTextField(uk, Locale.UK, date, formatString);
770
addDatePickerWithLocaleSet(uk, Locale.UK, date, formatString);
771
addDatePickerWithLocaleSet(uk, Locale.UK, null, formatString);
772
addFormattedTextField(uk, Locale.UK, null, formatString);
774
JComponent us = new JPanel(layout);
775
addFormattedTextField(us, Locale.US, date, formatString);
776
addDatePickerWithLocaleSet(us, Locale.US, date, formatString);
777
addDatePickerWithLocaleSet(us, Locale.US, null, formatString);
778
addFormattedTextField(us, Locale.US, null, formatString);
780
JComponent outer = Box.createHorizontalBox();
785
final JXFrame frame = wrapInFrame(outer, "Sizing DatePicker");
786
Action pack = new AbstractAction("pack") {
789
public void actionPerformed(ActionEvent e) {
790
JFormattedTextField field = ((JXDatePicker) german.getComponent(1)).getEditor();
791
LOG.info("pref " + field.getValue() + field.getText() + field.getPreferredSize());
795
addAction(frame, pack);
796
addMessage(frame, "rows: picker/formatted field, columns: locales");
804
* Instantiates a datePicker using the default constructor, set
805
* its locale to the given and adds it to the comp.
806
* @param comp the container to add the picker to
807
* @param uk the locale to use.
809
private void addDatePickerWithLocaleSet(JComponent comp, Locale uk, Date date, String formatString) {
810
JXDatePicker datePicker = new JXDatePicker(date);
811
datePicker.setLocale(uk);
812
if (formatString != null) {
813
DateFormat format = new SimpleDateFormat(formatString, uk);
814
datePicker.setFormats(format);
816
comp.add(datePicker);
820
* Instantiates a datePicker using the default constructor, set
821
* its locale to the given and adds it to the comp.
822
* @param comp the container to add the picker to
823
* @param uk the locale to use.
825
private void addFormattedTextField(JComponent comp, Locale uk, Date date, String formatString) {
826
JFormattedTextField datePicker;
827
if (formatString != null) {
828
DateFormat format = new SimpleDateFormat(formatString, uk);
829
datePicker = new JFormattedTextField(format);
831
datePicker = new JFormattedTextField();
833
datePicker.setValue(date);
834
comp.add(datePicker);
839
* Issue #665-swingx: make JXDatePicker Locale-aware.
841
* Here: instantiate the picker with a non-default locale.
842
* Check that the dates in LinkPanel and editor
843
* are formatted as appropriate for the Locale
845
public void interactiveLocaleConstructor() {
846
JComponent other = new JPanel();
847
// wednesday - has width problems
848
calendar.set(2008, Calendar.FEBRUARY, 20);
849
Date date = calendar.getTime();
850
addDatePickerWithLocaleConstructor(other, Locale.US, date);
851
addDatePickerWithLocaleConstructor(other, Locale.UK, date);
852
addDatePickerWithLocaleConstructor(other, Locale.GERMAN, date);
853
addDatePickerWithLocaleConstructor(other, Locale.ITALIAN, date);
854
JComponent comp = new JPanel();
855
addDatePickerWithLocaleConstructor(comp, Locale.US, null);
856
addDatePickerWithLocaleConstructor(comp, Locale.UK, null);
857
addDatePickerWithLocaleConstructor(comp, Locale.GERMAN, null);
858
addDatePickerWithLocaleConstructor(comp, Locale.ITALIAN, null);
859
JComponent outer = Box.createVerticalBox();
862
showInFrame(outer, "Localized DatePicker: constructor");
866
* Instantiates a datePicker using the constructor with the given locale and
867
* adds it to the comp.
868
* @param comp the container to add the picker to
869
* @param uk the locale to use.
871
private void addDatePickerWithLocaleConstructor(JComponent comp, Locale uk, Date date) {
872
JXDatePicker datePicker = new JXDatePicker(uk);
873
datePicker.setDate(date);
874
comp.add(new JLabel(uk.getDisplayName()));
875
comp.add(datePicker);
879
* Issue #665-swingx: make JXDatePicker Locale-aware.
881
* Tests reaction to default locales set via both JComponent.setDefault and
882
* Locale.setDefault. Going that way, catches the locales fine.
884
* Also Issue #681-swingx - the first row of days in the monthview
885
* overlaps with the day names for locales which have the monday as the
888
public void interactiveLocaleDefault() {
889
JComponent comp = new JPanel();
890
Locale old = addDatePickerWithLocale(comp, Locale.US);
891
addDatePickerWithLocale(comp, Locale.UK);
892
addDatePickerWithLocale(comp, Locale.GERMAN);
893
addDatePickerWithLocale(comp, Locale.ITALIAN);
894
showInFrame(comp, "DatePicker takes default Locale");
899
* Sets the default Locale to the given, instantiates a JXDatePicker with
900
* default Locale and adds it to the given component. Returns the previous
903
* @param comp the container to add the picker to
904
* @param uk the new default Locale
906
* @return the previous default Locale
908
private Locale addDatePickerWithLocale(JComponent comp, Locale uk) {
909
Locale old = setLocale(uk);
910
JXDatePicker datePicker = new JXDatePicker();
911
comp.add(new JLabel(uk.getDisplayName()));
912
comp.add(datePicker);
917
* Sets default Locale (on Locale and JComponent) to the given Locale and
918
* returns the previous default.
920
* @param locale the default Locale to set.
921
* @return the previous default.
923
private Locale setLocale(Locale locale) {
924
Locale old = JComponent.getDefaultLocale();
925
JComponent.setDefaultLocale(locale);
926
Locale.setDefault(locale);
932
* Issue #566-swingx: JXRootPane eats picker's popup esc.
933
* to reproduce: open the picker's popup the press esc -
934
* not closed. Same with combo is working.
937
public void interactiveXRootPaneEatsEscape() {
938
JXDatePicker picker = new JXDatePicker();
939
JComboBox box = new JComboBox(new String[] {"one", "twos"});
940
box.setEditable(true);
941
JComponent panel = new JPanel();
944
showInFrame(panel, "Escape key");
949
* visual check that toggling the panel adds/removes it
950
* and installs the keybindings.
953
public void interactiveLinkPanelSet() {
954
final JXDatePicker picker = new JXDatePicker();
955
final JPanel panel = picker.getLinkPanel();
957
JXFrame frame = showInFrame(picker, "null panel");
958
Action toggleLinkPanel = new AbstractAction("toggleLinkPanel <-> null") {
960
public void actionPerformed(ActionEvent e) {
961
boolean hasLinkPanel = picker.getLinkPanel() != null;
962
picker.setLinkPanel(hasLinkPanel ? null : panel);
966
addAction(frame, toggleLinkPanel);
971
* Checking PropertyChangeEvent: report in forum that no event for date fired.
972
* here not reproducible.. problem of WizardPage?
974
public void interactivePCEvent() {
975
JXDatePicker picker = new JXDatePicker(new Date());
976
JFormattedTextField textField = new JFormattedTextField(DateFormat.getDateInstance());
977
textField.setValue(new Date());
979
PropertyChangeListener l = new PropertyChangeListener() {
981
public void propertyChange(PropertyChangeEvent e) {
982
LOG.info("got pce from: " + e.getSource().getClass().getSimpleName() +
983
"\n" + e.getPropertyName() + e.getNewValue());
988
textField.addPropertyChangeListener(l);
989
picker.addPropertyChangeListener(l);
990
// picker.getMonthView().addActionListener(l);
991
JPanel panel = new JPanel();
992
panel.add(textField);
994
JTabbedPane tab = new JTabbedPane();
995
tab.add("fields", panel);
996
tab.add("dummy", new JTextField("dummy"));
997
JXFrame frame = showInFrame(tab, "Compare propertChange events: keyboard/mouse");
1004
* Issue #235-swingx: action events
1006
* Compare textfield, formatted, picker, combo after keyboard.
1009
* - simple field fires on enter always
1010
* - formatted fire on enter if value had been edited
1013
* - fires on enter always
1014
* - fires on click in dropdown
1016
* Calendar widgets after cleanup:
1019
* - fires "datePickerCommit" on click (actually released) into monthView
1020
* - fires "datePickerCommit"/-"Cancel" on enter/escape, both in input field
1021
* and if popup is open
1024
* - fires "monthViewCommit" on click (actually released)
1025
* - fires "monthViewCommit"/-"Cancel" on enter/esc
1029
public void interactiveActionEventSetAction() {
1030
JXDatePicker picker = new JXDatePicker();
1031
JTextField simpleField = new JTextField("simple field");
1032
JFormattedTextField textField = new JFormattedTextField(DateFormat.getDateInstance());
1033
textField.setValue(new Date());
1034
JComboBox box = new JComboBox(new Object[] {"one", "two", "three"});
1035
box.setEditable(true);
1036
JComboBox nonEditableBox = new JComboBox(new Object[] {"one", "two", "three"});
1037
final Action l = new AbstractActionExt("recived") {
1039
public void actionPerformed(ActionEvent e) {
1040
LOG.info("got action from: " + e.getSource().getClass().getName() +
1045
simpleField.setAction(l);
1046
textField.setAction(l);
1047
// picker doesn't have action-related api
1048
picker.addActionListener(l);
1049
// picker.getMonthView().addActionListener(l);
1051
nonEditableBox.setAction(l);
1053
JPanel panel = new JPanel();
1054
panel.add(simpleField);
1055
panel.add(textField);
1058
panel.add(new JCheckBox(l));
1059
panel.add(nonEditableBox);
1060
JXFrame frame = wrapInFrame(panel, "SetAction - Compare action events: keyboard/mouse");
1061
Action toggleEnabled = new AbstractActionExt("toggleEnabledAction") {
1063
public void actionPerformed(ActionEvent e) {
1064
l.setEnabled(!l.isEnabled());
1068
addAction(frame, toggleEnabled);
1073
* Issue #235-swingx: action events
1075
* Compare textfield, formatted, picker and combo: programatic change.
1076
* - only combo fires
1079
public void interactiveActionEventSetValue() {
1080
final JXDatePicker picker = new JXDatePicker();
1081
final JTextField simpleField = new JTextField("simple field");
1082
final JFormattedTextField textField = new JFormattedTextField(DateFormat.getDateInstance());
1083
textField.setValue(new Date());
1084
final JComboBox box = new JComboBox(new Object[] {"one", "two", "three"});
1085
box.setEditable(true);
1087
ActionListener l = new ActionListener() {
1089
public void actionPerformed(ActionEvent e) {
1090
LOG.info("got action from: " + e.getSource().getClass().getName() +
1095
simpleField.addActionListener(l);
1096
textField.addActionListener(l);
1097
picker.addActionListener(l);
1098
picker.getMonthView().addActionListener(l);
1099
box.addActionListener(l);
1100
Action action = new AbstractAction("set new value") {
1102
public void actionPerformed(ActionEvent e) {
1103
Calendar cal = Calendar.getInstance();
1104
cal.add(Calendar.DAY_OF_MONTH, dayToAdd++);
1105
Date date = cal.getTime();
1106
String text = DateFormat.getDateInstance().format(date);
1107
simpleField.setText(text);
1108
textField.setValue(date);
1109
picker.setDate(date);
1110
box.setSelectedItem(text);
1115
JPanel panel = new JPanel();
1116
panel.add(simpleField);
1117
panel.add(textField);
1121
JXFrame frame = showInFrame(panel, "Compare action events: programmatic change");
1122
addAction(frame, action);
1128
* Issue #99-swingx: null date and opening popup forces selection.
1129
* Status? Looks fixed..
1131
* Sizing issue if init with null date
1133
public void interactiveNullDate() {
1134
JXDatePicker picker = new JXDatePicker();
1135
showInFrame(picker, "null date in picker");
1139
* something weird's going on: the picker's date must be null
1140
* after setting a monthView with null selection. It is, until
1142
* Looks fixed during synch control cleanup in datePicker.
1144
public void interactiveShowPickerSetMonthNull() {
1145
JXDatePicker picker = new JXDatePicker();
1146
JXMonthView intervalForPicker = new JXMonthView();
1147
intervalForPicker.setSelectionMode(SelectionMode.SINGLE_INTERVAL_SELECTION);
1148
picker.setMonthView(intervalForPicker);
1149
assertNull(picker.getDate());
1150
showInFrame(picker, "empty selection in monthView");
1151
assertNull(picker.getDate());
1154
public void interactiveDatePickerDisplay() {
1155
JXDatePicker datePicker = new JXDatePicker();
1156
showInFrame(datePicker, "show date picker");
1162
protected void setUp() throws Exception {
1163
calendar = Calendar.getInstance();
1166
private static class SetPlafAction extends AbstractAction {
1167
private String plaf;
1169
public SetPlafAction(String name, String plaf) {
1177
public void actionPerformed(ActionEvent e) {
1179
Component c = (Component) e.getSource();
1182
for (Container p = c.getParent(); p != null; p = p instanceof JPopupMenu ? (Container) ((JPopupMenu) p)
1183
.getInvoker() : p.getParent()) {
1184
if (p instanceof Window) {
1189
UIManager.setLookAndFeel(plaf);
1190
SwingUtilities.updateComponentTreeUI(w);
1192
} catch (ClassNotFoundException e1) {
1193
e1.printStackTrace();
1194
} catch (InstantiationException e1) {
1195
e1.printStackTrace();
1196
} catch (IllegalAccessException e1) {
1197
e1.printStackTrace();
1198
} catch (UnsupportedLookAndFeelException e1) {
1199
e1.printStackTrace();
1204
private JMenuBar createMenuBar() {
1205
LookAndFeelInfo[] plafs = UIManager.getInstalledLookAndFeels();
1206
JMenuBar bar = new JMenuBar();
1207
JMenu menu = new JMenu("Set L&F");
1209
for (LookAndFeelInfo info : plafs) {
1210
menu.add(new SetPlafAction(info.getName(), info.getClassName()));
1219
public JXFrame wrapInFrame(JComponent component, String title) {
1220
JXFrame frame = super.wrapInFrame(component, title);
1222
frame.setJMenuBar(createMenuBar());
1230
* Do nothing, make the test runner happy
1231
* (would output a warning without a test fixture).
1234
public void testDummy() {