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