View Javadoc

1   /*
2    *  File: PdiGridRenderer.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.pdi.swing;
21  
22  import java.awt.Color;
23  import java.awt.Graphics;
24  import java.util.Calendar;
25  
26  import javax.swing.JComponent;
27  
28  import de.jaret.examples.timebars.pdi.model.PdiCalendar;
29  import de.jaret.examples.timebars.pdi.model.PdiDay;
30  import de.jaret.util.date.JaretDate;
31  import de.jaret.util.ui.timebars.strategy.ITickProvider;
32  import de.jaret.util.ui.timebars.swing.TimeBarViewer;
33  import de.jaret.util.ui.timebars.swing.renderer.GridRenderer;
34  
35  /***
36   * @author Peter Kliem
37   * @version $Id: PdiGridRenderer.java 836 2009-02-14 21:24:39Z kliem $
38   */
39  public class PdiGridRenderer implements GridRenderer {
40      private Color MAJORGRIDCOLOR = new Color(200, 200, 200);
41      private Color SATURDAY_COLOR = new Color(255, 230, 230);
42      private Color SUNDAY_COLOR = new Color(255, 200, 200);
43  
44      protected PdiCalendar _kalender;
45      protected GridRenderer _component = new GridRenderer();
46  
47      public PdiGridRenderer(PdiCalendar kalender) {
48          _kalender = kalender;
49      }
50  
51      public JComponent getRendererComponent(TimeBarViewer tbv) {
52          _component.setTimeBarViewer(tbv);
53          return _component;
54      }
55  
56      class GridRenderer extends JComponent {
57          TimeBarViewer _tbv;
58  
59          public void setTimeBarViewer(TimeBarViewer tbv) {
60              _tbv = tbv;
61          }
62  
63          private int xForDate(JaretDate date) {
64              long seconds = date.diffSeconds(_tbv.getStartDate());
65              int x = (int) ((double) seconds * _tbv.getPixelPerSecond());
66              return x;
67          }
68  
69          private JaretDate dateForX(int x) {
70              int diffSec = (int) ((double) x / _tbv.getPixelPerSecond());
71              JaretDate date = new JaretDate(_tbv.getStartDate());
72              date.advanceSeconds(diffSec);
73              return date;
74          }
75  
76          public void paintComponent(Graphics g) {
77              JaretDate date = _tbv.getStartDate().copy();
78              date.setHours(0);
79              while (_tbv.getEndDate().compareTo(date) > 0) {
80                  PdiDay tag = _kalender.getTag(date);
81                  if (tag != null) {
82                      int x = xForDate(tag.getDate());
83                      int width = (int) (_tbv.getPixelPerSecond() * 24.0 * 60.0 * 60.0);
84                      g.setColor(MAJORGRIDCOLOR);
85                      g.drawLine(x, 0, x, getHeight());
86                      int dayOfWeek = date.getDayOfWeek();
87                      if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY) {
88                          g.setColor(dayOfWeek == Calendar.SATURDAY ? SATURDAY_COLOR : SUNDAY_COLOR);
89                          g.fillRect(x, 0, width, getHeight());
90                      }
91                  }
92                  date.advanceDays(1);
93              }
94          }
95      }
96  
97      /***
98       * {@inheritDoc} does nothing.
99       */
100     public void setTickProvider(ITickProvider tickProvider) {
101         // do nothing
102     }
103 }