View Javadoc

1   /*
2    *  File: MilliGrid.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.millis.swt.renderer;
21  
22  import org.eclipse.swt.graphics.Color;
23  import org.eclipse.swt.graphics.GC;
24  import org.eclipse.swt.graphics.Rectangle;
25  import org.eclipse.swt.printing.Printer;
26  import org.eclipse.swt.widgets.Display;
27  
28  import de.jaret.util.date.JaretDate;
29  import de.jaret.util.ui.timebars.TimeBarViewerDelegate;
30  import de.jaret.util.ui.timebars.strategy.ITickProvider;
31  import de.jaret.util.ui.timebars.swt.renderer.AbstractGridRenderer;
32  import de.jaret.util.ui.timebars.swt.renderer.GridRenderer;
33  
34  /***
35   * Simple grid renderer for the milli example.
36   * 
37   * @author Peter Kliem
38   * @version $Id: MilliGrid.java 856 2009-04-02 18:54:40Z kliem $
39   */
40  public class MilliGrid extends AbstractGridRenderer implements GridRenderer {
41      /*** color of the major grid. */
42      private Color _colorMajorGrid;
43  
44      /*** color of the minor grid. */
45      private Color _colorMinorGrid;
46  
47      public MilliGrid(Printer printer) {
48          super(printer);
49          _colorMajorGrid = new Color(printer, 200, 200, 200);
50          _colorMinorGrid = new Color(printer, 230, 230, 230);
51  
52      }
53  
54      public MilliGrid() {
55          super(null);
56          _colorMajorGrid = new Color(Display.getCurrent(), 200, 200, 200);
57          _colorMinorGrid = new Color(Display.getCurrent(), 230, 230, 230);
58      }
59  
60      /***
61       * {@inheritDoc}
62       */
63      public void draw(GC gc, TimeBarViewerDelegate delegate, Rectangle drawingArea, boolean printing) {
64          int ox = drawingArea.x;
65          int oy = drawingArea.y;
66          int width = drawingArea.width;
67          int height = drawingArea.height;
68  
69          Color fg = gc.getForeground();
70  
71          // first date
72          JaretDate date = delegate.getStartDate().copy();
73  
74          while (date.diffMilliSeconds(delegate.getMinDate()) % MilliScale.MAJOR != 0) {
75              date.advanceMillis(-1);
76          }
77  
78          JaretDate save = date.copy();
79  
80          date = save.copy();
81          // draw the minor grid
82          if (printing) {
83              gc.setLineWidth(3);
84          }
85          gc.setForeground(_colorMinorGrid);
86          while (delegate.xForDate(date) < ox + width) {
87              JaretDate eosc = MilliScale.endOfSpecialScaling(delegate, date);
88              if (eosc == null) {
89                  int x = delegate.xForDate(date);
90                  gc.drawLine(x, oy, x, oy + height);
91  
92                  date.advanceMillis(MilliScale.MINOR);
93              } else {
94                  long diff = eosc.diffMilliSeconds(date);
95                  date.advanceMillis(diff+MilliScale.MINOR);
96              }
97          }
98  
99          date = save.copy();
100         // draw the major grid
101         gc.setForeground(_colorMajorGrid);
102         while (delegate.xForDate(date) < ox + width) {
103             JaretDate eosc = MilliScale.endOfSpecialScaling(delegate, date);
104             if (eosc == null) {
105                 int x = delegate.xForDate(date);
106                 gc.drawLine(x, oy, x, oy + height);
107                 date.advanceMillis(MilliScale.MAJOR);
108             } else {
109                 long diff = eosc.diffMilliSeconds(date);
110                 date.advanceMillis(diff+MilliScale.MAJOR);
111             }
112         }
113         gc.setLineWidth(1);
114         gc.setForeground(fg);
115     }
116 
117     /***
118      * {@inheritDoc}
119      */
120     public void dispose() {
121         _colorMajorGrid.dispose();
122         _colorMinorGrid.dispose();
123     }
124 
125     /***
126      * {@inheritDoc}
127      */
128     public GridRenderer createPrintRenderer(Printer printer) {
129         MilliGrid renderer = new MilliGrid(printer);
130         return renderer;
131     }
132 
133     public void setTickProvider(ITickProvider tickProvider) {
134         // TODO Auto-generated method stub
135         
136     }
137 }