View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
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   * Allows to copy structure from one generation to another.
45   * 
46   * Created on March 12, 2007
47   * 
48   * @author Christina Bohk
49   * @author Roland Ewald
50   */
51  public class CopyGenerationsDialog extends JDialog {
52  
53  	/** Serialization ID. */
54  	private static final long serialVersionUID = -7632325769179634802L;
55  
56  	/** Width of the dialog. */
57  	public static final int DIALG_WIDTH = 600;
58  
59  	/** Height of the dialog. */
60  	public static final int DIALG_HEIGHT = 600;
61  
62  	/** Reference to current scenario. */
63  	private ProjectionModel projection;
64  
65  	/** Text field to set source generation. */
66  	private JTextField fromGeneration = new JTextField(2);
67  
68  	/** Text field to set target generation. */
69  	private JTextField toGeneration = new JTextField(2);
70  
71  	/** Select Settype for operation. */
72  	private JComboBox selectSetType;
73  
74  	/** Button to press if OK. */
75  	private final JButton okButton = new JButton("OK");
76  	{
77  		okButton.addActionListener(new CopyGenerations());
78  	}
79  
80  	/**
81  	 * Default constructor.
82  	 * 
83  	 * @param proj
84  	 *          the current projection
85  	 * @param owner
86  	 *          the owner of this dialog
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  	 * Constructs panel containing the user interface.
100 	 * 
101 	 * @return panel with UI
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 	 * Copies from one generation to the other.
119 	 */
120 	private class CopyGenerations implements ActionListener {
121 
122 		/*
123 		 * (non-Javadoc)
124 		 * 
125 		 * @see
126 		 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
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 		 * Fills source and target list.
152 		 * 
153 		 * @param setType
154 		 *          the Settype
155 		 * @param srcGen
156 		 *          the source generation
157 		 * @param targetGen
158 		 *          the target generation
159 		 * @param sources
160 		 *          the sources
161 		 * @param targets
162 		 *          the targets
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 		 * Construct instance mapping. Go through source instances and finds out
184 		 * which one has a matching target.
185 		 * 
186 		 * @param sources
187 		 *          the source instances
188 		 * @param targets
189 		 *          the target instances
190 		 * 
191 		 * @return the map source parameter instance => target parameter instance
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 		 * Copies the parameter assignments.
211 		 * 
212 		 * @param sets
213 		 *          the list of new sets
214 		 * @param mapping
215 		 *          the mapping from each parameter instance to its copy
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 }