OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 137 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
137 ilm 1
package org.jopencalendar.ui;
2
 
3
import java.awt.BorderLayout;
4
import java.awt.Color;
5
import java.awt.Dimension;
6
import java.awt.Font;
7
import java.awt.Graphics;
8
import java.awt.GridLayout;
9
import java.awt.event.ActionEvent;
10
import java.awt.event.ActionListener;
11
import java.awt.event.MouseAdapter;
12
import java.awt.event.MouseEvent;
13
import java.awt.event.MouseListener;
14
import java.beans.PropertyChangeEvent;
15
import java.beans.PropertyChangeListener;
16
import java.text.DateFormat;
17
import java.text.DateFormatSymbols;
18
import java.text.SimpleDateFormat;
19
import java.util.ArrayList;
20
import java.util.Calendar;
21
import java.util.Date;
22
import java.util.List;
23
 
24
import javax.swing.BorderFactory;
25
import javax.swing.ImageIcon;
26
import javax.swing.JButton;
27
import javax.swing.JFrame;
28
import javax.swing.JLabel;
29
import javax.swing.JPanel;
30
import javax.swing.JTextField;
31
import javax.swing.SwingConstants;
32
import javax.swing.SwingUtilities;
33
import javax.swing.UIManager;
34
 
35
public class DatePickerPanel extends JPanel implements ActionListener, MouseListener {
36
 
37
    private static final long serialVersionUID = -2882634897084487051L;
38
    public static final String TIME_IN_MILLIS = "timeInMillis";
39
    private int rowCount = 7;
40
    private int colCount = 7;
41
    private int currentYear;
42
    private int currentMonth;
43
    private List<DateLabel> labels = new ArrayList<DateLabel>();
44
    private JLabel title;
45
    private JButton bRight;
46
    private JButton bLeft;
47
    private Date selectedDate;
48
 
49
    public DatePickerPanel() {
50
        //
51
        Calendar cal = Calendar.getInstance();
52
        this.selectedDate = cal.getTime();
53
        this.currentYear = cal.get(Calendar.YEAR);
54
        this.currentMonth = cal.get(Calendar.MONTH);
55
        //
56
 
57
        this.setLayout(new BorderLayout(2, 2));
58
        JPanel navigator = new JPanel();
59
        navigator.setLayout(new BorderLayout());
153 ilm 60
        String leftFileName = "left.png";
61
        String rightFileName = "right.png";
62
        if (this.getFont().getSize() > 16) {
63
            leftFileName = "left_2x.png";
64
            rightFileName = "right_2x.png";
65
        }
66
        bLeft = new JButton(new ImageIcon(this.getClass().getResource(leftFileName)));
137 ilm 67
        configureButton(bLeft);
68
 
69
        navigator.add(bLeft, BorderLayout.WEST);
70
        title = new JLabel("...", SwingConstants.CENTER);
71
        title.setFont(title.getFont().deriveFont(Font.BOLD));
72
        navigator.add(title, BorderLayout.CENTER);
153 ilm 73
 
74
        bRight = new JButton(new ImageIcon(this.getClass().getResource(rightFileName)));
137 ilm 75
        configureButton(bRight);
76
        navigator.add(bRight, BorderLayout.EAST);
77
        this.add(navigator, BorderLayout.NORTH);
78
        final JPanel dayPanel = new JPanel() {
79
 
80
            private static final long serialVersionUID = 1L;
81
 
82
            @Override
83
            protected void paintComponent(Graphics g) {
84
                super.paintComponent(g);
85
                g.setColor(Color.GRAY);
86
                g.drawLine(0, 0, getWidth(), 0);
87
            }
88
        };
89
        dayPanel.setBackground(Color.WHITE);
90
        dayPanel.setLayout(new GridLayout(rowCount, colCount, 4, 4));
91
        dayPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
92
        //
93
 
94
        String[] dateFormatSymbols = new DateFormatSymbols().getShortWeekdays();
95
        int f = cal.getFirstDayOfWeek();
96
 
97
        for (int i = 0; i < colCount; i++) {
98
            int j = i + f;
99
            if (j >= 8) {
100
                j = 1;
101
            }
102
            String d = dateFormatSymbols[j];
153 ilm 103
            final JLabel lDay = new JLabel(d, SwingConstants.RIGHT);
104
            lDay.setMinimumSize(new Dimension(lDay.getPreferredSize().width, lDay.getPreferredSize().width));
105
            dayPanel.add(lDay);
137 ilm 106
        }
107
 
108
        //
109
        int c = 0;
110
        final MouseAdapter mListener = new MouseAdapter() {
111
            public long select(MouseEvent e) {
112
                if (e.getSource() instanceof DateLabel) {
113
                    final DateLabel dateLabel = (DateLabel) e.getSource();
114
                    final Calendar c = Calendar.getInstance();
115
                    final long timeInMillis = dateLabel.getTimeInMillis();
116
                    c.setTimeInMillis(timeInMillis);
117
                    setSelectedDate(c);
118
                    return timeInMillis;
119
                }
120
                return -1;
121
            }
122
 
123
            @Override
124
            public void mouseDragged(MouseEvent e) {
125
                e.setSource(dayPanel.getComponentAt(SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), dayPanel)));
126
                select(e);
127
            }
128
 
129
            @Override
130
            public void mousePressed(MouseEvent e) {
131
                // Nothing
132
            }
133
 
134
            @Override
135
            public void mouseReleased(MouseEvent e) {
136
                e.setSource(dayPanel.getComponentAt(SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), dayPanel)));
137
                long timeInMillis = select(e);
138
                if (timeInMillis >= 0) {
139
                    firePropertyChange(TIME_IN_MILLIS, Long.valueOf(0), Long.valueOf(timeInMillis));
140
                }
141
            }
142
        };
143
 
144
        for (int i = 0; i < rowCount - 1; i++) {
145
            for (int j = 0; j < colCount; j++) {
146
                final DateLabel label = new DateLabel(String.valueOf(c), SwingConstants.RIGHT);
147
                label.addMouseListener(mListener);
148
                label.addMouseMotionListener(mListener);
149
                label.setOpaque(true);
150
                this.labels.add(label);
151
                dayPanel.add(label);
152
                c++;
153
            }
154
 
155
        }
156
 
157
        this.add(dayPanel, BorderLayout.CENTER);
158
        DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);
159
        // Today
160
        final JLabel date = new JLabel(df.format(new Date()), SwingConstants.CENTER);
161
        date.setBorder(BorderFactory.createEmptyBorder(4, 3, 5, 3));
162
        this.add(date, BorderLayout.SOUTH);
163
        //
164
        updateLabels();
165
        bRight.addActionListener(this);
166
        bLeft.addActionListener(this);
167
        date.addMouseListener(this);
168
 
169
    }
170
 
171
    public Date getSelectedDate() {
172
        return selectedDate;
173
    }
174
 
175
    public void setSelectedDate(Calendar cal) {
176
        if (cal == null) {
177
            this.selectedDate = null;
178
            setDate(Calendar.getInstance());
179
        } else if (this.selectedDate == null || !this.selectedDate.equals(cal.getTime())) {
180
            this.selectedDate = cal.getTime();
181
            setDate(cal);
182
        }
183
    }
184
 
185
    public void setDate(Calendar cal) {
186
        this.currentYear = cal.get(Calendar.YEAR);
187
        this.currentMonth = cal.get(Calendar.MONTH);
188
        updateLabels();
189
    }
190
 
191
    private void configureButton(final JButton button) {
153 ilm 192
        int buttonSize = button.getIcon().getIconWidth() + 8;
137 ilm 193
        button.setMinimumSize(new Dimension(buttonSize, buttonSize));
194
        button.setPreferredSize(new Dimension(buttonSize, buttonSize));
195
        button.setFocusPainted(false);
196
        button.setBorderPainted(false);
197
        button.setBorder(null);
198
        button.setContentAreaFilled(false);
199
        button.setOpaque(false);
200
    }
201
 
202
    private void updateLabels() {
203
        int selectedYear = -1;
204
        int selectedDay = -1;
205
        if (selectedDate != null) {
206
            Calendar calSelected = Calendar.getInstance();
207
            calSelected.setTime(this.selectedDate);
208
            selectedYear = calSelected.get(Calendar.YEAR);
209
            selectedDay = calSelected.get(Calendar.DAY_OF_YEAR);
210
        }
211
 
212
        Calendar cal = Calendar.getInstance();
213
        int todayYear = cal.get(Calendar.YEAR);
214
        int todayDay = cal.get(Calendar.DAY_OF_YEAR);
215
        cal.clear();
216
        cal.set(Calendar.YEAR, this.currentYear);
217
        cal.set(Calendar.MONTH, this.currentMonth);
218
        cal.set(Calendar.DAY_OF_MONTH, 1);
219
        SimpleDateFormat sdf = new SimpleDateFormat("MMMM yyyy");
220
        title.setText(sdf.format(cal.getTime()));
221
 
222
        int f = cal.getFirstDayOfWeek();
223
        int offset = cal.get(Calendar.DAY_OF_WEEK) - f + 1; // offset du 1er jour du mois par
224
                                                            // rapport a
225
 
226
        if (offset < 2) {
227
            offset += 7;
228
        }
229
 
230
        // grille
231
        cal.add(Calendar.DAY_OF_YEAR, -offset);
232
 
233
        int c = 0;
234
        for (int i = 0; i < rowCount - 1; i++) {
235
            for (int j = 0; j < colCount; j++) {
236
                cal.add(Calendar.DAY_OF_YEAR, 1);
237
                final DateLabel label = this.labels.get(c);
238
                label.setDate(cal);
239
                label.setText(cal.get(Calendar.DAY_OF_MONTH) + "  ");
240
                if (cal.get(Calendar.MONTH) != this.currentMonth) {
241
                    if (!label.getForeground().equals(Color.GRAY))
242
                        label.setForeground(Color.GRAY);
243
                } else {
244
                    if (!label.getForeground().equals(Color.BLACK))
245
                        label.setForeground(Color.BLACK);
246
                }
247
                if (cal.get(Calendar.YEAR) == todayYear && cal.get(Calendar.DAY_OF_YEAR) == todayDay) {
248
                    label.setBackground(Color.LIGHT_GRAY);
249
                    label.setForeground(Color.WHITE);
250
                } else {
251
                    label.setBackground(Color.WHITE);
252
                }
253
                if (cal.get(Calendar.YEAR) == selectedYear && cal.get(Calendar.DAY_OF_YEAR) == selectedDay) {
254
                    label.setBackground(new Color(232, 242, 250));
255
                    label.setForeground(Color.BLACK);
256
                }
257
                c++;
258
            }
259
 
260
        }
261
 
262
    }
263
 
264
    @Override
265
    public void setFocusable(boolean focusable) {
266
        super.setFocusable(focusable);
267
        bRight.setFocusable(focusable);
268
        bLeft.setFocusable(focusable);
269
    }
270
 
271
    public static void main(String[] args) {
272
        SwingUtilities.invokeLater(new Runnable() {
273
            public void run() {
274
                try {
275
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
276
                } catch (Exception e1) {
277
                    e1.printStackTrace();
278
                }
279
                final JFrame f = new JFrame();
280
                final DatePickerPanel picker = new DatePickerPanel();
281
                picker.addPropertyChangeListener(TIME_IN_MILLIS, new PropertyChangeListener() {
282
 
283
                    @Override
284
                    public void propertyChange(PropertyChangeEvent evt) {
285
                        final Long millis = (Long) evt.getNewValue();
286
                        Calendar c = Calendar.getInstance();
287
                        c.setTimeInMillis(millis);
288
                        f.dispose();
289
 
290
                    }
291
                });
292
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
293
                f.setContentPane(picker);
294
                f.pack();
295
                f.setLocationRelativeTo(null);
296
                f.setVisible(true);
297
 
298
                final JFrame f1 = new JFrame();
299
                final DatePicker p = new DatePicker();
300
                Calendar c = Calendar.getInstance();
301
                c.set(Calendar.YEAR, 2010);
302
                c.set(Calendar.MONTH, 2);
303
                c.set(Calendar.DAY_OF_MONTH, 1);
304
 
305
                p.setDate(c.getTime());
306
                p.setDate(null);
307
                final JPanel pRed = new JPanel();
308
                pRed.setBackground(Color.RED);
309
                pRed.add(p);
310
                pRed.add(new JTextField("Dummy TextField"));
311
 
312
                f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
313
                f1.setContentPane(pRed);
314
                f1.pack();
315
                f1.setLocation(f.getLocation().x + f.getWidth() + 10, f.getLocation().y);
316
                f1.setVisible(true);
317
 
318
            }
319
        });
320
 
321
    }
322
 
323
    @Override
324
    public void actionPerformed(ActionEvent e) {
325
        // Buttons + / -
326
        Calendar cal = Calendar.getInstance();
327
        cal.clear();
328
        cal.set(Calendar.YEAR, this.currentYear);
329
        cal.set(Calendar.MONTH, this.currentMonth);
330
        if (e.getSource().equals(this.bLeft)) {
331
            cal.add(Calendar.MONTH, -1);
332
        } else if (e.getSource().equals(this.bRight)) {
333
            cal.add(Calendar.MONTH, 1);
334
        }
335
        setDate(cal);
336
    }
337
 
338
    @Override
339
    public void mouseClicked(MouseEvent e) {
340
    }
341
 
342
    @Override
343
    public void mousePressed(MouseEvent e) {
344
        Calendar cal = Calendar.getInstance();
345
        cal.set(Calendar.SECOND, 0);
346
        cal.set(Calendar.MILLISECOND, 0);
347
        if (e.getClickCount() > 1) {
348
            setSelectedDate(cal);
349
            firePropertyChange(TIME_IN_MILLIS, Long.valueOf(0), Long.valueOf(cal.getTimeInMillis()));
350
        } else {
351
            setDate(cal);
352
        }
353
    }
354
 
355
    @Override
356
    public void mouseReleased(MouseEvent e) {
153 ilm 357
        // Nothing
137 ilm 358
    }
359
 
360
    @Override
361
    public void mouseEntered(MouseEvent e) {
153 ilm 362
        // Nothing
137 ilm 363
    }
364
 
365
    @Override
366
    public void mouseExited(MouseEvent e) {
153 ilm 367
        // Nothing
137 ilm 368
    }
369
 
370
}