View Javadoc

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