View Javadoc

1   /*
2    *  File: MilliScale.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 java.util.List;
23  
24  import org.eclipse.swt.graphics.GC;
25  import org.eclipse.swt.graphics.Rectangle;
26  import org.eclipse.swt.printing.Printer;
27  
28  import de.jaret.util.date.Interval;
29  import de.jaret.util.date.JaretDate;
30  import de.jaret.util.swt.SwtGraphicsHelper;
31  import de.jaret.util.ui.timebars.TimeBarViewerDelegate;
32  import de.jaret.util.ui.timebars.swt.TimeBarViewer;
33  import de.jaret.util.ui.timebars.swt.renderer.RendererBase;
34  import de.jaret.util.ui.timebars.swt.renderer.TimeScaleRenderer;
35  
36  /***
37   * Scale for the milli second accuracy example. Regions with special scale will not painted.
38   * 
39   * @author Peter Kliem
40   * @version $Id: MilliScale.java 1086 2011-07-30 20:28:05Z kliem $
41   */
42  public class MilliScale extends RendererBase implements TimeScaleRenderer {
43      public static final boolean SUPPRESS_WHEN_SCALED = true;
44      public static final long MAJOR = 10;
45      public static final long MINOR = 1;
46  
47      private long _major = MAJOR;
48      private long _minor = MINOR;
49  
50      public MilliScale(Printer printer) {
51          super(printer);
52      }
53  
54      public MilliScale() {
55          super(null);
56      }
57  
58      public MilliScale(long minor, long major) {
59          super(null);
60          _major = major;
61          _minor = minor;
62      }
63  
64      /***
65       * {@inheritDoc}
66       */
67      public void draw(GC gc, Rectangle drawingArea, TimeBarViewerDelegate delegate, boolean top, boolean printing) {
68          int ox = drawingArea.x;
69  
70          int basey;
71          int minorOff;
72          int majorOff;
73          int majorLabelOff;
74  
75          if (!top) {
76              basey = drawingArea.y;
77              minorOff = scaleY(5);
78              majorOff = scaleY(10);
79              majorLabelOff = scaleY(22);
80          } else {
81              basey = drawingArea.y + drawingArea.height - 1;
82              minorOff = scaleY(-5);
83              majorOff = scaleY(-10);
84              majorLabelOff = scaleY(-10);
85          }
86          int oy = basey;
87  
88          int width = drawingArea.width;
89          JaretDate date = delegate.getStartDate().copy();
90  
91          while (date.diffMilliSeconds(delegate.getMinDate()) % _major != 0) {
92              date.advanceMillis(-1);
93          }
94  
95          JaretDate save = date.copy();
96  
97          if (printing) {
98              gc.setLineWidth(1);
99          }
100         // draw top/bottom line
101         gc.drawLine(ox, oy, ox+width, oy);
102         
103         // draw the minor ticks
104         while (delegate.xForDate(date) < ox+width) {
105             JaretDate eosc = endOfSpecialScaling(delegate, date);
106             if (eosc == null || !SUPPRESS_WHEN_SCALED) {
107                 int x = delegate.xForDate(date);
108                 gc.drawLine(x, oy, x, oy + minorOff);
109                 date.advanceMillis(_minor);
110             } else {
111                 long diff = eosc.diffMilliSeconds(date);
112                 date.advanceMillis(diff+_minor);
113             }
114         }
115 
116         date = save.copy();
117         // draw the major ticks
118         while (delegate.xForDate(date) < ox+width) {
119             JaretDate eosc = endOfSpecialScaling(delegate, date);
120             if (eosc == null || !SUPPRESS_WHEN_SCALED) {
121                 int x = delegate.xForDate(date);
122                 gc.drawLine(x, oy, x, oy + majorOff);
123                 date.advanceMillis(_major);
124             } else {
125                 long diff = eosc.diffMilliSeconds(date);
126                 date.advanceMillis(diff+_major);
127             }
128         }
129 
130         gc.setLineWidth(1);
131 
132         // labels: draw every two major ticks
133         date = save.copy();
134         // Labels are drawn beyond the width. Otherwise when the beginning of
135         // the labels
136         // would not be drawn when the tick itself is out of sight
137         while (delegate.xForDate(date) < width + 100) {
138             JaretDate eosc = endOfSpecialScaling(delegate, date);
139             if (eosc == null  || !SUPPRESS_WHEN_SCALED) {
140                 int x = delegate.xForDate(date);
141                 if (date.diffMilliSeconds(delegate.getMinDate()) % _major == 0) {
142                     // Second line
143                     String str = date.getDate().getTime()+"";//date.diffMilliSeconds(delegate.getMinDate()) + "";
144 
145                     // draw
146                     if (x > SwtGraphicsHelper.getStringDrawingWidth(gc, str) / 2) {
147                         SwtGraphicsHelper.drawStringCentered(gc, str, x, oy + majorLabelOff);
148                     }
149                 }
150                 date.advanceMillis(_major);
151             } else {
152                 long diff = eosc.diffMilliSeconds(date);
153                 date.advanceMillis(diff+_major+1);
154                 while (date.diffMilliSeconds(delegate.getMinDate()) % _major != 0) {
155                     date.advanceMillis(-1);
156                 }
157 
158             }
159         }
160 
161     }
162 
163     /***
164      * If the given date hits a scaled area, return the end date of that area.
165      * 
166      * @param delegate
167      * @return end of special scaled area or null
168      */
169     public static JaretDate endOfSpecialScaling(TimeBarViewerDelegate delegate, JaretDate date) {
170         if (!delegate.hasVariableXScale()) {
171             return null;
172         } else {
173             List<Interval> l = delegate.getPpsRow().getIntervals(date);
174             if (l == null || l.size() == 0) {
175                 return null;
176             } else {
177                 return l.get(0).getEnd();
178             }
179         }
180 
181     }
182 
183     /***
184      * {@inheritDoc}
185      */
186     public String getToolTipText(TimeBarViewer tbv, Rectangle drawingArea, int x, int y) {
187         String str = null;
188         JaretDate date = tbv.dateForX(x);
189         long millis = date.diffMilliSeconds(tbv.getStartDate());
190         str = "" + millis;
191 
192         return str;
193     }
194 
195     /***
196      * {@inheritDoc}
197      */
198     public int getHeight() {
199         if (_printer == null) {
200             return 50;
201         } else {
202             return scaleY(50);
203         }
204     }
205 
206     public void dispose() {
207         // nothing to dispose
208     }
209 
210     public void print(GC gc, Rectangle drawingArea, TimeBarViewerDelegate delegate, boolean top) {
211         // TODO
212     }
213 
214     /***
215      * {@inheritDoc}
216      */
217     public TimeScaleRenderer createPrintRenderer(Printer printer) {
218         return new MilliScale(printer);
219     }
220 
221     /***
222      * {@inheritDoc}
223      */
224     public boolean supportsOptimizedScrolling() {
225         return true;
226     }
227 
228 }