1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package de.jaret.util.ui.timebars.swt.renderer;
21
22 import java.util.Calendar;
23
24 import org.eclipse.swt.graphics.Color;
25 import org.eclipse.swt.graphics.GC;
26 import org.eclipse.swt.graphics.RGB;
27 import org.eclipse.swt.graphics.Rectangle;
28 import org.eclipse.swt.printing.Printer;
29
30 import de.jaret.util.date.DateUtils;
31 import de.jaret.util.date.JaretDate;
32 import de.jaret.util.date.holidayenumerator.HolidayEnumerator;
33 import de.jaret.util.ui.timebars.TickScaler;
34 import de.jaret.util.ui.timebars.TickScaler.Range;
35 import de.jaret.util.ui.timebars.TimeBarViewerDelegate;
36 import de.jaret.util.ui.timebars.TimeBarViewerInterface;
37 import de.jaret.util.ui.timebars.strategy.ITickProvider;
38
39
40
41
42
43
44
45 public class DefaultGridRenderer extends AbstractGridRenderer {
46
47 private static final double DEFAULT_UPPERPPSMARKLIMIT = 0.01;
48
49
50 private static final RGB MAJORGRID_COLOR = new RGB(200, 200, 200);
51
52 private static final RGB MINORGRID_COLOR = new RGB(230, 230, 230);
53
54 private static final RGB SATURDAY_COLOR = new RGB(255, 230, 230);
55
56 private static final RGB SUNDAY_COLOR = new RGB(255, 200, 200);
57
58 private static final RGB SPECIALDAY_COLOR = new RGB(255, 255, 207);
59
60
61 private Color _colorMajorGrid;
62
63
64 private Color _colorMinorGrid;
65
66
67 private Color _colorSaturday;
68
69
70 private Color _colorSunday;
71
72
73 private Color _colorSpecialDay;
74
75
76 private HolidayEnumerator _holidayEnumerator;
77
78
79 private boolean _markWeekends = true;
80
81
82 private boolean _markSpecialdays = true;
83
84
85 private boolean _markHolidays = true;
86
87
88 private double _upperPPSMarkLimit = DEFAULT_UPPERPPSMARKLIMIT;
89
90
91 protected ITickProvider _tickProvider = null;
92
93
94
95
96
97
98 public DefaultGridRenderer(Printer printer) {
99 super(printer);
100 }
101
102
103
104
105 public DefaultGridRenderer() {
106 super(null);
107 }
108
109
110
111
112
113
114
115 public void setTickProvider(ITickProvider tickProvider) {
116 _tickProvider = tickProvider;
117 }
118
119
120
121
122
123
124 private void initializeColors(GC gc) {
125 _colorMajorGrid = new Color(gc.getDevice(), MAJORGRID_COLOR);
126 _colorMinorGrid = new Color(gc.getDevice(), MINORGRID_COLOR);
127 _colorSaturday = new Color(gc.getDevice(), SATURDAY_COLOR);
128 _colorSunday = new Color(gc.getDevice(), SUNDAY_COLOR);
129 _colorSpecialDay = new Color(gc.getDevice(), SPECIALDAY_COLOR);
130 }
131
132
133
134
135
136
137 public void setHolidayEnumerator(HolidayEnumerator he) {
138 _holidayEnumerator = he;
139 }
140
141
142
143
144
145
146 public void setMarkWeekends(boolean mark) {
147 _markWeekends = mark;
148 }
149
150
151
152
153
154
155 public void setMarkSpecialDays(boolean mark) {
156 _markSpecialdays = mark;
157 }
158
159
160
161
162
163
164 public void setMarkHolidays(boolean mark) {
165 _markHolidays = mark;
166 }
167
168
169
170
171
172
173 public void setUpperPPSMarkLimit(double limit) {
174 _upperPPSMarkLimit = limit;
175 }
176
177
178
179
180 public void draw(GC gc, TimeBarViewerDelegate delegate, Rectangle drawingArea, boolean printing) {
181
182 if (_colorMajorGrid == null) {
183 initializeColors(gc);
184 }
185 boolean horizontal = delegate.getOrientation() == TimeBarViewerInterface.Orientation.HORIZONTAL;
186
187 if (_tickProvider == null) {
188 drawIntern(gc, delegate, drawingArea, printing, horizontal);
189 } else {
190 drawIntern(gc, delegate, drawingArea, printing, horizontal, _tickProvider);
191 }
192
193 }
194
195 public void drawIntern(GC gc, TimeBarViewerDelegate delegate, Rectangle drawingArea, boolean printing,
196 boolean horizontal, ITickProvider tickProvider) {
197 int ox = drawingArea.x;
198 int oy = drawingArea.y;
199 int width = drawingArea.width;
200 int height = drawingArea.height;
201
202 Color fg = gc.getForeground();
203
204 if (printing) {
205 _upperPPSMarkLimit = _upperPPSMarkLimit * getScaleX();
206 }
207
208
209 JaretDate date = delegate.getStartDate().copy();
210 date.setTime(0, 0, 0);
211
212 int max = horizontal ? (ox + width) : (oy + height);
213
214
215 if (_markHolidays || _markSpecialdays || _markWeekends) {
216 Color bg = gc.getBackground();
217 if (delegate.getPixelPerSecond() < _upperPPSMarkLimit) {
218
219 int daywidth = (int) (delegate.getPixelPerSecond() * 24.0 * 60.0 * 60.0);
220 while (delegate.xForDate(date) < max) {
221 int x = delegate.xForDate(date);
222 Color mark = null;
223 if (_holidayEnumerator != null) {
224 if (_markHolidays && _holidayEnumerator.isHoliday(date.getDate())) {
225 mark = _colorSunday;
226 } else if (_markSpecialdays && _holidayEnumerator.isSpecialDay(date.getDate())) {
227 mark = _colorSpecialDay;
228 }
229 }
230 if (mark == null && _markWeekends) {
231 if (date.getDayOfWeek() == Calendar.SATURDAY) {
232 mark = _colorSaturday;
233 } else if (date.getDayOfWeek() == Calendar.SUNDAY) {
234 mark = _colorSunday;
235 }
236 }
237 if (mark != null) {
238 gc.setBackground(mark);
239
240 if (delegate.hasVariableXScale()) {
241 int x2 = delegate.xForDate(date.copy().advanceDays(1));
242 daywidth = x2 - x;
243 }
244 if (horizontal) {
245 gc.fillRectangle(x, oy, daywidth, height);
246 } else {
247
248 gc.fillRectangle(ox, x, width, daywidth);
249 }
250 }
251 date.advanceDays(1);
252 }
253
254 }
255
256 gc.setBackground(bg);
257 }
258
259
260 if (printing) {
261 gc.setLineWidth(getDefaultLineWidth());
262 }
263
264 gc.setForeground(_colorMinorGrid);
265 for (JaretDate d : tickProvider.getMinorTicks(delegate)) {
266 int x = delegate.xForDate(d);
267 if (horizontal) {
268 gc.drawLine(x, oy, x, oy + height);
269 } else {
270 gc.drawLine(ox, x, ox + width, x);
271 }
272 }
273
274
275 gc.setForeground(_colorMajorGrid);
276 for (JaretDate d : tickProvider.getMajorTicks(delegate)) {
277 int x = delegate.xForDate(d);
278 if (horizontal) {
279 gc.drawLine(x, oy, x, oy + height);
280 } else {
281 gc.drawLine(ox, x, ox + width, x);
282 }
283 }
284 gc.setLineWidth(1);
285 gc.setForeground(fg);
286 }
287
288 public void drawIntern(GC gc, TimeBarViewerDelegate delegate, Rectangle drawingArea, boolean printing,
289 boolean horizontal) {
290
291 int ox = drawingArea.x;
292 int oy = drawingArea.y;
293 int width = drawingArea.width;
294 int height = drawingArea.height;
295
296 Color fg = gc.getForeground();
297
298 int idx;
299 if (!printing) {
300 idx = TickScaler.getTickIdx(delegate.getPixelPerSecond() / getScaleX());
301 } else {
302 idx = TickScaler.getTickIdx(delegate.getPixelPerSecond() / getScaleX());
303 _upperPPSMarkLimit = _upperPPSMarkLimit * getScaleX();
304 }
305 int majTick = TickScaler.getMajorTickMinutes(idx);
306 int minTick = TickScaler.getMinorTickMinutes(idx);
307 Range range = TickScaler.getRange(idx);
308
309
310 JaretDate date = delegate.getStartDate().copy();
311
312
313
314 date.setMinutes(0);
315 date.setHours(0);
316 date.setSeconds(0);
317
318 if (range == Range.WEEK) {
319 while (date.getDayOfWeek() != DateUtils.getFirstDayOfWeek()) {
320 date.backDays(1);
321 }
322 } else if (range == Range.MONTH) {
323
324 date.setDay(1);
325 }
326 JaretDate save = date.copy();
327
328 int max = horizontal ? (ox + width) : (oy + height);
329
330
331 if (_markHolidays || _markSpecialdays || _markWeekends) {
332 Color bg = gc.getBackground();
333 if (range == Range.DAY || range == Range.WEEK
334 || (range == Range.HOUR && delegate.getPixelPerSecond() < _upperPPSMarkLimit)) {
335
336 int daywidth = (int) (delegate.getPixelPerSecond() * 24.0 * 60.0 * 60.0);
337 while (delegate.xForDate(date) < max) {
338 int x = delegate.xForDate(date);
339 Color mark = null;
340 if (_holidayEnumerator != null) {
341 if (_markHolidays && _holidayEnumerator.isHoliday(date.getDate())) {
342 mark = _colorSunday;
343 } else if (_markSpecialdays && _holidayEnumerator.isSpecialDay(date.getDate())) {
344 mark = _colorSpecialDay;
345 }
346 }
347 if (mark == null && _markWeekends) {
348 if (date.getDayOfWeek() == Calendar.SATURDAY) {
349 mark = _colorSaturday;
350 } else if (date.getDayOfWeek() == Calendar.SUNDAY) {
351 mark = _colorSunday;
352 }
353 }
354 if (mark != null) {
355 gc.setBackground(mark);
356
357 if (delegate.hasVariableXScale()) {
358 int x2 = delegate.xForDate(date.copy().advanceDays(1));
359 daywidth = x2 - x;
360 }
361 if (horizontal) {
362 gc.fillRectangle(x, oy, daywidth, height);
363 } else {
364
365 gc.fillRectangle(ox, x, width, daywidth);
366 }
367 }
368 date.advanceDays(1);
369 }
370
371 }
372
373 gc.setBackground(bg);
374 }
375
376 date = save.copy();
377
378 if (printing) {
379 gc.setLineWidth(getDefaultLineWidth());
380 }
381 gc.setForeground(_colorMinorGrid);
382 while (delegate.xForDate(date) < max) {
383 int x = delegate.xForDate(date);
384 if (horizontal) {
385 gc.drawLine(x, oy, x, oy + height);
386 } else {
387 gc.drawLine(ox, x, ox + width, x);
388 }
389
390 if (range == Range.MONTH) {
391 int adv = Math.round((float) minTick / (float) (24 * 60 * 7 * 4));
392 if (adv == 0) {
393 adv = 1;
394 }
395 date.advanceMonths(adv);
396 } else {
397 date.advanceMinutes(minTick);
398 }
399 }
400
401 date = save.copy();
402
403 gc.setForeground(_colorMajorGrid);
404 while (delegate.xForDate(date) < ox + width) {
405 int x = delegate.xForDate(date);
406 if (horizontal) {
407 gc.drawLine(x, oy, x, oy + height);
408 } else {
409 gc.drawLine(ox, x, ox + width, x);
410 }
411 if (range == Range.MONTH) {
412 int adv = Math.round((float) majTick / (float) (24 * 60 * 7 * 4));
413 if (adv == 0) {
414 adv = 1;
415 }
416 date.advanceMonths(adv);
417 } else {
418 date.advanceMinutes(majTick);
419 }
420 }
421 gc.setLineWidth(1);
422 gc.setForeground(fg);
423 }
424
425
426
427
428 public void dispose() {
429 super.dispose();
430 if (_colorMajorGrid != null) {
431 _colorMajorGrid.dispose();
432 _colorMinorGrid.dispose();
433 _colorSaturday.dispose();
434 _colorSpecialDay.dispose();
435 _colorSunday.dispose();
436 }
437 }
438
439
440
441
442 public GridRenderer createPrintRenderer(Printer printer) {
443 DefaultGridRenderer renderer = new DefaultGridRenderer(printer);
444 renderer.setHolidayEnumerator(_holidayEnumerator);
445 renderer.setMarkHolidays(_markHolidays);
446 renderer.setMarkSpecialDays(_markSpecialdays);
447 renderer.setMarkWeekends(_markWeekends);
448 renderer.setTickProvider(_tickProvider);
449 return renderer;
450 }
451
452 }