001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * -----------------
028 * JThermometer.java
029 * -----------------
030 * A plot that displays a single value in a thermometer type display.
031 *
032 * (C) Copyright 2000-2008, Australian Antarctic Division and Contributors.
033 *
034 * Original Author:  Bryan Scott.
035 * Contributor(s):   David Gilbert (for Object Refinery Limited);
036 *                   Irv Thomae;
037 *
038 * Changes (from 17-Sep-2002)
039 * --------------------------
040 * 17-Sep-2002 : Reviewed with Checkstyle utility (DG);
041 * 18-Sep-2003 : Integrated new methods contributed by Irv Thomae (DG);
042 * 08-Jan-2004 : Renamed AbstractTitle --> Title and moved to new package (DG);
043 * 31-May-2005 : Fixed typo in method name (DG);
044 *
045 */
046
047package org.jfree.chart.plot;
048
049import java.awt.CardLayout;
050import java.awt.Color;
051import java.awt.Font;
052import java.awt.Paint;
053import java.io.Serializable;
054import java.text.DecimalFormat;
055
056import javax.swing.JPanel;
057
058import org.jfree.chart.ChartPanel;
059import org.jfree.chart.JFreeChart;
060import org.jfree.chart.axis.ValueAxis;
061import org.jfree.chart.title.TextTitle;
062import org.jfree.chart.title.Title;
063import org.jfree.data.general.DefaultValueDataset;
064import org.jfree.ui.RectangleInsets;
065
066/**
067 * An initial quick and dirty.  The concept behind this class would be to
068 * generate a gui bean that could be used within JBuilder, Netbeans etc...
069 */
070public class JThermometer extends JPanel implements Serializable {
071
072    /** For serialization. */
073    private static final long serialVersionUID = 1079905665515589820L;
074
075    /** The dataset. */
076    private DefaultValueDataset data;
077
078    /** The chart. */
079    private JFreeChart chart;
080
081    /** The chart panel. */
082    private ChartPanel panel;
083
084    /** The thermometer plot. */
085    private ThermometerPlot plot = new ThermometerPlot();
086
087    /**
088     * Default constructor.
089     */
090    public JThermometer() {
091        super(new CardLayout());
092        this.plot.setInsets(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
093        this.data = new DefaultValueDataset();
094        this.plot.setDataset(this.data);
095        this.chart = new JFreeChart(null, JFreeChart.DEFAULT_TITLE_FONT,
096                this.plot, false);
097        this.panel = new ChartPanel(this.chart);
098        add(this.panel, "Panel");
099        setBackground(getBackground());
100    }
101
102    /**
103     * Adds a subtitle to the chart.
104     *
105     * @param subtitle  the subtitle.
106     */
107    public void addSubtitle(Title subtitle) {
108        this.chart.addSubtitle(subtitle);
109    }
110
111    /**
112     * Adds a subtitle to the chart.
113     *
114     * @param subtitle  the subtitle.
115     */
116    public void addSubtitle(String subtitle) {
117        this.chart.addSubtitle(new TextTitle(subtitle));
118    }
119
120    /**
121     * Adds a subtitle to the chart.
122     *
123     * @param subtitle  the subtitle.
124     * @param font  the subtitle font.
125     */
126    public void addSubtitle(String subtitle, Font font) {
127        this.chart.addSubtitle(new TextTitle(subtitle, font));
128    }
129
130    /**
131     * Sets the value format for the thermometer.
132     *
133     * @param df  the formatter.
134     */
135    public void setValueFormat(DecimalFormat df) {
136        this.plot.setValueFormat(df);
137    }
138
139    /**
140     * Sets the lower and upper bounds for the thermometer.
141     *
142     * @param lower  the lower bound.
143     * @param upper  the upper bound.
144     */
145    public void setRange(double lower, double upper) {
146        this.plot.setRange(lower, upper);
147    }
148
149    /**
150     * Sets the range.
151     *
152     * @param range  the range type.
153     * @param displayLow  the low value.
154     * @param displayHigh  the high value.
155     */
156    public void setSubrangeInfo(int range, double displayLow,
157                                double displayHigh) {
158        this.plot.setSubrangeInfo(range, displayLow, displayHigh);
159    }
160
161    /**
162     * Sets the range.
163     *
164     * @param range  the range type.
165     * @param rangeLow  the low value for the range.
166     * @param rangeHigh  the high value for the range.
167     * @param displayLow  the low value for display.
168     * @param displayHigh  the high value for display.
169     */
170    public void setSubrangeInfo(int range,
171                             double rangeLow, double rangeHigh,
172                             double displayLow, double displayHigh) {
173
174        this.plot.setSubrangeInfo(range, rangeLow, rangeHigh, displayLow,
175                displayHigh);
176
177    }
178
179    /**
180     * Sets the location at which the temperature value is displayed.
181     *
182     * @param loc  the location.
183     */
184    public void setValueLocation(int loc) {
185        this.plot.setValueLocation(loc);
186        this.panel.repaint();
187    }
188
189    /**
190     * Sets the value paint.
191     *
192     * @param paint  the paint.
193     */
194    public void setValuePaint(Paint paint) {
195        this.plot.setValuePaint(paint);
196    }
197
198    /**
199     * Returns the value of the thermometer.
200     *
201     * @return The value.
202     */
203    public Number getValue() {
204        if (this.data != null) {
205            return this.data.getValue();
206        }
207        else {
208            return null;
209        }
210    }
211
212    /**
213     * Sets the value of the thermometer.
214     *
215     * @param value  the value.
216     */
217    public void setValue(double value) {
218        setValue(new Double(value));
219    }
220
221    /**
222     * Sets the value of the thermometer.
223     *
224     * @param value  the value.
225     */
226    public void setValue(Number value) {
227        if (this.data != null) {
228            this.data.setValue(value);
229        }
230    }
231
232    /**
233     * Sets the unit type.
234     *
235     * @param i  the unit type.
236     */
237    public void setUnits(int i) {
238        if (this.plot != null) {
239            this.plot.setUnits(i);
240        }
241    }
242
243    /**
244     * Sets the outline paint.
245     *
246     * @param p  the paint.
247     */
248    public void setOutlinePaint(Paint p) {
249        if (this.plot != null) {
250            this.plot.setOutlinePaint(p);
251        }
252    }
253
254    /**
255     * Sets the foreground color.
256     *
257     * @param fg  the foreground color.
258     */
259    @Override
260    public void setForeground(Color fg) {
261        super.setForeground(fg);
262        if (this.plot != null) {
263            this.plot.setThermometerPaint(fg);
264        }
265    }
266
267    /**
268     * Sets the background color.
269     *
270     * @param bg  the background color.
271     */
272    @Override
273    public void setBackground(Color bg) {
274        super.setBackground(bg);
275        if (this.plot != null) {
276            this.plot.setBackgroundPaint(bg);
277        }
278        if (this.chart != null) {
279            this.chart.setBackgroundPaint(bg);
280        }
281        if (this.panel != null) {
282            this.panel.setBackground(bg);
283        }
284    }
285
286    /**
287     * Sets the value font.
288     *
289     * @param f  the font.
290     */
291    public void setValueFont(Font f) {
292        if (this.plot != null) {
293            this.plot.setValueFont(f);
294        }
295    }
296
297    /**
298     * Returns the tick label font.
299     *
300     * @return The tick label font.
301     */
302    public Font getTickLabelFont() {
303        ValueAxis axis = this.plot.getRangeAxis();
304        return axis.getTickLabelFont();
305    }
306
307    /**
308     * Sets the tick label font.
309     *
310     * @param font  the font.
311     */
312    public void setTickLabelFont(Font font) {
313        ValueAxis axis = this.plot.getRangeAxis();
314        axis.setTickLabelFont(font);
315    }
316
317    /**
318     * Increases or decreases the tick font size.
319     *
320     * @param delta  the change in size.
321     */
322    public void changeTickFontSize(int delta) {
323        Font f = getTickLabelFont();
324        String fName = f.getFontName();
325        Font newFont = new Font(fName, f.getStyle(), (f.getSize() + delta));
326        setTickLabelFont(newFont);
327    }
328
329    /**
330     * Sets the tick font style.
331     *
332     * @param style  the style.
333     */
334    public void setTickFontStyle(int style) {
335        Font f = getTickLabelFont();
336        String fName = f.getFontName();
337        Font newFont = new Font(fName, style, f.getSize());
338        setTickLabelFont(newFont);
339    }
340
341    /**
342     * Sets the flag that controls whether or not the display range follows the
343     * data value.
344     *
345     * @param flag  the new value of the flag.
346     */
347    public void setFollowDataInSubranges(boolean flag) {
348        this.plot.setFollowDataInSubranges(flag);
349    }
350
351    /**
352     * Sets the flag that controls whether or not value lines are displayed.
353     *
354     * @param b  the new flag value.
355     */
356    public void setShowValueLines(boolean b) {
357        this.plot.setShowValueLines(b);
358    }
359
360    /**
361     * Sets the location for the axis.
362     *
363     * @param location  the location.
364     */
365    public void setShowAxisLocation(int location) {
366        this.plot.setAxisLocation(location);
367    }
368
369    /**
370     * Returns the location for the axis.
371     *
372     * @return The location.
373     */
374    public int getShowAxisLocation() {
375      return this.plot.getAxisLocation();
376    }
377
378}