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.event.ActionEvent;
19  import java.awt.event.ActionListener;
20  
21  import javax.swing.JLabel;
22  import javax.swing.JTextField;
23  import javax.swing.SwingWorker;
24  
25  import p3j.database.DatabaseFactory;
26  import p3j.gui.P3J;
27  import p3j.gui.panels.PropertiesShowPanelFactory;
28  import p3j.misc.MatrixDimension;
29  import p3j.misc.gui.GUI;
30  import p3j.misc.math.Matrix2D;
31  import p3j.pppm.ProjectionModel;
32  import p3j.pppm.parameters.ParameterAssignment;
33  import p3j.pppm.parameters.ParameterAssignmentSet;
34  import p3j.pppm.sets.SetType;
35  
36  /**
37   * Simple dialog to adjust maximum age.
38   * 
39   * @author Christina Bohk
40   * @author Roland Ewald
41   */
42  public class AdjustMaxAgeDialog extends ProcessProjectionDialog {
43  
44  	/** The Constant serialVersionUID. */
45  	private static final long serialVersionUID = -3471025733131933944L;
46  
47  	/** The text field to set the new number of age classes. */
48  	private final JTextField newNumOfAgeClasses = new JTextField("");
49  
50  	/**
51  	 * Instantiates a new adjust max age dialog.
52  	 * 
53  	 * @param projModel
54  	 *          the projection model
55  	 */
56  	public AdjustMaxAgeDialog(ProjectionModel projModel) {
57  		super(projModel);
58  		initUI();
59  	}
60  
61  	/**
62  	 * Initialize the user interface.
63  	 */
64  	private void initUI() {
65  		setTitle("Number of Age Classes for '" + getProjectionModel().getName()
66  		    + "'");
67  		setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
68  		setModal(true);
69  		GUI.centerOnScreen(this);
70  		newNumOfAgeClasses.setText(""
71  		    + getProjectionModel().getNumberOfAgeClasses());
72  		PropertiesShowPanelFactory pspf = new PropertiesShowPanelFactory(
73  		    getButtons(), 1);
74  		pspf.sep("Adjust Age Classes");
75  		pspf.app("New Number of Age Classes:", newNumOfAgeClasses);
76  		getContentPane().add(pspf.constructPanel());
77  	}
78  
79  	/**
80  	 * Adjust the age of a projection model.
81  	 * 
82  	 * @param newAge
83  	 *          the new age to be set
84  	 */
85  	protected void adjustAge(final int newAge) {
86  		if (newAge < 0 || newAge == getProjectionModel().getNumberOfAgeClasses()) {
87  			return;
88  		}
89  
90  		final JLabel status = reinitializeUIForAdjustment();
91  
92  		SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
93  			@Override
94  			protected Void doInBackground() {
95  				int obsoleteAgeClasses = getProjectionModel().getNumberOfAgeClasses()
96  				    - newAge;
97  				for (SetType setType : getProjectionModel().getAllSetTypes()) {
98  					status.setText("Processing Settype '" + setType.getName() + "'");
99  					adjustAgeForSetType(setType, obsoleteAgeClasses);
100 				}
101 				getProjectionModel().setMaximumAge(newAge - 1);
102 				DatabaseFactory.getDatabaseSingleton().saveProjection(
103 				    getProjectionModel());
104 				return null;
105 			}
106 
107 			@Override
108 			public void done() {
109 				setVisible(false);
110 			}
111 		};
112 
113 		worker.execute();
114 	}
115 
116 	/**
117 	 * Adjust age for Settype.
118 	 * 
119 	 * @param setType
120 	 *          the Settype to be adjusted
121 	 * @param obsoleteClasses
122 	 *          the number obsolete age classes
123 	 */
124 	private void adjustAgeForSetType(SetType setType, int obsoleteClasses) {
125 		for (p3j.pppm.sets.Set set : setType.getSets()) {
126 			for (ParameterAssignmentSet assignmentSet : set.getSetData().values()) {
127 				for (ParameterAssignment assignment : assignmentSet.getAssignments()) {
128 					adjustParameterAssignment(assignment, obsoleteClasses);
129 				}
130 			}
131 		}
132 
133 	}
134 
135 	/**
136 	 * Adjust age for parameter assignment.
137 	 * 
138 	 * @param assignment
139 	 *          the assignment to be adjusted
140 	 * @param obsoleteClasses
141 	 *          the number of obsolete classes
142 	 */
143 	private void adjustParameterAssignment(ParameterAssignment assignment,
144 	    int obsoleteClasses) {
145 		boolean changeHeight = assignment.getParamInstance().getValueHeight() == MatrixDimension.AGES;
146 		boolean changeWidth = assignment.getParamInstance().getValueWidth() == MatrixDimension.AGES;
147 		if (!changeHeight && !changeWidth) {
148 			return;
149 		}
150 
151 		Matrix2D value = assignment.getMatrixValue();
152 		int numOfRows = value.rows();
153 		int numOfColumns = value.columns();
154 		// TODO: Remove double-transposition of matrices.
155 		assignment.setMatrixValue(value.getResizedMatrix((changeWidth ? numOfRows
156 		    - obsoleteClasses : numOfRows), (changeHeight ? numOfColumns
157 		    - obsoleteClasses : numOfColumns)));
158 		DatabaseFactory.getDatabaseSingleton().saveParameterAssignment(assignment);
159 	}
160 
161 	@Override
162 	protected void addOKButtonAction() {
163 		getOkButton().addActionListener(new ActionListener() {
164 			@Override
165 			public void actionPerformed(ActionEvent e) {
166 				int newNumAgeClasses = -1;
167 				try {
168 					newNumAgeClasses = Integer.parseInt(newNumOfAgeClasses.getText());
169 				} catch (NumberFormatException ex) {
170 					GUI.printErrorMessage(P3J.getInstance(),
171 					    "Error reading the new number of age classes", ex.getMessage(),
172 					    ex);
173 					return;
174 				}
175 				if (GUI.printQuestion(
176 				    P3J.getInstance(),
177 				    "Do you really want to set the number of age classes?",
178 				    "All obsolete data will be REMOVED PERMANENTLY. Unknown values will be filled with zeros.")) {
179 					adjustAge(newNumAgeClasses);
180 				}
181 			}
182 		});
183 	}
184 
185 }