1 //
2 // Neuros Database Manipulator
3 // Copyright (C) 2003 Neuros Database Manipulator
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 //
19 // For information about Neuros Database Manipulator and its authors,
20 // please contact the Neuros Database Manipulator Web Site at
21 // http://neurosdbm.sourceforge.net
22 //
23 //
24
25 package net.sourceforge.neurosdbm;
26
27
28 import javax.swing.JTree;
29 import javax.swing.event.TreeSelectionEvent;
30 import javax.swing.event.TreeSelectionListener;
31 import javax.swing.text.Position;
32 import javax.swing.tree.DefaultMutableTreeNode;
33 import javax.swing.tree.TreePath;
34 import javax.swing.tree.TreeSelectionModel;
35
36 import net.sourceforge.neurosdbm.db.Database;
37
38 class NeurosTree extends JTree {
39
40 static DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
41
42 private Database database;
43 private DisplayPanel displayPanel;
44 private NeurosTreeModel model;
45
46 NeurosTree(Database database, DisplayPanel displayPanel) {
47 super(root);
48 this.database = database;
49 this.displayPanel = displayPanel;
50
51 setRootVisible(false);
52
53 model = new NeurosTreeModel(root, database);
54 setModel(model);
55
56 getSelectionModel().setSelectionMode
57 (TreeSelectionModel.SINGLE_TREE_SELECTION);
58 addTreeSelectionListener(new MyTreeSelectionListener());
59 }
60
61 class MyTreeSelectionListener implements TreeSelectionListener {
62 public void valueChanged(TreeSelectionEvent e) {
63 DefaultMutableTreeNode node =
64 (DefaultMutableTreeNode)getLastSelectedPathComponent();
65 if (node != null) {
66 Object obj = node.getUserObject();
67 if (obj instanceof NeurosTreeNodeObject) {
68 NeurosTreeNodeObject nodeObj = (NeurosTreeNodeObject) obj;
69
70 displayPanel.setDataType(nodeObj.getDataType(), nodeObj.getKey());
71 }
72 }
73 }
74 }
75
76 void updateData() {
77 String n = null;
78 TreePath path = this.getSelectionPath();
79 if (path != null) {
80 Object selected = path.getLastPathComponent();
81 n = selected.toString();
82 model.updateData();
83 setExpandsSelectedPaths(true);
84 expandFully();
85 TreePath view = getNextMatch(n, 0, Position.Bias.Forward);
86 collapseFully();
87 setSelectionPath(view);
88 expandPath(view);
89 }
90 else {
91 model.updateData();
92 }
93
94
95 }
96
97 public void expandFully() {
98 boolean done = false;
99 while (!done) {
100 int r1 = getRowCount();
101 for (int i = r1 -1 ; i > -1; i--) {
102 expandRow(i);
103 }
104 if (r1 == getRowCount()){
105 done = true;
106 }
107 }
108 }
109
110 public void collapseFully() {
111 boolean done = false;
112 while (!done) {
113 int r1 = getRowCount();
114 for (int i = r1 -1 ; i > -1; i--) {
115 collapseRow(i);
116 }
117 if (r1 == getRowCount()){
118 done = true;
119 }
120 }
121 }
122 }
123