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.swing.renderer;
21  
22  import java.awt.Color;
23  import java.awt.Graphics;
24  
25  import javax.swing.JComponent;
26  
27  import de.jaret.util.date.Interval;
28  import de.jaret.util.misc.FormatHelper;
29  import de.jaret.util.swing.GraphicsHelper;
30  import de.jaret.util.ui.timebars.TimeBarViewerInterface;
31  import de.jaret.util.ui.timebars.model.TimeBarRow;
32  import de.jaret.util.ui.timebars.swing.TimeBarViewer;
33  
34  /**
35   * Default gap renderer showing the time between intervals and an double headed arrow.
36   * 
37   * @author Peter Kliem
38   * @version $Id: DefaultGapRenderer.java 1083 2011-07-01 20:29:16Z kliem $
39   */
40  public class DefaultGapRenderer implements TimeBarGapRenderer {
41      /** the rendering component. */
42      protected GapRenderer _component = new GapRenderer();
43  
44      /**
45       * {@inheritDoc}
46       */
47      public JComponent getTimeBarGapRendererComponent(TimeBarViewer tbv, TimeBarRow row, Interval interval1,
48              Interval interval2) {
49          boolean horizontal = tbv.getTBOrientation() == TimeBarViewerInterface.Orientation.HORIZONTAL;
50          _component.setUp(horizontal, interval1, interval2);
51  
52          return _component;
53      }
54  
55      /**
56       * {@inheritDoc}
57       */
58      public int getMinimumWidth() {
59          return -1; // no minimum width required
60      }
61  
62      /**
63       * Gap renderer component.
64       * 
65       * @author kliem
66       * @version $Id: DefaultGapRenderer.java 1083 2011-07-01 20:29:16Z kliem $
67       * @SuppressWarnings("serial")
68       */
69       
70      class GapRenderer extends JComponent {
71          /** earlier interval. */
72          private Interval _i1;
73          /** later interval. */
74          private Interval _i2;
75          /** true for horizontal orientation. */
76          private boolean _horizontal;
77  
78          /**
79           * Set up the component for rendering.
80           * 
81           * @param horizontal true for horizontal orientation
82           * @param interval1 earlier interval
83           * @param interval2 later interval
84           */
85          public void setUp(boolean horizontal, Interval interval1, Interval interval2) {
86              _horizontal = horizontal;
87              _i1 = interval1;
88              _i2 = interval2;
89          }
90  
91          /**
92           * {@inheritDoc}
93           */
94          protected void paintComponent(Graphics g) {
95              // on the edge one of the intervals may be null
96              // for this renderer this means there is no need to paint anything
97              if (_i1 != null && _i2 != null) {
98                  int diffminutes = (int) _i2.getBegin().diffMinutes(_i1.getEnd());
99                  String timeString = FormatHelper.NFInt2Digits().format(diffminutes / 60) + ":"
100                         + FormatHelper.NFInt2Digits().format(diffminutes % 60);
101                 int twidth = GraphicsHelper.getStringDrawingWidth(g, timeString);
102                 int theight = GraphicsHelper.getStringDrawingHeight(g, timeString);
103 
104                 g.setColor(Color.DARK_GRAY);
105                 if (_horizontal) {
106                     // center the timeString
107                     if (getWidth() > twidth + 3 && getHeight() / 2 >= theight) {
108                         GraphicsHelper.drawStringCentered(g, timeString, getWidth() / 2, getHeight() / 2 - 2);
109                     }
110                     // draw a line with arrow endings
111                     if (getWidth() > 15) {
112                         GraphicsHelper.drawArrowLine(g, 2, getHeight() / 2, getWidth() - 3, getHeight() / 2, 4, true,
113                                 true);
114                     }
115                 } else {
116                     // center the timeString
117                     if (getHeight() > theight + 3 && getWidth() >= twidth) {
118                         GraphicsHelper.drawStringCenteredPoint(g, timeString, getWidth() / 2, getHeight() / 2);
119                     }
120                     // draw a line with arrow endings
121                     if (getWidth() > 15) {
122                         GraphicsHelper.drawArrowLineVertical(g, getWidth() / 2, 2, getWidth() / 2, getHeight() - 3, 3,
123                                 4, true, true);
124                     }
125                 }
126 
127             }
128         }
129 
130     }
131 
132 }