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
34 import com.jgoodies.forms.builder.ButtonBarBuilder2;
35 import com.jgoodies.forms.layout.CellConstraints;
36 import com.jgoodies.forms.layout.FormLayout;
37
38
39
40
41
42
43
44 public class NewProjectionDialog extends JDialog {
45
46
47 private static final long serialVersionUID = -4176931594515368446L;
48
49
50 public static final int DIALOG_WIDTH = 400;
51
52
53 public static final int DIALOG_HEIGHT = 300;
54
55
56 private final IP3MDatabase database;
57
58
59 private JTextField name = new JTextField("New Projection");
60
61
62 private JTextArea description = new JTextArea("Description.");
63
64
65 private JTextField horizon = new JTextField(
66 Integer.toString(PPPModelFactory.DEFAULT_YEARS));
67
68
69 private JTextField generations = new JTextField(
70 Integer.toString(PPPModelFactory.DEFAULT_GENERATIONS));
71
72
73 private JTextField numAgeClasses = new JTextField(
74 Integer.toString(PPPModelFactory.DEFAULT_MAX_AGE));
75
76
77 private JTextField jumpOffYear = new JTextField(
78 Integer.toString(PPPModelFactory.DEFAULT_JUMP_OFF_YEAR));
79
80
81 private ProjectionModel newProjection;
82
83
84 private JButton newProjectionButton = new JButton("Add Projection");
85 {
86 newProjectionButton.addActionListener(new ActionListener() {
87 @Override
88 public void actionPerformed(ActionEvent e) {
89 createNewProjection();
90 }
91 });
92 }
93
94
95 private JButton cancelButton = new JButton("Cancel");
96 {
97 cancelButton.addActionListener(new ActionListener() {
98 @Override
99 public void actionPerformed(ActionEvent e) {
100 setVisible(false);
101 }
102 });
103 }
104
105
106
107
108
109
110
111
112
113 public NewProjectionDialog(Frame owner, IP3MDatabase p3mDatabase) {
114 super(owner, true);
115 setTitle("Define New Projection");
116 setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
117 database = p3mDatabase;
118 GUI.centerOnScreen(this);
119 initialize();
120 }
121
122
123
124
125 private void initialize() {
126 FormLayout layout = new FormLayout("10dlu,d:grow,10dlu,d:grow,10dlu",
127 "10dlu,d,10dlu,fill:d:grow,10dlu,d,10dlu,d,10dlu,d,10dlu,d,10dlu,d,10dlu");
128 JPanel contentPanel = new JPanel(layout);
129
130 int currRow = GUI.ROW_SKIP_LAYOUT;
131 currRow = GUI.addRowToPanel(contentPanel, Misc.GUI_LABEL_NAME, name,
132 currRow);
133 currRow = GUI.addRowToPanel(contentPanel, Misc.GUI_LABEL_DESCRIPTION,
134 description, currRow);
135 currRow = GUI.addRowToPanel(contentPanel,
136 Misc.GUI_LABEL_PROJECTION_HORIZON, horizon, currRow);
137 currRow = GUI.addRowToPanel(contentPanel,
138 Misc.GUI_LABEL_DESCENDANT_GENERATIONS, generations, currRow);
139 currRow = GUI.addRowToPanel(contentPanel, Misc.GUI_LABEL_NUM_AGE_CLASSES,
140 numAgeClasses, currRow);
141 currRow = GUI.addRowToPanel(contentPanel, Misc.GUI_LABEL_JUMP_OFF_YEAR,
142 jumpOffYear, currRow);
143
144 contentPanel.add(getButtonPanel(),
145 new CellConstraints().xy(GUI.INPUT_COLUMN_INDEX, currRow));
146 setContentPane(contentPanel);
147 }
148
149
150
151
152
153
154 private JPanel getButtonPanel() {
155 ButtonBarBuilder2 bbBuilder = new ButtonBarBuilder2();
156 bbBuilder.addButton(newProjectionButton);
157 bbBuilder.addRelatedGap();
158 bbBuilder.addButton(cancelButton);
159 return bbBuilder.getPanel();
160 }
161
162
163
164
165
166
167 public ProjectionModel getNewProjection() {
168 return newProjection;
169 }
170
171
172
173
174
175
176 public boolean hasCreatedNewProjection() {
177 return newProjection != null;
178 }
179
180
181
182
183 private void createNewProjection() {
184 int gens = -1;
185 int years = -1;
186 int maxAge = -1;
187 int jOffYear = -1;
188 try {
189 gens = Integer.parseInt(generations.getText()) + 1;
190 years = Integer.parseInt(horizon.getText());
191 maxAge = Integer.parseInt(numAgeClasses.getText()) - 1;
192 jOffYear = Integer.parseInt(jumpOffYear.getText());
193 } catch (NumberFormatException ex) {
194 GUI.printErrorMessage(
195 NewProjectionDialog.this,
196 "Erroneous input.",
197 "The numeric fields may only contain positive integers:"
198 + ex.getMessage(), ex);
199 }
200
201 try {
202 newProjection = new PPPModelFactory().createModel(name.getText(),
203 description.getText(), gens, years, maxAge, jOffYear);
204 database.newProjection(newProjection);
205 setVisible(false);
206 } catch (Exception ex) {
207 GUI.printErrorMessage(NewProjectionDialog.this,
208 "Error while creating new projection",
209 "Creation of new projection failed:" + ex.getMessage(), ex);
210 }
211 }
212
213 }