View Javadoc

1   /*
2    *  File: TourenElementRenderer.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.touren;
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.swing.GraphicsHelper;
30  import de.jaret.util.ui.timebars.TimeBarViewerDelegate;
31  import de.jaret.util.ui.timebars.swing.TimeBarViewer;
32  import de.jaret.util.ui.timebars.swing.renderer.TimeBarRenderer;
33  
34  /***
35   * @author Peter Kliem
36   * @version $Id: TourenElementRenderer.java 869 2009-07-07 19:32:45Z kliem $
37   */
38  public class TourenElementRenderer implements TimeBarRenderer {
39      RendererComponent _component;
40  
41      public TourenElementRenderer() {
42          _component = new RendererComponent();
43      }
44  
45      public JComponent getTimeBarRendererComponent(TimeBarViewer tbv, Interval value, boolean isSelected, boolean overlapping) {
46          _component.setTourElement((TourElement) value);
47          _component.setSelected(isSelected);
48          return _component;
49      }
50  
51      class RendererComponent extends JComponent {
52          TourElement _te;
53          boolean _selected;
54  
55          public RendererComponent() {
56              setLayout(null);
57              setOpaque(false);
58          }
59  
60          public void setTourElement(TourElement te) {
61              _te = te;
62          }
63  
64          /*
65           * (non-Javadoc)
66           * 
67           * @see javax.swing.JComponent#getToolTipText()
68           */
69          public String getToolTipText() {
70              return "<html><b>" + _te.getLabel() + "</b><br/" + _te.getBegin().toDisplayString() + " - "
71                      + _te.getEnd().toDisplayString() + "<br/ " + _te.getBeginOrt() + " - " + _te.getEndeOrt()
72                      + "</html>";
73          }
74  
75          public void setSelected(boolean selected) {
76              _selected = selected;
77          }
78  
79          /*
80           * (non-Javadoc)
81           * 
82           * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
83           */
84          protected void paintComponent(Graphics g) {
85              super.paintComponent(g);
86              int height = getHeight();
87              int width = getWidth();
88  
89              int y = height / 3;
90              int bheight = height / 3;
91              int yEnd = y + bheight;
92  
93              // balken
94              if (_selected) {
95                  g.setColor(Color.BLUE);
96              } else {
97                  if (_te.getTyp() == 0) {
98                      g.setColor(Color.YELLOW);
99                  } else {
100                     g.setColor(Color.CYAN);
101                 }
102             }
103             g.fillRect(0, y, width - 1, height / 3);
104             // Rahmen
105             g.setColor(Color.BLACK);
106             g.drawRect(0, y, width - 1, height / 3);
107 
108             // Balkenbeschriftung
109             GraphicsHelper.drawStringCenteredVCenter(g, _te.getLabel(), 0, width, height / 2);
110 
111         }
112 
113         /*
114          * (non-Javadoc)
115          * 
116          * @see javax.swing.JComponent#contains(int, int)
117          */
118         public boolean contains(int x, int y) {
119             if (y >= getHeight() / 3 && y <= getHeight() / 3 + getHeight() / 3) {
120                 return true;
121             } else {
122                 return false;
123             }
124         }
125     }
126     /***
127      * {@inheritDoc} Simple default implementation.
128      */
129 	public Rectangle getPreferredDrawingBounds(Rectangle intervalDrawingArea,
130 			TimeBarViewerDelegate delegate, Interval interval,
131 			boolean selected, boolean overlap) {
132 		return intervalDrawingArea;
133 	}
134 
135 }