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.timeline.swt.renderer;
21
22 import org.eclipse.swt.graphics.Color;
23 import org.eclipse.swt.graphics.GC;
24 import org.eclipse.swt.graphics.RGB;
25 import org.eclipse.swt.graphics.Rectangle;
26 import org.eclipse.swt.printing.Printer;
27 import org.eclipse.swt.widgets.Display;
28
29 import de.jaret.util.date.DateUtils;
30 import de.jaret.util.date.JaretDate;
31 import de.jaret.util.ui.timebars.TickScaler;
32 import de.jaret.util.ui.timebars.TickScaler.Range;
33 import de.jaret.util.ui.timebars.TimeBarViewerDelegate;
34 import de.jaret.util.ui.timebars.TimeBarViewerInterface;
35 import de.jaret.util.ui.timebars.strategy.ITickProvider;
36 import de.jaret.util.ui.timebars.swt.renderer.AbstractGridRenderer;
37 import de.jaret.util.ui.timebars.swt.renderer.GridRenderer;
38
39 /***
40 * Grid renderer for the lower viewer.
41 *
42 * @author Peter Kliem
43 * @version $Id: DefaultGridRenderer.java 556 2007-09-04 22:07:59Z olk $
44 */
45 public class LowerGridRenderer extends AbstractGridRenderer implements GridRenderer {
46 /*** default color for the major grid (r,g,b). */
47 private static final RGB MAJORGRID_COLOR = new RGB(200, 200, 200);
48 /*** default color for the minor grid (r,g,b). */
49 private static final RGB MINORGRID_COLOR = new RGB(230, 230, 230);
50 private static final RGB MARK_COLOR = new RGB(255, 200, 200);
51
52 /*** linewidth when printing. */
53 private static final int PRINTING_LINEWIDTH = 3;
54
55
56 /*** color of the major grid. */
57 private Color _colorMajorGrid;
58
59 /*** color of the minor grid. */
60 private Color _colorMinorGrid;
61
62 private Color _colorMark;
63
64 private JaretDate _startMark;
65 private JaretDate _endMark;
66
67
68 /***
69 * Create a DefaultGridRenderer for a printer.
70 *
71 * @param printer printer device
72 */
73 public LowerGridRenderer(Printer printer) {
74 super(printer);
75 _colorMajorGrid = new Color(printer, MAJORGRID_COLOR);
76 _colorMinorGrid = new Color(printer, MINORGRID_COLOR);
77 _colorMark = new Color(printer, MARK_COLOR);
78 }
79
80 /***
81 * Create a DefaultGridRenderer for the screen.
82 */
83 public LowerGridRenderer() {
84 super(null);
85 _colorMajorGrid = new Color(Display.getCurrent(), MAJORGRID_COLOR);
86 _colorMinorGrid = new Color(Display.getCurrent(), MINORGRID_COLOR);
87 _colorMark = new Color(Display.getCurrent(), MARK_COLOR);
88 }
89
90
91 /***
92 * {@inheritDoc}
93 */
94 public void draw(GC gc, TimeBarViewerDelegate delegate, Rectangle drawingArea, boolean printing) {
95 int ox = drawingArea.x;
96 int oy = drawingArea.y;
97 int width = drawingArea.width;
98 int height = drawingArea.height;
99
100 Color fg = gc.getForeground();
101
102 int idx;
103 if (!printing) {
104 idx = TickScaler.getTickIdx(delegate.getPixelPerSecond() / getScaleX());
105 } else {
106 idx = TickScaler.getTickIdx(delegate.getPixelPerSecond() / getScaleX());
107 }
108 int majTick = TickScaler.getMajorTickMinutes(idx);
109 int minTick = TickScaler.getMinorTickMinutes(idx);
110 Range range = TickScaler.getRange(idx);
111
112
113 JaretDate date = delegate.getStartDate().copy();
114
115
116
117 date.setMinutes(0);
118 date.setHours(0);
119 date.setSeconds(0);
120
121 if (range == Range.WEEK) {
122 while (date.getDayOfWeek() != DateUtils.getFirstDayOfWeek()) {
123 date.backDays(1);
124 }
125 } else if (range == Range.MONTH) {
126
127 date.setDay(1);
128 }
129 JaretDate save = date.copy();
130
131 boolean horizontal = delegate.getOrientation() == TimeBarViewerInterface.Orientation.HORIZONTAL;
132 int max = horizontal ? (ox + width):(oy + height);
133
134 Color bg = gc.getBackground();
135
136 if (_startMark != null && _endMark != null) {
137 int startX = delegate.xForDate(_startMark);
138 int endX = delegate.xForDate(_endMark);
139 gc.setBackground(_colorMark);
140 gc.fillRectangle(startX, oy, endX-startX, height);
141 }
142
143
144 gc.setBackground(bg);
145
146 date = save.copy();
147
148 if (printing) {
149 gc.setLineWidth(PRINTING_LINEWIDTH);
150 }
151 gc.setForeground(_colorMinorGrid);
152 while (delegate.xForDate(date) < max) {
153 int x = delegate.xForDate(date);
154 if (horizontal) {
155 gc.drawLine(x, oy, x, oy + height);
156 } else {
157 gc.drawLine(ox, x, ox+width, x);
158 }
159
160 if (range == Range.MONTH) {
161 int adv = Math.round(minTick / (24 * 60 * 7 * 4));
162 if (adv == 0) {
163 adv = 1;
164 }
165 date.advanceMonths(adv);
166 } else {
167 date.advanceMinutes(minTick);
168 }
169 }
170
171 date = save.copy();
172
173 gc.setForeground(_colorMajorGrid);
174 while (delegate.xForDate(date) < ox + width) {
175 int x = delegate.xForDate(date);
176 if (horizontal) {
177 gc.drawLine(x, oy, x, oy + height);
178 } else {
179 gc.drawLine(ox, x, ox+width, x);
180 }
181 if (range == Range.MONTH) {
182 int adv = Math.round(majTick / (24 * 60 * 7 * 4));
183 if (adv == 0) {
184 adv = 1;
185 }
186 date.advanceMonths(adv);
187 } else {
188 date.advanceMinutes(majTick);
189 }
190 }
191 gc.setLineWidth(1);
192 gc.setForeground(fg);
193 }
194
195 /***
196 * {@inheritDoc}
197 */
198 public void dispose() {
199 _colorMajorGrid.dispose();
200 _colorMinorGrid.dispose();
201 _colorMark.dispose();
202 }
203
204 /***
205 * {@inheritDoc}
206 */
207 public GridRenderer createPrintRenderer(Printer printer) {
208 LowerGridRenderer renderer = new LowerGridRenderer(printer);
209 return renderer;
210 }
211
212 public JaretDate getStartMark() {
213 return _startMark;
214 }
215
216 public void setStartMark(JaretDate startMark) {
217 _startMark = startMark;
218 }
219
220 public JaretDate getEndMark() {
221 return _endMark;
222 }
223
224 public void setEndMark(JaretDate endMark) {
225 _endMark = endMark;
226 }
227
228 public void setTickProvider(ITickProvider tickProvider) {
229
230
231 }
232 }