View Javadoc

1   /*
2    *  File: LowerGridRenderer.java 
3    *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
4    *  A commercial license is available, see http://www.jaret.de.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation; either version 2 of the License, or
9    *  (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   *  GNU General Public License for more details.
15   *
16   *  You should have received a copy of the GNU General Public License
17   *  along with this program; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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         // first date
113         JaretDate date = delegate.getStartDate().copy();
114 
115         // clean starting date on a major tick minute position (starting with a
116         // day)
117         date.setMinutes(0);
118         date.setHours(0);
119         date.setSeconds(0);
120         // if range is week take a week starting point
121         if (range == Range.WEEK) {
122             while (date.getDayOfWeek() != DateUtils.getFirstDayOfWeek()) {
123                 date.backDays(1);
124             }
125         } else if (range == Range.MONTH) {
126             // month -> month starting point
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         // draw the minor grid
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         // draw the major grid
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         // TODO Auto-generated method stub
230         
231     }
232 }