View Javadoc

1   /*
2    *  File: SumRenderer.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.hierarchy.swing;
21  
22  import java.awt.Color;
23  import java.awt.Graphics;
24  import java.awt.Rectangle;
25  
26  import javax.swing.JComponent;
27  
28  import de.jaret.util.date.Interval;
29  import de.jaret.util.ui.timebars.TimeBarViewerDelegate;
30  import de.jaret.util.ui.timebars.swing.TimeBarViewer;
31  import de.jaret.util.ui.timebars.swing.renderer.TimeBarRenderer;
32  
33  /***
34   * @author Peter Kliem
35   * @version $Id: SumRenderer.java 869 2009-07-07 19:32:45Z kliem $
36   */
37  
38  public class SumRenderer implements TimeBarRenderer {
39      SumRendererComponent _sumComponent;
40  
41      public SumRenderer() {
42          _sumComponent = new SumRendererComponent();
43      }
44  
45      public JComponent getTimeBarRendererComponent(TimeBarViewer tbv, Interval value, boolean isSelected, boolean overlapping) {
46          _sumComponent.setInterval(value);
47          _sumComponent.setSelected(isSelected);
48          return _sumComponent;
49      }
50  
51      public class SumRendererComponent extends JComponent {
52          Interval _interval;
53          boolean _selected;
54  
55          public SumRendererComponent() {
56              setLayout(null);
57              setOpaque(false);
58          }
59  
60          public void setInterval(Interval interval) {
61              _interval = interval;
62          }
63  
64          /*
65           * (non-Javadoc)
66           * 
67           * @see javax.swing.JComponent#getToolTipText()
68           */
69          public String getToolTipText() {
70              return _interval.toString();
71          }
72  
73          public void setSelected(boolean selected) {
74              _selected = selected;
75          }
76  
77          /*
78           * (non-Javadoc)
79           * 
80           * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
81           */
82          protected void paintComponent(Graphics g) {
83              super.paintComponent(g);
84              int height = getHeight();
85              int width = getWidth();
86  
87              int y = height / 3 - WIDTH;
88              int bheight = WIDTH;
89              int yEnd = y + bheight;
90  
91              // balken
92              if (_selected) {
93                  g.setColor(Color.BLUE);
94              } else {
95                  g.setColor(Color.BLACK);
96              }
97              g.fillRect(0, y, width - 1, bheight);
98  
99              int leftx[] = { 0, 0, 4 };
100             int lefty[] = { y + bheight, y + bheight + 7, y + bheight };
101 
102             int rightx[] = { width - 1, width - 1, width - 1 - 4 };
103             int righty[] = { y + bheight, y + bheight + 7, y + bheight };
104 
105             g.fillPolygon(leftx, lefty, 3);
106             g.fillPolygon(rightx, righty, 3);
107 
108         }
109 
110         /*
111          * (non-Javadoc)
112          * 
113          * @see javax.swing.JComponent#contains(int, int)
114          */
115         public boolean contains(int x, int y) {
116             if (y >= getHeight() / 3 && y <= getHeight() / 3 + getHeight() / 3) {
117                 return true;
118             } else {
119                 return false;
120             }
121         }
122     }
123     /***
124      * {@inheritDoc} Simple default implementation.
125      */
126 	public Rectangle getPreferredDrawingBounds(Rectangle intervalDrawingArea,
127 			TimeBarViewerDelegate delegate, Interval interval,
128 			boolean selected, boolean overlap) {
129 		return intervalDrawingArea;
130 	}
131 
132 }