KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > monitorenter > gui > chart > layout > views > ChartCoordinateView


1 /*
2  * ViewChartValue.java of project jchart2d, a view that displays the data value
3  * of the point the mouse pointer currently is over the Chart2D component.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * If you modify or optimize the code in a useful way please let me know.
20  * Achim.Westermann@gmx.de
21  */

22 package info.monitorenter.gui.chart.layout.views;
23
24 import info.monitorenter.gui.chart.Chart2D;
25 import info.monitorenter.gui.chart.TracePoint2D;
26
27 import java.awt.GridBagConstraints JavaDoc;
28 import java.awt.GridBagLayout JavaDoc;
29 import java.awt.event.MouseEvent JavaDoc;
30 import java.awt.event.MouseMotionAdapter JavaDoc;
31 import java.awt.event.MouseMotionListener JavaDoc;
32
33 import javax.swing.Box JavaDoc;
34 import javax.swing.JLabel JavaDoc;
35 import javax.swing.JPanel JavaDoc;
36 import javax.swing.JTextField JavaDoc;
37
38
39 /**
40  * A view that displays the data value of the point the mouse pointer currently
41  * is over the Chart2D component within two {@link javax.swing.JTextField}
42  * instances.
43  * <p>
44  *
45  * @author <a HREF="mailto:Achim.Westermann@gmx.de">Achim Westermann</a>
46  * @version $Revision: 1.1 $
47  */

48 public class ChartCoordinateView extends JPanel JavaDoc {
49
50   /**
51    * Generated <code>serial vesion UID</code>.
52    */

53   private static final long serialVersionUID = 2547926983897553336L;
54
55   /** The chart to display the values from. */
56   private Chart2D m_chart2D;
57
58   /** The x value view. */
59   private JTextField JavaDoc m_xView;
60
61   /** The y value view. */
62   private JTextField JavaDoc m_yView;
63
64   /**
65    * Handle to the mouse listenert that is registered to the chart component.
66    * Needed to remove it from the chart when this component dies.
67    * <p>
68    */

69   private MouseMotionListener JavaDoc m_mouseListener;
70
71   /**
72    * Creates an component that will contain two text fields that display the
73    * chart value (x,y) when moving the mouse over the chart component.
74    * <p>
75    *
76    * @param chart
77    * the chart to listen to for mouse events.
78    */

79   public ChartCoordinateView(final Chart2D chart) {
80     this.m_chart2D = chart;
81     this.m_xView = new JTextField JavaDoc(10);
82     this.m_xView.setEditable(false);
83     this.m_yView = new JTextField JavaDoc(10);
84     this.m_yView.setEditable(false);
85
86     this.m_mouseListener = new MouseMotionAdapter JavaDoc() {
87       public void mouseMoved(final MouseEvent JavaDoc me) {
88         TracePoint2D value = ChartCoordinateView.this.m_chart2D.translateMousePosition(me);
89         if (value != null) {
90           ChartCoordinateView.this.m_xView.setText(ChartCoordinateView.this.m_chart2D.getAxisX()
91               .getFormatter().format(value.x));
92           ChartCoordinateView.this.m_yView.setText(ChartCoordinateView.this.m_chart2D.getAxisY()
93               .getFormatter().format(value.y));
94         }
95       }
96     };
97     this.m_chart2D.addMouseMotionListener(this.m_mouseListener);
98
99     // layout: GridBagLayout as I was unable to respect the preferred size
100
// of visible components and use box glue for expansion (I did this once and
101
// they say everything is possible with nexted BoxLayout but I forgot how
102
// and there is no use in things that don't stick in mind.
103
this.setLayout(new GridBagLayout JavaDoc());
104
105     // First "row"
106
GridBagConstraints JavaDoc gbc = new GridBagConstraints JavaDoc();
107     gbc.insets.left = 4;
108     gbc.insets.right = 4;
109     gbc.insets.top = 4;
110     gbc.insets.bottom = 4;
111     gbc.gridx = 0;
112     gbc.gridy = 0;
113     gbc.gridwidth = 1;
114     gbc.weightx = 0;
115     gbc.gridheight = 1;
116     gbc.weighty = 0;
117     this.add(new JLabel JavaDoc("X value:"), gbc);
118
119     gbc.gridx = 1;
120     gbc.weightx = 0.2;
121     this.add(Box.createHorizontalStrut(4));
122
123     gbc.gridx = 2;
124     gbc.weightx = 0;
125     this.add(this.m_xView, gbc);
126
127     // second "row"
128
gbc.gridx = 0;
129     gbc.gridy = 1;
130     gbc.gridwidth = 1;
131     this.add(new JLabel JavaDoc("Y value:"), gbc);
132
133     gbc.weightx = 0.2;
134     gbc.gridx = 1;
135     this.add(Box.createHorizontalStrut(4));
136
137     gbc.gridx = 2;
138     gbc.weightx = 0;
139     this.add(this.m_yView, gbc);
140
141   }
142
143   /**
144    * Removes the mouse motion listener from the chart.
145    * <p>
146    *
147    * @throws Throwable
148    * if sth. goes wrong in superclasses finalize.
149    * @see java.lang.Object#finalize()
150    */

151   public void finalize() throws Throwable JavaDoc {
152     super.finalize();
153     this.m_chart2D.removeMouseMotionListener(this.m_mouseListener);
154     this.m_chart2D = null;
155     this.m_xView = null;
156     this.m_yView = null;
157   }
158 }
159
Popular Tags