OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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.Font;
5
import java.awt.Graphics;
6
import java.awt.Graphics2D;
7
import java.awt.Rectangle;
8
import java.awt.RenderingHints;
9
import java.text.DateFormat;
10
import java.text.SimpleDateFormat;
11
import java.util.ArrayList;
12
import java.util.Calendar;
13
import java.util.List;
14
 
15
import javax.swing.Scrollable;
16
import javax.swing.SwingWorker;
17
 
18
import org.jopencalendar.model.JCalendarItem;
19
 
20
public class WeekView extends MultipleDayView implements Scrollable {
21
 
22
    private final JCalendarItemProvider manager;
23
 
24
    // Current week
25
    private int week;
26
    private int year;
27
    // Date of columns
28
    private int[] daysOfYear = new int[7];
29
    private int[] years = new int[7];
30
    private final SimpleDateFormat monthAndYearFormat = new SimpleDateFormat("MMMM yyyy");
31
 
32
    private boolean weekEndMinimised = true;
33
 
34
    public WeekView(JCalendarItemProvider manager) {
35
        if (manager == null) {
36
            throw new IllegalArgumentException("null manager");
37
        }
38
        this.manager = manager;
39
 
40
    }
41
 
42
    /**
43
     * Load week
44
     *
45
     * @param week the week number (1 - 52)
46
     * */
47
    public void loadWeek(final int week, final int year, boolean forceReload) {
48
        if (!forceReload) {
49
            if (week == this.week && year == this.year) {
50
                return;
51
            }
52
        }
53
        this.week = week;
54
        this.year = year;
55
        Calendar c = getFirstDayCalendar();
56
        final int columnCount = this.getColumnCount();
57
        for (int i = 0; i < columnCount; i++) {
58
            this.daysOfYear[i] = c.get(Calendar.DAY_OF_YEAR);
59
            this.years[i] = c.get(Calendar.YEAR);
60
            c.add(Calendar.HOUR_OF_DAY, 24);
61
        }
62
 
63
        SwingWorker<List<JCalendarItem>, Object> w = new SwingWorker<List<JCalendarItem>, Object>() {
64
 
65
            @Override
66
            protected List<JCalendarItem> doInBackground() throws Exception {
67
                return manager.getItemInWeek(week, year);
68
            }
69
 
70
            protected void done() {
71
                try {
72
                    List<JCalendarItem> l = get();
73
                    List<List<JCalendarItem>> items = new ArrayList<List<JCalendarItem>>();
74
                    for (int i = 0; i < columnCount; i++) {
75
                        List<JCalendarItem> list = new ArrayList<JCalendarItem>();
76
                        int d = daysOfYear[i];
77
                        int y = years[i];
78
                        for (JCalendarItem jCalendarItem : l) {
79
                            if (jCalendarItem.getDtStart().get(Calendar.DAY_OF_YEAR) == d && jCalendarItem.getDtStart().get(Calendar.YEAR) == y) {
80
                                list.add(jCalendarItem);
81
                            }
82
                        }
83
                        items.add(list);
84
                    }
85
                    setItems(items);
86
                } catch (Exception e) {
87
                    throw new IllegalStateException("error while getting items", e);
88
                }
89
 
90
            };
91
        };
92
        w.execute();
93
    }
94
 
95
    public void setWeekEndMinimised(boolean b) {
96
        weekEndMinimised = b;
97
        repaint();
98
    }
99
 
100
    public void paintHeader(Graphics g) {
101
 
102
        g.setColor(Color.WHITE);
103
        g.fillRect(0, 0, this.getWidth() + 100, getHeaderHeight());
104
        g.setColor(Color.GRAY);
105
        // Vertical lines
106
        int columnCount = 7;
107
        int x = HOURS_LABEL_WIDTH;
108
 
109
        for (int i = 0; i < columnCount - 1; i++) {
110
            x += getColumnWidth(i);
111
            g.drawLine(x, YEAR_HEIGHT, x, this.getHeaderHeight());
112
        }
113
        g.setColor(Color.DARK_GRAY);
114
        // Text : month & year
115
        Graphics2D g2 = (Graphics2D) g;
116
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
117
        g.setFont(getFont().deriveFont(13f));
118
        int month = getMonth(this.daysOfYear[0], this.years[0]);
119
        int count = 1;
120
        for (int i = 1; i < columnCount; i++) {
121
            int m = getMonth(this.daysOfYear[i], this.years[i]);
122
            if (m == month) {
123
                count++;
124
            }
125
        }
126
        if (count == 7) {
127
            String str = getMonthName(this.daysOfYear[0], this.years[0]);
128
            Rectangle r = g.getFontMetrics().getStringBounds(str, g).getBounds();
129
            int w = 0;
130
            for (int i = 0; i < count; i++) {
131
                w += getColumnWidth(i);
132
            }
133
            g.drawString(str, (int) (HOURS_LABEL_WIDTH + (w - r.getWidth()) / 2), 15);
134
        } else {
135
            // draw left month
136
            String strLeft = getMonthName(this.daysOfYear[0], this.years[0]);
137
            Rectangle r1 = g.getFontMetrics().getStringBounds(strLeft, g).getBounds();
138
 
139
            int wLeft = 0;
140
            for (int i = 0; i < count; i++) {
141
                wLeft += getColumnWidth(i);
142
            }
143
            int wRight = 0;
144
            for (int i = count; i < columnCount; i++) {
145
                wRight += getColumnWidth(i);
146
            }
147
 
148
            final int xLeft = (int) (HOURS_LABEL_WIDTH + (wLeft - r1.getWidth()) / 2);
149
            g.drawString(strLeft, xLeft, 15);
150
 
151
            // draw right month
152
            String strRight = getMonthName(this.daysOfYear[columnCount - 1], this.years[columnCount - 1]);
153
            Rectangle r2 = g.getFontMetrics().getStringBounds(strRight, g).getBounds();
154
            final int xRight = (int) (HOURS_LABEL_WIDTH + wLeft + (wRight - r2.getWidth()) / 2);
155
            g.drawString(strRight, xRight, 15);
156
 
157
            // draw separator
158
            g.setColor(Color.GRAY);
159
            int xSep = HOURS_LABEL_WIDTH + wLeft;
160
            g.drawLine(xSep, 0, xSep, YEAR_HEIGHT);
161
        }
162
 
163
        // Text : day
164
        final Font font1 = getFont().deriveFont(12f);
165
        g.setFont(font1);
166
        x = HOURS_LABEL_WIDTH;
167
        Calendar today = Calendar.getInstance();
168
        int todayYear = today.get(Calendar.YEAR);
169
        int todayDay = today.get(Calendar.DAY_OF_YEAR);
170
 
171
        Calendar c = getFirstDayCalendar();
172
        final DateFormat df;
173
        if (!weekEndMinimised) {
174
            df = new SimpleDateFormat("EEEE d");
175
        } else {
176
            df = new SimpleDateFormat("E d");
177
        }
178
        for (int i = 0; i < columnCount; i++) {
179
            String str = df.format(c.getTime());
180
            str = str.substring(0, 1).toUpperCase() + str.substring(1);
181
            int columnWidth = getColumnWidth(i);
182
            Rectangle r = g.getFontMetrics().getStringBounds(str, g).getBounds();
183
            if (c.get(Calendar.YEAR) == todayYear && c.get(Calendar.DAY_OF_YEAR) == todayDay) {
184
                g.setColor(new Color(232, 242, 254));
185
                g.fillRect(x + 1, YEAR_HEIGHT, columnWidth - 1, (int) r.getHeight() + 4);
186
                g.setColor(Color.BLACK);
187
            } else {
188
                g.setColor(Color.DARK_GRAY);
189
            }
190
            g.drawString(str, (int) (x + (columnWidth - r.getWidth()) / 2), YEAR_HEIGHT + 15);
191
            x += columnWidth;
192
            c.add(Calendar.HOUR_OF_DAY, 24);
193
        }
194
        paintHeaderAlldays(g);
195
    }
196
 
197
    private int getMonth(int day, int year) {
198
        Calendar c = Calendar.getInstance();
199
        c.set(year, 1, 0, 0, 0, 0);
200
        c.set(Calendar.MILLISECOND, 0);
201
        c.set(Calendar.DAY_OF_YEAR, day);
202
        return c.get(Calendar.MONTH);
203
    }
204
 
205
    private String getMonthName(int day, int year) {
206
        Calendar c = Calendar.getInstance();
207
        c.set(year, 1, 0, 0, 0, 0);
208
        c.set(Calendar.MILLISECOND, 0);
209
        c.set(Calendar.DAY_OF_YEAR, day);
210
        return monthAndYearFormat.format(c.getTime()).toUpperCase();
211
    }
212
 
213
    private Calendar getFirstDayCalendar() {
214
        Calendar c = Calendar.getInstance();
215
        c.set(this.year, 1, 0, 0, 0, 0);
216
        c.set(Calendar.MILLISECOND, 0);
217
        c.set(Calendar.WEEK_OF_YEAR, this.week);
218
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
219
        return c;
220
    }
221
 
222
    @Override
223
    public String getColumnTitle(int index) {
224
        final DateFormat df;
225
        if (!weekEndMinimised) {
226
            df = new SimpleDateFormat("EEEE d");
227
        } else {
228
            df = new SimpleDateFormat("E d");
229
        }
230
        Calendar c = getFirstDayCalendar();
231
        c.add(Calendar.DAY_OF_YEAR, index);
232
 
233
        String str = df.format(c.getTime());
234
        str = str.substring(0, 1).toUpperCase() + str.substring(1);
235
        return str;
236
    }
237
 
238
    @Override
239
    public void reload() {
240
        loadWeek(this.week, this.year, true);
241
    }
242
 
243
    @Override
244
    public int getColumnCount() {
245
        return 7;
246
    }
247
 
248
    @Override
249
    public int getColumnWidth(int column) {
250
        final int c;
251
        if (weekEndMinimised) {
252
            int minSize = 50;
253
            if (column == 5 || column == 6) {
254
                return minSize;
255
            } else {
256
                c = (this.getWidth() - HOURS_LABEL_WIDTH - 2 * minSize) / (getColumnCount() - 2);
257
            }
258
 
259
        } else {
260
            c = (this.getWidth() - HOURS_LABEL_WIDTH) / getColumnCount();
261
        }
262
        return c;
263
    }
264
}