OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 168 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 168 Rev 178
Line 33... Line 33...
33
    private final JSpinner spinWeek;
33
    private final JSpinner spinWeek;
34
    private final JSpinner spinYear;
34
    private final JSpinner spinYear;
35
    private final DatePicker picker;
35
    private final DatePicker picker;
36
    private String title;
36
    private String title;
37
    final JScrollPane contentPane = new JScrollPane();
37
    final JScrollPane contentPane = new JScrollPane();
-
 
38
    private int week;
-
 
39
    private int year;
38
 
40
 
39
    public CalendarWithToolBar(JCalendarItemProvider manager) {
41
    public CalendarWithToolBar(JCalendarItemProvider manager) {
40
        this(manager, false);
42
        this(manager, false);
41
    }
43
    }
42
 
44
 
43
    public CalendarWithToolBar(JCalendarItemProvider manager, boolean showPrintButton) {
45
    public CalendarWithToolBar(JCalendarItemProvider manager, boolean showPrintButton) {
44
        this.weekView = new WeekView(manager);
46
        this.weekView = new WeekView(manager);
-
 
47
        Calendar currentDate = Calendar.getInstance();
-
 
48
        this.year = currentDate.get(Calendar.YEAR);
-
 
49
        this.week = currentDate.get(Calendar.WEEK_OF_YEAR);
45
 
50
 
46
        JPanel toolbar = new JPanel();
51
        JPanel toolbar = new JPanel();
47
        toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
52
        toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
48
 
53
 
49
        Calendar cal = Calendar.getInstance();
-
 
50
        int week = cal.get(Calendar.WEEK_OF_YEAR);
-
 
51
        int year = cal.get(Calendar.YEAR);
-
 
52
        JButton bPrevious = new JButton(new ImageIcon(this.getClass().getResource("left.png")));
54
        JButton bPrevious = new JButton(new ImageIcon(this.getClass().getResource("left.png")));
53
        JButton bNext = new JButton(new ImageIcon(this.getClass().getResource("right.png")));
55
        JButton bNext = new JButton(new ImageIcon(this.getClass().getResource("right.png")));
54
        configureButton(bPrevious);
56
        configureButton(bPrevious);
55
        configureButton(bNext);
57
        configureButton(bNext);
56
        toolbar.add(bPrevious);
58
        toolbar.add(bPrevious);
57
        toolbar.add(bNext);
59
        toolbar.add(bNext);
58
        bNext.addActionListener(new ActionListener() {
60
        bNext.addActionListener(new ActionListener() {
59
 
61
 
60
            @Override
62
            @Override
61
            public void actionPerformed(ActionEvent e) {
63
            public void actionPerformed(ActionEvent e) {
62
                addWeek(1);
64
                nextWeek();
63
 
-
 
64
            }
65
            }
65
        });
66
        });
66
        bPrevious.addActionListener(new ActionListener() {
67
        bPrevious.addActionListener(new ActionListener() {
67
 
68
 
68
            @Override
69
            @Override
69
            public void actionPerformed(ActionEvent e) {
70
            public void actionPerformed(ActionEvent e) {
70
                addWeek(-1);
71
                previousWeek();
71
 
-
 
72
            }
72
            }
73
        });
73
        });
74
 
74
 
75
        toolbar.add(new JLabel("Semaine"));
75
        toolbar.add(new JLabel("Semaine"));
76
        spinWeek = new JSpinner(new SpinnerNumberModel(week, 1, 53, 1));
76
        this.spinWeek = new JSpinner(new SpinnerNumberModel(this.week, 1, 53, 1));
77
        toolbar.add(spinWeek);
77
        toolbar.add(this.spinWeek);
78
        toolbar.add(new JLabel(" de "));
78
        toolbar.add(new JLabel(" de "));
79
        spinYear = new JSpinner(new SpinnerNumberModel(year, 1000, year + 20, 1));
79
        this.spinYear = new JSpinner(new SpinnerNumberModel(this.year, 1000, this.year + 20, 1));
80
        toolbar.add(spinYear);
80
        toolbar.add(this.spinYear);
81
        //
81
        //
82
        this.picker = new DatePicker(false);
82
        this.picker = new DatePicker(false);
83
        toolbar.add(picker);
83
        toolbar.add(this.picker);
84
        //
84
        //
85
 
85
 
86
        final JSlider zoomSlider = createZoomSlider(this.weekView);
86
        final JSlider zoomSlider = createZoomSlider(this.weekView);
87
        toolbar.add(new JLabel(" Zoom"));
87
        toolbar.add(new JLabel(" Zoom"));
88
        toolbar.add(zoomSlider);
88
        toolbar.add(zoomSlider);
Line 122... Line 122...
122
        this.add(new JSeparator(JSeparator.HORIZONTAL), c);
122
        this.add(new JSeparator(JSeparator.HORIZONTAL), c);
123
        c.gridy++;
123
        c.gridy++;
124
        c.fill = GridBagConstraints.BOTH;
124
        c.fill = GridBagConstraints.BOTH;
125
        c.weighty = 1;
125
        c.weighty = 1;
126
 
126
 
127
        contentPane.setBorder(BorderFactory.createEmptyBorder());
127
        this.contentPane.setBorder(BorderFactory.createEmptyBorder());
128
        contentPane.setOpaque(false);
128
        this.contentPane.setOpaque(false);
129
        contentPane.setColumnHeaderView(new WeekViewHeader(weekView));
129
        this.contentPane.setColumnHeaderView(new WeekViewHeader(this.weekView));
130
        contentPane.setViewportView(weekView);
130
        this.contentPane.setViewportView(this.weekView);
131
        contentPane.getViewport().setBackground(Color.WHITE);
131
        this.contentPane.getViewport().setBackground(Color.WHITE);
132
        final MouseWheelListener[] l = contentPane.getMouseWheelListeners();
132
        final MouseWheelListener[] l = this.contentPane.getMouseWheelListeners();
133
        for (int i = 0; i < l.length; i++) {
133
        for (int i = 0; i < l.length; i++) {
134
            MouseWheelListener string = l[i];
134
            MouseWheelListener string = l[i];
135
            contentPane.removeMouseWheelListener(string);
135
            this.contentPane.removeMouseWheelListener(string);
136
        }
136
        }
137
        contentPane.addMouseWheelListener(new MouseWheelListener() {
137
        this.contentPane.addMouseWheelListener(new MouseWheelListener() {
138
 
138
 
139
            @Override
139
            @Override
140
            public void mouseWheelMoved(MouseWheelEvent e) {
140
            public void mouseWheelMoved(MouseWheelEvent e) {
141
                weekView.mouseWheelMoved(e, l);
141
                CalendarWithToolBar.this.weekView.mouseWheelMoved(e, l);
142
            }
142
            }
143
        });
143
        });
144
 
144
 
145
        this.add(contentPane, c);
145
        this.add(this.contentPane, c);
146
        final int value = 300;
146
        final int value = 300;
147
        contentPane.getVerticalScrollBar().setValue(value);
147
        this.contentPane.getVerticalScrollBar().setValue(value);
148
        weekView.loadWeek(week, year, false);
148
        this.weekView.loadWeek(this.week, this.year, false);
149
        final ChangeListener listener = new ChangeListener() {
149
        final ChangeListener listener = new ChangeListener() {
150
 
150
 
151
            @Override
151
            @Override
152
            public void stateChanged(ChangeEvent e) {
152
            public void stateChanged(ChangeEvent e) {
153
                Calendar c = getCurrentDate();
-
 
154
                picker.setDate(c.getTime());
-
 
155
                weekView.loadWeek(getWeek(), getYear(), false);
153
                setDate(((Number) CalendarWithToolBar.this.spinWeek.getValue()).intValue(), CalendarWithToolBar.this.year);
-
 
154
 
156
            }
155
            }
157
        };
156
        };
158
        spinWeek.addChangeListener(listener);
157
        this.spinWeek.addChangeListener(listener);
159
        spinYear.addChangeListener(listener);
-
 
160
        picker.addPropertyChangeListener("value", new PropertyChangeListener() {
-
 
161
 
158
 
-
 
159
        final ChangeListener listenerYear = new ChangeListener() {
162
            @Override
160
            @Override
163
            public void propertyChange(PropertyChangeEvent evt) {
161
            public void stateChanged(ChangeEvent e) {
-
 
162
                setDate(CalendarWithToolBar.this.week, ((Number) CalendarWithToolBar.this.spinYear.getValue()).intValue());
-
 
163
            }
-
 
164
        };
-
 
165
 
-
 
166
        this.spinYear.addChangeListener(listenerYear);
-
 
167
 
-
 
168
        this.picker.addActionListener(new ActionListener() {
-
 
169
 
-
 
170
            @Override
164
                if (evt.getNewValue() != null) {
171
            public void actionPerformed(ActionEvent e) {
-
 
172
 
165
                    final Date d = (Date) evt.getNewValue();
173
                final Date d = CalendarWithToolBar.this.picker.getDate();
166
                    final Calendar c = Calendar.getInstance();
174
                final Calendar c = Calendar.getInstance();
167
                    c.setTime(d);
175
                c.setTime(d);
168
                    final int y = c.get(Calendar.YEAR);
176
                final int y = c.get(Calendar.YEAR);
169
                    final int w = c.get(Calendar.WEEK_OF_YEAR);
177
                final int w = c.get(Calendar.WEEK_OF_YEAR);
170
                    spinWeek.removeChangeListener(listener);
-
 
171
                    spinYear.removeChangeListener(listener);
-
 
172
                    spinYear.setValue(y);
-
 
173
                    spinWeek.setValue(w);
178
                setDate(w, y);
174
                    spinWeek.addChangeListener(listener);
-
 
175
                    spinYear.addChangeListener(listener);
-
 
176
                    weekView.loadWeek(getWeek(), getYear(), false);
-
 
177
                }
-
 
178
 
-
 
179
            }
179
            }
180
        });
180
        });
181
 
181
 
182
    }
182
    }
183
 
183
 
Line 199... Line 199...
199
            }
199
            }
200
        });
200
        });
201
        return zoomSlider;
201
        return zoomSlider;
202
    }
202
    }
203
 
203
 
204
    protected void addWeek(int i) {
204
    protected void nextWeek() {
205
        Calendar c = getCurrentDate();
205
        if (getWeek() >= 52) {
206
        c.add(Calendar.DAY_OF_YEAR, i * 7);
206
            setDate(1, this.year + 1);
207
        final int year = c.get(Calendar.YEAR);
207
        } else {
208
        final int week = c.get(Calendar.WEEK_OF_YEAR);
208
            setDate(this.week + 1, this.year);
209
        spinYear.setValue(year);
209
        }
210
        spinWeek.setValue(week);
210
    }
211
 
211
 
-
 
212
    protected void previousWeek() {
-
 
213
        if (getWeek() == 1) {
-
 
214
            setDate(52, this.year - 1);
-
 
215
        } else {
-
 
216
            setDate(this.week - 1, this.year);
-
 
217
        }
212
    }
218
    }
213
 
219
 
214
    public Calendar getCurrentDate() {
220
    public Calendar getCurrentDate() {
215
        Calendar c = Calendar.getInstance();
221
        Calendar c = Calendar.getInstance();
216
        c.clear();
222
        c.clear();
217
        c.set(Calendar.YEAR, getYear());
223
        c.set(Calendar.YEAR, this.year);
218
        c.set(Calendar.WEEK_OF_YEAR, getWeek());
224
        c.set(Calendar.WEEK_OF_YEAR, this.week);
219
        return c;
225
        return c;
220
    }
226
    }
221
 
227
 
222
    public final void setCurrentDate(final Calendar c) {
228
    public final void setCurrentDate(final Calendar c) {
223
        this.setCurrentDate(c.getTime());
229
        this.setCurrentDate(c.getTime());
224
    }
230
    }
225
 
231
 
226
    public final void setCurrentDate(final Date d) {
232
    public final void setCurrentDate(final Date d) {
-
 
233
        Calendar c = Calendar.getInstance();
-
 
234
        c.clear();
-
 
235
        c.setTime(d);
-
 
236
        this.setDate(c.get(Calendar.WEEK_OF_YEAR), c.get(Calendar.YEAR));
-
 
237
 
-
 
238
    }
-
 
239
 
-
 
240
    private void setDate(int week, int year) {
-
 
241
        if (this.week != week || this.year != year) {
-
 
242
            int oldWeek = this.week;
-
 
243
            int oldYear = this.year;
-
 
244
            this.week = week;
-
 
245
            this.year = year;
-
 
246
            if (this.week != oldWeek) {
-
 
247
                this.spinWeek.setValue(week);
-
 
248
            }
-
 
249
            if (this.year != oldYear) {
-
 
250
                this.spinYear.setValue(year);
-
 
251
            }
-
 
252
 
-
 
253
            Calendar c = Calendar.getInstance();
-
 
254
            c.clear();
-
 
255
            c.set(Calendar.YEAR, this.year);
-
 
256
            c.set(Calendar.WEEK_OF_YEAR, this.week);
227
        this.picker.setDate(d);
257
            this.picker.setDate(c.getTime());
-
 
258
 
-
 
259
            CalendarWithToolBar.this.weekView.loadWeek(getWeek(), getYear(), false);
-
 
260
 
-
 
261
        }
228
    }
262
    }
229
 
263
 
230
    private void configureButton(JButton b) {
264
    private void configureButton(JButton b) {
231
        b.setOpaque(false);
265
        b.setOpaque(false);
232
        b.setBorderPainted(false);
266
        b.setBorderPainted(false);
Line 234... Line 268...
234
        b.setContentAreaFilled(false);
268
        b.setContentAreaFilled(false);
235
        b.setMargin(new Insets(1, 5, 1, 5));
269
        b.setMargin(new Insets(1, 5, 1, 5));
236
    }
270
    }
237
 
271
 
238
    public void reload() {
272
    public void reload() {
239
        weekView.reload();
273
        this.weekView.reload();
240
    }
274
    }
241
 
275
 
242
    public int getWeek() {
276
    public int getWeek() {
243
        return ((Number) spinWeek.getValue()).intValue();
277
        return this.week;
244
    }
278
    }
245
 
279
 
246
    public int getYear() {
280
    public int getYear() {
247
        return ((Number) spinYear.getValue()).intValue();
281
        return this.year;
248
    }
282
    }
249
 
283
 
250
    public WeekView getWeekView() {
284
    public WeekView getWeekView() {
251
        return weekView;
285
        return this.weekView;
252
    }
286
    }
253
 
287
 
254
    public void setTitle(String title) {
288
    public void setTitle(String title) {
255
        this.title = title;
289
        this.title = title;
256
    }
290
    }
257
 
291
 
258
    public void scrollTo(int hour) {
292
    public void scrollTo(int hour) {
259
        contentPane.getVerticalScrollBar().setValue(hour * weekView.getRowHeight());
293
        this.contentPane.getVerticalScrollBar().setValue(hour * this.weekView.getRowHeight());
260
    }
294
    }
261
 
295
 
262
    public JScrollPane getContentPane() {
296
    public JScrollPane getContentPane() {
263
        return contentPane;
297
        return this.contentPane;
264
    }
298
    }
265
}
299
}