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.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 = GUI.createIconButton("arrow_r2l.gif",
128       "<=");
129   {
130     addParamToNewType.addActionListener(new ActionListener() {
131       @Override
132       public void actionPerformed(ActionEvent e) {
133         transferInstances(defTypeParamsList, defTypeParamsModel,
134             newTypeParamsModel, newSetType);
135       }
136     });
137   }
138 
139   /** Button to remove a instance from the new type. */
140   private JButton remParamFromNewType = GUI.createIconButton("arrow_l2r.gif",
141       "=>");
142   {
143     remParamFromNewType.addActionListener(new ActionListener() {
144       @Override
145       public void actionPerformed(ActionEvent e) {
146         transferInstances(newTypeParamsList, newTypeParamsModel,
147             defTypeParamsModel, getEntity().getDefaultSetType());
148       }
149     });
150   }
151 
152   /**
153    * List of all available parameters. All parameters from the default type are
154    * available.
155    */
156   private final JList<ParameterInstance> defTypeParamsList = new JList<>(
157       defTypeParamsModel);
158 
159   /** List model of all parameters of new Settype. */
160   private final ParameterListModel newTypeParamsModel = new ParameterListModel();
161 
162   /** List of parameters of new Settype. */
163   private final JList<ParameterInstance> newTypeParamsList = new JList<>(
164       newTypeParamsModel);
165 
166   /**
167    * Default constructor.
168    * 
169    * @param projNode
170    *          the node representing the {@link ProjectionModel} for which a new
171    *          Settype shall be created
172    * @param projTree
173    *          the projection tree
174    */
175   public NewSetTypeDialog(ProjectionNode projNode, IProjectionTree projTree) {
176     super(projNode, projTree, DIALOG_WIDTH, DIALOG_HEIGHT, "Add Settype");
177     setModal(true);
178     newSetType = getEntity().createSetType("", "");
179     initUI();
180   }
181 
182   /**
183    * Initializes user interface.
184    */
185   private void initUI() {
186     setTitle("Create New Settype");
187 
188     JPanel namePanel = new JPanel(GUI.getStdBorderLayout());
189     namePanel.add(new JLabel("Name:"), BorderLayout.WEST);
190     namePanel.add(stName, BorderLayout.CENTER);
191 
192     // Set up parameter lists
193     defTypeParamsModel.updateSetType(getEntity().getDefaultSetType());
194     newTypeParamsModel.updateSetType(newSetType);
195 
196     JPanel addRemovePanel = new JPanel();
197     addRemovePanel.setLayout(new BoxLayout(addRemovePanel, BoxLayout.Y_AXIS));
198     addRemovePanel.add(addParamToNewType);
199     addRemovePanel.add(remParamFromNewType);
200 
201     // Create overall layout
202     FormLayout layout = new FormLayout(
203         "10dlu,d:grow,center:50dlu,right:d:grow,10dlu",
204         "10dlu,20dlu,10dlu,d:grow,50dlu,d:grow,10dlu,20dlu,10dlu");
205     JPanel contentPanel = new JPanel(layout);
206     layout.setColumnGroups(new int[][] { COLUMN_GROUPING });
207 
208     CellConstraints cc = new CellConstraints();
209     contentPanel.add(namePanel, cc.xy(COLUMN_INDEX_LHS, 2));
210 
211     contentPanel.add(new JScrollPane(newTypeParamsList), cc.xywh(
212         COLUMN_INDEX_LHS, SELECTION_PANEL_ROW_INDEX, SELECTION_PANEL_COL_WIDTH,
213         SELECTION_PANEL_ROW_HEIGHT));
214 
215     contentPanel.add(new JScrollPane(defTypeParamsList), cc.xywh(
216         COLUMN_INDEX_RHS, SELECTION_PANEL_ROW_INDEX, SELECTION_PANEL_COL_WIDTH,
217         SELECTION_PANEL_ROW_HEIGHT));
218 
219     contentPanel.add(addRemovePanel,
220         cc.xy(ADD_REMOVE_PANEL_COL_INDEX, ADD_REMOVE_PANEL_ROW_INDEX));
221 
222     contentPanel.add(getButtonPanel(),
223         cc.xy(BUTTON_PANEL_COL_INDEX, BUTTON_PANEL_ROW_INDEX));
224     setContentPane(contentPanel);
225   }
226 
227   /**
228    * Transfers instances from one Settype to the other.
229    * 
230    * @param fromList
231    *          the list containing the parameter instances of the source type
232    * @param from
233    *          the model of the source type's parameter instance list
234    * @param to
235    *          the model of the destination type's parameter instance list
236    * @param targetSetType
237    *          the target Settype
238    */
239   public void transferInstances(JList<ParameterInstance> fromList,
240       ParameterListModel from, ParameterListModel to, SetType targetSetType) {
241 
242     int firstIndex = fromList.getSelectedIndex();
243     List<ParameterInstance> instances = fromList.getSelectedValuesList();
244 
245     for (Object instance : instances) {
246       ParameterInstance instanceToBeAdded = (ParameterInstance) instance;
247       getEntity().assignParameterInstance(instanceToBeAdded, targetSetType,
248           true);
249     }
250 
251     to.refresh();
252     from.refresh();
253 
254     if (instances.size() > 0 && from.getSize() > 0) {
255       fromList
256           .setSelectedIndex(from.getSize() == firstIndex ? from.getSize() - 1
257               : firstIndex);
258     }
259   }
260 
261   /**
262    * Create Settype.
263    */
264   void createSetType() {
265     if (newTypeParamsModel.getSize() == 0) {
266       GUI.printErrorMessage(this, "Settype is empty",
267           "Settypes should at least cover one parameter.");
268       return;
269     }
270 
271     IP3MDatabase database = DatabaseFactory.getDatabaseSingleton();
272     newSetType.setName(stName.getText());
273     database.saveProjection(getEntity());
274     getProjectionTree().removeNodes(getEntityNode(),
275         newSetType.getDefinedParameters());
276     getProjectionTree().cleanTree();
277     SetTypeNode newNode = new SetTypeNode(newSetType);
278     getEntityNode().add(newNode);
279     getProjectionTree().refreshNodeSubStructure(getEntityNode());
280     getProjectionTree().selectNode(newNode);
281     setVisible(false);
282   }
283 
284   @Override
285   protected void cancel() {
286     getEntity().removeSetType(newSetType);
287     setVisible(false);
288   }
289 
290   /**
291    * Gets the new Settype.
292    * 
293    * @return the new Settype
294    */
295   public SetType getNewSetType() {
296     return newSetType;
297   }
298 
299   @Override
300   protected void ok() {
301     createSetType();
302   }
303 }