001: /*
002: * Javu WingS - Lightweight Java Component Set
003: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
004: * e-mail: ksadlocha@programics.com
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package com.javujavu.javux.demo;
022:
023: import java.awt.BorderLayout;
024: import java.awt.Color;
025: import java.awt.GridBagConstraints;
026: import java.awt.GridBagLayout;
027: import java.awt.GridLayout;
028: import java.awt.Insets;
029: import java.awt.event.ActionEvent;
030: import java.awt.event.ActionListener;
031: import java.awt.event.ItemEvent;
032: import java.awt.event.ItemListener;
033: import com.javujavu.javux.wings.WingButton;
034: import com.javujavu.javux.wings.WingCheckBox;
035: import com.javujavu.javux.wings.WingCombo;
036: import com.javujavu.javux.wings.WingComponent;
037: import com.javujavu.javux.wings.WingImage;
038: import com.javujavu.javux.wings.WingLabel;
039: import com.javujavu.javux.wings.WingPanel;
040: import com.javujavu.javux.wings.WingScrollPane;
041: import com.javujavu.javux.wings.WingTree;
042: import com.javujavu.javux.wings.extra.ColorItem;
043: import com.javujavu.javux.wings.item.DefaultItemEditor;
044: import com.javujavu.javux.wings.item.ItemOwner;
045: import com.javujavu.javux.wings.item.LabelItem;
046: import com.javujavu.javux.wings.tree.TreeNode;
047:
048: public class TreePanel extends WingPanel implements ActionListener,
049: ItemListener {
050: private WingButton bAdd;
051: private WingButton bInsert;
052: private WingButton bRemove;
053: private WingTree tree1;
054: private WingTree target;
055: private WingTree tree2;
056: private WingTree tree3;
057: private WingButton bBranch;
058: private WingCheckBox cbEditable;
059:
060: public TreePanel(WingSetPanel owner) {
061: this .setLayout(new GridBagLayout());
062: WingPanel panel = new WingPanel(new GridLayout(0, 2, 8, 8));
063: GridBagConstraints c = new GridBagConstraints();
064: c.insets = new Insets(10, 10, 10, 10);
065: c.anchor = GridBagConstraints.CENTER;
066: c.fill = GridBagConstraints.BOTH;
067: c.weightx = 1.0;
068: c.weighty = 1.0;
069: c.gridwidth = GridBagConstraints.REMAINDER;
070: this .add(panel, c);
071:
072: WingTree tree;
073: WingPanel panel2;
074: panel.add(panel2 = new WingPanel(new BorderLayout()));
075: panel2.add(new WingScrollPane(tree1 = tree = new WingTree()),
076: BorderLayout.CENTER);
077: WingPanel panel3;
078: panel2.add(panel3 = new WingPanel(new BorderLayout()),
079: BorderLayout.NORTH);
080: panel3.setStyleId("border2");
081: panel3.add(new WingLabel("root not visible", WingLabel.CENTER),
082: BorderLayout.CENTER);
083: addItems(tree);
084:
085: panel.add(panel2 = new WingPanel(new BorderLayout()));
086: panel2.add(new WingScrollPane(tree2 = tree = new WingTree()),
087: BorderLayout.CENTER);
088: WingPanel p3;
089: panel2.add(p3 = new WingPanel(new GridBagLayout()),
090: BorderLayout.NORTH);
091: GridBagConstraints c2 = new GridBagConstraints();
092: c2.weightx = 1.0;
093: p3.add(new WingLabel("root visible", WingLabel.CENTER), c2);
094: WingButton b;
095: c2.weightx = 0.0;
096: p3.add(b = new WingButton("TopStyle"), c2);
097: b.setStyleId("vpopup");
098: b.setTextPosition(LEFT);
099: b.setFastAction(true);
100: b
101: .setTooltip(new LabelItem(
102: "modify TopStyle of this WingTree\nTopStyle is an extra style overriding stylesheet settings \ndynamically at run time\n\nclick button to change settings",
103: WingSet.imgIcon, LEFT, RIGHT));
104: new TopStyleEditor(b, tree2, 0, 0, false);
105: tree.setRootVisible(true);
106: addItems(tree);
107:
108: panel.add(panel2 = new WingPanel(new BorderLayout()));
109: panel2.add(new WingScrollPane(tree3 = tree = new WingTree(
110: new TreeNode("root node"))), BorderLayout.CENTER);
111: tree.setStyleId("custom_tree");
112: panel2.add(new WingLabel("custom style", WingLabel.CENTER),
113: BorderLayout.NORTH);
114: addItems(tree);
115:
116: tree1.setEditor(new MyTreeItemEditor());
117: tree2.setEditor(new MyTreeItemEditor());
118: tree3.setEditor(new MyTreeItemEditor());
119:
120: panel.add(panel2 = new WingPanel(new BorderLayout()));
121: panel2.add(new WingScrollPane(tree = new WingTree()));
122: panel2.add(new WingLabel("disabled tree", WingLabel.CENTER),
123: BorderLayout.NORTH);
124: addItems(tree);
125: tree.setEnabled(false);
126:
127: String keys = "\n\nKeys:\nUp,Down - move\nLeft,Right,mouse doubleclick - expand/collapse\nEnter, F2, mouse doubleclick - open editor\nEnter,Esc - close editor";
128: tree1.setTooltip(new LabelItem(
129: "WingTree\nroot not visible\ndefault style" + keys,
130: WingSet.imgGlasses, LEFT, RIGHT));
131: tree2.setTooltip(new LabelItem(
132: "WingTree\nroot visible\ndefault style" + keys,
133: WingSet.imgCrying, LEFT, LEFT));
134: tree3.setTooltip(new LabelItem(
135: "WingTree\nroot not visible\nstyle: custom_tree (stylesheet wingsetdemo.ini)"
136: + keys, WingSet.imgJokingly, RIGHT, LEFT));
137: tree.setTooltip(new LabelItem(
138: "disabled WingTree\nroot not visible\ndefault style"
139: + keys, WingSet.imgConfused, RIGHT, RIGHT));
140:
141: c.insets = new Insets(0, 10, 2, 10);
142: c.weighty = 0.0;
143: this .add(p3 = new WingPanel(new GridLayout(1, 0)), c);
144: p3.add(bBranch = new WingButton("add branch"));
145: p3.add(bAdd = new WingButton("add item"));
146: p3.add(bInsert = new WingButton("insert item"));
147: p3.add(bRemove = new WingButton("remove selected"));
148:
149: c.gridwidth = 1;
150: this .add(cbEditable = new WingCheckBox("editable", TOGGLE), c);
151: cbEditable.setSelected(true);
152: c.gridwidth = GridBagConstraints.REMAINDER;
153: this
154: .add(
155: new WingLabel(
156: "TIP: doubleclick, Enter, F2 - open editor ; Esc, Enter - close"),
157: c);
158:
159: this .addActionListener(this );
160: this .addItemListener(this );
161: }
162:
163: protected static void addItems(WingTree tree) {
164: TreeNode node, node2;
165: tree.addNode(node = new TreeNode(new LabelItem(
166: "text and icon 1", WingSet.imgSmile)), null);
167: tree.addNode(new TreeNode(new LabelItem("text and icon 2",
168: WingSet.imgGlasses)), node);
169: tree.addNode(node2 = new TreeNode(new LabelItem(
170: "text and icon 3", WingSet.imgTongue)), node);
171: tree.addNode(new TreeNode(new LabelItem("text and icon 4",
172: WingSet.imgSilence)), node2);
173: tree.addNode(new TreeNode(new LabelItem("animated",
174: WingSet.imgAnim)), node2);
175: tree.addNode(node = new TreeNode("text items"), null);
176: tree.addNode(new TreeNode("text item 2"), node);
177: tree.addNode(node2 = new TreeNode("text item 3"), node);
178: tree.addNode(new TreeNode("text item 4"), node2);
179: tree.addNode(node = new TreeNode(WingSet.imgSilence), null);
180: tree.addNode(new TreeNode(WingSet.imgSmile), node);
181: tree.addNode(node2 = new TreeNode(WingSet.imgGlasses), node);
182: tree.addNode(new TreeNode(WingSet.imgTongue), node2);
183: tree.addNode(node = new TreeNode(new ColorItem(
184: "custom renderer", Color.red)), null);
185: tree.addNode(new TreeNode(new ColorItem("color green",
186: Color.green)), node);
187: tree.addNode(node2 = new TreeNode(new ColorItem("color blue",
188: Color.blue)), node);
189: tree.addNode(new TreeNode(new ColorItem("color magenta",
190: Color.magenta)), node2);
191: }
192:
193: public void actionPerformed(ActionEvent e) {
194: if (target == null)
195: target = tree1;
196: Object src = e.getSource();
197: if (src == bBranch) {
198: TreeNode n = target.getSelected();
199: if (n == null)
200: n = target.getRoot();
201: target.addNode(makeNode(), n);
202: target.expandNode(n);
203: } else if (src == bAdd) {
204: TreeNode n = target.getSelected();
205: if (n != null && n != target.getRoot()) {
206: target.addNode(makeNode(), n.getParent());
207: }
208: } else if (src == bInsert) {
209: TreeNode n = target.getSelected();
210: if (n != null && n != target.getRoot()) {
211: TreeNode p = n.getParent();
212: target.insertNode(makeNode(), p, p.indexOf(n));
213: }
214: } else if (src == bRemove) {
215: TreeNode n = target.getSelected();
216: if (n != null && n != target.getRoot()) {
217: target.removeNode(n);
218: }
219: }
220: }
221:
222: private TreeNode makeNode() {
223: Object[] its = ListPanel.getItems();
224: return new TreeNode(its[(int) (Math.random() * its.length)]);
225: }
226:
227: public void itemStateChanged(ItemEvent e) {
228: Object src = e.getSource();
229: if (src == tree1)
230: target = tree1;
231: else if (src == tree2)
232: target = tree2;
233: else if (src == tree3)
234: target = tree3;
235: else if (src == cbEditable) {
236: if (e.getStateChange() == ItemEvent.SELECTED) {
237: tree1.setEditor(new MyTreeItemEditor());
238: tree2.setEditor(new MyTreeItemEditor());
239: tree3.setEditor(new MyTreeItemEditor());
240: } else {
241: tree1.setEditor(null);
242: tree2.setEditor(null);
243: tree3.setEditor(null);
244: }
245: }
246: }
247:
248: public static class MyTreeItemEditor extends DefaultItemEditor {
249: public WingComponent getComponent(ItemOwner owner,
250: Object value, Object context) {
251: if (value instanceof ColorItem) {
252: WingCombo editor = getComboEditor(owner, false);
253: editor.addItem(value);
254: Object[] items = ListPanel.getItems();
255: for (int i = 0; i < items.length; i++) {
256: if (items[i] instanceof ColorItem)
257: editor.addItem(items[i]);
258: }
259: editor.setSelectedItem(value);
260: return editor;
261: }
262: if (value instanceof WingImage) {
263: WingCombo editor = getComboEditor(owner, false);
264: editor.addItem(value);
265: Object[] items = ListPanel.getItems();
266: for (int i = 0; i < items.length; i++) {
267: if (items[i] instanceof WingImage)
268: editor.addItem(items[i]);
269: }
270: editor.setSelectedItem(value);
271: return editor;
272: }
273: if (value instanceof LabelItem) {
274: WingCombo editor = getComboEditor(owner, false);
275: editor.addItem(value);
276: Object[] items = ListPanel.getItems();
277: for (int i = 0; i < items.length; i++) {
278: if (items[i] instanceof LabelItem)
279: editor.addItem(items[i]);
280: }
281: editor.setSelectedItem(value);
282: return editor;
283: }
284: return super.getComponent(owner, value, context);
285: }
286: }
287: }
|