Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / de / ios / framework / swing / DateComboBox.java @ 13655

History | View | Annotate | Download (13 KB)

1
package de.ios.framework.swing;
2

    
3
import java.awt.BorderLayout;
4
import java.awt.Color;
5
import java.awt.Container;
6
import java.awt.FlowLayout;
7
import java.awt.GridLayout;
8
import java.awt.event.KeyEvent;
9
import java.awt.event.KeyListener;
10
import java.awt.event.MouseAdapter;
11
import java.awt.event.MouseEvent;
12
import java.awt.event.MouseListener;
13
import java.awt.event.MouseMotionListener;
14
import java.awt.event.WindowAdapter;
15
import java.awt.event.WindowEvent;
16
import java.text.SimpleDateFormat;
17
import java.util.Calendar;
18

    
19
import javax.swing.BorderFactory;
20
import javax.swing.Box;
21
import javax.swing.BoxLayout;
22
import javax.swing.JComboBox;
23
import javax.swing.JFrame;
24
import javax.swing.JLabel;
25
import javax.swing.JList;
26
import javax.swing.JPanel;
27
import javax.swing.JPopupMenu;
28
import javax.swing.SwingUtilities;
29
import javax.swing.UIManager;
30
import javax.swing.border.Border;
31
import javax.swing.border.EmptyBorder;
32
import javax.swing.border.EtchedBorder;
33
import javax.swing.event.PopupMenuEvent;
34
import javax.swing.event.PopupMenuListener;
35
import javax.swing.plaf.ComboBoxUI;
36
import javax.swing.plaf.basic.ComboPopup;
37
import javax.swing.plaf.metal.MetalComboBoxUI;
38

    
39
import com.sun.java.swing.plaf.motif.MotifComboBoxUI;
40
import com.sun.java.swing.plaf.windows.WindowsComboBoxUI;
41

    
42
/**
43
 * @version 1.0 11/02/2000
44
 */
45

    
46
//////////////////////////////////////////////////////////////
47

    
48
public class DateComboBox extends JComboBox {
49
  private static final long serialVersionUID = 1L;
50

    
51
static java.util.ResourceBundle resource = java.util.ResourceBundle.getBundle("resources.Traduction")/*#BundleType=List*/;
52

    
53
    protected SimpleDateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy");
54
    public void setDateFormat(SimpleDateFormat dateFormat) {
55
        this.dateFormat = dateFormat;
56
    }
57
    public void setSelectedItem(Object item) {
58
        // Could put extra logic here or in renderer when item is instanceof Date, Calendar, or String
59
        // Dont keep a list ... just the currently selected item
60
        removeAllItems(); // hides the popup if visible
61
        addItem(item);
62
        super.setSelectedItem(item);
63
    }
64

    
65
    public void updateUI() {
66
        ComboBoxUI cui = (ComboBoxUI) UIManager.getUI(this);
67
        if (cui instanceof MetalComboBoxUI) {
68
            cui = new MetalDateComboBoxUI();
69
        } else if (cui instanceof MotifComboBoxUI) {
70
            cui = new MotifDateComboBoxUI();
71
        } else if (cui instanceof WindowsComboBoxUI) {
72
            cui = new WindowsDateComboBoxUI();
73
        }
74
        setUI(cui);
75
    }
76

    
77
    // Inner classes are used purely to keep DateComboBox component in one file
78
    //////////////////////////////////////////////////////////////
79
    // UI Inner classes -- one for each supported Look and Feel
80
    //////////////////////////////////////////////////////////////
81

    
82
    class MetalDateComboBoxUI extends MetalComboBoxUI {
83
        protected ComboPopup createPopup() {
84
            return new DatePopup( comboBox );
85
        }
86
    }
87

    
88
    class WindowsDateComboBoxUI extends WindowsComboBoxUI {
89
        protected ComboPopup createPopup() {
90
            return new DatePopup( comboBox );
91
        }
92
    }
93

    
94
    class MotifDateComboBoxUI extends MotifComboBoxUI {
95
      private static final long serialVersionUID = 5669807256653124411L;
96

    
97
        protected ComboPopup createPopup() {
98
            return new DatePopup( comboBox );
99
        }
100
    }
101

    
102
    //////////////////////////////////////////////////////////////
103
    // DatePopup inner class
104
    //////////////////////////////////////////////////////////////
105

    
106
    class DatePopup implements ComboPopup, MouseMotionListener,
107
                               MouseListener, KeyListener, PopupMenuListener {
108

    
109
        protected JComboBox comboBox;
110
        protected Calendar calendar;
111
        protected JPopupMenu popup;
112
        protected JLabel monthLabel;
113
        protected JPanel days = null;
114
        protected SimpleDateFormat monthFormat = new SimpleDateFormat("MMM yyyy");
115

    
116
        protected Color selectedBackground;
117
        protected Color selectedForeground;
118
        protected Color background;
119
        protected Color foreground;
120

    
121
        public DatePopup(JComboBox comboBox) {
122
            this.comboBox = comboBox;
123
            calendar = Calendar.getInstance();
124
            // check Look and Feel
125
            background = UIManager.getColor("ComboBox.background");
126
            foreground = UIManager.getColor("ComboBox.foreground");
127
            selectedBackground = UIManager.getColor("ComboBox.selectionBackground");
128
            selectedForeground = UIManager.getColor("ComboBox.selectionForeground");
129

    
130
            initializePopup();
131
        }
132

    
133
        //========================================
134
        // begin ComboPopup method implementations
135
        //
136
        public void show() {
137
            try {
138
                // if setSelectedItem() was called with a valid date, adjust the calendar
139
                calendar.setTime( dateFormat.parse( comboBox.getSelectedItem().toString() ) );
140
            } catch (Exception e) {}
141
            updatePopup();
142
            popup.show(comboBox, 0, comboBox.getHeight());
143
        }
144

    
145
        public void hide() {
146
            popup.setVisible(false);
147
        }
148

    
149
        protected JList list = new JList();
150
        public JList getList() {
151
            return list;
152
        }
153

    
154
        public MouseListener getMouseListener() {
155
            return this;
156
        }
157

    
158
        public MouseMotionListener getMouseMotionListener() {
159
            return this;
160
        }
161

    
162
        public KeyListener getKeyListener() {
163
            return this;
164
        }
165

    
166
        public boolean isVisible() {
167
            return popup.isVisible();
168
        }
169

    
170
        public void uninstallingUI() {
171
            popup.removePopupMenuListener(this);
172
        }
173

    
174
        //
175
        // end ComboPopup method implementations
176
        //======================================
177

    
178

    
179

    
180
        //===================================================================
181
        // begin Event Listeners
182
        //
183

    
184
        // MouseListener
185

    
186
        public void mousePressed( MouseEvent e ) {}
187
        public void mouseReleased( MouseEvent e ) {}
188
        // something else registered for MousePressed
189
        public void mouseClicked(MouseEvent e) {
190
            if ( !SwingUtilities.isLeftMouseButton(e) )
191
                return;
192
            if ( !comboBox.isEnabled() )
193
                return;
194
            if ( comboBox.isEditable() ) {
195
                comboBox.getEditor().getEditorComponent().requestFocus();
196
            } else {
197
                comboBox.requestFocus();
198
            }
199
            togglePopup();
200
        }
201

    
202
        protected boolean mouseInside = false;
203
        public void mouseEntered(MouseEvent e) {
204
            mouseInside = true;
205
        }
206
        public void mouseExited(MouseEvent e) {
207
            mouseInside = false;
208
        }
209

    
210
        // MouseMotionListener
211
        public void mouseDragged(MouseEvent e) {}
212
        public void mouseMoved(MouseEvent e) {}
213

    
214
        // KeyListener
215
        public void keyPressed(KeyEvent e) {}
216
        public void keyTyped(KeyEvent e) {}
217
        public void keyReleased( KeyEvent e ) {
218
            if ( e.getKeyCode() == KeyEvent.VK_SPACE ||
219
                 e.getKeyCode() == KeyEvent.VK_ENTER ) {
220
                togglePopup();
221
            }
222
        }
223

    
224
        /**
225
         * Variables hideNext and mouseInside are used to
226
         * hide the popupMenu by clicking the mouse in the JComboBox
227
         */
228
        public void popupMenuCanceled(PopupMenuEvent e) {}
229
        protected boolean hideNext = false;
230
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
231
            hideNext = mouseInside;
232
        }
233
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {}
234

    
235
        //
236
        // end Event Listeners
237
        //=================================================================
238

    
239
        //===================================================================
240
        // begin Utility methods
241
        //
242

    
243
        protected void togglePopup() {
244
            if ( isVisible() || hideNext ) {
245
                hide();
246
            } else {
247
                show();
248
            }
249
            hideNext = false;
250
        }
251

    
252
        //
253
        // end Utility methods
254
        //=================================================================
255

    
256
        // Note *** did not use JButton because Popup closes when pressed
257
        protected JLabel createUpdateButton(final int field, final int amount) {
258
            final JLabel label = new JLabel();
259
            final Border selectedBorder = new EtchedBorder();
260
            final Border unselectedBorder = new EmptyBorder(selectedBorder.getBorderInsets(new JLabel()));
261
            label.setBorder(unselectedBorder);
262
            label.setForeground(foreground);
263
            label.addMouseListener(new MouseAdapter() {
264
                    public void mouseReleased(MouseEvent e) {
265
                        calendar.add(field, amount);
266
                        updatePopup();
267
                    }
268
                    public void mouseEntered(MouseEvent e) {
269
                        label.setBorder(selectedBorder);
270
                    }
271
                    public void mouseExited(MouseEvent e) {
272
                        label.setBorder(unselectedBorder);
273
                    }
274
                });
275
            return label;
276
        }
277

    
278

    
279
        protected void initializePopup() {
280
            JPanel header = new JPanel(); // used Box, but it wasn't Opaque
281
            header.setLayout(new BoxLayout(header, BoxLayout.X_AXIS));
282
            header.setBackground(background);
283
            header.setOpaque(true);
284

    
285
            JLabel label;
286
            label = createUpdateButton(Calendar.YEAR, -1);
287
            label.setText("<<");
288
            label.setToolTipText(resource.getString("datecombobox.prevyear"));
289

    
290
            header.add(Box.createHorizontalStrut(12));
291
            header.add(label);
292
            header.add(Box.createHorizontalStrut(12));
293

    
294
            label = createUpdateButton(Calendar.MONTH, -1);
295
            label.setText("<");
296
            label.setToolTipText(resource.getString("datecombobox.prevmonth"));
297
            header.add(label);
298

    
299
            monthLabel = new JLabel("", JLabel.CENTER);
300
            monthLabel.setForeground(foreground);
301
            header.add(Box.createHorizontalGlue());
302
            header.add(monthLabel);
303
            header.add(Box.createHorizontalGlue());
304

    
305
            label = createUpdateButton(Calendar.MONTH, 1);
306
            label.setText(">");
307
            label.setToolTipText(resource.getString("datecombobox.nextmonth"));
308
            header.add(label);
309

    
310
            label = createUpdateButton(Calendar.YEAR, 1);
311
            label.setText(">>");
312
            label.setToolTipText(resource.getString("datecombobox.nextyear"));
313

    
314
            header.add(Box.createHorizontalStrut(12));
315
            header.add(label);
316
            header.add(Box.createHorizontalStrut(12));
317

    
318
            popup = new JPopupMenu();
319
            popup.setBorder(BorderFactory.createLineBorder(Color.black));
320
            popup.setLayout(new BorderLayout());
321
            popup.setBackground(background);
322
            popup.addPopupMenuListener(this);
323
            popup.add(BorderLayout.NORTH, header);
324
        }
325

    
326
        // update the Popup when either the month or the year of the calendar has been changed
327
        protected void updatePopup() {
328
            monthLabel.setText( monthFormat.format(calendar.getTime()) );
329
            if (days != null) {
330
                popup.remove(days);
331
            }
332
            days = new JPanel(new GridLayout(0, 7));
333
            days.setBackground(background);
334
            days.setOpaque(true);
335

    
336
            Calendar setupCalendar = (Calendar) calendar.clone();
337
            setupCalendar.set(Calendar.DAY_OF_WEEK, setupCalendar.getFirstDayOfWeek());
338
            for (int i = 0; i < 7; i++) {
339
                int dayInt = setupCalendar.get(Calendar.DAY_OF_WEEK);
340
                JLabel label = new JLabel();
341
                label.setHorizontalAlignment(JLabel.CENTER);
342
                label.setForeground(foreground);
343
                if (dayInt == Calendar.SUNDAY) {
344
                    label.setText(resource.getString("datecombobox.sun"));
345
                } else if (dayInt == Calendar.MONDAY) {
346
                    label.setText(resource.getString("datecombobox.mon"));
347
                } else if (dayInt == Calendar.TUESDAY) {
348
                    label.setText(resource.getString("datecombobox.tue"));
349
                } else if (dayInt == Calendar.WEDNESDAY) {
350
                    label.setText(resource.getString("datecombobox.wed"));
351
                } else if (dayInt == Calendar.THURSDAY) {
352
                    label.setText(resource.getString("datecombobox.thu"));
353
                } else if (dayInt == Calendar.FRIDAY) {
354
                    label.setText(resource.getString("datecombobox.fri"));
355
                } else if (dayInt == Calendar.SATURDAY){
356
                    label.setText(resource.getString("datecombobox.sat"));
357
                }
358
                days.add(label);
359
                setupCalendar.roll(Calendar.DAY_OF_WEEK, true);
360
            }
361

    
362
            setupCalendar = (Calendar) calendar.clone();
363
            setupCalendar.set(Calendar.DAY_OF_MONTH, 1);
364
            int first = setupCalendar.get(Calendar.DAY_OF_WEEK);
365
            for (int i = 0; i < (first - 1); i++) {
366
                days.add(new JLabel(""));
367
            }
368
            for (int i = 1; i <= setupCalendar.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
369
                final int day = i;
370
                final JLabel label = new JLabel(String.valueOf(day));
371
                label.setHorizontalAlignment(JLabel.CENTER);
372
                label.setForeground(foreground);
373
                label.addMouseListener(new MouseListener() {
374
                        public void mousePressed(MouseEvent e) {}
375
                        public void mouseClicked(MouseEvent e) {}
376
                        public void mouseReleased(MouseEvent e) {
377
                            label.setOpaque(false);
378
                            label.setBackground(background);
379
                            label.setForeground(foreground);
380
                            calendar.set(Calendar.DAY_OF_MONTH, day);
381
                            comboBox.setSelectedItem(dateFormat.format(calendar.getTime()));
382
                            // hide();
383
                            // hide is called with setSelectedItem() ... removeAll()
384
                            comboBox.requestFocus();
385
                        }
386
                        public void mouseEntered(MouseEvent e) {
387
                            label.setOpaque(true);
388
                            label.setBackground(selectedBackground);
389
                            label.setForeground(selectedForeground);
390
                        }
391
                        public void mouseExited(MouseEvent e) {
392
                            label.setOpaque(false);
393
                            label.setBackground(background);
394
                            label.setForeground(foreground);
395
                        }
396
                    });
397

    
398
                days.add(label);
399
            }
400

    
401
            popup.add(BorderLayout.CENTER, days);
402
            popup.pack();
403
        }
404
    }
405

    
406
    //////////////////////////////////////////////////////////////
407
    // This is only included to provide a sample GUI
408
    //////////////////////////////////////////////////////////////
409
    public static void main(String args[]) {
410
        JFrame f = new JFrame();
411
        Container c = f.getContentPane();
412
        c.setLayout(new FlowLayout());
413
        c.add(new JLabel("Date 1:"));
414
        c.add(new DateComboBox());
415
        c.add(new JLabel("Date 2:"));
416
        DateComboBox dcb = new DateComboBox();
417
        dcb.setEditable(true);
418
        c.add(dcb);
419
        f.addWindowListener(new WindowAdapter() {
420
                public void windowClosing(WindowEvent e) {
421
                    System.exit(0);
422
                }
423
            });
424
        f.setSize(500, 200);
425
        f.setVisible(true);
426
    }
427

    
428
}
429

    
430

    
431

    
432

    
433