KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > instruments > DummyPositionControl


1 package JSci.instruments;
2
3 import JSci.swing.*;
4
5 import java.awt.*;
6 import java.awt.event.*;
7 import javax.swing.*;
8 import javax.swing.event.*;
9
10 /** A simple PositionControl that does nothing!
11  */

12 public class DummyPositionControl extends PositionControlAdapter {
13
14     private double position = Double.NaN;
15
16     public void setPosition(double p) {
17     if (p<getMinimum() || p>getMaximum() || position==p ) return;
18     position=p;
19     doSetPosition(p);
20     fireStateChanged();
21     }
22     
23     protected void doSetPosition(double p) {}
24
25     public double getPosition() {
26     if (Double.isNaN(position)) position=getActualPosition();
27     return position;
28     }
29
30     public double getActualPosition() {
31     if (Double.isNaN(position)) position=50.0;
32     return position;
33     }
34
35     public double getMinimum() {
36     return 0.0;
37     }
38
39     public double getMaximum() {
40     return 100.0;
41     }
42
43     public double getMinorStep() {
44     return 10.0;
45     }
46
47     public double getMajorStep() {
48     return 20.0;
49     }
50
51     public void sleep() {
52     }
53
54     private JComponent ccomp = null;
55     
56     private JComponent createControlPanel() {
57     JPanel panel;
58     final JPointer slider;
59     final JSpinner spinner;
60
61     JSliderPlus sliderHolder = new JSliderPlus(JSlider.VERTICAL,getMinimum(),getMaximum());
62     slider = new JPointer(JPointer.SLIDER_SIMPLE_TRIANGLE);
63     slider.setValue(getPosition());
64     slider.setColor(Color.MAGENTA);
65     sliderHolder.setMinorTickSpacing(getMinorStep());
66     sliderHolder.setMajorTickSpacing(getMajorStep());
67     sliderHolder.setPaintMinorTicks(true);
68     sliderHolder.setPaintMajorTicks(true);
69     sliderHolder.setPaintLabels(true);
70     sliderHolder.addJPointer(slider);
71
72     spinner = new JSpinner(new SpinnerNumberModel(getPosition(),getMinimum(),getMaximum(),getMinorStep()));
73     panel = new JPanel() {
74         public void setEnabled(boolean b) {
75             super.setEnabled(b);
76             slider.setEnabled(b);
77             spinner.setEnabled(b);
78         }
79         };
80     panel.setLayout(new BorderLayout());
81     panel.add("West",sliderHolder);
82     JPanel spinnerPanel = new JPanel();
83     spinnerPanel.add(spinner);
84     panel.add("East",spinnerPanel);
85
86     spinner.addChangeListener(new ChangeListener() {
87         public void stateChanged(ChangeEvent event) {
88             double p = ((Double JavaDoc)spinner.getValue()).doubleValue();
89             setPosition(p);
90         }
91         });
92     slider.addChangeListener(new ChangeListener() {
93         public void stateChanged(ChangeEvent event) {
94             double p = slider.getValue();
95             setPosition(p);
96         }
97         });
98     addChangeListener(new ChangeListener() {
99         public void stateChanged(ChangeEvent event) {
100             double p = getPosition();
101             spinner.setValue(new Double JavaDoc(p));
102             slider.setValue(p);
103         }
104         });
105
106     return panel;
107     }
108
109     public Component getControlComponent() {
110     if (ccomp==null) ccomp=createControlPanel();
111     return ccomp;
112     }
113
114     public String JavaDoc getUnit() {
115     return "unknown";
116     }
117
118     public static void main(String JavaDoc [] args) {
119     PositionControl pc = new DummyPositionControl() {
120         
121         };
122     JFrame frm = new JFrame("Testing the DummyPositionControl");
123     frm.getContentPane().add(pc.getControlComponent());
124     frm.pack();
125     frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
126     frm.show();
127     }
128
129 }
130
Popular Tags