View Javadoc
1   /*
2    * ObjectLab, http://www.objectlab.co.uk/open is supporting JTreeMap.
3    *
4    * Based in London, we are world leaders in the design and development
5    * of bespoke applications for the securities financing markets.
6    *
7    * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a>
8    *           ___  _     _           _   _          _
9    *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
10   *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
11   *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
12   *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
13   *                   |__/
14   *
15   *                     www.ObjectLab.co.uk
16   *
17   * $Id$
18   *
19   * Copyright 2006 the original author or authors.
20   *
21   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
22   * use this file except in compliance with the License. You may obtain a copy of
23   * the License at
24   *
25   * http://www.apache.org/licenses/LICENSE-2.0
26   *
27   * Unless required by applicable law or agreed to in writing, software
28   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
29   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
30   * License for the specific language governing permissions and limitations under
31   * the License.
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 lombok.extern.slf4j.Slf4j;
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.ColorProvider;
70  import net.sf.jtreemap.swing.provider.HSBTreeMapColorProvider;
71  import net.sf.jtreemap.swing.provider.RandomColorProvider;
72  import net.sf.jtreemap.swing.provider.RedGreenColorProvider;
73  import net.sf.jtreemap.swing.provider.ZoomPopupMenu;
74  
75  /**
76   * Test of JTreeMap in a JApplet.
77   * Accepts 3 parameters;
78   * "dataFile" which is the path to data file relative to code base.
79   * "dataFileType" is either dt3 or xml.
80   * "showTM3CTonf", true if the tm3 configuration panel should be shown for tm3 files.
81   *
82   * @author Laurent Dutheil
83   */
84  @Slf4j
85  public class JTreeMapAppletExample extends JApplet {
86  
87      private static final double CONSTRAINT_WEIGHTX = 0.5;
88  
89      private static final int SCROLLPANE_WIDTH = 140;
90  
91      private static final String XML = "xml";
92  
93      private static final String TM3 = "tm3";
94  
95      private static final int DEFAULT_FONT_SIZE = 12;
96  
97      private static final long serialVersionUID = -8376357344981512167L;
98  
99      private JTreeMap jTreeMap;
100 
101     private javax.swing.JPanel jContentPane = null;
102 
103     private JComboBox cmbValue;
104 
105     private JComboBox cmbWeight;
106 
107     private JPanel panelTM3;
108 
109     private BuilderTM3 builderTM3;
110 
111     private boolean showTM3CTonf;
112 
113     private boolean showTree;
114 
115     private boolean showWeight;
116 
117     private String weightPrefix;
118 
119     private String valuePrefix;
120 
121     private JTree treeView;
122     private DefaultTreeModel treeModel;
123 
124     /**
125      * This is the default constructor
126      */
127     public JTreeMapAppletExample() {
128         super();
129     }
130 
131     /**
132      *
133      */
134     private void initGUI() {
135         // Width and height params are mandatory for an applet/object element in html
136         // and should be defined via html.
137         // this.setSize(APPLET_WIDTH, APPLET_HEIGHT);
138         this.setContentPane(getJContentPane());
139         showTM3CTonf = "true".equalsIgnoreCase(getParameter("showTM3Conf"));
140         showTree = "true".equalsIgnoreCase(getParameter("viewTree"));
141         showWeight = "true".equalsIgnoreCase(getParameter("showWeight"));
142         weightPrefix = getParameter("weightPrefix");
143         valuePrefix = getParameter("valuePrefix");
144         if (showTM3CTonf) {
145             addPanelEast(getJContentPane());
146         }
147 
148         final String dataFile = getParameter("dataFile");
149         final String dataFileType = getParameter("dataFileType");
150         TreeMapNode root = null;
151         if (TM3.equalsIgnoreCase(dataFileType)) {
152             try {
153                 builderTM3 = new BuilderTM3(createReader(dataFile));
154                 root = builderTM3.getRoot();
155                 if (showTM3CTonf) {
156                     setTM3Fields();
157                     panelTM3.setVisible(true);
158                 }
159             } catch (final IOException e) {
160                 root = handleException(e);
161             }
162         } else if (XML.equalsIgnoreCase(dataFileType)) {
163             try {
164                 final URL url = new URL(getCodeBase(), dataFile);
165                 final URLConnection connection = url.openConnection();
166                 final BuilderXML bXml = new BuilderXML(connection.getInputStream());
167                 root = bXml.getRoot();
168             } catch (final ParseException e) {
169                 root = handleException(e);
170             } catch (final MalformedURLException e) {
171                 root = handleException(e);
172             } catch (final IOException e) {
173                 root = handleException(e);
174             }
175         } else {
176             root = DemoUtil.buildDemoRoot();
177         }
178 
179         this.jTreeMap = new JTreeMap(root, new SplitBySortedWeight(), treeView, weightPrefix, valuePrefix, showWeight);
180         this.jTreeMap.setFont(new Font(null, Font.BOLD, DEFAULT_FONT_SIZE));
181 
182         final String colourProvider = getParameter("colorProvider");
183 
184         ColorProvider colourProviderInstance = null;
185         if ("Random".equalsIgnoreCase(colourProvider)) {
186             colourProviderInstance = new RandomColorProvider(this.jTreeMap);
187         } else if ("HSBLinear".equalsIgnoreCase(colourProvider)) {
188             colourProviderInstance = new HSBTreeMapColorProvider(jTreeMap, HSBTreeMapColorProvider.ColorDistributionTypes.LINEAR, Color.GREEN,
189                     Color.RED);
190         } else if ("HSBLog".equalsIgnoreCase(colourProvider)) {
191             colourProviderInstance = new HSBTreeMapColorProvider(jTreeMap, HSBTreeMapColorProvider.ColorDistributionTypes.LOG, Color.GREEN,
192                     Color.RED);
193         } else if ("HSBSquareRoot".equalsIgnoreCase(colourProvider)) {
194             colourProviderInstance = new RandomColorProvider(this.jTreeMap);
195         } else if ("HSBCubicRoot".equalsIgnoreCase(colourProvider)) {
196             colourProviderInstance = new HSBTreeMapColorProvider(jTreeMap, HSBTreeMapColorProvider.ColorDistributionTypes.CUBIC_ROOT, Color.GREEN,
197                     Color.RED);
198         } else if ("HSBExp".equalsIgnoreCase(colourProvider)) {
199             colourProviderInstance = new HSBTreeMapColorProvider(jTreeMap, HSBTreeMapColorProvider.ColorDistributionTypes.EXP, Color.GREEN,
200                     Color.RED);
201         }
202 
203         if (colourProviderInstance == null) {
204             colourProviderInstance = new RedGreenColorProvider(this.jTreeMap);
205         }
206 
207         this.jTreeMap.setColorProvider(colourProviderInstance);
208 
209         // Add a popupMenu to zoom
210         new ZoomPopupMenu(this.jTreeMap, true);
211 
212         if (showTree) {
213             final JSplitPane splitPaneCenter = new JSplitPane();
214             splitPaneCenter.setBorder(BorderFactory.createEmptyBorder());
215             getJContentPane().add(splitPaneCenter, BorderLayout.CENTER);
216 
217             final JScrollPane jScrollPane1 = new JScrollPane();
218             splitPaneCenter.setTopComponent(jScrollPane1);
219             splitPaneCenter.setBottomComponent(this.jTreeMap);
220 
221             treeModel = new DefaultTreeModel(root);
222             treeView = new JTree(this.treeModel);
223             jTreeMap.setTreeView(treeView);
224             jScrollPane1.getViewport().add(this.treeView);
225             jScrollPane1.setPreferredSize(new Dimension(SCROLLPANE_WIDTH, jTreeMap.getRoot().getHeight()));
226             treeView.addTreeSelectionListener(new TreeSelectionListener() {
227                 @Override
228                 public void valueChanged(final TreeSelectionEvent e) {
229                     // for each selected elements ont the treeView, we zoom the
230                     // JTreeMap
231                     TreeMapNode dest = (TreeMapNode) JTreeMapAppletExample.this.treeView.getLastSelectedPathComponent();
232 
233                     // if the element is a leaf, we select the parent
234                     if (dest != null && dest.isLeaf()) {
235                         dest = (TreeMapNode) dest.getParent();
236                     }
237                     if (dest == null) {
238                         return;
239                     }
240 
241                     JTreeMapAppletExample.this.jTreeMap.zoom(dest);
242                     JTreeMapAppletExample.this.jTreeMap.repaint();
243                 }
244             });
245         } else {
246             getJContentPane().add(this.jTreeMap, BorderLayout.CENTER);
247         }
248     }
249 
250     /**
251      * @param dataFile
252      * @return
253      * @throws IOException
254      */
255     private BufferedReader createReader(final String dataFile) throws IOException {
256         final URL url = new URL(getCodeBase(), dataFile);
257         final URLConnection connection = url.openConnection();
258         return new BufferedReader(new InputStreamReader(connection.getInputStream()));
259     }
260 
261     private TreeMapNode handleException(final Exception e) {
262         log.error("Issue", e);
263         JOptionPane.showMessageDialog(this, e.getMessage(), "File error", JOptionPane.ERROR_MESSAGE);
264         return DemoUtil.buildDemoRoot();
265     }
266 
267     /**
268      * Add a pane to choose the weight and the value for TM3 file
269      */
270     private void addPanelEast(final Container parent) {
271         GridBagConstraints gridBagConstraints;
272         panelTM3 = new JPanel();
273         parent.add(this.panelTM3, BorderLayout.EAST);
274 
275         final JPanel choicePanel = new JPanel();
276         choicePanel.setLayout(new java.awt.GridBagLayout());
277         choicePanel.setBorder(new TitledBorder("Choose the TM3 fields"));
278         panelTM3.add(choicePanel);
279 
280         final JLabel lblWeight = new JLabel(" weight : ");
281         gridBagConstraints = new java.awt.GridBagConstraints();
282         gridBagConstraints.gridx = 0;
283         gridBagConstraints.gridy = 0;
284         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
285         choicePanel.add(lblWeight, gridBagConstraints);
286 
287         cmbWeight = new JComboBox();
288         gridBagConstraints = new java.awt.GridBagConstraints();
289         gridBagConstraints.gridx = 1;
290         gridBagConstraints.gridy = 0;
291         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
292         gridBagConstraints.weightx = CONSTRAINT_WEIGHTX;
293         choicePanel.add(this.cmbWeight, gridBagConstraints);
294         cmbWeight.addActionListener(new ActionListener() {
295             @Override
296             public void actionPerformed(final ActionEvent e) {
297                 final JComboBox cmb = (JComboBox) e.getSource();
298                 final String field = (String) cmb.getSelectedItem();
299                 JTreeMapAppletExample.this.builderTM3.setWeights(field);
300                 JTreeMapAppletExample.this.repaint();
301             }
302         });
303 
304         final JLabel lblValue = new JLabel(" value : ");
305         gridBagConstraints = new java.awt.GridBagConstraints();
306         gridBagConstraints.gridx = 0;
307         gridBagConstraints.gridy = 1;
308         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
309         gridBagConstraints.weighty = 1.0;
310         choicePanel.add(lblValue, gridBagConstraints);
311 
312         cmbValue = new JComboBox();
313         gridBagConstraints = new java.awt.GridBagConstraints();
314         gridBagConstraints.gridx = 1;
315         gridBagConstraints.gridy = 1;
316         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
317         gridBagConstraints.weightx = CONSTRAINT_WEIGHTX;
318         gridBagConstraints.weighty = 1.0;
319         choicePanel.add(this.cmbValue, gridBagConstraints);
320         cmbValue.addActionListener(new ActionListener() {
321             @Override
322             public void actionPerformed(final ActionEvent e) {
323                 final JComboBox cmb = (JComboBox) e.getSource();
324                 final String field = (String) cmb.getSelectedItem();
325                 JTreeMapAppletExample.this.builderTM3.setValues(field);
326                 // createColorProviders();
327                 // updateLegendPanel();
328                 JTreeMapAppletExample.this.repaint();
329             }
330         });
331 
332         panelTM3.setVisible(false);
333     }
334 
335     private void setTM3Fields() {
336         final String[] numberFields = builderTM3.getNumberFields();
337         final String[] cmbValues = new String[numberFields.length + 1];
338         cmbValues[0] = "";
339         for (int i = 1; i < cmbValues.length; i++) {
340             cmbValues[i] = numberFields[i - 1];
341         }
342         cmbWeight.removeAllItems();
343         cmbValue.removeAllItems();
344         for (final String item : cmbValues) {
345             cmbWeight.addItem(item);
346             cmbValue.addItem(item);
347         }
348     }
349 
350     /**
351      * This method initializes this
352      */
353     @Override
354     public void init() {
355         initGUI();
356     }
357 
358     /**
359      * This method initializes jContentPane
360      *
361      * @return javax.swing.JPanel
362      */
363     private javax.swing.JPanel getJContentPane() {
364         if (this.jContentPane == null) {
365             this.jContentPane = new javax.swing.JPanel();
366             this.jContentPane.setLayout(new BorderLayout());
367         }
368 
369         return this.jContentPane;
370     }
371 
372     @Override
373     public void destroy() {
374         super.destroy();
375         this.jContentPane.removeAll();
376     }
377 
378 }
379 /*
380  *                 ObjectLab is supporing JTreeMap
381  *
382  * Based in London, we are world leaders in the design and development
383  * of bespoke applications for the securities financing markets.
384  *
385  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
386  *           ___  _     _           _   _          _
387  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
388  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
389  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
390  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
391  *                   |__/
392  *
393  *                     www.ObjectLab.co.uk
394  */