This repository has been archived by the owner on Sep 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CB_ConstraintPropertiesPanel.java
340 lines (327 loc) · 13.7 KB
/
CB_ConstraintPropertiesPanel.java
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
* Copyright © 2006-2008 Paul van Santen & Erik Kerkvliet,
*
* This file is part of ClassicalBuilder.
*
* ClassicalBuilder is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* ClassicalBuilder is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with ClassicalBuilder; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* http://www.gnu.org/licenses/gpl.txt
**/
package ClassicalBuilder;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
//this class creates a panel containing the properties of constraints and enables them to be changed
public class CB_ConstraintPropertiesPanel extends JPanel implements ActionListener {
private final CB_Main main;
private final JTextField nameTF;
private final JFormattedTextField distanceF, maxstepF, errorF;
private final JCheckBox distanceCB, maxstepCB, errorCB;
private final JComboBox constraintChooser;
private boolean systemChange = false;
public CB_ConstraintPropertiesPanel(CB_Main main) {
this.main = main;
this.setPreferredSize(new Dimension(150,550));
this.setLayout(new FlowLayout());
constraintChooser = new JComboBox(CB_Constraint.NAMES);
constraintChooser.setPreferredSize(new Dimension(130, 23));
constraintChooser.addActionListener(this);
nameTF = new JTextField("");
nameTF.addActionListener(this);
distanceF = CB_PropertiesPanel.createDoubleField(this);
maxstepF = CB_PropertiesPanel.createIntField(this);
errorF = CB_PropertiesPanel.createDoubleField(this);
distanceCB = createCheckBox();
maxstepCB = createCheckBox();
errorCB = createCheckBox();
JPanel namePanel = CB_PropertiesPanel.createPropertyPanel("Name", nameTF);
JPanel typePanel = CB_PropertiesPanel.createChooserPanel("Constraint Type", constraintChooser);
JPanel distancePanel = CB_PropertiesPanel.createOptionalPropertyPanel("Distance", distanceCB, distanceF);
JPanel maxstepPanel = CB_PropertiesPanel.createOptionalPropertyPanel("Maxstep", maxstepCB, maxstepF);
JPanel errorPanel = CB_PropertiesPanel.createOptionalPropertyPanel("Error Parameter", errorCB, errorF);
this.add(namePanel);
this.add(typePanel);
this.add(distancePanel);
this.add(maxstepPanel);
this.add(errorPanel);
}
public JCheckBox createCheckBox() {
JCheckBox checkBox = new JCheckBox();
checkBox.addActionListener(this);
return checkBox;
}
//enable needed textfields, considering the constraint type
public void setEnabled(int type) {
this.disableAll();
this.setEmpty();
if (type != -2) {
switch (type) {
case -1:
constraintChooser.setEnabled(true);
nameTF.setEnabled(true);
distanceF.setEnabled(true);
maxstepF.setEnabled(true);
errorF.setEnabled(true);
distanceCB.setEnabled(true);
maxstepCB.setEnabled(true);
errorCB.setEnabled(true);
break;
case CB_Constraint.DISTANCE:
constraintChooser.setEnabled(true);
nameTF.setEnabled(true);
distanceF.setEnabled(true);
maxstepF.setEnabled(true);
errorF.setEnabled(true);
distanceCB.setEnabled(true);
maxstepCB.setEnabled(true);
errorCB.setEnabled(true);
break;
}
}
}
//empty all the textfield values
public void setEmpty() {
systemChange = true;
constraintChooser.setSelectedIndex(-1);
nameTF.setText(null);
distanceF.setText(null);
maxstepF.setText(null);
errorF.setText(null);
distanceCB.setSelected(false);
maxstepCB.setSelected(false);
errorCB.setSelected(false);
systemChange = false;
}
//disable all the textfields
public void disableAll() {
constraintChooser.setEnabled(false);
nameTF.setEnabled(false);
distanceF.setEnabled(false);
maxstepF.setEnabled(false);
errorF.setEnabled(false);
distanceCB.setEnabled(false);
maxstepCB.setEnabled(false);
errorCB.setEnabled(false);
}
//update the selected interaction
public void updateSelection() {
if (main.getInstance().getSelectionType() == CB_Instance.CONSTRAINTS) {
if (main.getInstance().getSelectionSize() == 1) {
this.setEnabled(main.getConstraint(main.getInstance().getSelection(0)).getType());
this.updateProperties();
} else if (main.getInstance().getSelectionSize() > 1) {
this.setEnabled(-1);
this.setEmpty();
}
} else {
this.setEnabled(-2);
}
}
public void updateProperties() {
systemChange = true;
constraintChooser.setSelectedIndex(main.getConstraint(main.getInstance().getSelection(0)).getType());
systemChange = false;
nameTF.setText(main.getConstraint(main.getInstance().getSelection(0)).getName());
distanceF.setValue(main.getConstraint(main.getInstance().getSelection(0)).getDistance());
maxstepF.setValue(main.getConstraint(main.getInstance().getSelection(0)).getMaxstep());
errorF.setValue(main.getConstraint(main.getInstance().getSelection(0)).getError());
distanceCB.setSelected(main.getConstraint(main.getInstance().getSelection(0)).isDistanceEnabled());
maxstepCB.setSelected(main.getConstraint(main.getInstance().getSelection(0)).isMaxstepEnabled());
errorCB.setSelected(main.getConstraint(main.getInstance().getSelection(0)).isErrorEnabled());
distanceF.setEnabled(main.getConstraint(main.getInstance().getSelection(0)).isDistanceEnabled());
maxstepF.setEnabled(main.getConstraint(main.getInstance().getSelection(0)).isMaxstepEnabled());
errorF.setEnabled(main.getConstraint(main.getInstance().getSelection(0)).isErrorEnabled());
}
//get the ids of the selected constraints
public CB_Relations getSelectedConstraints() {
CB_Relations constraints = new CB_Relations(main.getInstance().getSelectionSize());
for (int i = 0; i < main.getInstance().getSelectionSize(); i++) {
constraints.add(main.getConstraint(main.getInstance().getSelection(i)).clone(false));
}
return constraints;
}
//create a change which can be undone
public CB_UndoableEdit createUndoableEdit(CB_Relations oldConstraints, CB_Relations newConstraints) {
return new CB_UndoableEdit(main.getInstance().getSelectionClone(), oldConstraints, newConstraints) {
public void redo() {
for (int i = 0; i < getIds().length; i++) {
main.getConstraints().set(getId(i), newConstraints.get(i));
}
main.getConstraintPanel().update();
main.getInstance().updateSelection();
super.redo();
}
public void undo() {
for (int i = 0; i < getIds().length; i++) {
main.getConstraints().set(getId(i), oldConstraints.get(i));
}
main.getConstraintPanel().update();
main.getInstance().updateSelection();
super.undo();
}
public String getUndoRedoPresentationName() {
return "Edit Multiple Constraints";
}
};
}
//set all the textfield values to the values of the constraint
public void setProperties(String name, int type, boolean distanceEnabled, boolean maxstepEnabled, boolean errorEnabled, double distance, int maxStep, double error) {
CB_Constraint oldConstraint = main.getConstraint(main.getInstance().getSelection(0)).clone(false);
main.getConstraint(main.getInstance().getSelection(0)).setName(name);
main.getConstraint(main.getInstance().getSelection(0)).setType(type);
main.getConstraint(main.getInstance().getSelection(0)).setDistance(distance);
main.getConstraint(main.getInstance().getSelection(0)).setMaxstep(maxStep);
main.getConstraint(main.getInstance().getSelection(0)).setError(error);
main.getConstraint(main.getInstance().getSelection(0)).enableDistance(distanceEnabled);
main.getConstraint(main.getInstance().getSelection(0)).enableMaxstep(maxstepEnabled);
main.getConstraint(main.getInstance().getSelection(0)).enableError(errorEnabled);
main.getInstance().updateSelection();
main.getConstraintPanel().update();
CB_Constraint newConstraint = main.getConstraint(main.getInstance().getSelection(0)).clone(false);
main.getInstance().addUndoableEdit(
new CB_UndoableEdit(main.getInstance().getSelection(0), oldConstraint, newConstraint) {
public void redo() {
main.getConstraints().set(getId(), newConstraint);
main.getConstraintPanel().update();
main.getInstance().updateSelection();
super.redo();
}
public void undo() {
main.getConstraints().set(getId(), oldConstraint);
main.getConstraintPanel().update();
main.getInstance().updateSelection();
super.undo();
}
public String getUndoRedoPresentationName() {
return "Edit Constraint";
}
}
);
}
//set the constraint name
public void setName() {
CB_Relations oldConstraints = this.getSelectedConstraints();
for (int i = 0; i < main.getInstance().getSelectionSize(); i++) {
main.getConstraint(main.getInstance().getSelection(i)).setName(nameTF.getText());
}
main.getInstance().updateSelection();
main.getInstance().addUndoableEdit(createUndoableEdit(oldConstraints, this.getSelectedConstraints()));
}
//set the constraint type
public void setType() {
CB_Relations oldConstraints = this.getSelectedConstraints();
for (int i = 0; i < main.getInstance().getSelectionSize(); i++) {
main.getConstraint(main.getInstance().getSelection(i)).setType(constraintChooser.getSelectedIndex());
}
main.getInstance().updateSelection();
main.getInstance().addUndoableEdit(createUndoableEdit(oldConstraints, this.getSelectedConstraints()));
}
//set the constraint distance
public void setDistance() {
CB_Relations oldConstraints = this.getSelectedConstraints();
for (int i = 0; i < main.getInstance().getSelectionSize(); i++) {
main.getConstraint(main.getInstance().getSelection(i)).setDistance(((Number)distanceF.getValue()).doubleValue());
}
main.getInstance().updateSelection();
main.getInstance().addUndoableEdit(createUndoableEdit(oldConstraints, this.getSelectedConstraints()));
}
//set the constraint distance
public void setMaxstep() {
CB_Relations oldConstraints = this.getSelectedConstraints();
for (int i = 0; i < main.getInstance().getSelectionSize(); i++) {
main.getConstraint(main.getInstance().getSelection(i)).setMaxstep(((Number)maxstepF.getValue()).intValue());
}
main.getInstance().updateSelection();
main.getInstance().addUndoableEdit(createUndoableEdit(oldConstraints, this.getSelectedConstraints()));
}
//set the constraint error
public void setError() {
CB_Relations oldConstraints = this.getSelectedConstraints();
for (int i = 0; i < main.getInstance().getSelectionSize(); i++) {
main.getConstraint(main.getInstance().getSelection(i)).setError(((Number)errorF.getValue()).doubleValue());
}
main.getInstance().updateSelection();
main.getInstance().addUndoableEdit(createUndoableEdit(oldConstraints, this.getSelectedConstraints()));
}
//set the value if distance is enabled or not
public void setDistanceEnabled() {
CB_Relations oldConstraints = this.getSelectedConstraints();
for (int i = 0; i < main.getInstance().getSelectionSize(); i++) {
main.getConstraint(main.getInstance().getSelection(i)).enableDistance(distanceCB.isSelected());
}
main.getInstance().addUndoableEdit(createUndoableEdit(oldConstraints, this.getSelectedConstraints()));
}
//set the value if distance is enabled or not
public void setMaxstepEnabled() {
CB_Relations oldConstraints = this.getSelectedConstraints();
for (int i = 0; i < main.getInstance().getSelectionSize(); i++) {
main.getConstraint(main.getInstance().getSelection(i)).enableMaxstep(maxstepCB.isSelected());
}
main.getInstance().addUndoableEdit(createUndoableEdit(oldConstraints, this.getSelectedConstraints()));
}
//set the value if distance is enabled or not
public void setErrorEnabled() {
CB_Relations oldConstraints = this.getSelectedConstraints();
for (int i = 0; i < main.getInstance().getSelectionSize(); i++) {
main.getConstraint(main.getInstance().getSelection(i)).enableError(errorCB.isSelected());
}
main.getInstance().addUndoableEdit(createUndoableEdit(oldConstraints, this.getSelectedConstraints()));
}
//action event
public void actionPerformed(ActionEvent e) {
if (systemChange) {
return;
}
Object event = e.getSource();
//if exactly one constraint is selected, set all values.
if (main.getInstance().getSelectionSize() == 1) {
this.setProperties(
nameTF.getText(),
constraintChooser.getSelectedIndex(),
distanceCB.isSelected(),
maxstepCB.isSelected(),
errorCB.isSelected(),
((Number)distanceF.getValue()).doubleValue(),
((Number)maxstepF.getValue()).intValue(),
((Number)errorF.getValue()).doubleValue()
);
if (event == distanceCB) {
distanceF.setEnabled(distanceCB.isSelected());
} else if (event == maxstepCB) {
maxstepF.setEnabled(maxstepCB.isSelected());
} else if (event == errorCB) {
errorF.setEnabled(errorCB.isSelected());
}
} else {
if (event == nameTF) {
this.setName();
} else if (event == constraintChooser) {
this.setType();
} else if (event == distanceCB) {
this.setDistanceEnabled();
} else if (event == maxstepCB) {
this.setMaxstepEnabled();
} else if (event == errorCB) {
this.setErrorEnabled();
} else if (event == distanceF) {
this.setDistance();
} else if (event == maxstepF) {
this.setMaxstep();
} else if (event == errorF) {
this.setError();
}
}
}
}