/uClinux-dist/lib/classpath/examples/gnu/classpath/examples/swing/TreeDemo.java

https://bitbucket.org/__wp__/mb-linux-msli · Java · 318 lines · 230 code · 28 blank · 60 comment · 6 complexity · 79b40a4251073eb387a14fc1a9f80866 MD5 · raw file

  1. /* TreeDemo.java -- Demostrates JTree
  2. Copyright (C) 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.classpath.examples.swing;
  32. import java.awt.BorderLayout;
  33. import java.awt.event.ActionEvent;
  34. import java.awt.event.ActionListener;
  35. import javax.swing.ButtonGroup;
  36. import javax.swing.JButton;
  37. import javax.swing.JCheckBox;
  38. import javax.swing.JComponent;
  39. import javax.swing.JFrame;
  40. import javax.swing.JLabel;
  41. import javax.swing.JPanel;
  42. import javax.swing.JRadioButton;
  43. import javax.swing.JScrollPane;
  44. import javax.swing.JTree;
  45. import javax.swing.SwingUtilities;
  46. import javax.swing.event.TreeSelectionEvent;
  47. import javax.swing.event.TreeSelectionListener;
  48. import javax.swing.tree.DefaultMutableTreeNode;
  49. import javax.swing.tree.DefaultTreeModel;
  50. import javax.swing.tree.DefaultTreeSelectionModel;
  51. import javax.swing.tree.TreePath;
  52. public class TreeDemo
  53. extends JPanel
  54. implements ActionListener
  55. {
  56. TreeDemo()
  57. {
  58. super();
  59. createContent();
  60. }
  61. private void createContent()
  62. {
  63. // non-leafs
  64. DefaultMutableTreeNode root = new DefaultMutableTreeNode("Exotic Subsistence");
  65. DefaultMutableTreeNode fruit = new DefaultMutableTreeNode("Interesting Fruit");
  66. DefaultMutableTreeNode veg = new DefaultMutableTreeNode("Extraordinary Vegetables");
  67. DefaultMutableTreeNode liq = new DefaultMutableTreeNode("Peculiar Liquids");
  68. // leafs
  69. DefaultMutableTreeNode f1 = new DefaultMutableTreeNode("Abiu");
  70. DefaultMutableTreeNode f2 = new DefaultMutableTreeNode("Bamboo Shoots");
  71. DefaultMutableTreeNode f3 = new DefaultMutableTreeNode("Breadfruit");
  72. DefaultMutableTreeNode f4 = new DefaultMutableTreeNode("Canistel");
  73. DefaultMutableTreeNode f5 = new DefaultMutableTreeNode("Duku");
  74. DefaultMutableTreeNode f6 = new DefaultMutableTreeNode("Guava");
  75. DefaultMutableTreeNode f7 = new DefaultMutableTreeNode("Jakfruit");
  76. DefaultMutableTreeNode f8 = new DefaultMutableTreeNode("Quaribea");
  77. DefaultMutableTreeNode v1 = new DefaultMutableTreeNode("Amaranth");
  78. DefaultMutableTreeNode v2 = new DefaultMutableTreeNode("Kiwano");
  79. DefaultMutableTreeNode v3 = new DefaultMutableTreeNode("Leeks");
  80. DefaultMutableTreeNode v4 = new DefaultMutableTreeNode("Luffa");
  81. DefaultMutableTreeNode v5 = new DefaultMutableTreeNode("Chayote");
  82. DefaultMutableTreeNode v6 = new DefaultMutableTreeNode("Jicama");
  83. DefaultMutableTreeNode v7 = new DefaultMutableTreeNode("Okra");
  84. DefaultMutableTreeNode l1 = new DefaultMutableTreeNode("Alcoholic");
  85. DefaultMutableTreeNode l11 = new DefaultMutableTreeNode("Caipirinha");
  86. DefaultMutableTreeNode l21 = new DefaultMutableTreeNode("Mojito");
  87. DefaultMutableTreeNode l31 = new DefaultMutableTreeNode("Margarita");
  88. DefaultMutableTreeNode l41 = new DefaultMutableTreeNode("Martini");
  89. DefaultMutableTreeNode l5 = new DefaultMutableTreeNode("Non Alcoholic");
  90. DefaultMutableTreeNode l55 = new DefaultMutableTreeNode("Babaji");
  91. DefaultMutableTreeNode l65 = new DefaultMutableTreeNode("Chikita");
  92. root.add(fruit);
  93. root.add(veg);
  94. root.add(liq);
  95. fruit.add(f1);
  96. fruit.add(f2);
  97. fruit.add(f3);
  98. fruit.add(f4);
  99. fruit.add(f5);
  100. fruit.add(f6);
  101. fruit.add(f7);
  102. fruit.add(f8);
  103. veg.add(v1);
  104. veg.add(v2);
  105. veg.add(v3);
  106. veg.add(v4);
  107. veg.add(v5);
  108. veg.add(v6);
  109. veg.add(v7);
  110. liq.add(l1);
  111. l1.add(l11);
  112. l1.add(l21);
  113. l1.add(l31);
  114. l1.add(l41);
  115. liq.add(l5);
  116. l5.add(l55);
  117. l5.add(l65);
  118. final JTree tree = new JTree(root);
  119. tree.setLargeModel(true);
  120. tree.setEditable(true);
  121. final DefaultTreeSelectionModel selModel = new DefaultTreeSelectionModel();
  122. selModel.setSelectionMode(
  123. DefaultTreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
  124. tree.setSelectionModel(selModel);
  125. // buttons to add and delete
  126. JButton add = new JButton("add element");
  127. add.addActionListener(new ActionListener()
  128. {
  129. public void actionPerformed(ActionEvent e)
  130. {
  131. for (int i = 0; i < tree.getRowCount(); i++)
  132. {
  133. if (tree.isRowSelected(i))
  134. {
  135. TreePath p = tree.getPathForRow(i);
  136. DefaultMutableTreeNode n = (DefaultMutableTreeNode) p.
  137. getLastPathComponent();
  138. n.add(new DefaultMutableTreeNode("New Element"));
  139. // The expansion state of the parent node does not change
  140. // by default. We will expand it manually, to ensure that the
  141. // added node is immediately visible.
  142. tree.expandPath(p);
  143. // Refresh the tree (.repaint would be not enough both in
  144. // Classpath and Sun implementations).
  145. DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
  146. model.reload(n);
  147. break;
  148. }
  149. }
  150. }
  151. });
  152. // Demonstration of the various selection modes
  153. final JCheckBox cbSingle = new JCheckBox("single selection");
  154. cbSingle.addActionListener(new ActionListener()
  155. {
  156. public void actionPerformed(ActionEvent e)
  157. {
  158. if (cbSingle.isSelected())
  159. selModel.setSelectionMode(
  160. DefaultTreeSelectionModel.SINGLE_TREE_SELECTION);
  161. else
  162. selModel.setSelectionMode(
  163. DefaultTreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
  164. }
  165. });
  166. // Demonstration of the root visibility changes
  167. final JCheckBox cbRoot = new JCheckBox("root");
  168. cbRoot.addActionListener(new ActionListener()
  169. {
  170. public void actionPerformed(ActionEvent e)
  171. {
  172. tree.setRootVisible(cbRoot.isSelected());
  173. }
  174. });
  175. cbRoot.setSelected(true);
  176. // Demonstration of the tree selection listener.
  177. final JLabel choice = new JLabel("Make a choice");
  178. tree.getSelectionModel().addTreeSelectionListener(
  179. new TreeSelectionListener()
  180. {
  181. public void valueChanged(TreeSelectionEvent event)
  182. {
  183. TreePath was = event.getOldLeadSelectionPath();
  184. TreePath now = event.getNewLeadSelectionPath();
  185. String swas =
  186. was == null ? "none":was.getLastPathComponent().toString();
  187. String snow =
  188. now == null ? "none":now.getLastPathComponent().toString();
  189. choice.setText("From "+swas+" to "+snow);
  190. }
  191. }
  192. );
  193. setLayout(new BorderLayout());
  194. JPanel p2 = new JPanel();
  195. p2.add(add);
  196. p2.add(cbSingle);
  197. p2.add(cbRoot);
  198. tree.getSelectionModel().
  199. setSelectionMode(DefaultTreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
  200. // Panel for selecting line style.
  201. ActionListener l = new ActionListener()
  202. {
  203. public void actionPerformed(ActionEvent e)
  204. {
  205. JRadioButton b = (JRadioButton) e.getSource();
  206. tree.putClientProperty("JTree.lineStyle", b.getText());
  207. tree.repaint();
  208. }
  209. };
  210. JPanel lineStylePanel = new JPanel();
  211. ButtonGroup buttons = new ButtonGroup();
  212. lineStylePanel.add(new JLabel("Line style: "));
  213. JRadioButton none = new JRadioButton("None");
  214. lineStylePanel.add(none);
  215. buttons.add(none);
  216. none.addActionListener(l);
  217. JRadioButton angled = new JRadioButton("Angled");
  218. lineStylePanel.add(angled);
  219. buttons.add(angled);
  220. angled.addActionListener(l);
  221. JRadioButton horizontal = new JRadioButton("Horizontal");
  222. lineStylePanel.add(horizontal);
  223. buttons.add(horizontal);
  224. horizontal.addActionListener(l);
  225. p2.add(lineStylePanel);
  226. add(p2, BorderLayout.NORTH);
  227. add(new JScrollPane(tree), BorderLayout.CENTER);
  228. add(choice, BorderLayout.SOUTH);
  229. }
  230. public void actionPerformed(ActionEvent e)
  231. {
  232. if (e.getActionCommand().equals("CLOSE"))
  233. {
  234. System.exit(0);
  235. }
  236. }
  237. /**
  238. * When the demo is run independently, the frame is displayed, so we should
  239. * initialise the content panel (including the demo content and a close
  240. * button). But when the demo is run as part of the Swing activity board,
  241. * only the demo content panel is used, the frame itself is never displayed,
  242. * so we can avoid this step.
  243. */
  244. void initFrameContent()
  245. {
  246. JPanel closePanel = new JPanel();
  247. JButton closeButton = new JButton("Close");
  248. closeButton.setActionCommand("CLOSE");
  249. closeButton.addActionListener(this);
  250. closePanel.add(closeButton);
  251. add(closePanel, BorderLayout.SOUTH);
  252. }
  253. public static void main(String[] args)
  254. {
  255. SwingUtilities.invokeLater
  256. (new Runnable()
  257. {
  258. public void run()
  259. {
  260. TreeDemo app = new TreeDemo();
  261. app.initFrameContent();
  262. JFrame frame = new JFrame("Tree Demo");
  263. frame.getContentPane().add(app);
  264. frame.pack();
  265. frame.setVisible(true);
  266. }
  267. });
  268. }
  269. /**
  270. * Returns a DemoFactory that creates a TreeDemo.
  271. *
  272. * @return a DemoFactory that creates a TreeDemo
  273. */
  274. public static DemoFactory createDemoFactory()
  275. {
  276. return new DemoFactory()
  277. {
  278. public JComponent createDemo()
  279. {
  280. return new TreeDemo();
  281. }
  282. };
  283. }
  284. }