1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
41
42
43
44
45 public class NewProjectionDialog extends JDialog {
46
47
48 private static final long serialVersionUID = -4176931594515368446L;
49
50
51 public static final int DIALOG_WIDTH = 600;
52
53
54 public static final int DIALOG_HEIGHT = 300;
55
56
57 private static final int GAP_SIZE_CANCEL = 3;
58
59
60 private final IP3MDatabase database;
61
62
63 private JTextField name = new JTextField("New Projection");
64
65
66 private JTextArea description = new JTextArea("Description.");
67
68
69 private JTextField horizon = new JTextField(
70 Integer.toString(PPPModelFactory.DEFAULT_YEARS));
71
72
73 private JTextField generations = new JTextField(
74 Integer.toString(PPPModelFactory.DEFAULT_GENERATIONS));
75
76
77 private JTextField numAgeClasses = new JTextField(
78 Integer.toString(PPPModelFactory.DEFAULT_MAX_AGE));
79
80
81 private JTextField jumpOffYear = new JTextField(
82 Integer.toString(PPPModelFactory.DEFAULT_JUMP_OFF_YEAR));
83
84
85 private ProjectionModel newProjection;
86
87
88 private SubPopulationModel subPopulationModel = PPPModelFactory.DEFAULT_SUBPOPULATION_MODEL;
89
90
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
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
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
129
130
131
132
133
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
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
173
174
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
189
190
191
192 public ProjectionModel getNewProjection() {
193 return newProjection;
194 }
195
196
197
198
199
200
201 public boolean hasCreatedNewProjection() {
202 return newProjection != null;
203 }
204
205
206
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 }