KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > monitorenter > reflection > ObjRecorder2Trace2DAdapter


1 /*
2  * ObjectRecorder2Trace2DAdpater, an adapter which enables drawing timestamped
3  * values inspected by the ObjectRecorder on a Chart2D.
4  * Copyright (C) 2002 Achim Westermann, Achim.Westermann@gmx.de
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  *
20  * If you modify or optimize the code in a useful way please let me know.
21  * Achim.Westermann@gmx.de
22  */

23
24 package info.monitorenter.reflection;
25
26 import info.monitorenter.gui.chart.ITrace2D;
27 import info.monitorenter.util.TimeStampedValue;
28
29 import javax.swing.event.ChangeEvent JavaDoc;
30 import javax.swing.event.ChangeListener JavaDoc;
31
32
33 /**
34  * A simple adapter that allows displaying of timestamped values from an
35  * inspection of the <code>{@link info.monitorenter.reflection.ObjectRecorder}</code> on a
36  * Chart2D.
37  * <p>
38  *
39  * @author <a HREF='mailto:Achim.Westermann@gmx.de'>Achim Westermann </a>
40  *
41  * @version $Revision: 1.1 $
42  */

43 public class ObjRecorder2Trace2DAdapter implements ChangeListener JavaDoc {
44
45   /** The field name to inpsect. */
46   private String JavaDoc m_fieldname;
47
48   /** The source inspector to connect to the trace. */
49   private ObjectRecorder m_inspector;
50
51   /**
52    * The starting timestamp of this inspection that is used to put the timestamp
53    * into relation to the first inspection.
54    */

55   private long m_start = System.currentTimeMillis();
56
57   /** The target trace to use. */
58   private ITrace2D m_view;
59
60   /**
61    * Creates a bridge from the given field of the given instance to inspect to
62    * the trace.
63    * <p>
64    *
65    * @param view
66    * the target trace that will show the inspected value.
67    *
68    * @param toinspect
69    * the instance to inpsect.
70    *
71    * @param fieldname
72    * the field on the instance to inspect.
73    *
74    * @param interval
75    * the interval of inspections in ms.
76    */

77   public ObjRecorder2Trace2DAdapter(final ITrace2D view, final Object JavaDoc toinspect,
78       final String JavaDoc fieldname, final long interval) {
79     this.m_view = view;
80     this.m_fieldname = fieldname;
81     this.m_inspector = new ObjectRecorder(toinspect, interval);
82     this.m_inspector.addChangeListener(this);
83   }
84
85   /**
86    * Sets the interval for inspections in ms.
87    * <p>
88    *
89    * @param interval
90    * the interval for inspections in ms.
91    */

92   public void setInterval(final long interval) {
93     this.m_inspector.setInterval(interval);
94   }
95
96   /**
97    * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
98    */

99   public void stateChanged(final ChangeEvent JavaDoc e) {
100     TimeStampedValue last;
101     try {
102       last = this.m_inspector.getLastValue(this.m_fieldname);
103     } catch (Exception JavaDoc f) {
104       f.printStackTrace();
105       return;
106     }
107     if (last != null) {
108       double tmpx, tmpy;
109       tmpx = last.getTime() - this.m_start;
110       tmpy = Double.parseDouble(last.getValue().toString());
111       this.m_view.addPoint(tmpx, tmpy);
112     }
113   }
114 }
115
Popular Tags