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.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   * Dialog that allows the quick input of matrices.
48   * 
49   * TODO: This needs to be refactored.
50   * 
51   * Created on March 6, 2007
52   * 
53   * @author Christina Bohk
54   * @author Roland Ewald
55   */
56  @Deprecated
57  public class QuickInputDialog extends JDialog {
58  
59  	/** Serialization ID. */
60  	private static final long serialVersionUID = 971779309134143674L;
61  
62  	/** The height of the dialog. */
63  	private static final int DIALOG_HEIGHT = 480;
64  
65  	/** The width of the dialog. */
66  	private static final int DIALOG_WIDTH = 640;
67  
68  	/** The column size of the matrix description text area. */
69  	private static final int MATRIX_DESC_COL_SIZE = 15;
70  
71  	/** The row size of the matrix description text area. */
72  	private static final int MATRIX_DESC_ROW_SIZE = 7;
73  
74  	/**
75  	 * The size of the text area to enter a probability for the matrix
76  	 * (assignment).
77  	 */
78  	private static final int MATRIX_PROB_FIELD_SIZE = 3;
79  
80  	/** The size of the text area to enter the matrix name. */
81  	private static final int MATRIX_NAME_FIELD_SIZE = 15;
82  
83  	/** Reference to current scenario. */
84  	private final ProjectionModel currentProjection;
85  
86  	/** Reference to current Settype. */
87  	private SetType currentSetType;
88  
89  	/** Parameter assignment to be copied. */
90  	private ParameterAssignment currentParameterAssignment = new ParameterAssignment();
91  
92  	// GUI elements
93  
94  	/** Combo-box of available Settypes. */
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 	/** List of parameters for given set. */
107 	private JList parametersList = new JList(new DefaultListModel());
108 
109 	/** List of sets for given Settype. */
110 	private JList setsList = new JList(new DefaultListModel());
111 
112 	/** Field to edit the name of the current matrix. */
113 	private JTextField currentMatrixName = new JTextField(MATRIX_NAME_FIELD_SIZE);
114 
115 	/** Field to edit the probability of the current matrix. */
116 	private JTextField currentMatrixProb = new JTextField(MATRIX_PROB_FIELD_SIZE);
117 
118 	/** Field to edit the description of the current matrix. */
119 	private JTextArea currentMatrixDesc = new JTextArea(MATRIX_DESC_ROW_SIZE,
120 	    MATRIX_DESC_COL_SIZE);
121 
122 	/** Button to edit the values of the matrix. */
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 	/** Close button. */
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 	/** Button to start copy process. */
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 	 * Default constructor.
157 	 * 
158 	 * @param owner
159 	 *          the owning window
160 	 * @param projection
161 	 *          the projection to be edited
162 	 */
163 	public QuickInputDialog(Frame owner, ProjectionModel projection) {
164 		super(owner);
165 		this.currentProjection = projection;
166 		init();
167 		resetUI();
168 	}
169 
170 	/**
171 	 * UI initialization.
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 	 * Gets the content panel.
183 	 * 
184 	 * @return panel that contains all content
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 	 * Gets the control panel.
197 	 * 
198 	 * @return panel with control buttons
199 	 */
200 	private JPanel getControlPanel() {
201 		JPanel returnPanel = new JPanel();
202 		returnPanel.add(copyButton);
203 		return returnPanel;
204 	}
205 
206 	/**
207 	 * Gets the choose target panel.
208 	 * 
209 	 * @return panel to select targets
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 	 * Gets the edit matrix panel.
230 	 * 
231 	 * @return panel to edit matrix
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 	 * Gets the select Settype panel.
259 	 * 
260 	 * @return the select Settype panel
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 	 * Refreshes parameter and set lists on selection of another Settype.
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 	 * Refresh interface.
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 		// This is the maximum
302 		if (currentParameterAssignment.getMatrix() == null) {
303 			currentParameterAssignment.setMatrixValue(new Matrix2D(currentProjection
304 			    .getMaximumAge() + 1, currentProjection.getYears()));
305 		}
306 
307 	}
308 
309 	/**
310 	 * Edit the current matrix.
311 	 */
312 	protected void editCurrentMatrix() {
313 		EditMatrixDialog editMatrixDialog = new EditMatrixDialog(P3J.getInstance(),
314 		    currentParameterAssignment);
315 		editMatrixDialog.setVisible(true);
316 	}
317 
318 	/**
319 	 * Copy parameter assignment.
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 		// Prepare assignment variables
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 }