-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathChartBeanCustomizer.java
More file actions
239 lines (210 loc) · 6.61 KB
/
Copy pathChartBeanCustomizer.java
File metadata and controls
239 lines (210 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package chart2;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
/**
* A customizer for the chart bean that allows the user to edit all chart properties in a single
* tabbed dialog.
* @version 1.12 2007-10-03
* @author Cay Horstmann
*/
public class ChartBeanCustomizer extends JTabbedPane implements Customizer
{
private ChartBean bean;
private PropertyEditor colorEditor;
private JTextArea data;
private JRadioButton normal;
private JRadioButton inverse;
private JRadioButton[] position;
private JTextField titleField;
public ChartBeanCustomizer()
{
data = new JTextArea();
JPanel dataPane = new JPanel();
dataPane.setLayout(new BorderLayout());
dataPane.add(new JScrollPane(data), BorderLayout.CENTER);
JButton dataButton = new JButton("Set data");
dataButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
setData(data.getText());
}
});
JPanel panel = new JPanel();
panel.add(dataButton);
dataPane.add(panel, BorderLayout.SOUTH);
JPanel colorPane = new JPanel();
colorPane.setLayout(new BorderLayout());
normal = new JRadioButton("Normal", true);
inverse = new JRadioButton("Inverse", false);
panel = new JPanel();
panel.add(normal);
panel.add(inverse);
ButtonGroup group = new ButtonGroup();
group.add(normal);
group.add(inverse);
normal.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
setInverse(false);
}
});
inverse.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
setInverse(true);
}
});
colorEditor = PropertyEditorManager.findEditor(Color.class);
colorEditor.addPropertyChangeListener(new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent event)
{
setGraphColor((Color) colorEditor.getValue());
}
});
colorPane.add(panel, BorderLayout.NORTH);
colorPane.add(colorEditor.getCustomEditor(), BorderLayout.CENTER);
JPanel titlePane = new JPanel();
titlePane.setLayout(new BorderLayout());
group = new ButtonGroup();
position = new JRadioButton[3];
position[0] = new JRadioButton("Left");
position[1] = new JRadioButton("Center");
position[2] = new JRadioButton("Right");
panel = new JPanel();
for (int i = 0; i < position.length; i++)
{
final ChartBean.Position pos = ChartBean.Position.values()[i];
panel.add(position[i]);
group.add(position[i]);
position[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
setTitlePosition(pos);
}
});
}
titleField = new JTextField();
titleField.getDocument().addDocumentListener(new DocumentListener()
{
public void changedUpdate(DocumentEvent evt)
{
setTitle(titleField.getText());
}
public void insertUpdate(DocumentEvent evt)
{
setTitle(titleField.getText());
}
public void removeUpdate(DocumentEvent evt)
{
setTitle(titleField.getText());
}
});
titlePane.add(titleField, BorderLayout.NORTH);
JPanel panel2 = new JPanel();
panel2.add(panel);
titlePane.add(panel2, BorderLayout.CENTER);
addTab("Color", colorPane);
addTab("Title", titlePane);
addTab("Data", dataPane);
}
/**
* Sets the data to be shown in the chart.
* @param s a string containing the numbers to be displayed, separated by white space
*/
public void setData(String s)
{
StringTokenizer tokenizer = new StringTokenizer(s);
int i = 0;
double[] values = new double[tokenizer.countTokens()];
while (tokenizer.hasMoreTokens())
{
String token = tokenizer.nextToken();
try
{
values[i] = Double.parseDouble(token);
i++;
}
catch (NumberFormatException e)
{
}
}
setValues(values);
}
/**
* Sets the title of the chart.
* @param newValue the new title
*/
public void setTitle(String newValue)
{
if (bean == null) return;
String oldValue = bean.getTitle();
bean.setTitle(newValue);
firePropertyChange("title", oldValue, newValue);
}
/**
* Sets the title position of the chart.
* @param i the new title position (ChartBean.LEFT, ChartBean.CENTER, or ChartBean.RIGHT)
*/
public void setTitlePosition(ChartBean.Position pos)
{
if (bean == null) return;
ChartBean.Position oldValue = bean.getTitlePosition();
bean.setTitlePosition(pos);
firePropertyChange("titlePosition", oldValue, pos);
}
/**
* Sets the inverse setting of the chart.
* @param b true if graph and background color are inverted
*/
public void setInverse(boolean b)
{
if (bean == null) return;
boolean oldValue = bean.isInverse();
bean.setInverse(b);
firePropertyChange("inverse", oldValue, b);
}
/**
* Sets the values to be shown in the chart.
* @param newValue the new value array
*/
public void setValues(double[] newValue)
{
if (bean == null) return;
double[] oldValue = bean.getValues();
bean.setValues(newValue);
firePropertyChange("values", oldValue, newValue);
}
/**
* Sets the color of the chart
* @param newValue the new color
*/
public void setGraphColor(Color newValue)
{
if (bean == null) return;
Color oldValue = bean.getGraphColor();
bean.setGraphColor(newValue);
firePropertyChange("graphColor", oldValue, newValue);
}
public void setObject(Object obj)
{
bean = (ChartBean) obj;
data.setText("");
for (double value : bean.getValues())
data.append(value + "\n");
normal.setSelected(!bean.isInverse());
inverse.setSelected(bean.isInverse());
titleField.setText(bean.getTitle());
for (int i = 0; i < position.length; i++)
position[i].setSelected(i == bean.getTitlePosition().ordinal());
colorEditor.setValue(bean.getGraphColor());
}
}