1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.components;
21
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24 import java.beans.Customizer;
25 import java.beans.PropertyChangeEvent;
26 import java.beans.PropertyChangeListener;
27
28 import javax.swing.BoxLayout;
29 import javax.swing.ButtonGroup;
30 import javax.swing.ButtonModel;
31 import javax.swing.JPanel;
32 import javax.swing.JRadioButton;
33
34 import com.cosylab.gui.components.range2.RangedValuePolicy;
35 import com.cosylab.gui.components.range2.RescalingValuePolicy;
36 import com.cosylab.gui.components.range2.ShiftValuePolicy;
37 import com.cosylab.gui.components.range2.TrimValuePolicy;
38
39
40
41
42
43
44
45
46
47 public class ValuePolicyCustomizer extends JPanel implements Customizer
48 {
49 private static final long serialVersionUID = 1L;
50 private ButtonGroup bg;
51 private DialKnob displayer;
52 private JRadioButton rescalingButton;
53 private JRadioButton shiftButton;
54 private JRadioButton trimButton;
55
56 private RangedValuePolicy policy;
57
58
59
60 public ValuePolicyCustomizer()
61 {
62 bg = new ButtonGroup();
63
64 trimButton = new JRadioButton("Trim Value Range");
65 rescalingButton = new JRadioButton("Rescale Value Range");
66 shiftButton = new JRadioButton("Shift Value Range");
67
68 bg.add(trimButton);
69 bg.add(rescalingButton);
70 bg.add(shiftButton);
71
72 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
73 add(trimButton);
74 add(rescalingButton);
75 add(shiftButton);
76
77 ActionListener l= new ActionListener() {
78
79 public void actionPerformed(ActionEvent e) {
80 applySettings();
81 }
82 };
83
84 trimButton.addActionListener(l);
85 rescalingButton.addActionListener(l);
86 shiftButton.addActionListener(l);
87
88 }
89
90
91
92
93 public void setObject(Object bean) {
94 displayer= (DialKnob)bean;
95 displayer.addPropertyChangeListener("valuePolicy",new PropertyChangeListener() {
96 public void propertyChange(PropertyChangeEvent evt) {
97 if (!"valuePolicy".equals(evt.getPropertyName())) {
98 return;
99 }
100 setValuePolicy(displayer.getValuePolicy());
101 }
102
103 });
104 setValuePolicy(displayer.getValuePolicy());
105 }
106
107
108
109
110
111
112
113
114 public void applySettings()
115 {
116 ButtonModel bm = bg.getSelection();
117
118 if (bm == null) {
119 return;
120 }
121 RangedValuePolicy policy = null;
122 if (bm == shiftButton.getModel()) {
123 policy = new ShiftValuePolicy();
124 } else if (bm == rescalingButton.getModel()) {
125 policy = new RescalingValuePolicy();
126 } else if (bm == trimButton.getModel()) {
127 policy = new TrimValuePolicy();
128 } else {
129 throw new AssertionError();
130 }
131
132 displayer.setValuePolicy(policy);
133
134 firePropertyChange("valuePolicy", null, policy);
135
136 }
137
138
139
140
141
142
143 public void setValuePolicy(RangedValuePolicy policy) {
144 if (displayer != null) {
145 if (policy instanceof RescalingValuePolicy && !rescalingButton.isSelected()) {
146 rescalingButton.setSelected(true);
147 } else if (policy instanceof TrimValuePolicy && !trimButton.isSelected()) {
148 trimButton.setSelected(true);
149 } else if (policy instanceof ShiftValuePolicy && !shiftButton.isSelected()) {
150 shiftButton.setSelected(true);
151 }
152 }
153 this.policy = policy;
154 firePropertyChange("valuePolicy", null, policy);
155 }
156
157
158
159
160
161
162 public RangedValuePolicy getValuePolicy() {
163 return policy;
164 }
165 }