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.panels.projections;
17  
18  import java.awt.event.ActionEvent;
19  import java.awt.event.ActionListener;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import javax.swing.JButton;
24  import javax.swing.JLabel;
25  import javax.swing.JPanel;
26  import javax.swing.tree.TreePath;
27  
28  import p3j.database.DatabaseFactory;
29  import p3j.gui.P3J;
30  import p3j.gui.misc.SubNodeSummary;
31  import p3j.gui.panels.PropertiesShowPanelFactory;
32  import p3j.misc.gui.GUI;
33  import p3j.misc.math.Matrix2D;
34  import p3j.pppm.parameters.ParameterAssignment;
35  import p3j.pppm.parameters.ParameterAssignmentSet;
36  import p3j.pppm.parameters.ParameterInstance;
37  import p3j.pppm.sets.Set;
38  
39  /**
40   * Node that represents a {@link ParameterInstance}.
41   * 
42   * Created: August 24, 2008
43   * 
44   * @author Christina Bohk
45   * @author Roland Ewald
46   * 
47   */
48  public class ParameterInstanceNode extends
49      ProjectionTreeNode<ParameterInstance> {
50  
51    /**
52     * Serialization ID.
53     */
54    private static final long serialVersionUID = 1069251581222368972L;
55  
56    /**
57     * Name for new assignment and title for the button to create one.
58     */
59    public static final String NEW_ASSIGNMENT = "New Assignment";
60  
61    /**
62     * Default constructor.
63     * 
64     * @param instance
65     *          the instance that is represented by this node
66     */
67    public ParameterInstanceNode(ParameterInstance instance) {
68      super(instance, getDisplayName(instance));
69    }
70  
71    @Override
72    public JPanel selected(final TreePath selectionPath,
73        final IProjectionTree projTree) {
74  
75      final ParameterInstanceNode node = this;
76      final Set set = getProjectionEntity(Set.class);
77  
78      JButton adjustProbability = new JButton("Adjust Probability");
79  
80      // Create new assignment
81      JButton newAssignment = new JButton(NEW_ASSIGNMENT);
82      newAssignment.addActionListener(new ParamAssignmentCreator(node, projTree));
83  
84      List<JButton> buttons = new ArrayList<JButton>();
85      buttons.add(adjustProbability);
86      buttons.add(newAssignment);
87      PropertiesShowPanelFactory pspf = new PropertiesShowPanelFactory(buttons, 1);
88      pspf.sep("General Information");
89      pspf.app("Population:", getEntity().getParameter().getPopulation());
90      pspf.app("Parameter:", getDisplayName(getEntity()));
91      pspf.app("Descendant Generation:", getEntity().getGeneration() <= 0 ? "-"
92          : getEntity().getGeneration());
93      pspf.app("Matrix width:", getEntity().getValueWidth().getDimension());
94      pspf.app("Matrix height:", getEntity().getValueHeight().getDimension());
95  
96      ParameterAssignmentSet assignmentSet = set
97          .getParameterAssignments(getEntity());
98      java.util.Set<ParameterAssignment> assignments = assignmentSet
99          .getAssignments();
100     int size = assignments.size();
101     pspf.app("Assignments:", size);
102     final JLabel probSum = new JLabel(assignmentSet.getProbabilitySum() + "");
103     pspf.app("Probability sum:", probSum);
104     pspf.appPreview(new SubNodeSummary<ParameterAssignment>(this, projTree,
105         ParameterAssignment.class));
106 
107     // Adjust probability
108     adjustProbability.addActionListener(new ActionListener() {
109       @Override
110       public void actionPerformed(ActionEvent e) {
111         ParameterAssignmentSet paSet = set.getParameterAssignments(getEntity());
112         paSet.adjustProbability();
113         DatabaseFactory.getDatabaseSingleton().saveSet(set);
114         probSum.setText(paSet.getProbabilitySum() + "");
115         getContentPanel().repaint();
116         projTree.recursiveRefresh(node);
117       }
118     });
119 
120     return pspf.constructPanel();
121   }
122 
123   /**
124    * Gets the display name. In case this refers to direct
125    * emigration/immigration, the name to displayed is prefixed with 'Direct '.
126    * 
127    * @param instance
128    *          the instance
129    * 
130    * @return the display name
131    */
132   public static String getDisplayName(ParameterInstance instance) {
133     return instance.getParameter().getName();
134   }
135 
136 }
137 
138 /**
139  * 
140  * Creates a new parameter assignment.
141  * 
142  * Created: August 26, 2008
143  * 
144  * @author Christina Bohk
145  * @author Roland Ewald
146  * 
147  */
148 class ParamAssignmentCreator implements ActionListener {
149 
150   /** Tree projection tree. */
151   private final IProjectionTree projectionTree;
152 
153   /** Node holding parameter instance. */
154   private final ParameterInstanceNode node;
155 
156   /**
157    * Default constructor.
158    * 
159    * @param piNode
160    *          current node
161    * @param projTree
162    *          the projection tree (for changing the selection, etc.)
163    */
164   ParamAssignmentCreator(ParameterInstanceNode piNode, IProjectionTree projTree) {
165     this.projectionTree = projTree;
166     this.node = piNode;
167   }
168 
169   @Override
170   public void actionPerformed(ActionEvent e) {
171     Set set = node.getProjectionEntity(Set.class);
172     if (set == null) {
173       return;
174     }
175     try {
176       ParameterAssignment newAssignment = DatabaseFactory
177           .getDatabaseSingleton().newParameterAssignment(
178               node.getEntity(),
179               ParameterInstanceNode.NEW_ASSIGNMENT,
180               "",
181               0.0,
182               0.0,
183               // TODO: Matrices should be stored just the other way round, this
184               // would be more 'natural' (take care: this requires to remove
185               // automatic transposition in the UI)
186               new Matrix2D(new double[node.getEntity().getValueWidth()
187                   .getDimension()][node.getEntity().getValueHeight()
188                   .getDimension()],
189                   node.getEntity().getValueWidth().getLabel(), node.getEntity()
190                       .getValueHeight().getLabel()));
191       set.addParameterAssignment(newAssignment);
192       DatabaseFactory.getDatabaseSingleton().saveSet(set);
193       ParameterAssignmentNode child = new ParameterAssignmentNode(newAssignment);
194       node.add(child);
195       projectionTree.nodeAdded(node,
196           set.getParameterAssignments(node.getEntity()).size() - 1);
197       projectionTree.selectNode(child);
198     } catch (Exception ex) {
199       GUI.printErrorMessage(P3J.getInstance(), "Error: Matrix creation failed",
200           ex.getMessage(), ex);
201     }
202   }
203 }