-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathDoubleArrayEditorPanel.java
More file actions
148 lines (130 loc) · 4.36 KB
/
Copy pathDoubleArrayEditorPanel.java
File metadata and controls
148 lines (130 loc) · 4.36 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
package chart;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.event.*;
/**
* The panel inside the DoubleArrayEditor. It contains a list of the array values, together with
* buttons to resize the array and change the currently selected list value.
* @version 1.31 2012-06-10
* @author Cay Horstmann
*/
public class DoubleArrayEditorPanel extends JPanel
{
private PropertyEditorSupport editor;
private double[] array;
private JFormattedTextField sizeField = new JFormattedTextField(new Integer(0));
private JFormattedTextField valueField = new JFormattedTextField(new Double(0.0));
private JButton sizeButton = new JButton("Resize");
private JButton valueButton = new JButton("Change");
private JList<String> elementList = new JList<>();
private DoubleArrayListModel model = new DoubleArrayListModel();
public DoubleArrayEditorPanel(PropertyEditorSupport ed)
{
editor = ed;
setArray((double[]) ed.getValue());
setLayout(new GridBagLayout());
add(sizeField, new GBC(0, 0, 1, 1).setWeight(100, 0).setFill(GBC.HORIZONTAL));
add(valueField, new GBC(0, 1, 1, 1).setWeight(100, 0).setFill(GBC.HORIZONTAL));
add(sizeButton, new GBC(1, 0, 1, 1).setWeight(100, 0));
add(valueButton, new GBC(1, 1, 1, 1).setWeight(100, 0));
add(new JScrollPane(elementList), new GBC(0, 2, 2, 1).setWeight(100, 100).setFill(GBC.BOTH));
ActionListener listener = EventHandler.create(ActionListener.class, this, "changeSize");
sizeButton.addActionListener(listener);
sizeField.addActionListener(listener);
listener = EventHandler.create(ActionListener.class, this, "changeValue");
valueButton.addActionListener(listener);
valueField.addActionListener(listener);
elementList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
elementList.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent event)
{
int i = elementList.getSelectedIndex();
if (i < 0) return;
valueField.setValue(array[i]);
}
});
elementList.setModel(model);
elementList.setSelectedIndex(0);
}
/**
* This method is called when the user wants to change the size of the array.
*/
public void changeSize()
{
int s = (Integer) sizeField.getValue();
if (s < 0 || s == array.length) return;
setArray(Arrays.copyOf(array, s));
editor.setValue(array);
}
/**
* This method is called when the user wants to change the currently selected array value.
*/
public void changeValue()
{
double v = (Double) valueField.getValue();
int currentIndex = elementList.getSelectedIndex();
if (0 <= currentIndex && currentIndex < array.length)
{
model.setValue(currentIndex, v);
elementList.setSelectedIndex(currentIndex);
}
editor.firePropertyChange();
}
/**
* Sets the indexed array property.
* @param v the array to edit
*/
private void setArray(double[] v)
{
array = v;
model.setArray(array);
sizeField.setValue(array.length);
if (array.length > 0)
{
valueField.setValue(array[0]);
elementList.setSelectedIndex(0);
}
else valueField.setValue(0.0);
}
}
/**
* The list model for the element list in the editor.
*/
class DoubleArrayListModel extends AbstractListModel<String>
{
private double[] array;
public int getSize()
{
return array.length;
}
public String getElementAt(int i)
{
return "[" + i + "] " + array[i];
}
/**
* Sets a new array to be displayed in the list.
* @param a the new array
*/
public void setArray(double[] a)
{
int oldLength = array == null ? 0 : array.length;
if (oldLength > 0) fireIntervalRemoved(this, 0, oldLength);
array = a;
int newLength = array == null ? 0 : array.length;
if (newLength > 0) fireIntervalAdded(this, 0, newLength);
}
/**
* Changes a value in the array to be displayed in the list.
* @param i the index whose value to change
* @param value the new value for the given index
*/
public void setValue(int i, double value)
{
array[i] = value;
fireContentsChanged(this, i, i);
}
}