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