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.event.ActionEvent;
20 import java.awt.event.ActionListener;
21 import java.util.List;
22
23 import javax.swing.BoxLayout;
24 import javax.swing.ImageIcon;
25 import javax.swing.JButton;
26 import javax.swing.JLabel;
27 import javax.swing.JList;
28 import javax.swing.JPanel;
29 import javax.swing.JScrollPane;
30 import javax.swing.JTextField;
31
32 import p3j.database.DatabaseFactory;
33 import p3j.database.IP3MDatabase;
34 import p3j.gui.misc.ParameterListModel;
35 import p3j.gui.panels.projections.IProjectionTree;
36 import p3j.gui.panels.projections.ProjectionNode;
37 import p3j.gui.panels.projections.SetTypeNode;
38 import p3j.misc.gui.GUI;
39 import p3j.pppm.ProjectionModel;
40 import p3j.pppm.parameters.ParameterInstance;
41 import p3j.pppm.sets.SetType;
42
43 import com.jgoodies.forms.layout.CellConstraints;
44 import com.jgoodies.forms.layout.FormLayout;
45
46
47
48
49
50
51
52
53
54
55
56 public class NewSetTypeDialog extends
57 AbstractProjectionTreeDialog<ProjectionModel, ProjectionNode> {
58
59
60 private static final long serialVersionUID = 3065923250959082561L;
61
62
63 public static final int DIALOG_WIDTH = 800;
64
65
66 public static final int DIALOG_HEIGHT = 600;
67
68
69 private static final int[] COLUMN_GROUPING = { 2, 4 };
70
71
72 private final ParameterListModel defTypeParamsModel = new ParameterListModel();
73
74
75 private final SetType newSetType;
76
77
78
79
80
81 private static final int ADD_REMOVE_PANEL_ROW_INDEX = 5;
82
83
84
85
86
87 private static final int ADD_REMOVE_PANEL_COL_INDEX = 3;
88
89
90
91
92 private static final int SELECTION_PANEL_ROW_INDEX = 4;
93
94
95
96
97 private static final int SELECTION_PANEL_ROW_HEIGHT = 3;
98
99
100
101
102 private static final int SELECTION_PANEL_COL_WIDTH = 1;
103
104
105
106
107
108 private static final int COLUMN_INDEX_RHS = 4;
109
110
111
112
113
114 private static final int COLUMN_INDEX_LHS = 2;
115
116
117 private static final int BUTTON_PANEL_ROW_INDEX = 8;
118
119
120 private static final int BUTTON_PANEL_COL_INDEX = 4;
121
122
123
124
125 private JTextField stName = new JTextField("New Settype");
126
127
128 private JButton addParamToNewType = new JButton();
129 {
130 ImageIcon icon = new ImageIcon(
131 NewSetTypeDialog.class.getResource("/p3j/icons/arrow_r2l.gif"));
132 if (icon.getImage() != null) {
133 addParamToNewType.setIcon(icon);
134 } else {
135 addParamToNewType.setText("<=");
136 }
137
138 addParamToNewType.addActionListener(new ActionListener() {
139 @Override
140 public void actionPerformed(ActionEvent e) {
141 transferInstances(defTypeParamsList, defTypeParamsModel,
142 newTypeParamsModel, newSetType);
143 }
144 });
145 }
146
147
148 private JButton remParamFromNewType = new JButton();
149 {
150 ImageIcon icon = new ImageIcon(
151 NewSetTypeDialog.class.getResource("/p3j/icons/arrow_l2r.gif"));
152 if (icon.getImage() != null) {
153 remParamFromNewType.setIcon(icon);
154 } else {
155 remParamFromNewType.setText("=>");
156 }
157
158 remParamFromNewType.addActionListener(new ActionListener() {
159 @Override
160 public void actionPerformed(ActionEvent e) {
161 transferInstances(newTypeParamsList, newTypeParamsModel,
162 defTypeParamsModel, getEntity().getDefaultSetType());
163 }
164 });
165 }
166
167
168
169
170
171 private final JList<ParameterInstance> defTypeParamsList = new JList<>(
172 defTypeParamsModel);
173
174
175 private final ParameterListModel newTypeParamsModel = new ParameterListModel();
176
177
178 private final JList<ParameterInstance> newTypeParamsList = new JList<>(
179 newTypeParamsModel);
180
181
182
183
184
185
186
187
188
189
190 public NewSetTypeDialog(ProjectionNode projNode, IProjectionTree projTree) {
191 super(projNode, projTree, DIALOG_WIDTH, DIALOG_HEIGHT, "Add Settype");
192 setModal(true);
193 newSetType = getEntity().createSetType("", "");
194 initUI();
195 }
196
197
198
199
200 private void initUI() {
201 setTitle("Create New Settype");
202
203 JPanel namePanel = new JPanel(GUI.getStdBorderLayout());
204 namePanel.add(new JLabel("Name:"), BorderLayout.WEST);
205 namePanel.add(stName, BorderLayout.CENTER);
206
207
208 defTypeParamsModel.updateSetType(getEntity().getDefaultSetType());
209 newTypeParamsModel.updateSetType(newSetType);
210
211 JPanel addRemovePanel = new JPanel();
212 addRemovePanel.setLayout(new BoxLayout(addRemovePanel, BoxLayout.Y_AXIS));
213 addRemovePanel.add(addParamToNewType);
214 addRemovePanel.add(remParamFromNewType);
215
216
217 FormLayout layout = new FormLayout(
218 "10dlu,d:grow,center:50dlu,right:d:grow,10dlu",
219 "10dlu,20dlu,10dlu,d:grow,50dlu,d:grow,10dlu,20dlu,10dlu");
220 JPanel contentPanel = new JPanel(layout);
221 layout.setColumnGroups(new int[][] { COLUMN_GROUPING });
222
223 CellConstraints cc = new CellConstraints();
224 contentPanel.add(namePanel, cc.xy(COLUMN_INDEX_LHS, 2));
225
226 contentPanel.add(new JScrollPane(newTypeParamsList), cc.xywh(
227 COLUMN_INDEX_LHS, SELECTION_PANEL_ROW_INDEX, SELECTION_PANEL_COL_WIDTH,
228 SELECTION_PANEL_ROW_HEIGHT));
229
230 contentPanel.add(new JScrollPane(defTypeParamsList), cc.xywh(
231 COLUMN_INDEX_RHS, SELECTION_PANEL_ROW_INDEX, SELECTION_PANEL_COL_WIDTH,
232 SELECTION_PANEL_ROW_HEIGHT));
233
234 contentPanel.add(addRemovePanel,
235 cc.xy(ADD_REMOVE_PANEL_COL_INDEX, ADD_REMOVE_PANEL_ROW_INDEX));
236
237 contentPanel.add(getButtonPanel(),
238 cc.xy(BUTTON_PANEL_COL_INDEX, BUTTON_PANEL_ROW_INDEX));
239 setContentPane(contentPanel);
240 }
241
242
243
244
245
246
247
248
249
250
251
252
253
254 public void transferInstances(JList<ParameterInstance> fromList,
255 ParameterListModel from, ParameterListModel to, SetType targetSetType) {
256
257 int firstIndex = fromList.getSelectedIndex();
258 List<ParameterInstance> instances = fromList.getSelectedValuesList();
259
260 for (Object instance : instances) {
261 ParameterInstance instanceToBeAdded = (ParameterInstance) instance;
262 getEntity().assignParameterInstance(instanceToBeAdded, targetSetType,
263 true);
264 }
265
266 to.refresh();
267 from.refresh();
268
269 if (instances.size() > 0 && from.getSize() > 0) {
270 fromList
271 .setSelectedIndex(from.getSize() == firstIndex ? from.getSize() - 1
272 : firstIndex);
273 }
274 }
275
276
277
278
279 void createSetType() {
280 if (newTypeParamsModel.getSize() == 0) {
281 GUI.printErrorMessage(this, "Settype is empty",
282 "Settypes should at least cover one parameter.");
283 return;
284 }
285
286 IP3MDatabase database = DatabaseFactory.getDatabaseSingleton();
287 newSetType.setName(stName.getText());
288 database.saveProjection(getEntity());
289 getProjectionTree().removeNodes(getEntityNode(),
290 newSetType.getDefinedParameters());
291 getProjectionTree().cleanTree();
292 SetTypeNode newNode = new SetTypeNode(newSetType);
293 getEntityNode().add(newNode);
294 getProjectionTree().refreshNodeSubStructure(getEntityNode());
295 getProjectionTree().selectNode(newNode);
296 setVisible(false);
297 }
298
299 @Override
300 protected void cancel() {
301 getEntity().removeSetType(newSetType);
302 setVisible(false);
303 }
304
305
306
307
308
309
310 public SetType getNewSetType() {
311 return newSetType;
312 }
313
314 @Override
315 protected void ok() {
316 createSetType();
317 }
318 }