1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package p3j.gui.dialogs;
17
18 import java.awt.BorderLayout;
19 import java.awt.Frame;
20 import java.awt.event.ActionEvent;
21 import java.awt.event.ActionListener;
22 import java.util.List;
23
24 import javax.swing.DefaultComboBoxModel;
25 import javax.swing.DefaultListModel;
26 import javax.swing.JButton;
27 import javax.swing.JComboBox;
28 import javax.swing.JDialog;
29 import javax.swing.JLabel;
30 import javax.swing.JList;
31 import javax.swing.JPanel;
32 import javax.swing.JScrollPane;
33 import javax.swing.JTextArea;
34 import javax.swing.JTextField;
35
36 import p3j.gui.P3J;
37 import p3j.misc.Misc;
38 import p3j.misc.gui.GUI;
39 import p3j.misc.math.Matrix2D;
40 import p3j.pppm.ProjectionModel;
41 import p3j.pppm.parameters.ParameterAssignment;
42 import p3j.pppm.parameters.ParameterInstance;
43 import p3j.pppm.sets.Set;
44 import p3j.pppm.sets.SetType;
45
46
47
48
49
50
51
52
53
54
55
56 @Deprecated
57 public class QuickInputDialog extends JDialog {
58
59
60 private static final long serialVersionUID = 971779309134143674L;
61
62
63 private static final int DIALOG_HEIGHT = 480;
64
65
66 private static final int DIALOG_WIDTH = 640;
67
68
69 private static final int MATRIX_DESC_COL_SIZE = 15;
70
71
72 private static final int MATRIX_DESC_ROW_SIZE = 7;
73
74
75
76
77
78 private static final int MATRIX_PROB_FIELD_SIZE = 3;
79
80
81 private static final int MATRIX_NAME_FIELD_SIZE = 15;
82
83
84 private final ProjectionModel currentProjection;
85
86
87 private SetType currentSetType;
88
89
90 private ParameterAssignment currentParameterAssignment = new ParameterAssignment();
91
92
93
94
95 private JComboBox availableSetTypesCombo = new JComboBox();
96 {
97 availableSetTypesCombo.addActionListener(new ActionListener() {
98 @Override
99 public void actionPerformed(ActionEvent e) {
100 currentSetType = (SetType) availableSetTypesCombo.getSelectedItem();
101 refreshLists();
102 }
103 });
104 }
105
106
107 private JList parametersList = new JList(new DefaultListModel());
108
109
110 private JList setsList = new JList(new DefaultListModel());
111
112
113 private JTextField currentMatrixName = new JTextField(MATRIX_NAME_FIELD_SIZE);
114
115
116 private JTextField currentMatrixProb = new JTextField(MATRIX_PROB_FIELD_SIZE);
117
118
119 private JTextArea currentMatrixDesc = new JTextArea(MATRIX_DESC_ROW_SIZE,
120 MATRIX_DESC_COL_SIZE);
121
122
123 private JButton editCurrentMatrixButton = new JButton("Edit values");
124 {
125 editCurrentMatrixButton.addActionListener(new ActionListener() {
126 @Override
127 public void actionPerformed(ActionEvent e) {
128 editCurrentMatrix();
129 }
130 });
131 }
132
133
134 private JButton closeButton = new JButton("Close");
135 {
136 closeButton.addActionListener(new ActionListener() {
137 @Override
138 public void actionPerformed(ActionEvent e) {
139 setVisible(false);
140 }
141 });
142 }
143
144
145 private JButton copyButton = new JButton("Copy");
146 {
147 copyButton.addActionListener(new ActionListener() {
148 @Override
149 public void actionPerformed(ActionEvent e) {
150 copyParameterAssignment();
151 }
152 });
153 }
154
155
156
157
158
159
160
161
162
163 public QuickInputDialog(Frame owner, ProjectionModel projection) {
164 super(owner);
165 this.currentProjection = projection;
166 init();
167 resetUI();
168 }
169
170
171
172
173 private void init() {
174 this.setTitle("Input matrix for multiple parameters");
175 this.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
176 this.setModal(true);
177 GUI.centerOnScreen(this);
178 this.setContentPane(getContentPanel());
179 }
180
181
182
183
184
185
186 private JPanel getContentPanel() {
187 JPanel contentPanel = new JPanel(GUI.getStdBorderLayout());
188 contentPanel.add(getSelectSetTypePanel(), BorderLayout.NORTH);
189 contentPanel.add(getEditMatrixPanel(), BorderLayout.WEST);
190 contentPanel.add(getChooseTargetPanel(), BorderLayout.CENTER);
191 contentPanel.add(getControlPanel(), BorderLayout.SOUTH);
192 return contentPanel;
193 }
194
195
196
197
198
199
200 private JPanel getControlPanel() {
201 JPanel returnPanel = new JPanel();
202 returnPanel.add(copyButton);
203 return returnPanel;
204 }
205
206
207
208
209
210
211 private JPanel getChooseTargetPanel() {
212 JPanel returnPanel = new JPanel();
213
214 JPanel setListPanel = new JPanel();
215 setListPanel.add(new JLabel("Sets:"));
216 setListPanel.add(new JScrollPane(setsList));
217
218 JPanel parameterListPanel = new JPanel();
219 parameterListPanel.add(new JLabel("Parameters:"));
220 parameterListPanel.add(new JScrollPane(parametersList));
221
222 returnPanel.add(setListPanel);
223 returnPanel.add(parameterListPanel);
224
225 return returnPanel;
226 }
227
228
229
230
231
232
233 private JPanel getEditMatrixPanel() {
234 JPanel returnPanel = new JPanel();
235 returnPanel.setLayout(GUI.getStdBorderLayout());
236
237 JPanel editNamePanel = new JPanel();
238 editNamePanel.add(new JLabel(Misc.GUI_LABEL_NAME));
239 editNamePanel.add(this.currentMatrixName);
240 editNamePanel.add(this.editCurrentMatrixButton);
241
242 JPanel editProbPanel = new JPanel();
243 editProbPanel.add(new JLabel(Misc.GUI_LABEL_PROBABILITY));
244 editProbPanel.add(this.currentMatrixProb);
245
246 JPanel editDescPanel = new JPanel();
247 editDescPanel.add(new JLabel(Misc.GUI_LABEL_DESCRIPTION));
248 editDescPanel.add(this.currentMatrixDesc);
249
250 returnPanel.add(editNamePanel, BorderLayout.NORTH);
251 returnPanel.add(editDescPanel, BorderLayout.CENTER);
252 returnPanel.add(editProbPanel, BorderLayout.SOUTH);
253
254 return returnPanel;
255 }
256
257
258
259
260
261
262 private JPanel getSelectSetTypePanel() {
263 JPanel returnPanel = new JPanel();
264 returnPanel.add(new JLabel("Settype:"));
265 returnPanel.add(availableSetTypesCombo);
266 return returnPanel;
267 }
268
269 @Override
270 public void setVisible(boolean visible) {
271 resetUI();
272 super.setVisible(visible);
273 }
274
275
276
277
278 private void refreshLists() {
279
280 if (currentSetType == null) {
281 return;
282 }
283
284 GUI.replaceListContents((DefaultListModel) parametersList.getModel(),
285 currentSetType.getDefinedParameters());
286 GUI.replaceListContents((DefaultListModel) setsList.getModel(),
287 currentSetType.getSets());
288 }
289
290
291
292
293 private void resetUI() {
294
295 List<SetType> setTypes = currentProjection.getUserDefinedTypes();
296 availableSetTypesCombo
297 .setModel(new DefaultComboBoxModel(setTypes.toArray()));
298 currentSetType = setTypes.size() > 0 ? setTypes.get(0) : null;
299 refreshLists();
300
301
302 if (currentParameterAssignment.getMatrix() == null) {
303 currentParameterAssignment.setMatrixValue(new Matrix2D(currentProjection
304 .getMaximumAge() + 1, currentProjection.getYears()));
305 }
306
307 }
308
309
310
311
312 protected void editCurrentMatrix() {
313 EditMatrixDialog editMatrixDialog = new EditMatrixDialog(P3J.getInstance(),
314 currentParameterAssignment);
315 editMatrixDialog.setVisible(true);
316 }
317
318
319
320
321 protected void copyParameterAssignment() {
322
323 Object[] selectedSets = setsList.getSelectedValues();
324 Object[] selectedParameters = parametersList.getSelectedValues();
325
326 if (selectedSets.length == 0 || selectedParameters.length == 0) {
327 return;
328 }
329
330
331 String name = this.currentMatrixName.getText();
332 String desc = this.currentMatrixDesc.getText();
333 double prob = Misc.parseToDoubleProb(currentMatrixProb.getText());
334
335 for (int i = 0; i < selectedSets.length; i++) {
336 Set currSet = (Set) selectedSets[i];
337 for (int j = 0; j < selectedParameters.length; j++) {
338 ParameterInstance currParam = (ParameterInstance) selectedParameters[j];
339 ParameterAssignment newAssignment = new ParameterAssignment(currParam);
340 newAssignment.setName(name);
341 newAssignment.setDescription(desc);
342 newAssignment.setProbability(prob);
343 Matrix2D newValue = currParam.createEmptyValue();
344 Matrix2D.subMatrix(currentParameterAssignment.getMatrixValue(),
345 newValue);
346 newAssignment.setMatrixValue(newValue);
347 currSet.addParameterAssignment(newAssignment);
348 }
349 }
350
351 }
352 }