1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
42
43
44
45
46
47
48
49 public class ParameterInstanceNode extends
50 ProjectionTreeNode<ParameterInstance> {
51
52
53
54
55 private static final long serialVersionUID = 1069251581222368972L;
56
57
58
59
60 public static final String NEW_ASSIGNMENT = "New Assignment";
61
62
63
64
65
66
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
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
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
126
127
128
129
130
131
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
147
148
149
150
151
152
153
154 class ParamAssignmentCreator implements ActionListener {
155
156
157 private final IProjectionTree projectionTree;
158
159
160 private final ParameterInstanceNode node;
161
162
163
164
165
166
167
168
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
190
191
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 }