-
Notifications
You must be signed in to change notification settings - Fork 0
/
JTreePanel.java
203 lines (150 loc) · 5.54 KB
/
JTreePanel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class JTreePanel {
public static JPanel jPan = new JPanel();
private static JTree dir;
private static String[][] branches = new String[0][0]; //edit this String Array and then call the constructor to update
// the directory
public static DefaultMutableTreeNode selectedNode;
public static void makeJTreePanel(){
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Database");
for(int i = 0;i<branches.length;i++){ //first for loop, creates the main branch(Directories)
DefaultMutableTreeNode sub1 = new DefaultMutableTreeNode(
new DefaultMutableTreeNode(branches[i][0]));
for(int n=1;n<branches[i].length;n++){ //second loop, creates the second line(Tables)
sub1.add(new DefaultMutableTreeNode(branches[i][n]));
}
root.add(sub1);
}
dir = new JTree(root);
dir.addTreeSelectionListener(e -> {
selectedNode = (DefaultMutableTreeNode)dir.getLastSelectedPathComponent();
});
dir.setRootVisible(true);
dir.setShowsRootHandles(true);
jPan.add(dir);
}
public static JPanel getJPanel(){
return jPan;
}
// Using these we assume it has gotten past the other Error checks and will work
public static void addDirectory(String dirName){
jPan.remove(dir);
String[][] temp = new String[branches.length+1][];
for(int i = 0; i<branches.length;i++){
temp[i] = new String[branches[i].length];
for(int n = 0;n<branches[i].length;n++){
temp[i][n] = branches[i][n];
}
}
temp[branches.length] = new String[1]; //+1
temp[branches.length][0] = dirName;
//temp[branches.length][1] = "temp"; // makes it so the node is a folder // if you uncomment this add 1 to temp length
branches = temp;
makeJTreePanel();
}
public static void addTable(String dirName, String tableName){
jPan.remove(dir);
int[] index = new int[2];
for(int i = 0; i<branches.length;i++){
if(branches[i][0].equals(dirName)){
index[0]=i;
index[1]=0;
}
}
String[][] temp = new String[branches.length][];
for(int i=0; i<index[0];i++){
temp[i] = new String[branches[i].length];
for(int n = 0; n<branches[i].length;n++){
temp[i][n] = branches[i][n];
}
}
temp[index[0]] = new String[branches[index[0]].length+1];
for(int i = 0;i<branches[index[0]].length;i++){
temp[index[0]][i]=branches[index[0]][i];
}
temp[index[0]][branches[index[0]].length]=tableName;
for(int i = index[0]+1; i<branches.length;i++){
temp[i] = new String[branches[i].length];
for(int n = 0; n<branches[i].length;n++){
temp[i][n] = branches[i][n];
}
}
branches = temp;
makeJTreePanel();
}
// for this to work the directory must be there
public static void removeDirectory(String dirName){
jPan.remove(dir);
int[] index = new int[2];
for(int i = 0; i<branches.length;i++){
if(branches[i][0].equals(dirName)){
index[0]=i;
index[1]=0;
break;
}
}
String[][] temp = new String[branches.length-1][];
for(int i=0; i<index[0];i++){
temp[i] = new String[branches[i].length];
for(int n = 0; n<branches[i].length;n++){
temp[i][n] = branches[i][n];
}
}
for(int i = index[0]; i<temp.length;i++){
temp[i] = new String[branches[i+1].length];
for(int n = 0; n<branches[i+1].length;n++){
temp[i][n] = branches[i+1][n];
}
}
branches = temp;
makeJTreePanel();
}
//what did you do again
public static void refreshTree(){
jPan.remove(dir);
makeJTreePanel();
}
public static void removeTable(String dirName, String tableName){
jPan.remove(dir);
int[] index = new int[2];
for(int i = 0; i<branches.length;i++){
if(branches[i][0].equals(dirName)){
for(int n = 1;n<branches[i].length;n++){
if(branches[i][n].equals(tableName)){
index[0]=i;
index[1]=n;
}
}
}
}
String[][] temp = new String[branches.length][];
for(int i=0; i<index[0];i++){
temp[i] = new String[branches[i].length];
for(int n = 0; n<branches[i].length;n++){
temp[i][n] = branches[i][n];
}
}
temp[index[0]] = new String[branches[index[0]].length-1];
for(int n = 0;n<index[1];n++){
temp[index[0]][n]=branches[index[0]][n];
}
for(int n = index[1];n<temp[index[0]].length;n++){
temp[index[0]][n]=branches[index[0]][n+1];
}
for(int i = index[0]+1; i<branches.length;i++){
temp[i] = new String[branches[i].length];
for(int n = 0; n<branches[i].length;n++){
temp[i][n] = branches[i][n];
}
}
branches = temp;
makeJTreePanel();
}
}
/*
for(int i = 0; i<branches.length;i++){
for(int n = 0;n<branches[i].length;n++){
}
}
*/