/Users/lyon/j4p/src/gui/run/ObservableSliderBank.java

1    // Glenn Josefiak 
2    // Fairfield University 
3    // SW513 
4    // Spring 2003 
5     
6    package gui.run; 
7     
8    import javax.swing.*; 
9    import java.awt.*; 
10   import java.util.Observer; 
11    
12   /** 
13    * A GUI panel that contains a variable number of identical sliders. 
14    */ 
15   public class ObservableSliderBank extends JPanel { 
16       private ObservableSlider[] sliders; 
17    
18       /** 
19        * Construct a new ObservableSliderBank containing the specified number 
20        * of sliders. 
21        * 
22        * @param nSliders Number of sliders 
23        */ 
24       public ObservableSliderBank(int nSliders) { 
25           //setLayout(new GridLayout(1, 0, 10, 6)); 
26           setLayout(new GridLayout(1,0)); 
27           //setBorder(new EmptyBorder(10, 10, 10, 10)); 
28    
29           sliders = new ObservableSlider[nSliders]; 
30    
31           for (int i = 0; i < nSliders; i++) { 
32               sliders[i] = new ObservableSlider(); 
33               add(sliders[i]); 
34           } 
35       } 
36    
37       /** 
38        * Set the label which will be displayed next to a particular 
39        * slider in the ObservableSliderBank. 
40        * 
41        * @param i     Index of the slider 
42        * @param label Text of the label 
43        */ 
44       public void setSliderLabel(int i, String label) { 
45           sliders[i].setLabel(label); 
46       } 
47    
48       /** 
49        * Set the min and max values of a particular slider 
50        * 
51        * @param i   Index of the slider 
52        * @param min Minimum value 
53        * @param max Maximum value 
54        */ 
55       public void setSliderRange(int i, float min, float max) { 
56           sliders[i].setMinimum(min); 
57           sliders[i].setMaximum(max); 
58       } 
59    
60       /** 
61        * Set a slider as linear or log scale. 
62        * 
63        * @param i     Index of the slider 
64        * @param state True = log; false = linear 
65        */ 
66       public void setSliderLogScale(int i, boolean state) { 
67           sliders[i].setLogScale(state); 
68       } 
69    
70       /** 
71        * Set a tick label which will be displayed on a particular 
72        * slider in the ObservableSliderBank. 
73        * 
74        * @param i     Index of the slider 
75        * @param tick  Position of the label 
76        * @param label Text of the label 
77        */ 
78       public void setTickLabel(int i, float tick, String label) { 
79           sliders[i].setTickLabel(tick, label); 
80       } 
81    
82       /** 
83        * Retrieve an array of values representing the position of 
84        * all the sliders. 
85        * 
86        * @return Array of slider values 
87        */ 
88       public float[] getValues() { 
89           float j[] = new float[sliders.length]; 
90           for (int i = 0; i < sliders.length; i++) { 
91               j[i] = sliders[i].getValue(); 
92           } 
93           return j; 
94       } 
95    
96       /** 
97        * Set the positions of all the sliders 
98        * 
99        * @param values Array of slider values 
100       */ 
101      public void setValues(float values[]) { 
102          for (int i = 0; i < sliders.length; i++) { 
103              sliders[i].setValue(values[i]); 
104          } 
105      } 
106   
107      /** 
108       * Allows other classes to listen for ActionEvents generated 
109       * by the ObservableSliderBank. 
110       * 
111       * @param o The class that wants to listen 
112       */ 
113      public void addObserver(Observer o) { 
114          for (int i = 0; i < sliders.length; i++) { 
115              sliders[i].addObserver(o); 
116          } 
117      } 
118  } 
119   
120