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.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
35
36
37
38
39
40
41 public class DefaultGridRenderer implements GridRenderer {
42
43 private static final Color MAJORGRIDCOLOR = new Color(200, 200, 200);
44
45 private static final Color MINORGRIDCOLOR = new Color(230, 230, 230);
46
47 private static final double APPROX_MONTH_MINUTES = (24.0 * 60.0 * 7.0 * 4.0);
48
49
50 protected Color _majorGridColor = MAJORGRIDCOLOR;
51
52
53 protected Color _minorGridColor = MINORGRIDCOLOR;
54
55
56
57 protected MyGridRenderer _component = new MyGridRenderer();
58
59
60 protected ITickProvider _tickProvider = null;
61
62
63
64
65 public JComponent getRendererComponent(TimeBarViewer tbv) {
66 _component.setTimeBarViewer(tbv);
67 return _component;
68 }
69
70
71
72
73
74
75
76 public void setTickProvider(ITickProvider tickProvider) {
77 _tickProvider = tickProvider;
78 }
79
80
81
82
83
84 public Color getMajorGridColor() {
85 return _majorGridColor;
86 }
87
88
89
90
91
92 public void setMajorGridColor(Color majorGridColor) {
93 _majorGridColor = majorGridColor;
94 }
95
96
97
98
99
100 public Color getMinorGridColor() {
101 return _minorGridColor;
102 }
103
104
105
106
107
108 public void setMinorGridColor(Color minorGridColor) {
109 _minorGridColor = minorGridColor;
110 }
111
112
113
114
115
116
117
118
119 @SuppressWarnings("serial")
120 class MyGridRenderer extends JComponent {
121
122 private TimeBarViewer _tbv;
123
124
125
126
127
128
129 public void setTimeBarViewer(TimeBarViewer tbv) {
130 _tbv = tbv;
131 }
132
133
134
135
136
137
138
139 private int xForDate(JaretDate date) {
140 int x = _tbv.xForDate(date);
141 x -= _tbv.getHierarchyWidth() + _tbv.getYAxisWidth();
142 return x;
143 }
144
145
146
147
148 public void paintComponent(Graphics g) {
149
150 boolean horizontal = _tbv.getTBOrientation() == TimeBarViewerInterface.Orientation.HORIZONTAL;
151
152 if (_tickProvider == null) {
153 paint(g, horizontal);
154 } else {
155 paint(g, horizontal, _tickProvider);
156 }
157 }
158
159 protected void paint(Graphics g, boolean horizontal, ITickProvider tickProvider) {
160
161 Color color = g.getColor();
162 g.setColor(_minorGridColor);
163
164 for (JaretDate tickDate : tickProvider.getMinorTicks(_tbv.getDelegate())) {
165
166 int coord = xForDate(tickDate);
167 if (horizontal) {
168 g.drawLine(coord, 0, coord, getHeight());
169 } else {
170 g.drawLine(0, coord, getWidth(), coord);
171 }
172 }
173
174 g.setColor(_majorGridColor);
175
176 for (JaretDate tickDate : tickProvider.getMajorTicks(_tbv.getDelegate())) {
177 int coord = xForDate(tickDate);
178 if (horizontal) {
179 g.drawLine(coord, 0, coord, getHeight());
180 } else {
181 g.drawLine(0, coord, getWidth(), coord);
182 }
183 }
184 g.setColor(color);
185 }
186
187 protected void paint(Graphics g, boolean horizontal) {
188 Color color = g.getColor();
189
190 JaretDate date = _tbv.getStartDate().copy();
191
192 int idx = TickScaler.getTickIdx(_tbv.getPixelPerSecond());
193 int majTick = TickScaler.getMajorTickMinutes(idx);
194 int minTick = TickScaler.getMinorTickMinutes(idx);
195 Range range = TickScaler.getRange(idx);
196
197
198
199 date.setMinutes(0);
200 date.setHours(0);
201 date.setSeconds(0);
202
203 if (range == Range.WEEK) {
204 while (date.getDayOfWeek() != DateUtils.getFirstDayOfWeek()) {
205 date.backDays(1.0);
206 }
207 } else if (range == Range.MONTH) {
208
209 date.setDay(1);
210 }
211 JaretDate save = date.copy();
212
213 date.backMinutes(majTick);
214
215
216 int max = horizontal ? getWidth() : getHeight();
217
218
219 g.setColor(MINORGRIDCOLOR);
220 while (xForDate(date) < max) {
221 int coord = xForDate(date);
222 if (horizontal) {
223 g.drawLine(coord, 0, coord, getHeight());
224 } else {
225 g.drawLine(0, coord, getWidth(), coord);
226 }
227 if (range == Range.MONTH) {
228 int adv = (int) Math.round((double) minTick / APPROX_MONTH_MINUTES);
229 if (adv == 0) {
230 adv = 1;
231 }
232 date.advanceMonths(adv);
233 } else {
234 date.advanceMinutes(minTick);
235 }
236 }
237
238 date = save.copy();
239
240 g.setColor(MAJORGRIDCOLOR);
241 while (xForDate(date) < max) {
242 int coord = xForDate(date);
243 if (horizontal) {
244 g.drawLine(coord, 0, coord, getHeight());
245 } else {
246 g.drawLine(0, coord, getWidth(), coord);
247 }
248 if (range == Range.MONTH) {
249 int adv = (int) Math.round((double) majTick / APPROX_MONTH_MINUTES);
250 if (adv == 0) {
251 adv = 1;
252 }
253 date.advanceMonths(adv);
254 } else {
255 date.advanceMinutes(majTick);
256 }
257 }
258 g.setColor(color);
259 }
260 }
261
262 }