OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 150 | 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();
38
 
39
    public CalendarWithToolBar(JCalendarItemProvider manager) {
40
        this(manager, false);
41
    }
42
 
43
    public CalendarWithToolBar(JCalendarItemProvider manager, boolean showPrintButton) {
150 ilm 44
        this.weekView = new WeekView(manager);
45
 
137 ilm 46
        JPanel toolbar = new JPanel();
47
        toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
48
 
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")));
53
        JButton bNext = new JButton(new ImageIcon(this.getClass().getResource("right.png")));
54
        configureButton(bPrevious);
55
        configureButton(bNext);
56
        toolbar.add(bPrevious);
57
        toolbar.add(bNext);
58
        bNext.addActionListener(new ActionListener() {
59
 
60
            @Override
61
            public void actionPerformed(ActionEvent e) {
62
                addWeek(1);
63
 
64
            }
65
        });
66
        bPrevious.addActionListener(new ActionListener() {
67
 
68
            @Override
69
            public void actionPerformed(ActionEvent e) {
70
                addWeek(-1);
71
 
72
            }
73
        });
74
 
75
        toolbar.add(new JLabel("Semaine"));
76
        spinWeek = new JSpinner(new SpinnerNumberModel(week, 1, 53, 1));
77
        toolbar.add(spinWeek);
78
        toolbar.add(new JLabel(" de "));
79
        spinYear = new JSpinner(new SpinnerNumberModel(year, 1000, year + 20, 1));
80
        toolbar.add(spinYear);
81
        //
150 ilm 82
        this.picker = new DatePicker(false);
137 ilm 83
        toolbar.add(picker);
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
 
127
        contentPane.setBorder(BorderFactory.createEmptyBorder());
128
        contentPane.setOpaque(false);
129
        contentPane.setColumnHeaderView(new WeekViewHeader(weekView));
130
        contentPane.setViewportView(weekView);
131
        contentPane.getViewport().setBackground(Color.WHITE);
132
        final MouseWheelListener[] l = contentPane.getMouseWheelListeners();
133
        for (int i = 0; i < l.length; i++) {
134
            MouseWheelListener string = l[i];
135
            contentPane.removeMouseWheelListener(string);
136
        }
137
        contentPane.addMouseWheelListener(new MouseWheelListener() {
138
 
139
            @Override
140
            public void mouseWheelMoved(MouseWheelEvent e) {
141
                weekView.mouseWheelMoved(e, l);
142
            }
143
        });
144
 
145
        this.add(contentPane, c);
146
        final int value = 300;
147
        contentPane.getVerticalScrollBar().setValue(value);
148
        weekView.loadWeek(week, year, false);
149
        final ChangeListener listener = new ChangeListener() {
150
 
151
            @Override
152
            public void stateChanged(ChangeEvent e) {
153
                Calendar c = getCurrentDate();
154
                picker.setDate(c.getTime());
155
                weekView.loadWeek(getWeek(), getYear(), false);
156
            }
157
        };
158
        spinWeek.addChangeListener(listener);
159
        spinYear.addChangeListener(listener);
160
        picker.addPropertyChangeListener("value", new PropertyChangeListener() {
161
 
162
            @Override
163
            public void propertyChange(PropertyChangeEvent evt) {
164
                if (evt.getNewValue() != null) {
165
                    final Date d = (Date) evt.getNewValue();
166
                    final Calendar c = Calendar.getInstance();
167
                    c.setTime(d);
168
                    final int y = c.get(Calendar.YEAR);
169
                    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);
174
                    spinWeek.addChangeListener(listener);
175
                    spinYear.addChangeListener(listener);
176
                    weekView.loadWeek(getWeek(), getYear(), false);
177
                }
178
 
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
 
137 ilm 204
    protected void addWeek(int i) {
205
        Calendar c = getCurrentDate();
206
        c.add(Calendar.DAY_OF_YEAR, i * 7);
207
        final int year = c.get(Calendar.YEAR);
208
        final int week = c.get(Calendar.WEEK_OF_YEAR);
209
        spinYear.setValue(year);
210
        spinWeek.setValue(week);
211
 
212
    }
213
 
214
    public Calendar getCurrentDate() {
215
        Calendar c = Calendar.getInstance();
216
        c.clear();
217
        c.set(Calendar.YEAR, getYear());
218
        c.set(Calendar.WEEK_OF_YEAR, getWeek());
219
        return c;
220
    }
221
 
150 ilm 222
    public final void setCurrentDate(final Calendar c) {
223
        this.setCurrentDate(c.getTime());
224
    }
225
 
226
    public final void setCurrentDate(final Date d) {
227
        this.picker.setDate(d);
228
    }
229
 
137 ilm 230
    private void configureButton(JButton b) {
231
        b.setOpaque(false);
232
        b.setBorderPainted(false);
233
        b.setFocusPainted(false);
234
        b.setContentAreaFilled(false);
235
        b.setMargin(new Insets(1, 5, 1, 5));
236
    }
237
 
238
    public void reload() {
239
        weekView.reload();
240
    }
241
 
242
    public int getWeek() {
243
        return ((Number) spinWeek.getValue()).intValue();
244
    }
245
 
246
    public int getYear() {
247
        return ((Number) spinYear.getValue()).intValue();
248
    }
249
 
250
    public WeekView getWeekView() {
251
        return weekView;
252
    }
253
 
254
    public void setTitle(String title) {
255
        this.title = title;
256
    }
257
 
258
    public void scrollTo(int hour) {
259
        contentPane.getVerticalScrollBar().setValue(hour * weekView.getRowHeight());
260
    }
261
 
262
    public JScrollPane getContentPane() {
263
        return contentPane;
264
    }
265
}