1 | /**
|
---|
2 | *#########################################################################
|
---|
3 | *
|
---|
4 | * A component of the Gatherer application, part of the Greenstone digital
|
---|
5 | * library suite from the New Zealand Digital Library Project at the
|
---|
6 | * University of Waikato, New Zealand.
|
---|
7 | *
|
---|
8 | * <BR><BR>
|
---|
9 | *
|
---|
10 | * Author: John Thompson, Greenstone Digital Library, University of Waikato
|
---|
11 | *
|
---|
12 | * <BR><BR>
|
---|
13 | *
|
---|
14 | * Copyright (C) 1999 New Zealand Digital Library Project
|
---|
15 | *
|
---|
16 | * <BR><BR>
|
---|
17 | *
|
---|
18 | * This program is free software; you can redistribute it and/or modify
|
---|
19 | * it under the terms of the GNU General Public License as published by
|
---|
20 | * the Free Software Foundation; either version 2 of the License, or
|
---|
21 | * (at your option) any later version.
|
---|
22 | *
|
---|
23 | * <BR><BR>
|
---|
24 | *
|
---|
25 | * This program is distributed in the hope that it will be useful,
|
---|
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
28 | * GNU General Public License for more details.
|
---|
29 | *
|
---|
30 | * <BR><BR>
|
---|
31 | *
|
---|
32 | * You should have received a copy of the GNU General Public License
|
---|
33 | * along with this program; if not, write to the Free Software
|
---|
34 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
35 | *########################################################################
|
---|
36 | */
|
---|
37 | package org.greenstone.gatherer.mem;
|
---|
38 |
|
---|
39 | import java.io.*;
|
---|
40 | import java.util.*;
|
---|
41 | import javax.swing.tree.*;
|
---|
42 | import org.greenstone.gatherer.Configuration;
|
---|
43 | import org.greenstone.gatherer.Dictionary;
|
---|
44 | import org.greenstone.gatherer.Gatherer;
|
---|
45 | import org.greenstone.gatherer.collection.BasicCollectionConfiguration;
|
---|
46 | import org.greenstone.gatherer.mem.AttributeTableModel;
|
---|
47 | import org.greenstone.gatherer.msm.ElementWrapper;
|
---|
48 | import org.greenstone.gatherer.msm.MetadataSet;
|
---|
49 | import org.greenstone.gatherer.msm.MetadataSetManager;
|
---|
50 | import org.greenstone.gatherer.util.Utility;
|
---|
51 | import org.w3c.dom.*;
|
---|
52 | /**
|
---|
53 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
54 | * @version 2.3
|
---|
55 | */
|
---|
56 | public class MEMNode
|
---|
57 | extends DefaultMutableTreeNode {
|
---|
58 | private AttributeTableModel model = null;
|
---|
59 | private int type = 0;
|
---|
60 | private String text = null;
|
---|
61 | static final public int COLLECTION = 0;
|
---|
62 | static final public int ELEMENT = 1;
|
---|
63 | static final public int PROFILER = 2;
|
---|
64 | static final public int ROOT = 3;
|
---|
65 | static final public int SET = 4;
|
---|
66 |
|
---|
67 | public MEMNode() {
|
---|
68 | this.type = ROOT;
|
---|
69 | }
|
---|
70 |
|
---|
71 | public MEMNode(int type, Object object, MEMNode parent) {
|
---|
72 | this.userObject = object;
|
---|
73 | this.parent = parent;
|
---|
74 | this.type = type;
|
---|
75 | this.text = null;
|
---|
76 | }
|
---|
77 |
|
---|
78 | public Enumeration children() {
|
---|
79 | if(children == null) {
|
---|
80 | mapChildren();
|
---|
81 | }
|
---|
82 | return children.elements();
|
---|
83 | }
|
---|
84 |
|
---|
85 | public boolean getAllowsChildren() {
|
---|
86 | return true;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public TreeNode getChildAt(int index) {
|
---|
90 | if(children == null) {
|
---|
91 | mapChildren();
|
---|
92 | }
|
---|
93 | return (TreeNode) children.get(index);
|
---|
94 | }
|
---|
95 | public int getChildCount() {
|
---|
96 | if(children == null) {
|
---|
97 | mapChildren();
|
---|
98 | }
|
---|
99 | return children.size();
|
---|
100 | }
|
---|
101 | public ElementWrapper getElement() {
|
---|
102 | ElementWrapper result = null;
|
---|
103 | if(type == ELEMENT) {
|
---|
104 | result = (ElementWrapper) userObject;
|
---|
105 | }
|
---|
106 | return result;
|
---|
107 | }
|
---|
108 | public int getIndex(TreeNode node) {
|
---|
109 | if(children == null) {
|
---|
110 | mapChildren();
|
---|
111 | }
|
---|
112 | return children.indexOf(node);
|
---|
113 | }
|
---|
114 | public AttributeTableModel getModel() {
|
---|
115 | return model;
|
---|
116 | }
|
---|
117 | public TreeNode getParent() {
|
---|
118 | return parent;
|
---|
119 | }
|
---|
120 | public MetadataSet getSet() {
|
---|
121 | MetadataSet result = null;
|
---|
122 | if(type == SET) {
|
---|
123 | result = (MetadataSet) userObject;
|
---|
124 | }
|
---|
125 | return result;
|
---|
126 | }
|
---|
127 | public int getType() {
|
---|
128 | return type;
|
---|
129 | }
|
---|
130 | public boolean isLeaf() {
|
---|
131 | if(children == null) {
|
---|
132 | mapChildren();
|
---|
133 | }
|
---|
134 | return children.size() == 0;
|
---|
135 | }
|
---|
136 | public void setModel(AttributeTableModel model) {
|
---|
137 | this.model = model;
|
---|
138 | }
|
---|
139 |
|
---|
140 | public String toString() {
|
---|
141 | if(text == null) {
|
---|
142 | if(userObject != null) {
|
---|
143 | if(userObject instanceof ElementWrapper) {
|
---|
144 | text = ((ElementWrapper)userObject).getName();
|
---|
145 | }
|
---|
146 | // In the case of a 'collection' source path we need to examine the path closely. If it is a descendant of the greenstone collections path then supress everything but the collection name
|
---|
147 | else if(type == COLLECTION) {
|
---|
148 | try {
|
---|
149 | boolean is_descendant = false;
|
---|
150 | File source_path = new File((String)userObject);
|
---|
151 | File collect_path = new File(Utility.getCollectDir(Gatherer.config.gsdl_path));
|
---|
152 | File current_path = source_path;
|
---|
153 | while(!is_descendant && current_path != null) {
|
---|
154 | is_descendant = current_path.equals(collect_path);
|
---|
155 | current_path = current_path.getParentFile();
|
---|
156 | }
|
---|
157 | current_path = null;
|
---|
158 | collect_path = null;
|
---|
159 | // We've either found we are a descendant, or run out of path
|
---|
160 | if(is_descendant) {
|
---|
161 | // Create a basic config class
|
---|
162 | BasicCollectionConfiguration config = new BasicCollectionConfiguration(new File(source_path, Utility.CONFIG_FILE));
|
---|
163 | text = config.toString();
|
---|
164 | config = null;
|
---|
165 | }
|
---|
166 | else {
|
---|
167 | text = source_path.getAbsolutePath();
|
---|
168 | }
|
---|
169 | }
|
---|
170 | catch(Exception exception) {
|
---|
171 | Gatherer.println("Exception in MEMNode.toString() - exception is unexpected");
|
---|
172 | Gatherer.printStackTrace(exception);
|
---|
173 | text = userObject.toString();
|
---|
174 | }
|
---|
175 | }
|
---|
176 | else {
|
---|
177 | text = userObject.toString();
|
---|
178 | }
|
---|
179 | }
|
---|
180 | else {
|
---|
181 | text = "error";
|
---|
182 | }
|
---|
183 | }
|
---|
184 | return text;
|
---|
185 | }
|
---|
186 | private void mapChildren() {
|
---|
187 | ///ystem.err.println("Mapping the children of " + this);
|
---|
188 | children = new Vector();
|
---|
189 | // How we build children depends on the node type
|
---|
190 | switch(type) {
|
---|
191 | case PROFILER: // Add the collections as children
|
---|
192 | ArrayList a = Gatherer.c_man.msm.profiler.getCollections();
|
---|
193 | for(int i = 0; i < a.size(); i++) {
|
---|
194 | children.add(new MEMNode(COLLECTION, a.get(i), this));
|
---|
195 | }
|
---|
196 | a = null;
|
---|
197 | break;
|
---|
198 | case ROOT:
|
---|
199 | Vector v = Gatherer.c_man.msm.getSets();
|
---|
200 | for(int i = 0; i < v.size(); i++) {
|
---|
201 | children.add(new MEMNode(SET, v.get(i), this));
|
---|
202 | }
|
---|
203 | v = null;
|
---|
204 | // Add the profile set.
|
---|
205 | children.add(new MEMNode(PROFILER, Dictionary.get("MEM.Profiles"), this));
|
---|
206 | break;
|
---|
207 | case SET: // Add the elements as children
|
---|
208 | MetadataSet set = (MetadataSet) userObject;
|
---|
209 | NodeList elements = set.getElements();
|
---|
210 | set = null;
|
---|
211 | for(int i = 0; i < elements.getLength(); i++) {
|
---|
212 | children.add(new MEMNode(ELEMENT, new ElementWrapper((Element) elements.item(i)), this));
|
---|
213 | }
|
---|
214 | elements = null;
|
---|
215 | break;
|
---|
216 | case COLLECTION:
|
---|
217 | case ELEMENT:
|
---|
218 | default:
|
---|
219 | }
|
---|
220 | }
|
---|
221 | }
|
---|