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