View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common 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 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common 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 CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.range2;
21  
22  import java.awt.CardLayout;
23  import java.awt.Color;
24  import java.awt.FontMetrics;
25  import java.awt.Graphics;
26  
27  import javax.swing.JComponent;
28  import javax.swing.JFrame;
29  
30  import com.cosylab.util.PrintfFormat;
31  
32  
33  /**
34   * Simple demonstration of how to use classes in range package.
35   *
36   * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a>
37   * @version $id$
38   */
39  public class SimpleDemoDisplayer extends JComponent implements TickParameters
40  {
41  	private static final long serialVersionUID = 1L;
42  
43  	private class Updater implements RangedValueListener
44  	{
45  		/**
46  		 * Called whenever any of the parameters change.
47  		 * 
48  		 * @see com.cosylab.gui.components.range.RangedValueListener#valueChanged(com.cosylab.gui.components.range.RangedValueEvent)
49  		 */
50  		public void valueChanged(RangedValueEvent event)
51  		{
52  			repaint();
53  		}
54  	}
55  
56  	private RangedValueController rv;
57  	private PrintfFormat pf = new PrintfFormat("%3.2f");
58  
59  	/**
60  	 * Creates a new SimpleDemoDisplayer object.
61  	 */
62  	public SimpleDemoDisplayer()
63  	{
64  		rv = new RangedValueController();
65  		rv.setRange(new LogarithmicRange());
66  		rv.setValue(0, 10, 2);
67  
68  		rv.addRangedValueListener(new Updater());
69  
70  		rv.setPolicy(new RescalingValuePolicy());
71  	}
72  
73  	/**
74  	 * Sets new minimum.
75  	 *
76  	 * @param min value.
77  	 */
78  	public void setMinimum(double min)
79  	{
80  		rv.setValue(min, rv.getMaximum(), rv.getValue());
81  	}
82  
83  	/**
84  	 * Sets new maximum.
85  	 *
86  	 * @param max value.
87  	 */
88  	public void setMaximum(double max)
89  	{
90  		rv.setValue(rv.getMinimum(), max, rv.getValue());
91  	}
92  
93  	/**
94  	 * Sets new value.
95  	 *
96  	 * @param val new value.
97  	 */
98  	public void setValue(double val)
99  	{
100 		rv.setValue(rv.getMinimum(), rv.getMaximum(), val);
101 	}
102 
103 	/**
104 	 * Returns minimum value displayed.
105 	 *
106 	 * @return double minimum value.
107 	 */
108 	public double getMinimum()
109 	{
110 		return rv.getMinimum();
111 	}
112 
113 	/**
114 	 * Returns maximum value displayed.
115 	 *
116 	 * @return double maximum value.
117 	 */
118 	public double getMaximum()
119 	{
120 		return rv.getMaximum();
121 	}
122 
123 	/**
124 	 * Returns current value displayed.
125 	 *
126 	 * @return double current value.
127 	 */
128 	public double getValue()
129 	{
130 		return rv.getValue();
131 	}
132 
133 	/**
134 	 * Code that renders the component. Paints background, ticks and value.
135 	 *
136 	 * @param g graphics.
137 	 */
138 	public void paintComponent(Graphics g)
139 	{
140 		super.paintComponent(g);
141 
142 		g.setColor(Color.BLACK);
143 		g.fillRect(0, 0, getWidth(), getHeight());
144 
145 		g.setColor(Color.WHITE);
146 		g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
147 
148 		Tick[] ticks = rv.calculateTicks(getWidth(),this);
149 
150 		for (int i = 0; i < ticks.length; i++) {
151 			Tick t = ticks[i];
152 			if (t==null) continue;
153 			double x = t.proportional * getWidth();
154 
155 			if (t.major) {
156 				g.drawString(t.text,
157 				    (int)x - g.getFontMetrics().stringWidth(t.text) / 2,
158 				    getHeight() / 4 - 2);
159 				g.drawLine((int)x, getHeight(), (int)x, getHeight() / 4);
160 			} else {
161 				g.drawLine((int)x, getHeight(), (int)x, getHeight() / 2);
162 			}
163 		}
164 
165 		g.setColor(Color.RED);
166 
167 		double x = rv.getRelativeValue() * getWidth();
168 		g.drawLine((int)x, getHeight(), (int)x, 0);
169 	}	
170 
171 	/*
172 	 * (non-Javadoc)
173 	 * @see com.cosylab.gui.components.range2.TickParameters#measureTick(double, java.lang.String)
174 	 */
175 	public int measureTick(double position, String text)
176 	{
177 		FontMetrics fm = getFontMetrics(getFont());
178 
179 		return fm.stringWidth(text);
180 	}
181 	
182 	/*
183 	 * (non-Javadoc)
184 	 * @see com.cosylab.gui.components.range2.TickParameters#formatNumber(double)
185 	 */
186 	public String formatNumber(double value) {
187 		return pf.sprintf(value);
188 	}
189 	
190 	/**
191 	 * Test method.
192 	 *
193 	 * @param args command-line arguments.
194 	 */
195 	public static void main(String[] args)
196 	{
197 		JFrame jf = new JFrame();
198 		jf.setSize(200, 80);
199 		jf.getContentPane().setLayout(new CardLayout());
200 
201 		SimpleDemoDisplayer sdd = new SimpleDemoDisplayer();
202 		sdd.setMinimum(0.2);
203 
204 		//sdd.setMaximum(90.312524);
205 		sdd.setMaximum(2);
206 		jf.getContentPane().add(sdd, "SimpleDemoDisplayer");
207 
208 		jf.setVisible(true);
209 
210 		while (true) {
211 			sdd.setValue(Math.random() * 9);
212 
213 			try {
214 				Thread.sleep(100);
215 			} catch (InterruptedException e) {
216 			}
217 		}
218 	}
219 }
220 
221 /* __oOo__ */