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  
23  import javax.swing.BoxLayout;
24  import javax.swing.JButton;
25  import javax.swing.JDialog;
26  import javax.swing.JLabel;
27  import javax.swing.JList;
28  import javax.swing.JPanel;
29  import javax.swing.JScrollPane;
30  import javax.swing.JTextArea;
31  import javax.swing.JTextField;
32  import javax.swing.ListSelectionModel;
33  import javax.swing.event.ListSelectionEvent;
34  import javax.swing.event.ListSelectionListener;
35  
36  import p3j.gui.misc.ParameterListModel;
37  import p3j.gui.misc.SetTypesListModel;
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  /**
44   * A dialog to edit the set of defined Settypes.
45   * 
46   * TODO: Needs major refactoring.
47   * 
48   * Created on August 2006
49   * 
50   * @author Christina Bohk
51   * @author Roland Ewald
52   */
53  @Deprecated
54  public class EditSetTypesDialog extends JDialog {
55  
56  	/** The text field width of the new and current Settypes' names. */
57  	private static final int SET_TYPE_NAME_TEXTWIDTH = 15;
58  
59  	/** The height of the dialog. */
60  	private static final int DIALOG_HEIGHT = 768;
61  
62  	/** The width of the dialog. */
63  	private static final int DIALOG_WIDTH = 1200;
64  
65  	/** Serialization ID. */
66  	private static final long serialVersionUID = -1223610044645461902L;
67  
68  	/** Content panel of dialog. */
69  	private JPanel contentPanel;
70  
71  	/** Sroll pane for the Settype list. */
72  	private JScrollPane setTypeListScrollPane;
73  
74  	/** Panel to edit a Settype. */
75  	private JPanel editPanel;
76  
77  	/** Scroll pane for parameter list of current Settype. */
78  	private JScrollPane parametersOfTypeScrollPane;
79  
80  	/** Panel to show the current type. */
81  	private JPanel currentTypePanel;
82  
83  	/** Panel to edit the current type. */
84  	private JPanel editTypePanel;
85  
86  	/** Panel for parameter selection. */
87  	private JPanel parameterSelectionPanel;
88  
89  	/** SCroll pane for all available parameters. */
90  	private JScrollPane overallParametersScroll;
91  
92  	/** Panel to display the parameters of the current Settype. */
93  	private JPanel parametersOfTypePanel;
94  
95  	/** Panel to hold a Settype's description and edit/delete controls. */
96  	private JPanel descAndButtonPanel;
97  
98  	/** Panel to hold the list of all existing Settypes. */
99  	private JPanel setTypeListPanel;
100 
101 	/** Text area for the current Settype's description. */
102 	private JTextArea currentSetTypeDescription = new JTextArea();
103 
104 	/** Field to change the current Settype's name. */
105 	private JTextField currentSetTypeName = new JTextField(
106 	    SET_TYPE_NAME_TEXTWIDTH);
107 
108 	// Basic data structures
109 
110 	/** Current projection that is edited. */
111 	private ProjectionModel currentProjection;
112 
113 	/** Index of current Settype. */
114 	private int indexOfCurrentSetType = -1;
115 
116 	// Parameter list
117 
118 	/** List model for all available parameters. */
119 	private ParameterListModel availableParameters = new ParameterListModel();
120 
121 	/** List of all available parameters. */
122 	private JList availableParametersList = new JList(availableParameters);
123 
124 	// Settype management
125 
126 	/** List of all available Settypes. */
127 	private SetTypesListModel availableSetTypes;
128 
129 	/** List of existing Settypes. */
130 	private JList setTypeList;
131 
132 	/** Field to put in name of new Settype. */
133 	private JTextField newSetTypeName = new JTextField(SET_TYPE_NAME_TEXTWIDTH);
134 
135 	/** Button to create a new Settype. */
136 	private JButton createNewSetTypeButton = new JButton("OK");
137 	{
138 		createNewSetTypeButton.addActionListener(new ActionListener() {
139 			@Override
140 			public void actionPerformed(ActionEvent e) {
141 				availableSetTypes.addSetType(newSetTypeName.getText(),
142 				    "Put in description here.");
143 				setTypeList.setSelectedIndex(availableSetTypes.getSize() - 1);
144 			}
145 		});
146 	}
147 
148 	/** Button to change the name/description of a Settype. */
149 	private JButton changeSetTypeButton = new JButton("Change");
150 	{
151 		changeSetTypeButton.addActionListener(new ActionListener() {
152 			@Override
153 			public void actionPerformed(ActionEvent e) {
154 				SetType currentSetType = getCurrentSetType();
155 				if (currentSetType == null) {
156 					return;
157 				}
158 				currentSetType.setName(currentSetTypeName.getText());
159 				currentSetType.setDescription(currentSetTypeDescription.getText());
160 				availableSetTypes.setTypeChanged(indexOfCurrentSetType);
161 			}
162 		});
163 	}
164 
165 	/** Button to delete a Settype. */
166 	private JButton deleteSetTypeButton = new JButton("Delete");
167 	{
168 		deleteSetTypeButton.addActionListener(new ActionListener() {
169 			@Override
170 			public void actionPerformed(ActionEvent e) {
171 				SetType currentSetType = getCurrentSetType();
172 				if (currentSetType == null) {
173 					return;
174 				}
175 				availableSetTypes.removeSetType(indexOfCurrentSetType);
176 				int numOfSetTypes = availableSetTypes.getSize();
177 				if (numOfSetTypes > 0) {
178 					updateCurrentSetType(indexOfCurrentSetType == numOfSetTypes ? numOfSetTypes - 1
179 					    : indexOfCurrentSetType);
180 				} else {
181 					resetCurrentSetType();
182 				}
183 			}
184 		});
185 	}
186 
187 	// Parameter instance management
188 
189 	/** List of all parameters of Settype. */
190 	private ParameterListModel parametersOfSetType = new ParameterListModel();
191 
192 	/** List of parameters of current Settype. */
193 	private JList parametersOfSetTypeList = new JList(parametersOfSetType);
194 
195 	/** Button to add a parameter instance to the current Settype. */
196 	private JButton addInstanceToSetTypeButton = new JButton("<=");
197 	{
198 		addInstanceToSetTypeButton.addActionListener(new ActionListener() {
199 			@Override
200 			public void actionPerformed(ActionEvent e) {
201 				transferInstances(availableParametersList, availableParameters,
202 				    parametersOfSetType, getCurrentSetType());
203 			}
204 		});
205 	}
206 
207 	/** Button to remove a parameter instance from the current Settype. */
208 	private JButton removeInstanceFromSetTypeButton = new JButton("=>");
209 	{
210 		removeInstanceFromSetTypeButton.addActionListener(new ActionListener() {
211 			@Override
212 			public void actionPerformed(ActionEvent e) {
213 				transferInstances(parametersOfSetTypeList, parametersOfSetType,
214 				    availableParameters, currentProjection.getDefaultSetType());
215 			}
216 		});
217 	}
218 
219 	/**
220 	 * Default constructor.
221 	 * 
222 	 * @param owner
223 	 *          the owner
224 	 * @param scenario
225 	 *          the scenario
226 	 */
227 	public EditSetTypesDialog(Frame owner, ProjectionModel scenario) {
228 		super(owner);
229 		initialize();
230 
231 		currentProjection = scenario;
232 		availableSetTypes = new SetTypesListModel(currentProjection);
233 		setTypeList = new JList(availableSetTypes);
234 		setTypeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
235 		setTypeList.addListSelectionListener(new ListSelectionListener() {
236 			@Override
237 			public void valueChanged(ListSelectionEvent e) {
238 				updateCurrentSetType(setTypeList.getSelectedIndex());
239 			}
240 		});
241 
242 		availableParameters.updateSetType(currentProjection.getDefaultSetType());
243 
244 		availableSetTypes.setProjection(currentProjection);
245 
246 		if (currentProjection.getNumOfSetTypes() > 0) {
247 			updateCurrentSetType(0);
248 		}
249 	}
250 
251 	/**
252 	 * Transfer instances.
253 	 * 
254 	 * @param fromList
255 	 *          the from list
256 	 * @param from
257 	 *          the from
258 	 * @param to
259 	 *          the to
260 	 * @param targetSetType
261 	 *          the target Settype
262 	 */
263 	public void transferInstances(JList fromList, ParameterListModel from,
264 	    ParameterListModel to, SetType targetSetType) {
265 
266 		if (targetSetType != null) {
267 
268 			int firstIndex = fromList.getSelectedIndex();
269 
270 			Object[] instances = fromList.getSelectedValues();
271 
272 			for (Object instance : instances) {
273 
274 				ParameterInstance instanceToBeAdded = (ParameterInstance) instance;
275 
276 				currentProjection.assignParameterInstance(instanceToBeAdded,
277 				    targetSetType, false);
278 			}
279 
280 			to.refresh();
281 			from.refresh();
282 
283 			if (instances.length > 0 && from.getSize() > 0) {
284 				fromList
285 				    .setSelectedIndex(from.getSize() == firstIndex ? from.getSize() - 1
286 				        : firstIndex);
287 			}
288 		}
289 	}
290 
291 	/**
292 	 * Update current Settype.
293 	 * 
294 	 * @param index
295 	 *          the index
296 	 */
297 	final void updateCurrentSetType(int index) {
298 
299 		this.indexOfCurrentSetType = index;
300 		this.setTypeList.setSelectedIndex(index);
301 
302 		SetType currentSetType = getCurrentSetType();
303 
304 		this.currentSetTypeDescription.setText(currentSetType == null ? ""
305 		    : currentSetType.getDescription());
306 		this.currentSetTypeName.setText(currentSetType == null ? ""
307 		    : currentSetType.getName());
308 		this.parametersOfSetType.updateSetType(currentSetType);
309 		this.availableParameters.refresh();
310 
311 	}
312 
313 	/**
314 	 * Resets selection of Settype.
315 	 */
316 	protected void resetCurrentSetType() {
317 		this.indexOfCurrentSetType = -1;
318 		this.currentSetTypeDescription.setText("");
319 		this.currentSetTypeName.setText("");
320 		this.setTypeList.getSelectionModel().clearSelection();
321 		this.availableParameters.refresh();
322 		this.parametersOfSetType.refresh();
323 	}
324 
325 	/**
326 	 * Gets the current Settype.
327 	 * 
328 	 * @return currently selected Settype (or null)
329 	 */
330 	final SetType getCurrentSetType() {
331 		if (this.indexOfCurrentSetType >= 0) {
332 			return this.currentProjection.getSetType(this.indexOfCurrentSetType);
333 		}
334 		return null;
335 	}
336 
337 	/**
338 	 * This method initializes this dialog.
339 	 */
340 	private void initialize() {
341 
342 		this.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
343 		this.setContentPane(getContentPanel());
344 		this.setTitle("Edit Settypes");
345 	}
346 
347 	/**
348 	 * This method initializes jContentPane.
349 	 * 
350 	 * @return the content panel
351 	 */
352 	final JPanel getContentPanel() {
353 		if (contentPanel == null) {
354 			contentPanel = new JPanel(GUI.getStdBorderLayout());
355 			contentPanel.add(getSetTypeListScroll(), BorderLayout.WEST);
356 			contentPanel.add(getEditPanel(), BorderLayout.CENTER);
357 		}
358 		return contentPanel;
359 	}
360 
361 	/**
362 	 * This method initializes setTypeListScroll.
363 	 * 
364 	 * @return scroll pane containing Settype list
365 	 */
366 	private JPanel getSetTypeListScroll() {
367 		if (setTypeListPanel == null) {
368 			setTypeListPanel = new JPanel(GUI.getStdBorderLayout());
369 			setTypeListScrollPane = new JScrollPane();
370 			setTypeListScrollPane.setViewportView(setTypeList);
371 			setTypeListPanel.add(setTypeListScrollPane, BorderLayout.CENTER);
372 			setTypeListPanel
373 			    .add(new JLabel("Existing Settypes:"), BorderLayout.NORTH);
374 			JPanel addNewSetTypePanel = new JPanel();
375 			setTypeListPanel.add(addNewSetTypePanel, BorderLayout.SOUTH);
376 
377 			addNewSetTypePanel.add(new JLabel("Add new Settype:"));
378 			addNewSetTypePanel.add(newSetTypeName);
379 			addNewSetTypePanel.add(createNewSetTypeButton);
380 		}
381 		return setTypeListPanel;
382 	}
383 
384 	/**
385 	 * This method initializes the editing panel.
386 	 * 
387 	 * @return the editing panel
388 	 */
389 	private JPanel getEditPanel() {
390 		if (editPanel == null) {
391 			editPanel = new JPanel(GUI.getStdBorderLayout());
392 			editPanel.add(getCurrentTypePanel(), BorderLayout.NORTH);
393 			editPanel.add(getEditTypePanel(), BorderLayout.CENTER);
394 			editPanel.add(getDescAndButtonPanel(), BorderLayout.SOUTH);
395 		}
396 		return editPanel;
397 	}
398 
399 	/**
400 	 * Gets the description and button panel.
401 	 * 
402 	 * @return panel to hold description and buttons
403 	 */
404 	final JPanel getDescAndButtonPanel() {
405 		if (descAndButtonPanel == null) {
406 			descAndButtonPanel = new JPanel(GUI.getStdBorderLayout());
407 			JPanel descPanel = new JPanel(GUI.getStdBorderLayout());
408 			JPanel buttonPanel = new JPanel();
409 			descAndButtonPanel.add(descPanel, BorderLayout.CENTER);
410 			descAndButtonPanel.add(buttonPanel, BorderLayout.EAST);
411 			descPanel.add(new JLabel("Description:"), BorderLayout.NORTH);
412 			descPanel.add(currentSetTypeDescription, BorderLayout.CENTER);
413 			buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
414 			buttonPanel.add(deleteSetTypeButton);
415 			buttonPanel.add(changeSetTypeButton);
416 		}
417 		return descAndButtonPanel;
418 	}
419 
420 	/**
421 	 * Gets the edit type panel.
422 	 * 
423 	 * @return panel to edit current Settype
424 	 */
425 	final JPanel getEditTypePanel() {
426 		if (editTypePanel == null) {
427 			editTypePanel = new JPanel(GUI.getStdBorderLayout());
428 			editTypePanel.add(getParametersOfTypeScroll(), BorderLayout.WEST);
429 			editTypePanel.add(getParameterSelection(), BorderLayout.EAST);
430 			JPanel buttonPanel = new JPanel();
431 			buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
432 			editTypePanel.add(buttonPanel, BorderLayout.CENTER);
433 			buttonPanel.add(addInstanceToSetTypeButton);
434 			buttonPanel.add(removeInstanceFromSetTypeButton);
435 		}
436 		return editTypePanel;
437 	}
438 
439 	/**
440 	 * Gets the parameter selection.
441 	 * 
442 	 * @return panel with elements for parameter selection
443 	 */
444 	final JPanel getParameterSelection() {
445 		if (parameterSelectionPanel == null) {
446 			parameterSelectionPanel = new JPanel(GUI.getStdBorderLayout());
447 			overallParametersScroll = new JScrollPane();
448 			overallParametersScroll.setViewportView(availableParametersList);
449 			parameterSelectionPanel.add(new JLabel("Available parameters:"),
450 			    BorderLayout.NORTH);
451 			parameterSelectionPanel.add(overallParametersScroll, BorderLayout.CENTER);
452 		}
453 		return parameterSelectionPanel;
454 	}
455 
456 	/**
457 	 * Initializes of panel that shows the name of the current Settype.
458 	 * 
459 	 * @return panel to edit current Settype
460 	 */
461 	final JPanel getCurrentTypePanel() {
462 		if (currentTypePanel == null) {
463 			currentTypePanel = new JPanel();
464 			currentTypePanel.add(new JLabel("Name of Settype:"));
465 			currentTypePanel.add(currentSetTypeName);
466 		}
467 		return currentTypePanel;
468 	}
469 
470 	/**
471 	 * This method initializes parametersOfTypeScroll.
472 	 * 
473 	 * @return javax.swing.JScrollPane
474 	 */
475 	final JPanel getParametersOfTypeScroll() {
476 		if (parametersOfTypePanel == null) {
477 			parametersOfTypePanel = new JPanel(GUI.getStdBorderLayout());
478 			parametersOfTypeScrollPane = new JScrollPane();
479 			parametersOfTypeScrollPane.setViewportView(parametersOfSetTypeList);
480 			parametersOfTypePanel.add(new JLabel("Defined parameters in Settype:"),
481 			    BorderLayout.NORTH);
482 			parametersOfTypePanel
483 			    .add(parametersOfTypeScrollPane, BorderLayout.CENTER);
484 		}
485 		return parametersOfTypePanel;
486 	}
487 
488 }