View Javadoc

1   /*
2    *  File: DefaultGridRenderer.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.util.ui.timebars.swing.renderer;
21  
22  import java.awt.Color;
23  import java.awt.Graphics;
24  
25  import javax.swing.JComponent;
26  
27  import de.jaret.util.date.DateUtils;
28  import de.jaret.util.date.JaretDate;
29  import de.jaret.util.ui.timebars.TickScaler;
30  import de.jaret.util.ui.timebars.TickScaler.Range;
31  import de.jaret.util.ui.timebars.TimeBarViewerInterface;
32  import de.jaret.util.ui.timebars.strategy.ITickProvider;
33  import de.jaret.util.ui.timebars.swing.TimeBarViewer;
34  
35  /**
36   * Simple default implementation of a GridRenderer rendering the major and minor ticks using two different colors.
37   * 
38   * @author Peter Kliem
39   * @version $Id: DefaultGridRenderer.java 1083 2011-07-01 20:29:16Z kliem $
40   */
41  public class DefaultGridRenderer implements GridRenderer {
42      /** default color for major grid lines. */
43      private static final Color MAJORGRIDCOLOR = new Color(200, 200, 200);
44      /** default color for minor grid lines. */
45      private static final Color MINORGRIDCOLOR = new Color(230, 230, 230);
46      /** approximate number of minutes in a month. */
47      private static final double APPROX_MONTH_MINUTES = (24.0 * 60.0 * 7.0 * 4.0);
48  
49      /** color used to paint the major grid. */
50      protected Color _majorGridColor = MAJORGRIDCOLOR;
51  
52  	/** color used to paint the minor grid. */
53      protected Color _minorGridColor = MINORGRIDCOLOR;
54      
55      
56      /** component used for painting. */
57      protected MyGridRenderer _component = new MyGridRenderer();
58  
59      /** external tick provider. */
60      protected ITickProvider _tickProvider = null;
61  
62      /**
63       * {@inheritDoc}
64       */
65      public JComponent getRendererComponent(TimeBarViewer tbv) {
66          _component.setTimeBarViewer(tbv);
67          return _component;
68      }
69  
70      /**
71       * Set a tick provider to determine the ticks to be drawn. If no tick provider is set the defaulst by the tick
72       * scaler will be used.
73       * 
74       * @param tickProvider tick provider to be used or <code>null</code>.
75       */
76      public void setTickProvider(ITickProvider tickProvider) {
77          _tickProvider = tickProvider;
78      }
79  
80      /**
81       * Retrieve the major grid color.
82       * @return the major grid color
83       */
84      public Color getMajorGridColor() {
85  		return _majorGridColor;
86  	}
87  
88      /**
89       * Set the major grid color.
90       * @param majorGridColor the Color to use for the major grid
91       */
92  	public void setMajorGridColor(Color majorGridColor) {
93  		_majorGridColor = majorGridColor;
94  	}
95  
96  	/**
97  	 * Retrieve the minor grid color.
98  	 * @return the minor grid color
99  	 */
100 	public Color getMinorGridColor() {
101 		return _minorGridColor;
102 	}
103 
104 	/**
105 	 * Set the minor grid color.
106 	 * @param minorGridColor the color to use for the minor grid
107 	 */
108 	public void setMinorGridColor(Color minorGridColor) {
109 		_minorGridColor = minorGridColor;
110 	}
111 
112     
113     /**
114      * JComponent for drawing the grid.
115      * 
116      * @author kliem
117      * @version $Id: DefaultGridRenderer.java 1083 2011-07-01 20:29:16Z kliem $
118      */
119     @SuppressWarnings("serial")
120     class MyGridRenderer extends JComponent {
121         /** the viewer. */
122         private TimeBarViewer _tbv;
123 
124         /**
125          * Set the viewer.
126          * 
127          * @param tbv the viewer
128          */
129         public void setTimeBarViewer(TimeBarViewer tbv) {
130             _tbv = tbv;
131         }
132 
133         /**
134          * Calculate the x coordinate for a given date.
135          * 
136          * @param date date in question
137          * @return x coord
138          */
139         private int xForDate(JaretDate date) {
140             int x = _tbv.xForDate(date);
141             x -= _tbv.getHierarchyWidth() + _tbv.getYAxisWidth();
142             return x;
143         }
144 
145         /**
146          * {@inheritDoc}
147          */
148         public void paintComponent(Graphics g) {
149 
150             boolean horizontal = _tbv.getTBOrientation() == TimeBarViewerInterface.Orientation.HORIZONTAL;
151 
152             if (_tickProvider == null) {
153                 paint(g, horizontal);
154             } else {
155                 paint(g, horizontal, _tickProvider);
156             }
157         }
158 
159         protected void paint(Graphics g, boolean horizontal, ITickProvider tickProvider) {
160             // draw minor ticks
161             Color color = g.getColor();
162             g.setColor(_minorGridColor);
163 
164             for (JaretDate tickDate : tickProvider.getMinorTicks(_tbv.getDelegate())) {
165 
166                 int coord = xForDate(tickDate);
167                 if (horizontal) {
168                     g.drawLine(coord, 0, coord, getHeight());
169                 } else {
170                     g.drawLine(0, coord, getWidth(), coord);
171                 }
172             }
173             // draw major ticks
174             g.setColor(_majorGridColor);
175 
176             for (JaretDate tickDate : tickProvider.getMajorTicks(_tbv.getDelegate())) {
177                 int coord = xForDate(tickDate);
178                 if (horizontal) {
179                     g.drawLine(coord, 0, coord, getHeight());
180                 } else {
181                     g.drawLine(0, coord, getWidth(), coord);
182                 }
183             }
184             g.setColor(color);
185         }
186 
187         protected void paint(Graphics g, boolean horizontal) {
188             Color color = g.getColor();
189             // first date
190             JaretDate date = _tbv.getStartDate().copy();
191 
192             int idx = TickScaler.getTickIdx(_tbv.getPixelPerSecond());
193             int majTick = TickScaler.getMajorTickMinutes(idx);
194             int minTick = TickScaler.getMinorTickMinutes(idx);
195             Range range = TickScaler.getRange(idx);
196 
197             // clean starting date on a major tick minute position (starting
198             // with a day)
199             date.setMinutes(0);
200             date.setHours(0);
201             date.setSeconds(0);
202             // if range is week take a week starting point
203             if (range == Range.WEEK) {
204                 while (date.getDayOfWeek() != DateUtils.getFirstDayOfWeek()) {
205                     date.backDays(1.0);
206                 }
207             } else if (range == Range.MONTH) {
208                 // month -> month starting point
209                 date.setDay(1);
210             }
211             JaretDate save = date.copy();
212 
213             date.backMinutes(majTick); // minor ticks should start before the
214             // major ticks
215 
216             int max = horizontal ? getWidth() : getHeight();
217 
218             // draw the minor grid
219             g.setColor(MINORGRIDCOLOR);
220             while (xForDate(date) < max) {
221                 int coord = xForDate(date);
222                 if (horizontal) {
223                     g.drawLine(coord, 0, coord, getHeight());
224                 } else {
225                     g.drawLine(0, coord, getWidth(), coord);
226                 }
227                 if (range == Range.MONTH) {
228                     int adv = (int) Math.round((double) minTick / APPROX_MONTH_MINUTES);
229                     if (adv == 0) {
230                         adv = 1;
231                     }
232                     date.advanceMonths(adv);
233                 } else {
234                     date.advanceMinutes(minTick);
235                 }
236             }
237 
238             date = save.copy();
239             // draw the major grid
240             g.setColor(MAJORGRIDCOLOR);
241             while (xForDate(date) < max) {
242                 int coord = xForDate(date);
243                 if (horizontal) {
244                     g.drawLine(coord, 0, coord, getHeight());
245                 } else {
246                     g.drawLine(0, coord, getWidth(), coord);
247                 }
248                 if (range == Range.MONTH) {
249                     int adv = (int) Math.round((double) majTick / APPROX_MONTH_MINUTES);
250                     if (adv == 0) {
251                         adv = 1;
252                     }
253                     date.advanceMonths(adv);
254                 } else {
255                     date.advanceMinutes(majTick);
256                 }
257             }
258             g.setColor(color);
259         }
260     }
261 
262 }