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  
34  import com.jgoodies.forms.builder.ButtonBarBuilder2;
35  import com.jgoodies.forms.layout.CellConstraints;
36  import com.jgoodies.forms.layout.FormLayout;
37  
38  /**
39   * Creates a new projection.
40   * 
41   * @author Christina Bohk
42   * @author Roland Ewald
43   */
44  public class NewProjectionDialog extends JDialog {
45  
46  	/** Serialization ID. */
47  	private static final long serialVersionUID = -4176931594515368446L;
48  
49  	/** Width of the dialog. */
50  	public static final int DIALOG_WIDTH = 400;
51  
52  	/** Height of the dialog. */
53  	public static final int DIALOG_HEIGHT = 300;
54  
55  	/** The database in which the new projection shall be stored. */
56  	private final IP3MDatabase database;
57  
58  	/** The field to enter a name. */
59  	private JTextField name = new JTextField("New Projection");
60  
61  	/** The description of the projection. */
62  	private JTextArea description = new JTextArea("Description.");
63  
64  	/** The field to enter the projection horizon. */
65  	private JTextField horizon = new JTextField(
66  	    Integer.toString(PPPModelFactory.DEFAULT_YEARS));
67  
68  	/** The field to enter the number of generations. */
69  	private JTextField generations = new JTextField(
70  	    Integer.toString(PPPModelFactory.DEFAULT_GENERATIONS));
71  
72  	/** The maximum age to be considered. */
73  	private JTextField numAgeClasses = new JTextField(
74  	    Integer.toString(PPPModelFactory.DEFAULT_MAX_AGE));
75  
76  	/** The field to enter the jump-off year. */
77  	private JTextField jumpOffYear = new JTextField(
78  	    Integer.toString(PPPModelFactory.DEFAULT_JUMP_OFF_YEAR));
79  
80  	/** Reference to the newly created projection. */
81  	private ProjectionModel newProjection;
82  
83  	/** The button to create a new projection. */
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  	/** The button to cancel projection creation. */
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 	 * Instantiates a new new projection dialog.
107 	 * 
108 	 * @param owner
109 	 *          the owner frame
110 	 * @param p3mDatabase
111 	 *          the p3m database
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 	 * Initializes content of the panel.
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 	 * Creates a panel containing the buttons.
151 	 * 
152 	 * @return a panel containing the buttons
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 	 * Gets the new projection.
164 	 * 
165 	 * @return the new projection
166 	 */
167 	public ProjectionModel getNewProjection() {
168 		return newProjection;
169 	}
170 
171 	/**
172 	 * Checks for created new projection.
173 	 * 
174 	 * @return true, if successful
175 	 */
176 	public boolean hasCreatedNewProjection() {
177 		return newProjection != null;
178 	}
179 
180 	/**
181 	 * Creates a new projection.
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 }