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
27
28
29
30
31
32
33 package net.sf.jtreemap.swing.example;
34
35 import java.awt.BorderLayout;
36 import java.awt.Color;
37 import java.awt.Container;
38 import java.awt.Dimension;
39 import java.awt.Font;
40 import java.awt.GridBagConstraints;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.io.BufferedReader;
44 import java.io.IOException;
45 import java.io.InputStreamReader;
46 import java.net.MalformedURLException;
47 import java.net.URL;
48 import java.net.URLConnection;
49 import java.text.ParseException;
50
51 import javax.swing.BorderFactory;
52 import javax.swing.JApplet;
53 import javax.swing.JComboBox;
54 import javax.swing.JLabel;
55 import javax.swing.JOptionPane;
56 import javax.swing.JPanel;
57 import javax.swing.JScrollPane;
58 import javax.swing.JSplitPane;
59 import javax.swing.JTree;
60 import javax.swing.border.TitledBorder;
61 import javax.swing.event.TreeSelectionEvent;
62 import javax.swing.event.TreeSelectionListener;
63 import javax.swing.tree.DefaultTreeModel;
64
65 import net.sf.jtreemap.swing.ColorProvider;
66 import net.sf.jtreemap.swing.JTreeMap;
67 import net.sf.jtreemap.swing.SplitBySortedWeight;
68 import net.sf.jtreemap.swing.TreeMapNode;
69 import net.sf.jtreemap.swing.provider.HSBTreeMapColorProvider;
70 import net.sf.jtreemap.swing.provider.RandomColorProvider;
71 import net.sf.jtreemap.swing.provider.RedGreenColorProvider;
72 import net.sf.jtreemap.swing.provider.ZoomPopupMenu;
73
74 /**
75 * Test of JTreeMap in a JApplet.
76 * Accepts 3 parameters;
77 * "dataFile" which is the path to data file relative to code base.
78 * "dataFileType" is either dt3 or xml.
79 * "showTM3CTonf", true if the tm3 configuration panel should be shown for tm3 files.
80 *
81 * @author Laurent Dutheil
82 */
83 public class JTreeMapAppletExample extends JApplet {
84
85 private static final double CONSTRAINT_WEIGHTX = 0.5;
86
87 private static final int SCROLLPANE_WIDTH = 140;
88
89 private static final String XML = "xml";
90
91 private static final String TM3 = "tm3";
92
93 private static final int DEFAULT_FONT_SIZE = 12;
94
95 private static final long serialVersionUID = -8376357344981512167L;
96
97 private JTreeMap jTreeMap;
98
99 private javax.swing.JPanel jContentPane = null;
100
101 private JComboBox cmbValue;
102
103 private JComboBox cmbWeight;
104
105 private JPanel panelTM3;
106
107 private BuilderTM3 builderTM3;
108
109 private boolean showTM3CTonf;
110
111 private boolean showTree;
112
113 private boolean showWeight;
114
115 private String weightPrefix;
116
117 private String valuePrefix;
118
119 private JTree treeView;
120 private DefaultTreeModel treeModel;
121
122 /**
123 * This is the default constructor
124 */
125 public JTreeMapAppletExample() {
126 super();
127 }
128
129 /**
130 *
131 */
132 private void initGUI() {
133
134
135
136 this.setContentPane(getJContentPane());
137 showTM3CTonf = "true".equalsIgnoreCase(getParameter("showTM3Conf"));
138 showTree = "true".equalsIgnoreCase(getParameter("viewTree"));
139 showWeight = "true".equalsIgnoreCase(getParameter("showWeight"));
140 weightPrefix = getParameter("weightPrefix");
141 valuePrefix = getParameter("valuePrefix");
142 if (showTM3CTonf) {
143 addPanelEast(getJContentPane());
144 }
145
146 final String dataFile = getParameter("dataFile");
147 final String dataFileType = getParameter("dataFileType");
148 TreeMapNode root = null;
149 if(TM3.equalsIgnoreCase(dataFileType)) {
150 try {
151 builderTM3 = new BuilderTM3(createReader(dataFile));
152 root = builderTM3.getRoot();
153 if (showTM3CTonf) {
154 setTM3Fields();
155 panelTM3.setVisible(true);
156 }
157 } catch (final IOException e) {
158 root = handleException(e);
159 }
160 } else if(XML.equalsIgnoreCase(dataFileType)) {
161 try {
162 final URL url = new URL(getCodeBase() + dataFile);
163 final URLConnection connection = url.openConnection();
164 final BuilderXML bXml = new BuilderXML(connection.getInputStream());
165 root = bXml.getRoot();
166 } catch (final ParseException e) {
167 root = handleException(e);
168 } catch (final MalformedURLException e) {
169 root = handleException(e);
170 } catch (final IOException e) {
171 root = handleException(e);
172 }
173 } else {
174 root = DemoUtil.buildDemoRoot();
175 }
176
177 this.jTreeMap = new JTreeMap(root, new SplitBySortedWeight(), treeView, weightPrefix, valuePrefix, showWeight);
178 this.jTreeMap.setFont(new Font(null, Font.BOLD, DEFAULT_FONT_SIZE));
179
180 final String colourProvider = getParameter("colorProvider");
181
182 ColorProvider colourProviderInstance = null;
183 if ("Random".equalsIgnoreCase(colourProvider)) {
184 colourProviderInstance = new RandomColorProvider(this.jTreeMap);
185 } else if ("HSBLinear".equalsIgnoreCase(colourProvider)) {
186 colourProviderInstance = new HSBTreeMapColorProvider(jTreeMap,
187 HSBTreeMapColorProvider.ColorDistributionTypes.Linear, Color.GREEN, Color.RED);
188 } else if ("HSBLog".equalsIgnoreCase(colourProvider)) {
189 colourProviderInstance = new HSBTreeMapColorProvider(jTreeMap, HSBTreeMapColorProvider.ColorDistributionTypes.Log,
190 Color.GREEN, Color.RED);
191 } else if ("HSBSquareRoot".equalsIgnoreCase(colourProvider)) {
192 colourProviderInstance = new RandomColorProvider(this.jTreeMap);
193 } else if ("HSBCubicRoot".equalsIgnoreCase(colourProvider)) {
194 colourProviderInstance = new HSBTreeMapColorProvider(jTreeMap,
195 HSBTreeMapColorProvider.ColorDistributionTypes.CubicRoot, Color.GREEN, Color.RED);
196 } else if ("HSBExp".equalsIgnoreCase(colourProvider)) {
197 colourProviderInstance = new HSBTreeMapColorProvider(jTreeMap,
198 HSBTreeMapColorProvider.ColorDistributionTypes.Exp, Color.GREEN, Color.RED);
199 }
200
201 if (colourProviderInstance == null) {
202 colourProviderInstance = new RedGreenColorProvider(this.jTreeMap);
203 }
204
205 this.jTreeMap.setColorProvider(colourProviderInstance);
206
207
208 new ZoomPopupMenu(this.jTreeMap, true);
209
210 if (showTree) {
211 final JSplitPane splitPaneCenter = new JSplitPane();
212 splitPaneCenter.setBorder(BorderFactory.createEmptyBorder());
213 getJContentPane().add(splitPaneCenter, BorderLayout.CENTER);
214
215 final JScrollPane jScrollPane1 = new JScrollPane();
216 splitPaneCenter.setTopComponent(jScrollPane1);
217 splitPaneCenter.setBottomComponent(this.jTreeMap);
218
219 treeModel = new DefaultTreeModel(root);
220 treeView = new JTree(this.treeModel);
221 jTreeMap.setTreeView(treeView);
222 jScrollPane1.getViewport().add(this.treeView);
223 jScrollPane1.setPreferredSize(new Dimension(SCROLLPANE_WIDTH, jTreeMap.getRoot().getHeight()));
224 treeView.addTreeSelectionListener(new TreeSelectionListener() {
225 public void valueChanged(final TreeSelectionEvent e) {
226
227
228 TreeMapNode dest = (TreeMapNode) JTreeMapAppletExample.this.treeView.getLastSelectedPathComponent();
229
230
231 if (dest != null && dest.isLeaf()) {
232 dest = (TreeMapNode) dest.getParent();
233 }
234 if (dest == null) {
235 return;
236 }
237
238 JTreeMapAppletExample.this.jTreeMap.zoom(dest);
239 JTreeMapAppletExample.this.jTreeMap.repaint();
240 }
241 });
242 } else {
243 getJContentPane().add(this.jTreeMap, BorderLayout.CENTER);
244 }
245 }
246
247 /**
248 * @param dataFile
249 * @return
250 * @throws IOException
251 */
252 private BufferedReader createReader(final String dataFile) throws IOException {
253 final URL url = new URL(getCodeBase() + dataFile);
254 final URLConnection connection = url.openConnection();
255 final BufferedReader reader = new BufferedReader(
256 new InputStreamReader(connection.getInputStream()));
257 return reader;
258 }
259
260 private TreeMapNode handleException(final Exception e) {
261 e.printStackTrace();
262 JOptionPane.showMessageDialog(this, e.getMessage(), "File error", JOptionPane.ERROR_MESSAGE);
263 return DemoUtil.buildDemoRoot();
264 }
265
266 /**
267 * Add a pane to choose the weight and the value for TM3 file
268 */
269 private void addPanelEast(final Container parent) {
270 GridBagConstraints gridBagConstraints;
271 panelTM3 = new JPanel();
272 parent.add(this.panelTM3, BorderLayout.EAST);
273
274 final JPanel choicePanel = new JPanel();
275 choicePanel.setLayout(new java.awt.GridBagLayout());
276 choicePanel.setBorder(new TitledBorder("Choose the TM3 fields"));
277 panelTM3.add(choicePanel);
278
279 final JLabel lblWeight = new JLabel(" weight : ");
280 gridBagConstraints = new java.awt.GridBagConstraints();
281 gridBagConstraints.gridx = 0;
282 gridBagConstraints.gridy = 0;
283 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
284 choicePanel.add(lblWeight, gridBagConstraints);
285
286 cmbWeight = new JComboBox();
287 gridBagConstraints = new java.awt.GridBagConstraints();
288 gridBagConstraints.gridx = 1;
289 gridBagConstraints.gridy = 0;
290 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
291 gridBagConstraints.weightx = CONSTRAINT_WEIGHTX;
292 choicePanel.add(this.cmbWeight, gridBagConstraints);
293 cmbWeight.addActionListener(new ActionListener() {
294 public void actionPerformed(final ActionEvent e) {
295 final JComboBox cmb = (JComboBox) e.getSource();
296 final String field = (String) cmb.getSelectedItem();
297 JTreeMapAppletExample.this.builderTM3.setWeights(field);
298 JTreeMapAppletExample.this.repaint();
299 }
300 });
301
302 final JLabel lblValue = new JLabel(" value : ");
303 gridBagConstraints = new java.awt.GridBagConstraints();
304 gridBagConstraints.gridx = 0;
305 gridBagConstraints.gridy = 1;
306 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
307 gridBagConstraints.weighty = 1.0;
308 choicePanel.add(lblValue, gridBagConstraints);
309
310 cmbValue = new JComboBox();
311 gridBagConstraints = new java.awt.GridBagConstraints();
312 gridBagConstraints.gridx = 1;
313 gridBagConstraints.gridy = 1;
314 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
315 gridBagConstraints.weightx = CONSTRAINT_WEIGHTX;
316 gridBagConstraints.weighty = 1.0;
317 choicePanel.add(this.cmbValue, gridBagConstraints);
318 cmbValue.addActionListener(new ActionListener() {
319 public void actionPerformed(final ActionEvent e) {
320 final JComboBox cmb = (JComboBox) e.getSource();
321 final String field = (String) cmb.getSelectedItem();
322 JTreeMapAppletExample.this.builderTM3.setValues(field);
323
324
325 JTreeMapAppletExample.this.repaint();
326 }
327 });
328
329 panelTM3.setVisible(false);
330 }
331
332 private void setTM3Fields() {
333 final String[] numberFields = builderTM3.getNumberFields();
334 final String[] cmbValues = new String[numberFields.length + 1];
335 cmbValues[0] = "";
336 for (int i = 1; i < cmbValues.length; i++) {
337 cmbValues[i] = numberFields[i - 1];
338 }
339 cmbWeight.removeAllItems();
340 cmbValue.removeAllItems();
341 for (final String item : cmbValues) {
342 cmbWeight.addItem(item);
343 cmbValue.addItem(item);
344 }
345 }
346
347 /**
348 * This method initializes this
349 */
350 @Override
351 public void init() {
352 initGUI();
353 }
354
355 /**
356 * This method initializes jContentPane
357 *
358 * @return javax.swing.JPanel
359 */
360 private javax.swing.JPanel getJContentPane() {
361 if (this.jContentPane == null) {
362 this.jContentPane = new javax.swing.JPanel();
363 this.jContentPane.setLayout(new BorderLayout());
364 }
365
366 return this.jContentPane;
367 }
368
369 @Override
370 public void destroy() {
371 super.destroy();
372 this.jContentPane.removeAll();
373 }
374
375
376 }
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392