KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > wingset > TreeExample


1 /*
2  * $Id: TreeExample.java,v 1.7 2005/04/08 17:42:26 blueshift Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package wingset;
15
16 import org.wings.*;
17 import org.wings.util.PropertyAccessor;
18
19 import javax.swing.tree.DefaultTreeModel JavaDoc;
20 import java.awt.event.ActionEvent JavaDoc;
21 import java.awt.event.ActionListener JavaDoc;
22 import java.awt.event.ItemEvent JavaDoc;
23 import java.awt.event.ItemListener JavaDoc;
24
25 /**
26  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
27  * @version $Revision: 1.7 $
28  */

29 public class TreeExample
30         extends WingSetPane {
31     private STree tree;
32     private static SIcon ARROW_DOWN = new SResourceIcon("org/wings/icons/ArrowDown.gif");
33     private static SIcon ARROW_RIGHT = new SResourceIcon("org/wings/icons/ArrowRight.gif");
34
35     private static SIcon PLUS = new SResourceIcon("org/wings/icons/plus.gif");
36     private static SIcon MINUS = new SResourceIcon("org/wings/icons/minus.gif");
37     private TreeControls controls;
38
39     public SComponent createExample() {
40         controls = new TreeControls();
41
42         tree = new STree(new DefaultTreeModel JavaDoc(HugeTreeModel.ROOT_NODE));
43         tree.setName("tree");
44         tree.setShowAsFormComponent(false);
45         controls.addSizable(tree);
46
47         SForm panel = new SForm(new SBorderLayout());
48         panel.add(controls, SBorderLayout.NORTH);
49         panel.add(tree, SBorderLayout.CENTER);
50         return panel;
51     }
52
53     class TreeControls extends ComponentControls {
54         private final String JavaDoc[] SELECTION_MODES = new String JavaDoc[]{"no", "single", "multiple"};
55         private final Integer JavaDoc[] WIDTHS = new Integer JavaDoc[]{new Integer JavaDoc(12), new Integer JavaDoc(24), new Integer JavaDoc(36), new Integer JavaDoc(48)};
56
57         public TreeControls() {
58             final SCheckBox showAsFormComponent = new SCheckBox("<html>Show as Form Component&nbsp;&nbsp;&nbsp;");
59             showAsFormComponent.addActionListener(new ActionListener JavaDoc() {
60                 public void actionPerformed(ActionEvent JavaDoc e) {
61                     tree.setShowAsFormComponent(showAsFormComponent.isSelected());
62                 }
63             });
64
65             final SComboBox selectionMode = new SComboBox(SELECTION_MODES);
66             selectionMode.addItemListener(new ItemListener JavaDoc() {
67                 public void itemStateChanged(ItemEvent JavaDoc e) {
68                     if ("no".equals(selectionMode.getSelectedItem()))
69                         tree.getSelectionModel().setSelectionMode(STree.NO_SELECTION);
70                     else if ("single".equals(selectionMode.getSelectedItem()))
71                         tree.getSelectionModel().setSelectionMode(STree.SINGLE_SELECTION);
72                     else if ("multiple".equals(selectionMode.getSelectedItem()))
73                         tree.getSelectionModel().setSelectionMode(STree.MULTIPLE_SELECTION);
74                 }
75             });
76
77             final SComboBox indentationWidth = new SComboBox(WIDTHS);
78             indentationWidth.addItemListener(new ItemListener JavaDoc() {
79                 public void itemStateChanged(ItemEvent JavaDoc e) {
80                     tree.setNodeIndentDepth(((Integer JavaDoc) indentationWidth.getSelectedItem()).intValue());
81                 }
82             });
83
84             final SRadioButton plusButton = new SRadioButton("plus/minus");
85             plusButton.setToolTipText("use [+] and [-] as expansion controls");
86
87             final SRadioButton arrowButton = new SRadioButton("arrows");
88             arrowButton.setToolTipText("use right-arrow and down-arrow as expansion controls");
89
90             SButtonGroup group = new SButtonGroup();
91             group.add(plusButton);
92             group.add(arrowButton);
93             plusButton.setSelected(true);
94
95             group.addActionListener(new ActionListener JavaDoc() {
96                 public void actionPerformed(ActionEvent JavaDoc e) {
97                     if (plusButton.isSelected()) {
98                         PropertyAccessor.setProperty(tree.getCG(), "collapseControlIcon", MINUS);
99                         PropertyAccessor.setProperty(tree.getCG(), "expandControlIcon", PLUS);
100                     } else {
101                         PropertyAccessor.setProperty(tree.getCG(), "collapseControlIcon", ARROW_DOWN);
102                         PropertyAccessor.setProperty(tree.getCG(), "expandControlIcon", ARROW_RIGHT);
103                     }
104                 }
105             });
106
107             add(showAsFormComponent);
108             add(new SLabel(" selection mode "));
109             add(selectionMode);
110             add(new SLabel(" indentation width "));
111             add(indentationWidth);
112             add(new SLabel(" folding icons "));
113             add(plusButton);
114             add(arrowButton);
115         }
116     }
117 }
118
Popular Tags