1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package de.jaret.examples.timebars.calendar.swing.renderer;
21
22 import java.awt.Color;
23 import java.awt.Graphics;
24
25 import javax.swing.JComponent;
26
27 import de.jaret.util.date.DateUtils;
28 import de.jaret.util.date.JaretDate;
29 import de.jaret.util.ui.timebars.TickScaler;
30 import de.jaret.util.ui.timebars.TickScaler.Range;
31 import de.jaret.util.ui.timebars.TimeBarViewerInterface;
32 import de.jaret.util.ui.timebars.strategy.ITickProvider;
33 import de.jaret.util.ui.timebars.swing.TimeBarViewer;
34 import de.jaret.util.ui.timebars.swing.renderer.GridRenderer;
35
36 /***
37 * Grid renderer for the calendar example.
38 *
39 * @author Peter Kliem
40 * @version $Id: CalendarGridRenderer.java 1083 2011-07-01 20:29:16Z kliem $
41 */
42 public class CalendarGridRenderer implements GridRenderer {
43 /*** color for major grid lines. */
44 private static final Color MAJORGRIDCOLOR = new Color(200, 200, 200);
45 /*** color for minor grid lines. */
46 private static final Color MINORGRIDCOLOR = new Color(230, 230, 230);
47
48 private static final Color WORK_COLOR = new Color(246, 249, 169);
49 private static final Color NONWORK_COLOR = new Color(214, 218, 104);
50
51 private JaretDate _beginWork = new JaretDate(1, 5, 2007, 8, 0, 0);
52 private JaretDate _endWork = new JaretDate(1, 5, 2007, 18, 0, 0);
53
54
55 /*** component used for painting. */
56 protected MyGridRenderer _component = new MyGridRenderer();
57
58 /***
59 * {@inheritDoc}
60 */
61 public JComponent getRendererComponent(TimeBarViewer tbv) {
62 _component.setTimeBarViewer(tbv);
63 return _component;
64 }
65
66 /***
67 * JComponent for drawin gthe grid.
68 *
69 * @author kliem
70 * @version $Id: CalendarGridRenderer.java 1083 2011-07-01 20:29:16Z kliem $
71 */
72 @SuppressWarnings("serial")
73 class MyGridRenderer extends JComponent {
74 /*** the viewer. */
75 private TimeBarViewer _tbv;
76
77 /***
78 * Set the viewer.
79 *
80 * @param tbv the viewer
81 */
82 public void setTimeBarViewer(TimeBarViewer tbv) {
83 _tbv = tbv;
84 }
85
86 private int xForDate(JaretDate date) {
87 return _tbv.xForDate(date)-_tbv.getHierarchyWidth()-_tbv.getYAxisWidth();
88 }
89
90 /***
91 * {@inheritDoc}
92 */
93 public void paintComponent(Graphics g) {
94
95 boolean horizontal = _tbv.getTBOrientation() == TimeBarViewerInterface.Orientation.HORIZONTAL;
96
97
98 JaretDate date = _tbv.getStartDate().copy();
99
100 if (date.getHours() == 23 && date.getMinutes() == 59) {
101 date.advanceSeconds(1);
102 }
103
104 int idx = TickScaler.getTickIdx(_tbv.getPixelPerSecond());
105 int majTick = TickScaler.getMajorTickMinutes(idx);
106 int minTick = TickScaler.getMinorTickMinutes(idx);
107 Range range = TickScaler.getRange(idx);
108
109
110
111 date.setMinutes(0);
112 date.setHours(0);
113 date.setSeconds(0);
114
115 if (range == Range.WEEK) {
116 while (date.getDayOfWeek() != DateUtils.getFirstDayOfWeek()) {
117 date.backDays(1.0);
118 }
119 } else if (range == Range.MONTH) {
120
121 date.setDay(1);
122 }
123 JaretDate save = date.copy();
124
125
126
127 Color fg = g.getColor();
128 int end = xForDate(date.setTime(_beginWork.getHours(), _beginWork.getMinutes(), 0));
129 int end2 = xForDate(date.setTime(_endWork.getHours(), _endWork.getMinutes(), 0));
130 int end3 = xForDate(date.setTime(23,59,59));
131 if (horizontal) {
132 g.setColor(NONWORK_COLOR);
133 g.fillRect(0, 0, end, getHeight());
134 g.setColor(WORK_COLOR);
135 g.fillRect(end,0, end2 - end, getHeight());
136 g.setColor(NONWORK_COLOR);
137 g.fillRect(end2, 0, end3, getHeight());
138 } else {
139 g.setColor(NONWORK_COLOR);
140 g.fillRect(0, 0, getWidth(), end);
141 g.setColor(WORK_COLOR);
142 g.fillRect(0, end, getWidth(), end2 - end);
143 g.setColor(NONWORK_COLOR);
144 g.fillRect(0, end2, getWidth(), end3);
145 }
146 g.setColor(fg);
147
148
149 date.backMinutes(majTick);
150
151
152 int max = horizontal ? getWidth() : getHeight();
153
154 date = save.copy();
155
156 g.setColor(MINORGRIDCOLOR);
157 while (xForDate(date) < max) {
158 int coord = xForDate(date);
159 if (horizontal) {
160 g.drawLine(coord, 0, coord, getHeight());
161 } else {
162 g.drawLine(0, coord, getWidth(), coord);
163 }
164 if (range == Range.MONTH) {
165 int adv = Math.round(minTick / (24 * 60 * 7 * 4));
166 if (adv == 0) {
167 adv = 1;
168 }
169 date.advanceMonths(adv);
170 } else {
171 date.advanceMinutes(minTick);
172 }
173 }
174
175 date = save.copy();
176
177 g.setColor(MAJORGRIDCOLOR);
178 while (xForDate(date) < max) {
179 int coord = xForDate(date);
180 if (horizontal) {
181 g.drawLine(coord, 0, coord, getHeight());
182 } else {
183 g.drawLine(0, coord, getWidth(), coord);
184 }
185 if (range == Range.MONTH) {
186 int adv = Math.round(majTick / (24 * 60 * 7 * 4));
187 if (adv == 0) {
188 adv = 1;
189 }
190 date.advanceMonths(adv);
191 } else {
192 date.advanceMinutes(majTick);
193 }
194 }
195 }
196 }
197
198 /***
199 * {@inheritDoc} Does nothing.
200 */
201 public void setTickProvider(ITickProvider tickProvider) {
202
203 }
204
205 }