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.sets.Set;
38
39
40
41
42
43
44
45
46
47
48 public class ParameterInstanceNode extends
49 ProjectionTreeNode<ParameterInstance> {
50
51
52
53
54 private static final long serialVersionUID = 1069251581222368972L;
55
56
57
58
59 public static final String NEW_ASSIGNMENT = "New Assignment";
60
61
62
63
64
65
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
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
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
125
126
127
128
129
130
131
132 public static String getDisplayName(ParameterInstance instance) {
133 return instance.getParameter().getName();
134 }
135
136 }
137
138
139
140
141
142
143
144
145
146
147
148 class ParamAssignmentCreator implements ActionListener {
149
150
151 private final IProjectionTree projectionTree;
152
153
154 private final ParameterInstanceNode node;
155
156
157
158
159
160
161
162
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
184
185
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 }