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