01: package fr.aliacom.form.swt.ui;
02:
03: import org.apache.log4j.Logger;
04: import org.eclipse.swt.SWT;
05: import org.eclipse.swt.graphics.Image;
06: import org.eclipse.swt.widgets.Composite;
07: import org.eclipse.swt.widgets.Tree;
08: import org.eclipse.swt.widgets.TreeItem;
09:
10: import fr.aliacom.common.ui.ITree;
11: import fr.aliacom.common.ui.tree.ITreeNode;
12: import fr.aliacom.common.ui.tree.TreeModel;
13:
14: /**
15: * @author tom
16: *
17: * (C) 2001, 2002 Thomas Cataldo
18: */
19: public final class SWTTree implements ITree {
20:
21: private TreeModel model;
22: private Tree tree;
23:
24: private static final Logger log = Logger.getLogger(SWTTree.class);
25:
26: public SWTTree(Composite parent) {
27: tree = new Tree(parent, SWT.BORDER);
28: }
29:
30: private void createChildNodes(ITreeNode[] items, TreeItem parent) {
31: TreeItem it;
32: for (int i = 0, n = items.length; i < n; i++) {
33: it = new TreeItem(parent, SWT.NULL);
34: it.setText(items[i].getText());
35: if (items[i].getIcon() != null) {
36: it.setImage((Image) items[i].getIcon()
37: .getNativeWidget());
38: }
39:
40: if (!items[i].isLeaf()) {
41: createChildNodes(items[i].children(), it);
42: }
43: }
44: }
45:
46: /**
47: * @see fr.aliacom.common.ui.ITree#getModel()
48: */
49: public TreeModel getModel() {
50: return model;
51: }
52:
53: /**
54: * @see fr.aliacom.form.common.IFormComponent#getNativeWidget()
55: */
56: public Object getNativeWidget() {
57: return tree;
58: }
59:
60: private void refillTree() {
61: tree.removeAll();
62: ITreeNode root = model.getRoot();
63: TreeItem rootItem = new TreeItem(tree, SWT.NULL);
64: rootItem.setText(root.getText());
65: if (root.getIcon() != null) {
66: rootItem.setImage((Image) root.getIcon().getNativeWidget());
67: }
68: if (!root.isLeaf()) {
69: createChildNodes(root.children(), rootItem);
70: }
71: log.debug("Tree refilled");
72: }
73:
74: /**
75: * @see fr.aliacom.form.common.IFormComponent#reset()
76: */
77: public void reset() {
78: }
79:
80: public void setModel(TreeModel model) {
81: this .model = model;
82: treeChanged();
83: }
84:
85: /**
86: * @see fr.aliacom.form.common.IFormComponent#setValueBean(java.lang.Object)
87: */
88: public void setValueBean(Object bean) {
89: }
90:
91: /**
92: * @see fr.aliacom.common.ui.tree.ITreeModelListener#treeChanged()
93: */
94: public void treeChanged() {
95: refillTree();
96: }
97:
98: }
|