OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 168 | 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.Color;
4
import java.awt.FlowLayout;
5
import java.awt.GridBagConstraints;
6
import java.awt.GridBagLayout;
7
import java.awt.Insets;
8
import java.awt.event.ActionEvent;
9
import java.awt.event.ActionListener;
10
import java.awt.event.MouseWheelEvent;
11
import java.awt.event.MouseWheelListener;
12
import java.awt.print.PageFormat;
13
import java.beans.PropertyChangeEvent;
14
import java.beans.PropertyChangeListener;
15
import java.util.Calendar;
16
import java.util.Date;
17
 
18
import javax.swing.BorderFactory;
19
import javax.swing.ImageIcon;
20
import javax.swing.JButton;
21
import javax.swing.JLabel;
22
import javax.swing.JPanel;
23
import javax.swing.JScrollPane;
24
import javax.swing.JSeparator;
25
import javax.swing.JSlider;
26
import javax.swing.JSpinner;
27
import javax.swing.SpinnerNumberModel;
28
import javax.swing.event.ChangeEvent;
29
import javax.swing.event.ChangeListener;
30
 
31
public class CalendarWithToolBar extends JPanel {
150 ilm 32
    private final WeekView weekView;
33
    private final JSpinner spinWeek;
34
    private final JSpinner spinYear;
35
    private final DatePicker picker;
137 ilm 36
    private String title;
37
    final JScrollPane contentPane = new JScrollPane();
178 ilm 38
    private int week;
39
    private int year;
137 ilm 40
 
41
    public CalendarWithToolBar(JCalendarItemProvider manager) {
42
        this(manager, false);
43
    }
44
 
45
    public CalendarWithToolBar(JCalendarItemProvider manager, boolean showPrintButton) {
150 ilm 46
        this.weekView = new WeekView(manager);
178 ilm 47
        Calendar currentDate = Calendar.getInstance();
48
        this.year = currentDate.get(Calendar.YEAR);
49
        this.week = currentDate.get(Calendar.WEEK_OF_YEAR);
150 ilm 50
 
137 ilm 51
        JPanel toolbar = new JPanel();
52
        toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
53
 
54
        JButton bPrevious = new JButton(new ImageIcon(this.getClass().getResource("left.png")));
55
        JButton bNext = new JButton(new ImageIcon(this.getClass().getResource("right.png")));
56
        configureButton(bPrevious);
57
        configureButton(bNext);
58
        toolbar.add(bPrevious);
59
        toolbar.add(bNext);
60
        bNext.addActionListener(new ActionListener() {
61
 
62
            @Override
63
            public void actionPerformed(ActionEvent e) {
178 ilm 64
                nextWeek();
137 ilm 65
            }
66
        });
67
        bPrevious.addActionListener(new ActionListener() {
68
 
69
            @Override
70
            public void actionPerformed(ActionEvent e) {
178 ilm 71
                previousWeek();
137 ilm 72
            }
73
        });
74
 
75
        toolbar.add(new JLabel("Semaine"));
178 ilm 76
        this.spinWeek = new JSpinner(new SpinnerNumberModel(this.week, 1, 53, 1));
77
        toolbar.add(this.spinWeek);
137 ilm 78
        toolbar.add(new JLabel(" de "));
178 ilm 79
        this.spinYear = new JSpinner(new SpinnerNumberModel(this.year, 1000, this.year + 20, 1));
80
        toolbar.add(this.spinYear);
137 ilm 81
        //
150 ilm 82
        this.picker = new DatePicker(false);
178 ilm 83
        toolbar.add(this.picker);
137 ilm 84
        //
85
 
150 ilm 86
        final JSlider zoomSlider = createZoomSlider(this.weekView);
137 ilm 87
        toolbar.add(new JLabel(" Zoom"));
88
        toolbar.add(zoomSlider);
89
        if (showPrintButton) {
90
            final JButton jButton = new JButton("Imprimer");
91
            jButton.addActionListener(new ActionListener() {
92
 
93
                @Override
94
                public void actionPerformed(ActionEvent arg0) {
95
                    final PrintJComponentAction a = new PrintJComponentAction(CalendarWithToolBar.this.getContentPane(), PageFormat.LANDSCAPE, CalendarWithToolBar.this.title);
96
                    a.actionPerformed(arg0);
97
                }
98
            });
99
            toolbar.add(jButton);
100
        }
101
        JButton reloadButton = new JButton(new ImageIcon(this.getClass().getResource("auto.png")));
102
        configureButton(reloadButton);
103
        toolbar.add(reloadButton);
104
        reloadButton.addActionListener(new ActionListener() {
105
 
106
            @Override
107
            public void actionPerformed(ActionEvent e) {
108
                reload();
109
            }
110
        });
111
 
112
        this.setLayout(new GridBagLayout());
113
 
114
        //
115
        GridBagConstraints c = new GridBagConstraints();
116
        c.gridx = 0;
117
        c.gridy = 0;
118
        c.weightx = 1;
119
        c.fill = GridBagConstraints.HORIZONTAL;
120
        this.add(toolbar, c);
121
        c.gridy++;
122
        this.add(new JSeparator(JSeparator.HORIZONTAL), c);
123
        c.gridy++;
124
        c.fill = GridBagConstraints.BOTH;
125
        c.weighty = 1;
126
 
178 ilm 127
        this.contentPane.setBorder(BorderFactory.createEmptyBorder());
128
        this.contentPane.setOpaque(false);
129
        this.contentPane.setColumnHeaderView(new WeekViewHeader(this.weekView));
130
        this.contentPane.setViewportView(this.weekView);
131
        this.contentPane.getViewport().setBackground(Color.WHITE);
132
        final MouseWheelListener[] l = this.contentPane.getMouseWheelListeners();
137 ilm 133
        for (int i = 0; i < l.length; i++) {
134
            MouseWheelListener string = l[i];
178 ilm 135
            this.contentPane.removeMouseWheelListener(string);
137 ilm 136
        }
178 ilm 137
        this.contentPane.addMouseWheelListener(new MouseWheelListener() {
137 ilm 138
 
139
            @Override
140
            public void mouseWheelMoved(MouseWheelEvent e) {
178 ilm 141
                CalendarWithToolBar.this.weekView.mouseWheelMoved(e, l);
137 ilm 142
            }
143
        });
144
 
178 ilm 145
        this.add(this.contentPane, c);
137 ilm 146
        final int value = 300;
178 ilm 147
        this.contentPane.getVerticalScrollBar().setValue(value);
148
        this.weekView.loadWeek(this.week, this.year, false);
137 ilm 149
        final ChangeListener listener = new ChangeListener() {
150
 
151
            @Override
152
            public void stateChanged(ChangeEvent e) {
178 ilm 153
                setDate(((Number) CalendarWithToolBar.this.spinWeek.getValue()).intValue(), CalendarWithToolBar.this.year);
154
 
137 ilm 155
            }
156
        };
178 ilm 157
        this.spinWeek.addChangeListener(listener);
137 ilm 158
 
178 ilm 159
        final ChangeListener listenerYear = new ChangeListener() {
137 ilm 160
            @Override
178 ilm 161
            public void stateChanged(ChangeEvent e) {
162
                setDate(CalendarWithToolBar.this.week, ((Number) CalendarWithToolBar.this.spinYear.getValue()).intValue());
163
            }
164
        };
137 ilm 165
 
178 ilm 166
        this.spinYear.addChangeListener(listenerYear);
167
 
168
        this.picker.addActionListener(new ActionListener() {
169
 
170
            @Override
171
            public void actionPerformed(ActionEvent e) {
172
 
173
                final Date d = CalendarWithToolBar.this.picker.getDate();
174
                final Calendar c = Calendar.getInstance();
175
                c.setTime(d);
176
                final int y = c.get(Calendar.YEAR);
177
                final int w = c.get(Calendar.WEEK_OF_YEAR);
178
                setDate(w, y);
137 ilm 179
            }
180
        });
181
 
182
    }
183
 
150 ilm 184
    public static JSlider createZoomSlider(final MultipleDayView mdview) {
168 ilm 185
        final JSlider zoomSlider = new JSlider(1, 10, 1);
150 ilm 186
        zoomSlider.setSnapToTicks(true);
187
        zoomSlider.setMajorTickSpacing(1);
188
        zoomSlider.setPaintTicks(true);
189
        zoomSlider.addChangeListener(new ChangeListener() {
190
            @Override
191
            public void stateChanged(ChangeEvent e) {
192
                mdview.setZoom(zoomSlider.getValue());
193
            }
194
        });
195
        mdview.addPropertyChangeListener("zoom", new PropertyChangeListener() {
196
            @Override
197
            public void propertyChange(PropertyChangeEvent evt) {
198
                zoomSlider.setValue(((Number) evt.getNewValue()).intValue());
199
            }
200
        });
201
        return zoomSlider;
202
    }
203
 
178 ilm 204
    protected void nextWeek() {
205
        if (getWeek() >= 52) {
206
            setDate(1, this.year + 1);
207
        } else {
208
            setDate(this.week + 1, this.year);
209
        }
210
    }
137 ilm 211
 
178 ilm 212
    protected void previousWeek() {
213
        if (getWeek() == 1) {
214
            setDate(52, this.year - 1);
215
        } else {
216
            setDate(this.week - 1, this.year);
217
        }
137 ilm 218
    }
219
 
220
    public Calendar getCurrentDate() {
221
        Calendar c = Calendar.getInstance();
222
        c.clear();
178 ilm 223
        c.set(Calendar.YEAR, this.year);
224
        c.set(Calendar.WEEK_OF_YEAR, this.week);
137 ilm 225
        return c;
226
    }
227
 
150 ilm 228
    public final void setCurrentDate(final Calendar c) {
229
        this.setCurrentDate(c.getTime());
230
    }
231
 
232
    public final void setCurrentDate(final Date d) {
178 ilm 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
 
150 ilm 238
    }
239
 
178 ilm 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);
257
            this.picker.setDate(c.getTime());
258
 
259
            CalendarWithToolBar.this.weekView.loadWeek(getWeek(), getYear(), false);
260
 
261
        }
262
    }
263
 
137 ilm 264
    private void configureButton(JButton b) {
265
        b.setOpaque(false);
266
        b.setBorderPainted(false);
267
        b.setFocusPainted(false);
268
        b.setContentAreaFilled(false);
269
        b.setMargin(new Insets(1, 5, 1, 5));
270
    }
271
 
272
    public void reload() {
178 ilm 273
        this.weekView.reload();
137 ilm 274
    }
275
 
276
    public int getWeek() {
178 ilm 277
        return this.week;
137 ilm 278
    }
279
 
280
    public int getYear() {
178 ilm 281
        return this.year;
137 ilm 282
    }
283
 
284
    public WeekView getWeekView() {
178 ilm 285
        return this.weekView;
137 ilm 286
    }
287
 
288
    public void setTitle(String title) {
289
        this.title = title;
290
    }
291
 
292
    public void scrollTo(int hour) {
178 ilm 293
        this.contentPane.getVerticalScrollBar().setValue(hour * this.weekView.getRowHeight());
137 ilm 294
    }
295
 
296
    public JScrollPane getContentPane() {
178 ilm 297
        return this.contentPane;
137 ilm 298
    }
299
}