View Javadoc

1   /*
2    *  File: DefaultTimeScaleRenderer.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.util.ui.timebars.swing.renderer;
21  
22  import java.awt.Graphics;
23  import java.awt.event.MouseEvent;
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.swing.GraphicsHelper;
30  import de.jaret.util.ui.timebars.TickScaler;
31  import de.jaret.util.ui.timebars.TickScaler.Range;
32  import de.jaret.util.ui.timebars.TimeBarViewerInterface;
33  import de.jaret.util.ui.timebars.swing.TimeBarViewer;
34  
35  /**
36   * A default renderer for a time scale to be used in a TimeBarViewer.
37   * 
38   * @author Peter Kliem
39   * @version $Id: OldDefaultTimeScaleRenderer.java 1083 2011-07-01 20:29:16Z kliem $
40   */
41  public class OldDefaultTimeScaleRenderer implements TimeScaleRenderer {
42      /** rendering component. */
43      protected MyTimeScaleRenderer _renderer = new MyTimeScaleRenderer();
44  
45      /**
46       * {@inheritDoc}
47       */
48      public JComponent getRendererComponent(TimeBarViewer tbv, boolean top) {
49          _renderer.setTimeBarViewer(tbv);
50          _renderer.setTop(top);
51          return _renderer;
52      }
53  
54      /**
55       * {@inheritDoc}
56       */
57      public int getHeight() {
58          return 50;
59      }
60  
61      /**
62       * JComponent for renderuing the timescale.
63       * 
64       * @author kliem
65       * @version $Id: OldDefaultTimeScaleRenderer.java 1083 2011-07-01 20:29:16Z kliem $
66       */
67      @SuppressWarnings("serial")
68      class MyTimeScaleRenderer extends JComponent {
69          /** the viewer the renderer is used for. */
70          private TimeBarViewer _tbv;
71          /** true if the position is top. */
72          boolean _top;
73  
74          /**
75           * Set the viewer.
76           * 
77           * @param tbv viewer
78           */
79          public void setTimeBarViewer(TimeBarViewer tbv) {
80              _tbv = tbv;
81          }
82  
83          /**
84           * Set the time scale position.
85           * 
86           * @param top true for top
87           */
88          public void setTop(boolean top) {
89              _top = top;
90          }
91  
92          private int xForDate(JaretDate date) {
93              int x = _tbv.xForDate(date);
94              x -= _tbv.getHierarchyWidth() + _tbv.getYAxisWidth();
95              return x;
96          }
97  
98          private JaretDate dateForX(int x) {
99              return _tbv.dateForX(x + _tbv.getHierarchyWidth() + _tbv.getYAxisWidth());
100         }
101 
102         /**
103          * {@inheritDoc}
104          */
105         public void paintComponent(Graphics g) {
106             if (_tbv.getTBOrientation() == TimeBarViewerInterface.Orientation.HORIZONTAL) {
107                 paintHorizontal(g);
108             } else {
109                 paintVertical(g);
110             }
111         }
112 
113         /**
114          * paint the horizontal scale.
115          */
116         public void paintHorizontal(Graphics g) {
117 
118             int idx = TickScaler.getTickIdx(_tbv.getPixelPerSecond());
119             int majTick = TickScaler.getMajorTickMinutes(idx);
120             int minTick = TickScaler.getMinorTickMinutes(idx);
121             TickScaler.Range range = TickScaler.getRange(idx);
122 
123             // first date
124             JaretDate date = _tbv.getStartDate().copy().backMinutes(2 * majTick);
125 
126             // clean starting date on a major tick minute position (starting
127             // with a day)
128             date.setMinutes(0);
129             date.setHours(0);
130             date.setSeconds(0);
131 
132             // if range is week take a week starting point
133             if (range == Range.WEEK) {
134                 while (date.getDayOfWeek() != DateUtils.getFirstDayOfWeek()) {
135                     date.backDays(1.0);
136                 }
137             } else if (range == Range.MONTH) {
138                 // month -> month starting point
139                 date.setDay(1);
140             }
141             JaretDate save = date.copy();
142 
143             int basey;
144             int minorOff;
145             int majorOff;
146             int majorLabelOff;
147             int dayOff;
148             if (!_top) {
149                 basey = 0;
150                 minorOff = 5;
151                 majorOff = 10;
152                 majorLabelOff = 22;
153                 dayOff = 34;
154             } else {
155                 basey = getHeight() - 1;
156                 minorOff = -5;
157                 majorOff = -10;
158                 majorLabelOff = -10;
159                 dayOff = -22;
160             }
161             int oy = basey;
162 
163             // draw top line
164             g.drawLine(0, oy, getWidth(), oy);
165 
166             // draw the minor ticks
167             while (xForDate(date) < getWidth()) {
168                 int x = xForDate(date);
169                 g.drawLine(x, oy, x, oy + minorOff);
170                 if (range == Range.MONTH) {
171                     int adv = Math.round(minTick / (24 * 60 * 7 * 4));
172                     if (adv == 0) {
173                         adv = 1;
174                     }
175                     date.advanceMonths(adv);
176                 } else {
177                     date.advanceMinutes(minTick);
178                 }
179             }
180 
181             date = save.copy();
182             // draw the major ticks
183             while (xForDate(date) < getWidth()) {
184                 int x = xForDate(date);
185                 g.drawLine(x, oy, x, oy + majorOff);
186                 if (range == Range.MONTH) {
187                     int adv = Math.round(majTick / (24 * 60 * 7 * 4));
188                     if (adv == 0) {
189                         adv = 1;
190                     }
191                     date.advanceMonths(adv);
192                 } else {
193                     date.advanceMinutes(majTick);
194                 }
195             }
196 
197             // labels: draw every two major ticks
198             date = save.copy();
199             int lastDay = date.getDay();
200             int width = getWidth();
201             while (xForDate(date) < width + 100) {
202                 int x = xForDate(date);
203                 if (date.getMinutes() % (majTick * 2) == 0) {
204                     // Second line
205                     String str = null;
206                     if (range == Range.HOUR) {
207                         // time
208                         str = date.toDisplayStringTime();
209                     } else if (range == Range.DAY) {
210                         // day
211                         str = date.getShortDayOfWeekString();
212                     } else if (range == Range.WEEK) {
213                         // week
214                         str = "KW" + date.getWeekOfYear();
215                     } else if (range == Range.MONTH) {
216                         // month
217                         str = Integer.toString(date.getYear());
218                     }
219                     // draw
220                     GraphicsHelper.drawStringCentered(g, str, x, oy + majorLabelOff);
221                     // first line
222                     if (range == Range.HOUR) {
223                         if (date.getDay() != lastDay) {
224                             str = date.getDay() + ". (" + date.getDayOfWeekString() + ")";
225                         } else {
226                             str = "";
227                         }
228                         lastDay = date.getDay();
229                     } else if (range == Range.DAY || range == Range.WEEK) {
230                         str = date.getDay() + ".";
231                     } else if (range == Range.MONTH) {
232                         str = date.getMonthString();
233                     }
234                     GraphicsHelper.drawStringCentered(g, str, x, oy + dayOff);
235                 }
236                 if (range == Range.MONTH) {
237                     int adv = Math.round(majTick / (24 * 60 * 7 * 4));
238                     if (adv == 0) {
239                         adv = 1;
240                     }
241                     date.advanceMonths(adv);
242                 } else {
243                     date.advanceMinutes(majTick);
244                 }
245             }
246 
247         }
248 
249         /**
250          * paint the vertical scale.
251          */
252         public void paintVertical(Graphics g) {
253 
254             int idx = TickScaler.getTickIdx(_tbv.getPixelPerSecond());
255             int majTick = TickScaler.getMajorTickMinutes(idx);
256             int minTick = TickScaler.getMinorTickMinutes(idx);
257             TickScaler.Range range = TickScaler.getRange(idx);
258 
259             // first date
260             JaretDate date = _tbv.getStartDate().copy().backMinutes(majTick * 3);
261 
262             // clean starting date on a major tick minute position (starting
263             // with a day)
264             date.setMinutes(0);
265             date.setHours(0);
266             date.setSeconds(0);
267 
268             // if range is week take a week starting point
269             if (range == Range.WEEK) {
270                 while (date.getDayOfWeek() != DateUtils.getFirstDayOfWeek()) {
271                     date.backDays(1.0);
272                 }
273             } else if (range == Range.MONTH) {
274                 // month -> month starting point
275                 date.setDay(1);
276             }
277             JaretDate save = date.copy();
278 
279             int basex;
280             int minorOff;
281             int majorOff;
282             if (!_top) {
283                 basex = 0;
284                 minorOff = 5;
285                 majorOff = 10;
286             } else {
287                 basex = getWidth() - 1;
288                 minorOff = -5;
289                 majorOff = -10;
290             }
291             int ox = basex;
292 
293             // draw left/right line
294             if (_top) {
295                 g.drawLine(getWidth() - 1, 0, getWidth() - 1, getHeight());
296             } else {
297                 g.drawLine(0, 0, 0, getHeight());
298             }
299 
300             // draw the minor ticks
301             while (xForDate(date) < getHeight()) {
302                 int y = xForDate(date);
303                 g.drawLine(ox, y, ox + minorOff, y);
304                 if (range == Range.MONTH) {
305                     int adv = Math.round(minTick / (24 * 60 * 7 * 4));
306                     if (adv == 0) {
307                         adv = 1;
308                     }
309                     date.advanceMonths(adv);
310                 } else {
311                     date.advanceMinutes(minTick);
312                 }
313             }
314 
315             date = save.copy();
316             // draw the major ticks
317             while (xForDate(date) < getHeight()) {
318                 int y = xForDate(date);
319                 g.drawLine(ox, y, ox + majorOff, y);
320                 if (range == Range.MONTH) {
321                     int adv = Math.round(majTick / (24 * 60 * 7 * 4));
322                     if (adv == 0) {
323                         adv = 1;
324                     }
325                     date.advanceMonths(adv);
326                 } else {
327                     date.advanceMinutes(majTick);
328                 }
329             }
330 
331             // labels: draw every two major ticks
332             date = save.copy();
333             int height = getHeight();
334             while (xForDate(date) < height + 50) {
335                 int y = xForDate(date);
336                 if (date.getMinutes() % (majTick * 2) == 0) {
337                     // Second line
338                     String str = null;
339                     if (range == Range.HOUR) {
340                         // time
341                         str = date.toDisplayStringTime();
342                     } else if (range == Range.DAY) {
343                         // day
344                         str = date.getShortDayOfWeekString();
345                     } else if (range == Range.WEEK) {
346                         // week
347                         str = "KW" + date.getWeekOfYear();
348                     } else if (range == Range.MONTH) {
349                         // month
350                         str = Integer.toString(date.getYear());
351                     }
352                     // draw
353                     if (_top) {
354                         GraphicsHelper.drawStringRightAlignedVCenter(g, str, ox + majorOff - 1, y);
355                     } else {
356                         GraphicsHelper.drawStringLeftAlignedVCenter(g, str, ox + majorOff + 1, y);
357                     }
358                 }
359                 if (range == Range.MONTH) {
360                     int adv = Math.round(majTick / (24 * 60 * 7 * 4));
361                     if (adv == 0) {
362                         adv = 1;
363                     }
364                     date.advanceMonths(adv);
365                 } else {
366                     date.advanceMinutes(majTick);
367                 }
368             }
369 
370         }
371 
372         /**
373          * {@inheritDoc}
374          */
375         public String getToolTipText(MouseEvent event) {
376             if (_tbv.getTBOrientation() == TimeBarViewerInterface.Orientation.HORIZONTAL) {
377                 JaretDate date = dateForX(event.getX());
378                 return date.toDisplayString();
379             } else {
380                 JaretDate date = dateForX(event.getY());
381                 return date.toDisplayString();
382             }
383         }
384     }
385 
386 }