001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package wingset;
014:
015: import org.wings.*;
016: import org.wings.plaf.css.TreeCG;
017: import org.wings.border.SLineBorder;
018: import org.wings.event.SMouseEvent;
019: import org.wings.event.SMouseListener;
020: import org.wings.util.PropertyAccessor;
021:
022: import javax.swing.tree.DefaultTreeModel;
023: import javax.swing.tree.TreeNode;
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ActionListener;
026: import java.awt.event.ItemEvent;
027: import java.awt.event.ItemListener;
028: import java.awt.*;
029:
030: /**
031: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
032: */
033: public class TreeExample extends WingSetPane {
034: private STree tree;
035: private static SIcon ARROW_DOWN = new SResourceIcon(
036: "org/wings/icons/ArrowDown.gif");
037: private static SIcon ARROW_RIGHT = new SResourceIcon(
038: "org/wings/icons/ArrowRight.gif");
039:
040: private static SIcon DOT = new SResourceIcon(
041: "org/wings/icons/leaficon.gif");
042: private static SIcon PLUS = new SResourceIcon(
043: "org/wings/icons/plus.gif");
044: private static SIcon MINUS = new SResourceIcon(
045: "org/wings/icons/minus.gif");
046: private TreeControls controls;
047: private SLabel clicks = new SLabel();
048: private boolean consume;
049:
050: protected SComponent createControls() {
051: controls = new TreeControls();
052: return controls;
053: }
054:
055: public SComponent createExample() {
056: tree = new STree(new DefaultTreeModel(HugeTreeModel.ROOT_NODE));
057: tree.setName("tree");
058: tree.setShowAsFormComponent(false);
059: TreeCG treeCG = new TreeCG();
060: tree.setCG(treeCG);
061:
062: tree.addMouseListener(new SMouseListener() {
063: public void mouseClicked(SMouseEvent e) {
064: Object object = tree.getPathForRow(
065: tree.getRowForLocation(e.getPoint()))
066: .getLastPathComponent();
067: TreeNode node = (TreeNode) object;
068: if (consume && node.isLeaf())
069: e.consume();
070: clicks.setText("clicked " + e.getPoint());
071: }
072: });
073: tree.getSelectionModel().setSelectionMode(
074: STree.SINGLE_TREE_SELECTION);
075: tree.setNodeIndentDepth(20);
076: tree.setHorizontalAlignment(SConstants.CENTER_ALIGN);
077:
078: clicks.setHorizontalAlignment(SConstants.CENTER_ALIGN);
079:
080: SScrollPane scrollPane = new SScrollPane(tree);
081: scrollPane.setMode(SScrollPane.MODE_COMPLETE);
082: scrollPane.setBorder(new SLineBorder(Color.GRAY, 1));
083: scrollPane.setPreferredSize(new SDimension("400px", null));
084: scrollPane.setHorizontalAlignment(SConstants.CENTER_ALIGN);
085:
086: controls.addControllable(scrollPane);
087:
088: SPanel panel = new SPanel(new SBorderLayout(10, 10));
089: panel.add(scrollPane, SBorderLayout.CENTER);
090: panel.add(clicks, SBorderLayout.SOUTH);
091: return panel;
092: }
093:
094: class TreeControls extends ComponentControls {
095: private final String[] SELECTION_MODES = new String[] {
096: "single", "contiguous", "discontiguous" };
097: private final Integer[] WIDTHS = new Integer[] {
098: new Integer(15), new Integer(20), new Integer(25),
099: new Integer(30) };
100:
101: public TreeControls() {
102: widthTextField.setText("400px");
103: borderColorComboBox.setSelectedItem(COLORS[1]);
104: borderStyleComboBox.setSelectedItem(BORDERS[4]);
105: borderThicknessTextField.setText("1");
106:
107: final SCheckBox consume = new SCheckBox(
108: "Consume events on leaves");
109: consume
110: .setToolTipText("<html>A SMouseListener will intercept the mouse clicks.<br>"
111: + "Consumed events will not be processed by the tree anymore");
112: consume
113: .addActionListener(new java.awt.event.ActionListener() {
114: public void actionPerformed(ActionEvent e) {
115: TreeExample.this .consume = consume
116: .isSelected();
117: }
118: });
119:
120: final SComboBox selectionMode = new SComboBox(
121: SELECTION_MODES);
122: //sync selectionMode with tree
123: selectionMode.addItemListener(new ItemListener() {
124: public void itemStateChanged(ItemEvent e) {
125: if (SELECTION_MODES[0].equals(selectionMode
126: .getSelectedItem())) {
127: tree.getSelectionModel().setSelectionMode(
128: STree.SINGLE_TREE_SELECTION);
129: } else if (SELECTION_MODES[1].equals(selectionMode
130: .getSelectedItem())) {
131: tree.getSelectionModel().setSelectionMode(
132: STree.CONTIGUOUS_TREE_SELECTION);
133: } else if (SELECTION_MODES[2].equals(selectionMode
134: .getSelectedItem())) {
135: tree.getSelectionModel().setSelectionMode(
136: STree.DISCONTIGUOUS_TREE_SELECTION);
137: }
138: }
139: });
140:
141: final SComboBox indentationWidth = new SComboBox(WIDTHS);
142: // sync indentation width of tree with controller
143: indentationWidth.setSelectedIndex(1); // set to 20px indent
144: // now add the listener
145: indentationWidth.addItemListener(new ItemListener() {
146: public void itemStateChanged(ItemEvent e) {
147: tree.setNodeIndentDepth(((Integer) indentationWidth
148: .getSelectedItem()).intValue());
149: }
150: });
151:
152: final SRadioButton plusButton = new SRadioButton(
153: "plus/minus");
154: plusButton
155: .setToolTipText("use [+] and [-] as expansion controls");
156:
157: final SRadioButton arrowButton = new SRadioButton("arrows");
158: arrowButton
159: .setToolTipText("use right-arrow and down-arrow as expansion controls");
160:
161: SButtonGroup group = new SButtonGroup();
162: group.add(plusButton);
163: group.add(arrowButton);
164: plusButton.setSelected(true);
165:
166: group.addActionListener(new ActionListener() {
167: public void actionPerformed(ActionEvent e) {
168: if (plusButton.isSelected()) {
169: PropertyAccessor.setProperty(tree.getCG(),
170: "collapseControlIcon", MINUS);
171: PropertyAccessor.setProperty(tree.getCG(),
172: "expandControlIcon", PLUS);
173: PropertyAccessor.setProperty(tree.getCG(),
174: "leafControlIcon", DOT);
175: } else {
176: PropertyAccessor.setProperty(tree.getCG(),
177: "collapseControlIcon", ARROW_DOWN);
178: PropertyAccessor.setProperty(tree.getCG(),
179: "expandControlIcon", ARROW_RIGHT);
180: PropertyAccessor.setProperty(tree.getCG(),
181: "leafControlIcon", null);
182: }
183: tree.reload();
184: }
185: });
186:
187: final SCheckBox rootVisible = new SCheckBox("show root",
188: true);
189: rootVisible.addActionListener(new ActionListener() {
190: public void actionPerformed(ActionEvent e) {
191: tree.setRootVisible(rootVisible.isSelected());
192: }
193: });
194:
195: addControl(consume);
196: addControl(new SLabel(" selection mode"));
197: addControl(selectionMode);
198: addControl(new SLabel(" indentation width"));
199: addControl(indentationWidth);
200: addControl(new SLabel(" folding icons"));
201: addControl(plusButton);
202: addControl(arrowButton);
203: addControl(new SLabel(" "));
204: addControl(rootVisible);
205: }
206: }
207: }
|