~ubuntu-branches/ubuntu/trusty/libswingx-java/trusty

« back to all changes in this revision

Viewing changes to swingx-core/src/test/java/org/jdesktop/swingx/JXDatePickerVisualCheck.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2011-03-06 00:28:45 UTC
  • mfrom: (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110306002845-escned3cbqp5qx0t
Tags: 1:1.6.2-1
* New upstream release.
* Switch to maven as build system:
  - d/control: drop ant, add maven-debian-helper
  - d/rules: use maven.mk
* d/patches/pom.diff: drop, uneeded since upstream fixed its dependencies.
* d/watch: update to use java.net directly.
* d/rules: force debian version for JARs (Closes: #603495).
* d/copyright: Update to lastest DEP-5 r166.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 
3
 * California 95054, U.S.A. All rights reserved.
 
4
 * 
 
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)
 
8
 * any later version.
 
9
 * 
 
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
 
13
 * details.
 
14
 * 
 
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
 
18
 */
 
19
package org.jdesktop.swingx;
 
20
 
 
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;
 
40
 
 
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;
 
70
 
 
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;
 
77
 
 
78
/**
 
79
 * Simple tests to ensure that the {@code JXDatePicker} can be instantiated and
 
80
 * displayed.<p>
 
81
 * 
 
82
 * JW: being lazy - added visuals for <code>JXMonthView</code> as well.
 
83
 * 
 
84
 * @author Karl Schaefer
 
85
 */
 
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;
 
94
 
 
95
    public JXDatePickerVisualCheck() {
 
96
        super("JXDatePicker Test");
 
97
    }
 
98
 
 
99
    public static void main(String[] args) throws Exception {
 
100
       UIManager.put("JXDatePicker.forceZoomable", Boolean.TRUE);
 
101
//         setSystemLF(true);
 
102
        JXDatePickerVisualCheck test = new JXDatePickerVisualCheck();
 
103
        
 
104
        try {
 
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:");
 
115
            e.printStackTrace();
 
116
        }
 
117
    }
 
118
 
 
119
    public void interactivePopupMenuListener() {
 
120
        JXDatePicker picker = new JXDatePicker();
 
121
        PopupMenuListener l = new PopupMenuListener() {
 
122
            
 
123
            @Override
 
124
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
 
125
                LOG.info("visible");
 
126
            }
 
127
            
 
128
            @Override
 
129
            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
 
130
                LOG.info("invisible");
 
131
            }
 
132
            
 
133
            @Override
 
134
            public void popupMenuCanceled(PopupMenuEvent e) {
 
135
                LOG.info("canceled");
 
136
            }
 
137
        };
 
138
        picker.addPopupMenuListener(l);
 
139
        showInFrame(picker, "picker: notification?");
 
140
    }
 
141
    
 
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() {
 
147
            
 
148
            @Override
 
149
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
 
150
                LOG.info("visible");
 
151
            }
 
152
            
 
153
            @Override
 
154
            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
 
155
                LOG.info("invisible");
 
156
            }
 
157
            
 
158
            @Override
 
159
            public void popupMenuCanceled(PopupMenuEvent e) {
 
160
                LOG.info("canceled");
 
161
            }
 
162
        };
 
163
        textArea.setComponentPopupMenu(picker);
 
164
        picker.addPopupMenuListener(l);
 
165
        showInFrame(textArea, "plain Popup: notification");
 
166
    }
 
167
    /**
 
168
     * Issue #940-swingx: support multiple selection in picker.
 
169
     * Nothing out-off-the-box, trying to manipulate the mouseEvent.
 
170
     * 
 
171
     * No success: no way to unselect
 
172
     */
 
173
    public void interactiveMultipleSelectionWithoutCtrl() {
 
174
        JXMonthView monthView = new JXMonthView(new Date()) {
 
175
 
 
176
            @Override
 
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;
 
181
                
 
182
                MouseEvent myME = new MouseEvent(
 
183
                    (Component) e.getSource(), 
 
184
                    e.getID(), 
 
185
                    e.getWhen(), 
 
186
                    modifiers, // my changed modifier
 
187
                    e.getX(), 
 
188
                    e.getY(), 
 
189
                    e.getXOnScreen(), 
 
190
                    e.getYOnScreen(), 
 
191
                    e.getClickCount(), 
 
192
                    e.isPopupTrigger(), 
 
193
                    e.getButton());
 
194
                super.processMouseEvent(myME);
 
195
//                super.processMouseEvent(e);
 
196
            }
 
197
            
 
198
        };
 
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");
 
203
        show(frame);
 
204
    }
 
205
    
 
206
    /**
 
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.
 
212
     */
 
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");
 
220
        show(frame);
 
221
    }
 
222
    
 
223
    /**
 
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
 
227
     * </a>
 
228
     */
 
229
    public static class ToggleSelectionModel extends DaySelectionModel {
 
230
 
 
231
        @Override
 
232
        public void setSelectionInterval(Date startDate, Date endDate) {
 
233
            if (!isSameDay(startDate, endDate)) {
 
234
                super.setSelectionInterval(startDate, endDate);
 
235
                return;
 
236
            }
 
237
            if (isSelected(startDate)) {
 
238
                removeSelectionInterval(startDate, startDate);
 
239
            } else {
 
240
                addSelectionInterval(startDate, startDate);
 
241
            }
 
242
        }
 
243
        
 
244
        
 
245
    }
 
246
    
 
247
    /**
 
248
     * Issue #940-swingx: support multiple selection in picker.
 
249
     * Nothing out-off-the-box, trying to implement custom 
 
250
     * dateSelectionModel. No success.
 
251
     */
 
252
    public void interactiveMultipleSelectionAddingModel() {
 
253
        DateSelectionModel model = new AddingDaySelectionModel();
 
254
        model.setSelectionMode(SelectionMode.MULTIPLE_INTERVAL_SELECTION);
 
255
        JXMonthView monthView = new JXMonthView(new Date(), model) {
 
256
 
 
257
            @Override
 
258
            public Date getSelectionDate() {
 
259
                if (getSelectionModel() instanceof AddingDaySelectionModel) {
 
260
                    return ((AddingDaySelectionModel) getSelectionModel()).getLastAddedDate();
 
261
                }
 
262
                return super.getSelectionDate();
 
263
            }
 
264
            
 
265
        };
 
266
        JXDatePicker picker = new JXDatePicker();
 
267
        picker.setMonthView(monthView);
 
268
        JXFrame frame = showInFrame(picker, "multiple selection");
 
269
        show(frame);
 
270
    }
 
271
    
 
272
    /**
 
273
     * Custom model which always adds selection dates.
 
274
     */
 
275
    public static class AddingDaySelectionModel extends DaySelectionModel {
 
276
        
 
277
        Date lastAdded;
 
278
        
 
279
        @Override
 
280
        public void setSelectionInterval(Date startDate, Date endDate) {
 
281
            addSelectionInterval(startDate, endDate);
 
282
        }
 
283
        
 
284
        @Override
 
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)) {
 
290
              lastAdded = endDate;
 
291
            } else if (isSelected(startDate)) {
 
292
                lastAdded = startDate;
 
293
            } else {
 
294
                lastAdded = null;
 
295
            }
 
296
        }
 
297
        
 
298
        
 
299
        @Override
 
300
        public void clearSelection() {
 
301
            lastAdded = null;
 
302
            super.clearSelection();
 
303
        }
 
304
 
 
305
        public Date getLastAddedDate() {
 
306
            return lastAdded;
 
307
        }
 
308
    }
 
309
    /**
 
310
     * Issue #1196-swingx: must not allow edits if disabled.
 
311
     * 
 
312
     */
 
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") {
 
320
            
 
321
            @Override
 
322
            public void actionPerformed(ActionEvent e) {
 
323
                picker.setEnabled(!picker.isEnabled());
 
324
                
 
325
            }
 
326
        };
 
327
        addAction(frame, enabled);
 
328
        show(frame);
 
329
    }
 
330
    
 
331
    /**
 
332
     * Issue #910-swingx: commitToday must not be allowed if field not editable.
 
333
     * 
 
334
     */
 
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 ");
 
342
    }
 
343
    
 
344
    /**
 
345
     * Issue #910-swingx: commitToday must not be allowed if field not editable.
 
346
     * 
 
347
     */
 
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() {
 
353
 
 
354
            public void propertyChange(PropertyChangeEvent evt) {
 
355
                if ("date".equals(evt.getPropertyName())) {
 
356
                    if (picker.getDate() != null)
 
357
                     field.setValue(picker.getDate());
 
358
                }
 
359
            }
 
360
            
 
361
        };
 
362
        picker.addPropertyChangeListener(l);
 
363
        JXPanel panel = new JXPanel();
 
364
        panel.add(picker);
 
365
        panel.add(field);
 
366
        showInFrame(panel, "not editable field ");
 
367
    }
 
368
    /**
 
369
     * From forum: have spinner with the same timezone.
 
370
     */
 
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() {
 
383
 
 
384
            public void propertyChange(PropertyChangeEvent evt) {
 
385
                if ("date".equals(evt.getPropertyName())) {
 
386
                    if (picker.getDate() != null)
 
387
                     spinner.setValue(picker.getDate());
 
388
                }
 
389
            }
 
390
            
 
391
        };
 
392
        picker.addPropertyChangeListener(l);
 
393
        JXPanel panel = new JXPanel();
 
394
        panel.add(picker);
 
395
        panel.add(spinner);
 
396
        showInFrame(panel, "formats in picker and spinner");
 
397
    }
 
398
    /**
 
399
     * Issue #565-swingx: popup not closed if open and 
 
400
     * clicking into other focus-tricksing component (like
 
401
     * picker, combo).
 
402
     * 
 
403
     * Issue #1011-swingx: popup of first picker opened on click on second
 
404
     * (mac only?)
 
405
     * 
 
406
     * And: Compare picker and combo behaviour on toggle lf.
 
407
     * 
 
408
     * Not really comparable: the combo has complete control over the popup, re-creates
 
409
     * both popup and content on install.
 
410
     * 
 
411
     */
 
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();
 
419
        comp.add(picker);
 
420
        comp.add(new JXDatePicker(new Date()));
 
421
        comp.add(box);
 
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");
 
424
        show(frame);
 
425
    }
 
426
    
 
427
    /**
 
428
     * Issue #1011-swingx: popup of first picker opened on click on second
 
429
     * (mac only?)
 
430
     * 
 
431
     */
 
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();
 
437
        comp.add(picker);
 
438
        JXDatePicker other = new JXDatePicker(new Date());
 
439
        comp.add(other);
 
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);
 
445
    }
 
446
 
 
447
    
 
448
    /**
 
449
     * Visually characterize focus behaviour.
 
450
     * 
 
451
     * Issue #577-swingx: JXDatePicker focus cleanup.
 
452
     * After commit/cancel in popup: picker's editor should be focused.
 
453
     * 
 
454
     * 
 
455
     * Issue #757-swingx: JXDatePicker inconsistent focusLost firing.
 
456
     * 
 
457
     * JXDatePicker must not fire focusLost, the picker's editor should.
 
458
     * 
 
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
 
461
     * with tab
 
462
     * - open popup, 
 
463
     * - focus popup (by clicking next month, no keyboard, nor commit/cancel)
 
464
     * - click into textfield: popup closed, picker editor has focus
 
465
     *  
 
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.
 
469
     * 
 
470
     * 
 
471
     */
 
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() {
 
478
 
 
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);
 
483
            }
 
484
 
 
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);
 
489
            }};
 
490
        picker.getEditor().addFocusListener(l); 
 
491
        picker.addFocusListener(l);
 
492
        box.addFocusListener(l);
 
493
        box.getEditor().getEditorComponent().addFocusListener(l);
 
494
        JComponent panel = new JPanel();
 
495
        panel.add(box);
 
496
        panel.add(picker);
 
497
        panel.add(new JTextField("something to focus"));
 
498
        JXFrame frame = showInFrame(panel, "E: FocusEvents on editor");
 
499
        addAction(frame, togglePopup);
 
500
        frame.pack();
 
501
    }
 
502
 
 
503
 
 
504
 
 
505
    /**
 
506
     * Issue #568-swingx: DatePicker must not reset time fields.
 
507
     * 
 
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>
 
512
     * 
 
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.
 
517
     */
 
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() {
 
528
 
 
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());
 
535
                }
 
536
                
 
537
            }
 
538
            
 
539
        };
 
540
        picker.addPropertyChangeListener(l);
 
541
        Action setDate = new AbstractActionExt("set date") {
 
542
 
 
543
            public void actionPerformed(ActionEvent e) {
 
544
                picker.setDate(new Date());
 
545
                
 
546
            }
 
547
            
 
548
        };
 
549
        JComponent box = Box.createHorizontalBox();
 
550
        box.add(picker);
 
551
        box.add(field);
 
552
        JXFrame frame = wrapInFrame(box, "time fields");
 
553
        addAction(frame, setDate);
 
554
        frame.pack();
 
555
        frame.setVisible(true);
 
556
        
 
557
    }
 
558
 
 
559
    /**
 
560
     * Issue #568-swingx: DatePicker must not reset time fields.
 
561
     * 
 
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>
 
566
     * 
 
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.
 
571
     */
 
572
    public void interactiveKeepEndOfDay() {
 
573
        final JXDatePicker picker = new JXDatePicker();
 
574
        SingleDaySelectionModel selectionModel = new SingleDaySelectionModel() {
 
575
 
 
576
            @Override
 
577
            public Date getNormalizedDate(Date date) {
 
578
                return endOfDay(date);
 
579
            }
 
580
            
 
581
        };
 
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() {
 
589
 
 
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());
 
596
                }
 
597
                
 
598
            }
 
599
            
 
600
        };
 
601
        picker.addPropertyChangeListener(l);
 
602
        Action setDate = new AbstractActionExt("set date") {
 
603
 
 
604
            public void actionPerformed(ActionEvent e) {
 
605
                picker.setDate(new Date());
 
606
                
 
607
            }
 
608
            
 
609
        };
 
610
        JComponent box = Box.createHorizontalBox();
 
611
        box.add(picker);
 
612
        box.add(field);
 
613
        JXFrame frame = wrapInFrame(box, "end of day");
 
614
        addAction(frame, setDate);
 
615
        frame.pack();
 
616
        frame.setVisible(true);
 
617
        
 
618
    }
 
619
    /**
 
620
     * Issue #706-swingx: picker doesn't update monthView.
 
621
     * 
 
622
     */
 
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;
 
630
                if (!custom) {
 
631
                    UIManager.put(JXMonthView.uiClassID, "org.jdesktop.swingx.test.CustomMonthViewUI");
 
632
                } else {
 
633
                    UIManager.put(JXMonthView.uiClassID, null);
 
634
                }
 
635
                picker.updateUI();
 
636
                custom = !custom;
 
637
            }
 
638
            
 
639
        };
 
640
        addAction(frame, action);
 
641
        frame.pack();
 
642
    };
 
643
    
 
644
 
 
645
    /**
 
646
     * Issue #1292-swingx: prefsize growing on inserting text into empty editor.
 
647
     * 
 
648
     * PrefSize should be independent of empty/filled picker. 
 
649
     * If not, the initial size might appear kind of collapsed.
 
650
     *  
 
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. 
 
654
     */
 
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() {
 
660
 
 
661
            @Override
 
662
            public void insertUpdate(DocumentEvent e) {
 
663
                revalidate();
 
664
                
 
665
            }
 
666
            /**
 
667
             * @param panel
 
668
             */
 
669
            private void revalidate() {
 
670
                panel.revalidate();
 
671
            }
 
672
            
 
673
            @Override
 
674
            public void removeUpdate(DocumentEvent e) {
 
675
                revalidate();
 
676
            }
 
677
 
 
678
 
 
679
            @Override
 
680
            public void changedUpdate(DocumentEvent e) {
 
681
            }
 
682
        };
 
683
        picker.getEditor().getDocument().addDocumentListener(l);
 
684
        field.getDocument().addDocumentListener(l);
 
685
        
 
686
        JComboBox box = new JComboBox(new Object[] {});
 
687
        box.setEditable(true);
 
688
        ((JTextComponent) box.getEditor().getEditorComponent()).getDocument().addDocumentListener(l);
 
689
        panel.add(field);
 
690
        panel.add(picker);
 
691
        panel.add(box);
 
692
        JXFrame frame = wrapInFrame(panel, "compare prefSize on insert");
 
693
        addMessage(frame, "type text and compare formatted/picker/combo");
 
694
        show(frame);
 
695
    }
 
696
    
 
697
    /**
 
698
     * Issue #764-swingx: JXDatePicker sizing.
 
699
     * 
 
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
 
704
     */
 
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();
 
732
        outer.add(other);
 
733
        outer.add(comp);
 
734
        outer.add(fields);
 
735
        outer.add(fieldsNull);
 
736
        JXFrame frame = wrapInFrame(outer, "Sizing DatePicker");
 
737
        addMessage(frame, "rows: locales, columns: picker/formatted field");
 
738
        show(frame);
 
739
    }
 
740
    
 
741
    /**
 
742
     * Issue #764-swingx: JXDatePicker sizing.
 
743
     * 
 
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
 
748
     */
 
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();
 
755
        
 
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);
 
761
        
 
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);
 
767
        
 
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);
 
773
        
 
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);
 
779
        
 
780
        JComponent outer = Box.createHorizontalBox();
 
781
        outer.add(us);
 
782
        outer.add(uk);
 
783
        outer.add(german);
 
784
        outer.add(italian);
 
785
        final JXFrame frame = wrapInFrame(outer, "Sizing DatePicker");
 
786
        Action pack = new AbstractAction("pack") {
 
787
            
 
788
            @Override
 
789
            public void actionPerformed(ActionEvent e) {
 
790
                JFormattedTextField field = ((JXDatePicker) german.getComponent(1)).getEditor();
 
791
                LOG.info("pref " + field.getValue() + field.getText() + field.getPreferredSize());
 
792
                frame.pack();
 
793
            }
 
794
        };
 
795
        addAction(frame, pack);
 
796
        addMessage(frame, "rows: picker/formatted field, columns: locales");
 
797
        show(frame);
 
798
    }
 
799
 
 
800
    
 
801
    
 
802
 
 
803
    /**
 
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.
 
808
     */
 
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);
 
815
        }
 
816
        comp.add(datePicker);
 
817
    }
 
818
 
 
819
    /**
 
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.
 
824
     */
 
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);
 
830
        } else {
 
831
            datePicker = new JFormattedTextField();
 
832
        }
 
833
        datePicker.setValue(date);
 
834
        comp.add(datePicker);
 
835
    }
 
836
 
 
837
 
 
838
    /**
 
839
     * Issue #665-swingx: make JXDatePicker Locale-aware.
 
840
     * 
 
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 
 
844
     */
 
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();
 
860
        outer.add(other);
 
861
        outer.add(comp);
 
862
        showInFrame(outer, "Localized DatePicker: constructor");
 
863
    }
 
864
 
 
865
    /**
 
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.
 
870
     */
 
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);
 
876
    }
 
877
 
 
878
    /**
 
879
     * Issue #665-swingx: make JXDatePicker Locale-aware.
 
880
     * 
 
881
     * Tests reaction to default locales set via both JComponent.setDefault and
 
882
     * Locale.setDefault. Going that way, catches the locales fine.
 
883
     * 
 
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 
 
886
     * first day of week. 
 
887
     */
 
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");
 
895
        setLocale(old);
 
896
    }
 
897
 
 
898
    /**
 
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 
 
901
     * default Locale.
 
902
     *  
 
903
     * @param comp the container to add the picker to
 
904
     * @param uk the new default Locale
 
905
     *  
 
906
     * @return the previous default Locale
 
907
     */
 
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);
 
913
        return old;
 
914
    }
 
915
 
 
916
    /**
 
917
     * Sets default Locale (on Locale and JComponent) to the given Locale and
 
918
     * returns the previous default.
 
919
     * 
 
920
     * @param locale the default Locale to set.
 
921
     * @return the previous default.
 
922
     */
 
923
    private Locale setLocale(Locale locale) {
 
924
        Locale old = JComponent.getDefaultLocale();
 
925
        JComponent.setDefaultLocale(locale);
 
926
        Locale.setDefault(locale);
 
927
        return old;
 
928
    }
 
929
 
 
930
 
 
931
    /**
 
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.
 
935
     *
 
936
     */
 
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();
 
942
        panel.add(picker);
 
943
        panel.add(box);
 
944
        showInFrame(panel, "Escape key");
 
945
    }
 
946
    
 
947
 
 
948
    /**
 
949
     * visual check that toggling the panel adds/removes it
 
950
     * and installs the keybindings.
 
951
     *
 
952
     */
 
953
    public void interactiveLinkPanelSet() {
 
954
        final JXDatePicker picker = new JXDatePicker();
 
955
        final JPanel panel = picker.getLinkPanel();
 
956
        // initial null okay
 
957
        JXFrame frame = showInFrame(picker, "null panel");
 
958
        Action toggleLinkPanel = new AbstractAction("toggleLinkPanel <-> null") {
 
959
 
 
960
            public void actionPerformed(ActionEvent e) {
 
961
                boolean hasLinkPanel = picker.getLinkPanel() != null;
 
962
                picker.setLinkPanel(hasLinkPanel ? null : panel);
 
963
            }
 
964
            
 
965
        };
 
966
        addAction(frame, toggleLinkPanel);
 
967
        frame.pack();
 
968
    }
 
969
  
 
970
    /**
 
971
     * Checking PropertyChangeEvent: report in forum that no event for date fired.
 
972
     * here not reproducible.. problem of WizardPage?
 
973
     */
 
974
    public void interactivePCEvent() {
 
975
        JXDatePicker picker = new JXDatePicker(new Date());
 
976
        JFormattedTextField textField = new JFormattedTextField(DateFormat.getDateInstance());
 
977
        textField.setValue(new Date());
 
978
        
 
979
        PropertyChangeListener l = new PropertyChangeListener() {
 
980
 
 
981
            public void propertyChange(PropertyChangeEvent e) {
 
982
                LOG.info("got pce from: " + e.getSource().getClass().getSimpleName() + 
 
983
                        "\n" + e.getPropertyName() + e.getNewValue());
 
984
                
 
985
            }
 
986
            
 
987
        };
 
988
        textField.addPropertyChangeListener(l);
 
989
        picker.addPropertyChangeListener(l);
 
990
//        picker.getMonthView().addActionListener(l);
 
991
        JPanel panel = new JPanel();
 
992
        panel.add(textField);
 
993
        panel.add(picker);
 
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");
 
998
        frame.pack();
 
999
        
 
1000
    }
 
1001
    
 
1002
 
 
1003
    /**
 
1004
     * Issue #235-swingx: action events
 
1005
     * 
 
1006
     * Compare textfield, formatted, picker, combo after keyboard.
 
1007
     * 
 
1008
     * TextField
 
1009
     * - simple field fires on enter always
 
1010
     * - formatted fire on enter if value had been edited
 
1011
     *
 
1012
     * ComboBox
 
1013
     * - fires on enter always
 
1014
     * - fires on click in dropdown
 
1015
     * 
 
1016
     * Calendar widgets after cleanup: 
 
1017
     * 
 
1018
     * Picker
 
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
 
1022
     * 
 
1023
     * MonthView
 
1024
     * - fires "monthViewCommit" on click (actually released)
 
1025
     * - fires "monthViewCommit"/-"Cancel" on enter/esc 
 
1026
     * 
 
1027
     * 
 
1028
     */
 
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") {
 
1038
 
 
1039
            public void actionPerformed(ActionEvent e) {
 
1040
                LOG.info("got action from: " + e.getSource().getClass().getName() + 
 
1041
                        "\n" + e);
 
1042
            }
 
1043
            
 
1044
        };
 
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);
 
1050
        box.setAction(l);
 
1051
        nonEditableBox.setAction(l);
 
1052
        
 
1053
        JPanel panel = new JPanel();
 
1054
        panel.add(simpleField);
 
1055
        panel.add(textField);
 
1056
        panel.add(picker);
 
1057
        panel.add(box);
 
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") {
 
1062
 
 
1063
            public void actionPerformed(ActionEvent e) {
 
1064
                l.setEnabled(!l.isEnabled());
 
1065
            }
 
1066
            
 
1067
        };
 
1068
        addAction(frame, toggleEnabled);
 
1069
        show(frame);
 
1070
    }
 
1071
 
 
1072
    /**
 
1073
     * Issue #235-swingx: action events
 
1074
     * 
 
1075
     * Compare textfield, formatted, picker and combo: programatic change.
 
1076
     * - only combo fires
 
1077
     * 
 
1078
     */
 
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);
 
1086
        
 
1087
        ActionListener l = new ActionListener() {
 
1088
 
 
1089
            public void actionPerformed(ActionEvent e) {
 
1090
                LOG.info("got action from: " + e.getSource().getClass().getName() + 
 
1091
                        "\n" + e);
 
1092
            }
 
1093
            
 
1094
        };
 
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") {
 
1101
            int dayToAdd = 1;
 
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);
 
1111
            }
 
1112
            
 
1113
        };
 
1114
        
 
1115
        JPanel panel = new JPanel();
 
1116
        panel.add(simpleField);
 
1117
        panel.add(textField);
 
1118
        panel.add(picker);
 
1119
        panel.add(box);
 
1120
        
 
1121
        JXFrame frame = showInFrame(panel, "Compare action events: programmatic change");
 
1122
        addAction(frame, action);
 
1123
        frame.pack();
 
1124
    }
 
1125
 
 
1126
 
 
1127
    /**
 
1128
     * Issue #99-swingx: null date and opening popup forces selection.
 
1129
     * Status? Looks fixed..
 
1130
     * 
 
1131
     * Sizing issue if init with null date
 
1132
     */
 
1133
    public void interactiveNullDate() {
 
1134
        JXDatePicker picker = new JXDatePicker();
 
1135
        showInFrame(picker, "null date in picker");
 
1136
    }
 
1137
 
 
1138
    /**
 
1139
     * something weird's going on: the picker's date must be null
 
1140
     * after setting a monthView with null selection. It is, until
 
1141
     * shown?
 
1142
     * Looks fixed during synch control cleanup in datePicker.
 
1143
     */
 
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());
 
1152
    }
 
1153
    
 
1154
    public void interactiveDatePickerDisplay() {
 
1155
        JXDatePicker datePicker = new JXDatePicker();
 
1156
        showInFrame(datePicker, "show date picker");
 
1157
    }
 
1158
    
 
1159
 
 
1160
    
 
1161
    @Override
 
1162
    protected void setUp() throws Exception {
 
1163
        calendar = Calendar.getInstance();
 
1164
    }
 
1165
 
 
1166
    private static class SetPlafAction extends AbstractAction {
 
1167
        private String plaf;
 
1168
        
 
1169
        public SetPlafAction(String name, String plaf) {
 
1170
            super(name);
 
1171
            this.plaf = plaf;
 
1172
        }
 
1173
        
 
1174
        /**
 
1175
         * {@inheritDoc}
 
1176
         */
 
1177
        public void actionPerformed(ActionEvent e) {
 
1178
            try {
 
1179
                Component c = (Component) e.getSource();
 
1180
                Window w = null;
 
1181
                
 
1182
                for (Container p = c.getParent(); p != null; p = p instanceof JPopupMenu ? (Container) ((JPopupMenu) p)
 
1183
                        .getInvoker() : p.getParent()) {
 
1184
                    if (p instanceof Window) {
 
1185
                        w = (Window) p;
 
1186
                    }
 
1187
                }
 
1188
                
 
1189
                UIManager.setLookAndFeel(plaf);
 
1190
                SwingUtilities.updateComponentTreeUI(w);
 
1191
                w.pack();
 
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();
 
1200
            }
 
1201
        }
 
1202
    }
 
1203
    
 
1204
    private JMenuBar createMenuBar() {
 
1205
        LookAndFeelInfo[] plafs = UIManager.getInstalledLookAndFeels();
 
1206
        JMenuBar bar = new JMenuBar();
 
1207
        JMenu menu = new JMenu("Set L&F");
 
1208
        
 
1209
        for (LookAndFeelInfo info : plafs) {
 
1210
            menu.add(new SetPlafAction(info.getName(), info.getClassName()));
 
1211
        }
 
1212
        
 
1213
        bar.add(menu);
 
1214
        
 
1215
        return bar;
 
1216
    }
 
1217
    
 
1218
    @Override
 
1219
    public JXFrame wrapInFrame(JComponent component, String title) {
 
1220
        JXFrame frame = super.wrapInFrame(component, title);
 
1221
        if (showMenu) {
 
1222
            frame.setJMenuBar(createMenuBar());
 
1223
        }
 
1224
        return frame;
 
1225
    }
 
1226
    
 
1227
 
 
1228
 
 
1229
    /**
 
1230
     * Do nothing, make the test runner happy
 
1231
     * (would output a warning without a test fixture).
 
1232
     *
 
1233
     */
 
1234
    public void testDummy() {
 
1235
        
 
1236
    }
 
1237
    
 
1238
}