001    /**
002     * planningtool - A Planning Tool with Critiquing Support.
003     * 
004     * Copyright (C) 2006 olale
005    
006     * This program is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU General Public License
008     * as published by the Free Software Foundation; either version 2
009     * of the License, or (at your option) any later version.
010    
011     * This program is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015    
016     * You should have received a copy of the GNU General Public License
017     * along with this program; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019    
020     * Contact information: 
021     * E-mail: olale@ida.liu.se
022     *         olale@lysator.liu.se
023     */
024    package se.liu.ida.critiquer.gui;
025    
026    import java.awt.Color;
027    import java.awt.Graphics2D;
028    import java.awt.Point;
029    import java.awt.geom.Rectangle2D;
030    import java.util.Calendar;
031    import java.util.GregorianCalendar;
032    
033    import org.jfree.chart.axis.CategoryAxis;
034    import org.jfree.chart.axis.DateAxis;
035    import org.jfree.chart.axis.DateTickUnit;
036    import org.jfree.chart.plot.CategoryPlot;
037    import org.jfree.chart.plot.PlotRenderingInfo;
038    import org.jfree.data.gantt.TaskSeriesCollection;
039    import org.jfree.ui.RectangleEdge;
040    
041    public class TimeLinePlot extends CategoryPlot {
042            
043            /**
044             * 
045             */
046            private static final long serialVersionUID = 1L;
047            private View view;
048            private DateAxis dateAxis;
049            private Rectangle2D dataArea;
050            /**
051             * currentTime is a coordinate along the x axis of the time line, which can be converted into a date by the locationToDate method.
052             */
053            private Double currentTime = Double.NaN;
054            
055            
056            public TimeLinePlot(TaskSeriesCollection tasks, CategoryAxis categoryAxis, DateAxis dateAxis, TimeLineRenderer renderer) {
057                    super(tasks, categoryAxis, dateAxis, renderer);
058                    this.dateAxis=dateAxis;
059            }
060    
061            /**
062             * Render the plot as usual, but also notify the visualization constraints
063             */
064            @Override
065            public boolean render(Graphics2D g2, Rectangle2D dataArea, int index, PlotRenderingInfo info) {
066                    boolean val = super.render(g2, dataArea, index, info);
067                    /**
068                     * If currentTime has some sensible value, draw the current time as a vertical line 
069                     */
070                    if (! currentTime.isNaN()) {
071                            Color oldColor = g2.getColor();
072                            g2.setColor(Color.RED);
073                            g2.drawLine(currentTime.intValue(), (int) dataArea.getY(), currentTime.intValue(), (int) dataArea.getMaxY());
074                            g2.setColor(oldColor);
075                    }
076                    
077                    this.dataArea = dataArea; 
078                    view.updateView(g2);
079                    return val;
080            }
081    
082    
083            public void setView(View view) {
084                    this.view=view;
085            }
086            /**
087             * Translate an x coordinate in the coordinate space of the chart panel to a date on the time line.
088             * 
089             * @param x the coordinate in the chart panel coordinate space.
090             * @return a Calendar object representing the date.
091             */
092            public Calendar locationToDate(double x) {
093                    Calendar cal = new GregorianCalendar();
094                    cal.setTimeInMillis((long) dateAxis.java2DToValue(x, dataArea, getRangeAxisEdge()));
095                    return cal;
096            }
097    
098            /**
099             * @return Returns the currentTime.
100             */
101            public Calendar getCurrentTime() {
102                    if (currentTime.isNaN()) {
103                            currentTime=dataArea.getX();
104                    }
105                    return locationToDate(currentTime);
106            }
107    
108            /**
109             * @param currentTime The currentTime to set.
110             */
111            public void setCurrentTime(Double currentTime) {
112                    this.currentTime = currentTime;
113            }
114    
115            /**
116             * @return Returns the dataArea.
117             */
118            public Rectangle2D getDataArea() {
119                    return dataArea;
120            }
121            
122    }