View Javadoc

1   /*
2    *  File: DefaultGapRenderer.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.swt.renderer;
21  
22  import org.eclipse.swt.SWT;
23  import org.eclipse.swt.graphics.Color;
24  import org.eclipse.swt.graphics.GC;
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.Interval;
30  import de.jaret.util.misc.FormatHelper;
31  import de.jaret.util.swt.SwtGraphicsHelper;
32  import de.jaret.util.ui.timebars.TimeBarViewerDelegate;
33  import de.jaret.util.ui.timebars.TimeBarViewerInterface;
34  import de.jaret.util.ui.timebars.model.TimeBarRow;
35  
36  /**
37   * Gap renderer rendering a double headed arrow and the time between the intervals.
38   * 
39   * @author Peter Kliem
40   * @version $Id: DefaultGapRenderer.java 781 2008-09-20 20:10:18Z kliem $
41   */
42  public class DefaultGapRenderer extends RendererBase implements TimeBarGapRenderer {
43      /** color the arrow is rendered in. */
44      private static final Color DEFAULT_COLOR = Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GRAY);
45  
46      /**
47       * Construct a DefaultGapRanderer for a printer.
48       * 
49       * @param printer printer device to use
50       */
51      public DefaultGapRenderer(Printer printer) {
52          super(printer);
53      }
54  
55      /**
56       * Default constructor.
57       */
58      public DefaultGapRenderer() {
59          super(null);
60      }
61  
62      /**
63       * {@inheritDoc}
64       */
65      public void draw(GC gc, TimeBarViewerDelegate delegate, TimeBarRow row, Interval i1, Interval i2,
66              Rectangle drawingArea, boolean printing) {
67          int ox = drawingArea.x;
68          int oy = drawingArea.y;
69          int width = drawingArea.width;
70          int height = drawingArea.height;
71  
72          boolean horizontal = delegate.getOrientation() == TimeBarViewerInterface.Orientation.HORIZONTAL;
73  
74          Color fg = gc.getForeground();
75  
76          int diffminutes = (int) i2.getBegin().diffMinutes(i1.getEnd());
77          String timeString = FormatHelper.NFInt2Digits().format(diffminutes / 60) + ":"
78                  + FormatHelper.NFInt2Digits().format(diffminutes % 60);
79          int twidth = SwtGraphicsHelper.getStringDrawingWidth(gc, timeString);
80          int theight = SwtGraphicsHelper.getStringDrawingHeight(gc, timeString);
81  
82          gc.setForeground(DEFAULT_COLOR);
83          // center the timeString
84          if (horizontal) {
85              if (width > twidth + 3 && height / 2 >= theight) {
86                  SwtGraphicsHelper.drawStringCentered(gc, timeString, ox + width / 2, oy + height / 2 - 2);
87              }
88          } else {
89              if (height > theight + 3 && width / 2 >= twidth) {
90                  SwtGraphicsHelper.drawStringCenteredAroundPoint(gc, timeString, ox + width / 2, oy + height / 2);
91              }
92          }
93          // draw a line with arrow endings
94          if (printing) {
95              gc.setLineWidth(getDefaultLineWidth());
96          }
97          if (horizontal) {
98              if (width > 15) {
99                  SwtGraphicsHelper.drawArrowLine(gc, ox + 2, oy + height / 2, ox + width - 3, oy + height / 2,
100                         scaleX(4), scaleY(3), true, true);
101             }
102         } else {
103             if (height > 15) {
104                 SwtGraphicsHelper.drawArrowLineVertical(gc, ox + width / 2, oy + 2, ox + width / 2, oy + height - 3,
105                         scaleX(3), scaleY(4), true, true);
106             }
107         }
108         gc.setLineWidth(1);
109 
110         gc.setForeground(fg);
111     }
112 
113     /**
114      * {@inheritDoc}
115      */
116     public void dispose() {
117         // nothing to dispose
118     }
119 
120     /**
121      * {@inheritDoc}
122      */
123     public TimeBarGapRenderer createPrintRenderer(Printer printer) {
124         DefaultGapRenderer renderer = new DefaultGapRenderer(printer);
125         return renderer;
126     }
127 
128 }