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.Window;
19 import java.awt.event.ActionEvent;
20 import java.awt.event.ActionListener;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Map.Entry;
26
27 import javax.swing.JButton;
28 import javax.swing.JComboBox;
29 import javax.swing.JDialog;
30 import javax.swing.JLabel;
31 import javax.swing.JPanel;
32 import javax.swing.JTextField;
33
34 import p3j.misc.Misc;
35 import p3j.misc.gui.GUI;
36 import p3j.pppm.ProjectionModel;
37 import p3j.pppm.parameters.Parameter;
38 import p3j.pppm.parameters.ParameterAssignment;
39 import p3j.pppm.parameters.ParameterInstance;
40 import p3j.pppm.sets.Set;
41 import p3j.pppm.sets.SetType;
42
43
44
45
46
47
48
49
50
51 public class CopyGenerationsDialog extends JDialog {
52
53
54 private static final long serialVersionUID = -7632325769179634802L;
55
56
57 public static final int DIALG_WIDTH = 600;
58
59
60 public static final int DIALG_HEIGHT = 600;
61
62
63 private ProjectionModel projection;
64
65
66 private JTextField fromGeneration = new JTextField(2);
67
68
69 private JTextField toGeneration = new JTextField(2);
70
71
72 private JComboBox selectSetType;
73
74
75 private final JButton okButton = new JButton("OK");
76 {
77 okButton.addActionListener(new CopyGenerations());
78 }
79
80
81
82
83
84
85
86
87
88 public CopyGenerationsDialog(ProjectionModel proj, Window owner) {
89 super(owner);
90 this.projection = proj;
91 setModal(true);
92 setSize(DIALG_WIDTH, DIALG_HEIGHT);
93 setTitle("Duplicate Generations");
94 this.setContentPane(getContentPanel());
95 GUI.centerOnScreen(this);
96 }
97
98
99
100
101
102
103 protected final JPanel getContentPanel() {
104 JPanel returnPanel = new JPanel();
105
106 returnPanel.add(new JLabel("Duplicate all matrices from generation "));
107 returnPanel.add(fromGeneration);
108 returnPanel.add(new JLabel(" to generation "));
109 returnPanel.add(toGeneration);
110 returnPanel.add(new JLabel(" in Settype "));
111 selectSetType = new JComboBox(projection.getUserDefinedTypes().toArray());
112 returnPanel.add(selectSetType);
113 returnPanel.add(okButton);
114 return returnPanel;
115 }
116
117
118
119
120 private class CopyGenerations implements ActionListener {
121
122
123
124
125
126
127
128 @Override
129 public void actionPerformed(ActionEvent e) {
130
131 SetType setType = (SetType) selectSetType.getSelectedItem();
132
133 int from = Misc.parseToInt(fromGeneration.getText());
134 int to = Misc.parseToInt(toGeneration.getText());
135
136 if (setType == null || from < 0 || to < 0) {
137 return;
138 }
139
140 ArrayList<ParameterInstance> sources = new ArrayList<ParameterInstance>();
141 ArrayList<ParameterInstance> targets = new ArrayList<ParameterInstance>();
142 fillSourceAndTargetList(setType, from, to, sources, targets);
143
144 Map<ParameterInstance, ParameterInstance> mapping = constructInstanceMapping(
145 sources, targets);
146 copyParamAssignments(setType.getSets(), mapping);
147 setVisible(false);
148 }
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164 private void fillSourceAndTargetList(SetType setType, int srcGen,
165 int targetGen, List<ParameterInstance> sources,
166 List<ParameterInstance> targets) {
167 List<ParameterInstance> parameterInstances = setType
168 .getDefinedParameters();
169 for (int i = 0; i < parameterInstances.size(); i++) {
170 if (!parameterInstances.get(i).getParameter().isGenerationDependent()) {
171 continue;
172 }
173 if (parameterInstances.get(i).getGeneration() == srcGen) {
174 sources.add(parameterInstances.get(i));
175 }
176 if (parameterInstances.get(i).getGeneration() == targetGen) {
177 targets.add(parameterInstances.get(i));
178 }
179 }
180 }
181
182
183
184
185
186
187
188
189
190
191
192
193 private Map<ParameterInstance, ParameterInstance> constructInstanceMapping(
194 List<ParameterInstance> sources, List<ParameterInstance> targets) {
195 Map<ParameterInstance, ParameterInstance> mapping = new HashMap<ParameterInstance, ParameterInstance>();
196 for (int i = 0; i < sources.size(); i++) {
197 ParameterInstance source = sources.get(i);
198 Parameter param = source.getParameter();
199 for (int j = 0; j < targets.size(); j++) {
200 if (targets.get(j).getParameter().equals(param)) {
201 mapping.put(source, targets.get(j));
202 break;
203 }
204 }
205 }
206 return mapping;
207 }
208
209
210
211
212
213
214
215
216
217 private void copyParamAssignments(List<Set> sets,
218 Map<ParameterInstance, ParameterInstance> mapping) {
219 java.util.Set<Entry<ParameterInstance, ParameterInstance>> mappingSet = mapping
220 .entrySet();
221
222 for (Entry<ParameterInstance, ParameterInstance> mappingEntry : mappingSet) {
223 ParameterInstance source = mappingEntry.getKey();
224 ParameterInstance target = mappingEntry.getValue();
225
226 for (int i = 0; i < sets.size(); i++) {
227 Set set = sets.get(i);
228
229 List<ParameterAssignment> assignments = new ArrayList<ParameterAssignment>(
230 set.getParameterAssignments(source).getAssignments());
231
232 for (int j = 0; j < assignments.size(); j++) {
233 ParameterAssignment srcAssign = assignments.get(i);
234 ParameterAssignment cpyAssign = srcAssign.getCopy();
235 cpyAssign.setParamInstance(target);
236 set.addParameterAssignment(cpyAssign);
237 }
238 }
239 }
240 }
241 }
242 }