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.Frame;
19  import java.awt.event.ActionEvent;
20  import java.awt.event.ActionListener;
21  
22  import javax.swing.JButton;
23  import javax.swing.JDialog;
24  import javax.swing.JPanel;
25  import javax.swing.JTextArea;
26  import javax.swing.JTextField;
27  
28  import p3j.database.IP3MDatabase;
29  import p3j.misc.Misc;
30  import p3j.misc.gui.GUI;
31  import p3j.pppm.PPPModelFactory;
32  import p3j.pppm.ProjectionModel;
33  import p3j.pppm.SubPopulationModel;
34  
35  import com.jgoodies.forms.builder.ButtonBarBuilder2;
36  import com.jgoodies.forms.layout.CellConstraints;
37  import com.jgoodies.forms.layout.FormLayout;
38  
39  /**
40   * Creates a new projection.
41   * 
42   * @author Christina Bohk
43   * @author Roland Ewald
44   */
45  public class NewProjectionDialog extends JDialog {
46  
47    /** Serialization ID. */
48    private static final long serialVersionUID = -4176931594515368446L;
49  
50    /** Width of the dialog. */
51    public static final int DIALOG_WIDTH = 600;
52  
53    /** Height of the dialog. */
54    public static final int DIALOG_HEIGHT = 300;
55  
56    /** The number of 'unrelated' gaps between the 'Cancel' button and the others. */
57    private static final int GAP_SIZE_CANCEL = 3;
58  
59    /** The database in which the new projection shall be stored. */
60    private final IP3MDatabase database;
61  
62    /** The field to enter a name. */
63    private JTextField name = new JTextField("New Projection");
64  
65    /** The description of the projection. */
66    private JTextArea description = new JTextArea("Description.");
67  
68    /** The field to enter the projection horizon. */
69    private JTextField horizon = new JTextField(
70        Integer.toString(PPPModelFactory.DEFAULT_YEARS));
71  
72    /** The field to enter the number of generations. */
73    private JTextField generations = new JTextField(
74        Integer.toString(PPPModelFactory.DEFAULT_GENERATIONS));
75  
76    /** The maximum age to be considered. */
77    private JTextField numAgeClasses = new JTextField(
78        Integer.toString(PPPModelFactory.DEFAULT_MAX_AGE));
79  
80    /** The field to enter the jump-off year. */
81    private JTextField jumpOffYear = new JTextField(
82        Integer.toString(PPPModelFactory.DEFAULT_JUMP_OFF_YEAR));
83  
84    /** The newly created projection. */
85    private ProjectionModel newProjection;
86  
87    /** The sub-population model to be used. */
88    private SubPopulationModel subPopulationModel = PPPModelFactory.DEFAULT_SUBPOPULATION_MODEL;
89  
90    /** The button to create a new projection. */
91    private JButton newProjectionButton = new JButton("Add Projection");
92    {
93      newProjectionButton.addActionListener(new ActionListener() {
94        @Override
95        public void actionPerformed(ActionEvent e) {
96          createNewProjection();
97        }
98      });
99    }
100 
101   /** The button to cancel projection creation. */
102   private JButton cancelButton = new JButton("Cancel");
103   {
104     cancelButton.addActionListener(new ActionListener() {
105       @Override
106       public void actionPerformed(ActionEvent e) {
107         setVisible(false);
108       }
109     });
110   }
111 
112   /** The button to edit the sub-population model. */
113   private JButton editSubPopsButton = new JButton("Edit Sub-Populations");
114   {
115     editSubPopsButton.addActionListener(new ActionListener() {
116       @Override
117       public void actionPerformed(ActionEvent e) {
118         SubPopulationModelEditDialog dialog = new SubPopulationModelEditDialog(
119             subPopulationModel, true);
120         dialog.setVisible(true);
121         if (dialog.isConfirmed())
122           subPopulationModel = dialog.getSubPopulationModel();
123       }
124     });
125   }
126 
127   /**
128    * Instantiates a new new projection dialog.
129    * 
130    * @param owner
131    *          the owner frame
132    * @param p3mDatabase
133    *          the p3m database
134    */
135   public NewProjectionDialog(Frame owner, IP3MDatabase p3mDatabase) {
136     super(owner, true);
137     setTitle("Define New Projection");
138     setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
139     database = p3mDatabase;
140     GUI.centerOnScreen(this);
141     initialize();
142   }
143 
144   /**
145    * Initializes content of the panel.
146    */
147   private void initialize() {
148     FormLayout layout = new FormLayout("10dlu,d:grow,10dlu,d:grow,10dlu",
149         "10dlu,d,10dlu,fill:d:grow,10dlu,d,10dlu,d,10dlu,d,10dlu,d,10dlu,d,10dlu");
150     JPanel contentPanel = new JPanel(layout);
151 
152     int currRow = GUI.ROW_SKIP_LAYOUT;
153     currRow = GUI.addRowToPanel(contentPanel, Misc.GUI_LABEL_NAME, name,
154         currRow);
155     currRow = GUI.addRowToPanel(contentPanel, Misc.GUI_LABEL_DESCRIPTION,
156         description, currRow);
157     currRow = GUI.addRowToPanel(contentPanel,
158         Misc.GUI_LABEL_PROJECTION_HORIZON, horizon, currRow);
159     currRow = GUI.addRowToPanel(contentPanel,
160         Misc.GUI_LABEL_DESCENDANT_GENERATIONS, generations, currRow);
161     currRow = GUI.addRowToPanel(contentPanel, Misc.GUI_LABEL_NUM_AGE_CLASSES,
162         numAgeClasses, currRow);
163     currRow = GUI.addRowToPanel(contentPanel, Misc.GUI_LABEL_JUMP_OFF_YEAR,
164         jumpOffYear, currRow);
165 
166     contentPanel.add(getButtonPanel(),
167         new CellConstraints().xy(GUI.INPUT_COLUMN_INDEX, currRow));
168     setContentPane(contentPanel);
169   }
170 
171   /**
172    * Creates a panel containing the buttons.
173    * 
174    * @return a panel containing the buttons
175    */
176   private JPanel getButtonPanel() {
177     ButtonBarBuilder2 bbBuilder = new ButtonBarBuilder2();
178     bbBuilder.addButton(cancelButton);
179     for (int i = 0; i < GAP_SIZE_CANCEL; i++)
180       bbBuilder.addUnrelatedGap();
181     bbBuilder.addButton(editSubPopsButton);
182     bbBuilder.addRelatedGap();
183     bbBuilder.addButton(newProjectionButton);
184     return bbBuilder.getPanel();
185   }
186 
187   /**
188    * Gets the new projection.
189    * 
190    * @return the new projection
191    */
192   public ProjectionModel getNewProjection() {
193     return newProjection;
194   }
195 
196   /**
197    * Checks for created new projection.
198    * 
199    * @return true, if successful
200    */
201   public boolean hasCreatedNewProjection() {
202     return newProjection != null;
203   }
204 
205   /**
206    * Creates a new projection.
207    */
208   private void createNewProjection() {
209     int gens = -1;
210     int years = -1;
211     int maxAge = -1;
212     int jOffYear = -1;
213     try {
214       gens = Integer.parseInt(generations.getText()) + 1;
215       years = Integer.parseInt(horizon.getText());
216       maxAge = Integer.parseInt(numAgeClasses.getText()) - 1;
217       jOffYear = Integer.parseInt(jumpOffYear.getText());
218     } catch (NumberFormatException ex) {
219       GUI.printErrorMessage(
220           NewProjectionDialog.this,
221           "Erroneous input.",
222           "The numeric fields may only contain positive integers:"
223               + ex.getMessage(), ex);
224     }
225 
226     try {
227       newProjection = PPPModelFactory.createModel(name.getText(),
228           description.getText(), gens, years, maxAge, jOffYear,
229           subPopulationModel);
230       database.newProjection(newProjection);
231       setVisible(false);
232     } catch (Exception ex) {
233       GUI.printErrorMessage(NewProjectionDialog.this,
234           "Error while creating new projection",
235           "Creation of new projection failed:" + ex.getMessage(), ex);
236     }
237   }
238 
239 }