View Javadoc

1   /*
2    *  File: UmlaufRenderer.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.Umlauf;
29  import de.jaret.util.date.Interval;
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: UmlaufRenderer.java 869 2009-07-07 19:32:45Z kliem $
37   */
38  public class UmlaufRenderer implements TimeBarRenderer {
39      UmlaufRendererComponent _umlaufComponent;
40  
41      public UmlaufRenderer() {
42          _umlaufComponent = new UmlaufRendererComponent();
43      }
44  
45      /***
46       * {@inheritDoc}
47       */
48      public JComponent getTimeBarRendererComponent(TimeBarViewer tbv, Interval value, boolean isSelected, boolean overlapping) {
49          if (value instanceof Umlauf) {
50              _umlaufComponent.setUmlauf((Umlauf) value);
51              _umlaufComponent.setSelected(isSelected);
52              return _umlaufComponent;
53          } else {
54              throw new RuntimeException("unsupported "+value.getClass().getName());
55          }
56      }
57  
58      /***
59       * The rendering jcomponent for an umlauf.
60       * 
61       * @author kliem
62       * @version $Id: UmlaufRenderer.java 869 2009-07-07 19:32:45Z kliem $
63       */
64      public class UmlaufRendererComponent extends JComponent {
65          Umlauf _umlauf;
66          boolean _selected;
67  
68          public UmlaufRendererComponent() {
69              setLayout(null);
70              setOpaque(false);
71          }
72  
73          public void setUmlauf(Umlauf umlauf) {
74              _umlauf = umlauf;
75          }
76  
77          public String getToolTipText() {
78              return "<html><b>" + _umlauf.getUmlaufbezeichnug() + "</b><br/" + _umlauf.getBegin().toDisplayString()
79                      + " - " + _umlauf.getEnd().toDisplayString() + "<br/ " + _umlauf.getFahrten().size()
80                      + " Fahrten</html>";
81          }
82  
83          public void setSelected(boolean selected) {
84              _selected = selected;
85          }
86  
87          protected void paintComponent(Graphics g) {
88              super.paintComponent(g);
89              int height = getHeight();
90              int width = getWidth();
91  
92              // balken
93              int y = height / 5;
94              if (_selected) {
95                  g.setColor(Color.BLUE);
96              } else {
97                  g.setColor(Color.LIGHT_GRAY);
98              }
99              g.fillRect(0, y, width - 1, height - 2 * height / 5);
100             g.setColor(Color.BLACK);
101 
102         }
103 
104         public boolean contains(int x, int y) {
105             if (y >= getHeight() / 5 && y <= getHeight() / 5 + getHeight() - 2 * getHeight() / 5) {
106                 return true;
107             } else {
108                 return false;
109             }
110         }
111     }
112     /***
113      * {@inheritDoc} Simple default implementation.
114      */
115 	public Rectangle getPreferredDrawingBounds(Rectangle intervalDrawingArea,
116 			TimeBarViewerDelegate delegate, Interval interval,
117 			boolean selected, boolean overlap) {
118 		return intervalDrawingArea;
119 	}
120 
121 }