OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 150 | Go to most recent revision | Details | 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 {
32
    private WeekView weekView;
33
    private JSpinner spinWeek;
34
    private JSpinner spinYear;
35
    private String title;
36
    final JScrollPane contentPane = new JScrollPane();
37
 
38
    public CalendarWithToolBar(JCalendarItemProvider manager) {
39
        this(manager, false);
40
    }
41
 
42
    public CalendarWithToolBar(JCalendarItemProvider manager, boolean showPrintButton) {
43
        JPanel toolbar = new JPanel();
44
        toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
45
 
46
        Calendar cal = Calendar.getInstance();
47
        int week = cal.get(Calendar.WEEK_OF_YEAR);
48
        int year = cal.get(Calendar.YEAR);
49
        JButton bPrevious = new JButton(new ImageIcon(this.getClass().getResource("left.png")));
50
        JButton bNext = new JButton(new ImageIcon(this.getClass().getResource("right.png")));
51
        configureButton(bPrevious);
52
        configureButton(bNext);
53
        toolbar.add(bPrevious);
54
        toolbar.add(bNext);
55
        bNext.addActionListener(new ActionListener() {
56
 
57
            @Override
58
            public void actionPerformed(ActionEvent e) {
59
                addWeek(1);
60
 
61
            }
62
        });
63
        bPrevious.addActionListener(new ActionListener() {
64
 
65
            @Override
66
            public void actionPerformed(ActionEvent e) {
67
                addWeek(-1);
68
 
69
            }
70
        });
71
 
72
        toolbar.add(new JLabel("Semaine"));
73
        spinWeek = new JSpinner(new SpinnerNumberModel(week, 1, 53, 1));
74
        toolbar.add(spinWeek);
75
        toolbar.add(new JLabel(" de "));
76
        spinYear = new JSpinner(new SpinnerNumberModel(year, 1000, year + 20, 1));
77
        toolbar.add(spinYear);
78
        //
79
        final DatePicker picker = new DatePicker(false);
80
        toolbar.add(picker);
81
        //
82
 
83
        final JSlider zoomSlider = new JSlider(1, 9, 1);
84
        zoomSlider.setSnapToTicks(true);
85
        zoomSlider.setMajorTickSpacing(1);
86
        zoomSlider.setPaintTicks(true);
87
        zoomSlider.addChangeListener(new ChangeListener() {
88
 
89
            @Override
90
            public void stateChanged(ChangeEvent e) {
91
                weekView.setZoom(zoomSlider.getValue());
92
            }
93
        });
94
        toolbar.add(new JLabel(" Zoom"));
95
        toolbar.add(zoomSlider);
96
        if (showPrintButton) {
97
            final JButton jButton = new JButton("Imprimer");
98
            jButton.addActionListener(new ActionListener() {
99
 
100
                @Override
101
                public void actionPerformed(ActionEvent arg0) {
102
                    final PrintJComponentAction a = new PrintJComponentAction(CalendarWithToolBar.this.getContentPane(), PageFormat.LANDSCAPE, CalendarWithToolBar.this.title);
103
                    a.actionPerformed(arg0);
104
                }
105
            });
106
            toolbar.add(jButton);
107
        }
108
        JButton reloadButton = new JButton(new ImageIcon(this.getClass().getResource("auto.png")));
109
        configureButton(reloadButton);
110
        toolbar.add(reloadButton);
111
        reloadButton.addActionListener(new ActionListener() {
112
 
113
            @Override
114
            public void actionPerformed(ActionEvent e) {
115
                reload();
116
            }
117
        });
118
 
119
        this.setLayout(new GridBagLayout());
120
 
121
        //
122
        GridBagConstraints c = new GridBagConstraints();
123
        c.gridx = 0;
124
        c.gridy = 0;
125
        c.weightx = 1;
126
        c.fill = GridBagConstraints.HORIZONTAL;
127
        this.add(toolbar, c);
128
        c.gridy++;
129
        this.add(new JSeparator(JSeparator.HORIZONTAL), c);
130
        c.gridy++;
131
        c.fill = GridBagConstraints.BOTH;
132
        c.weighty = 1;
133
 
134
        contentPane.setBorder(BorderFactory.createEmptyBorder());
135
        contentPane.setOpaque(false);
136
        weekView = new WeekView(manager);
137
        contentPane.setColumnHeaderView(new WeekViewHeader(weekView));
138
        contentPane.setViewportView(weekView);
139
        contentPane.getViewport().setBackground(Color.WHITE);
140
        final MouseWheelListener[] l = contentPane.getMouseWheelListeners();
141
        for (int i = 0; i < l.length; i++) {
142
            MouseWheelListener string = l[i];
143
            contentPane.removeMouseWheelListener(string);
144
        }
145
        contentPane.addMouseWheelListener(new MouseWheelListener() {
146
 
147
            @Override
148
            public void mouseWheelMoved(MouseWheelEvent e) {
149
                weekView.mouseWheelMoved(e, l);
150
            }
151
        });
152
 
153
        this.add(contentPane, c);
154
        final int value = 300;
155
        contentPane.getVerticalScrollBar().setValue(value);
156
        weekView.loadWeek(week, year, false);
157
        final ChangeListener listener = new ChangeListener() {
158
 
159
            @Override
160
            public void stateChanged(ChangeEvent e) {
161
                Calendar c = getCurrentDate();
162
                picker.setDate(c.getTime());
163
                weekView.loadWeek(getWeek(), getYear(), false);
164
            }
165
        };
166
        spinWeek.addChangeListener(listener);
167
        spinYear.addChangeListener(listener);
168
        picker.addPropertyChangeListener("value", new PropertyChangeListener() {
169
 
170
            @Override
171
            public void propertyChange(PropertyChangeEvent evt) {
172
                if (evt.getNewValue() != null) {
173
                    final Date d = (Date) evt.getNewValue();
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
                    spinWeek.removeChangeListener(listener);
179
                    spinYear.removeChangeListener(listener);
180
                    spinYear.setValue(y);
181
                    spinWeek.setValue(w);
182
                    spinWeek.addChangeListener(listener);
183
                    spinYear.addChangeListener(listener);
184
                    weekView.loadWeek(getWeek(), getYear(), false);
185
                }
186
 
187
            }
188
        });
189
 
190
    }
191
 
192
    protected void addWeek(int i) {
193
        Calendar c = getCurrentDate();
194
        c.add(Calendar.DAY_OF_YEAR, i * 7);
195
        final int year = c.get(Calendar.YEAR);
196
        final int week = c.get(Calendar.WEEK_OF_YEAR);
197
        spinYear.setValue(year);
198
        spinWeek.setValue(week);
199
 
200
    }
201
 
202
    public Calendar getCurrentDate() {
203
        Calendar c = Calendar.getInstance();
204
        c.clear();
205
        c.set(Calendar.YEAR, getYear());
206
        c.set(Calendar.WEEK_OF_YEAR, getWeek());
207
        return c;
208
    }
209
 
210
    private void configureButton(JButton b) {
211
        b.setOpaque(false);
212
        b.setBorderPainted(false);
213
        b.setFocusPainted(false);
214
        b.setContentAreaFilled(false);
215
        b.setMargin(new Insets(1, 5, 1, 5));
216
    }
217
 
218
    public void reload() {
219
        weekView.reload();
220
    }
221
 
222
    public int getWeek() {
223
        return ((Number) spinWeek.getValue()).intValue();
224
    }
225
 
226
    public int getYear() {
227
        return ((Number) spinYear.getValue()).intValue();
228
    }
229
 
230
    public WeekView getWeekView() {
231
        return weekView;
232
    }
233
 
234
    public void setTitle(String title) {
235
        this.title = title;
236
    }
237
 
238
    public void scrollTo(int hour) {
239
        contentPane.getVerticalScrollBar().setValue(hour * weekView.getRowHeight());
240
    }
241
 
242
    public JScrollPane getContentPane() {
243
        return contentPane;
244
    }
245
}