1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package fr.ifremer.isisfish.ui.sensitivity;
27
28 import static org.nuiton.i18n.I18n.t;
29
30 import java.awt.HeadlessException;
31 import javax.swing.JDialog;
32 import javax.swing.JOptionPane;
33 import javax.swing.JTree;
34 import javax.swing.SwingUtilities;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.junit.Assume;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.nuiton.math.matrix.MatrixFactory;
42 import org.nuiton.math.matrix.MatrixND;
43
44 import fr.ifremer.isisfish.AbstractIsisFishTest;
45 import fr.ifremer.isisfish.simulator.sensitivity.Distribution;
46 import fr.ifremer.isisfish.simulator.sensitivity.Factor;
47 import fr.ifremer.isisfish.simulator.sensitivity.FactorGroup;
48 import fr.ifremer.isisfish.simulator.sensitivity.domain.ContinuousDomain;
49 import fr.ifremer.isisfish.simulator.sensitivity.domain.DiscreteDomain;
50 import fr.ifremer.isisfish.ui.sensitivity.model.FactorTreeCellRenderer;
51 import fr.ifremer.isisfish.ui.sensitivity.model.FactorTreeModel;
52
53
54
55
56
57
58
59
60
61
62
63
64 public class FactorTreeModelTest extends AbstractIsisFishTest {
65
66
67 private static final Log log = LogFactory.getLog(FactorTreeModelTest.class);
68
69 @BeforeClass
70 public static void disableTests() {
71 Assume.assumeTrue(!java.awt.GraphicsEnvironment.isHeadless());
72 }
73
74
75
76
77
78
79 protected FactorGroup getFactorGroup() {
80
81 Factor factor1 = new Factor(
82 "testint");
83 ContinuousDomain domain1 = new ContinuousDomain(Distribution.QUNIFMM);
84 domain1.addDistributionParam("min", 0.0);
85 domain1.addDistributionParam("max", 50.0);
86 factor1.setDomain(domain1);
87 factor1.setPath("org.nuiton.factor#1234567890#0.12242345354#name");
88 factor1.setValueForIdentifier(0.5);
89
90
91 MatrixND matrix1 = MatrixFactory.getInstance().create("test1",
92 new int[] { 3, 2 }, new String[] { "col1", "col2" });
93 matrix1.setValue(new int[] { 0, 0 }, 13);
94 matrix1.setValue(new int[] { 0, 1 }, -14);
95 matrix1.setValue(new int[] { 1, 0 }, 21);
96 matrix1.setValue(new int[] { 1, 1 }, 2);
97 matrix1.setValue(new int[] { 2, 0 }, 12);
98 matrix1.setValue(new int[] { 2, 1 }, -1);
99
100
101 MatrixND matrix2 = MatrixFactory.getInstance().create("test2",
102 new int[] { 2, 3 }, new String[] { "col1", "col2" });
103 matrix2.setValue(new int[] { 0, 0 }, 9999);
104 matrix2.setValue(new int[] { 0, 1 }, 15000);
105 matrix2.setValue(new int[] { 0, 2 }, -40000);
106 matrix2.setValue(new int[] { 1, 0 }, 21345);
107 matrix2.setValue(new int[] { 1, 1 }, 81000);
108 matrix2.setValue(new int[] { 1, 2 }, -13000);
109
110
111 Factor factor2 = new Factor(
112 "testmatrix");
113 DiscreteDomain domain2 = new DiscreteDomain();
114 domain2.getValues().put("m1", matrix1);
115 domain2.getValues().put("m2", matrix2);
116 factor2.setDomain(domain2);
117 factor2.setPath("org.nuiton.math.matrix.MatrixND#563456293453#2.456347646#dim");
118 factor2.setValueForIdentifier("m2");
119
120 FactorGroup factorGroup = new FactorGroup("test");
121 factorGroup.addFactor(factor1);
122 factorGroup.addFactor(factor2);
123
124 return factorGroup;
125 }
126
127
128
129
130 @Test
131 public void testJTreeModel() throws InterruptedException {
132
133 final JTree tree = new JTree();
134 tree.setRootVisible(true);
135 FactorTreeModel model = new FactorTreeModel(getFactorGroup());
136 tree.setModel(model);
137 tree.setCellRenderer(new FactorTreeCellRenderer());
138
139 try {
140 final JDialog dialog = new JDialog();
141 SwingUtilities.invokeLater(new Runnable() {
142 @Override
143 public void run() {
144 JOptionPane.showMessageDialog(dialog, tree,
145 t("Tree factor model"),
146 JOptionPane.INFORMATION_MESSAGE);
147 }
148 });
149
150 Thread t = new Thread(new Runnable() {
151 public void run() {
152 try {
153 Thread.sleep(1000);
154 } catch (InterruptedException e) {
155
156 }
157 dialog.dispose();
158 }
159 });
160 t.start();
161
162
163
164 t.join();
165 } catch (HeadlessException he) {
166 if (log.isErrorEnabled()) {
167 log.error("No X11 display available", he);
168 }
169 }
170 }
171 }