source: trunk/gli/src/org/greenstone/gatherer/mem/MEMNode.java@ 8236

Last change on this file since 8236 was 8236, checked in by mdewsnip, 20 years ago

Replaced all Gatherer.print* with DebugStream.print*.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.6 KB
Line 
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 */
37package org.greenstone.gatherer.mem;
38
39import java.io.*;
40import java.util.*;
41import javax.swing.tree.*;
42import org.greenstone.gatherer.Configuration;
43import org.greenstone.gatherer.DebugStream;
44import org.greenstone.gatherer.Dictionary;
45import org.greenstone.gatherer.Gatherer;
46import org.greenstone.gatherer.collection.BasicCollectionConfiguration;
47import org.greenstone.gatherer.mem.AttributeTableModel;
48import org.greenstone.gatherer.msm.ElementWrapper;
49import org.greenstone.gatherer.msm.MetadataSet;
50import org.greenstone.gatherer.msm.MetadataSetManager;
51import org.greenstone.gatherer.util.Utility;
52import org.w3c.dom.*;
53/**
54 * @author John Thompson, Greenstone Digital Library, University of Waikato
55 * @version 2.3
56 */
57public class MEMNode
58 extends DefaultMutableTreeNode {
59 private AttributeTableModel model = null;
60 private int type = 0;
61 private String text = null;
62 static final public int COLLECTION = 0;
63 static final public int ELEMENT = 1;
64 static final public int PROFILER = 2;
65 static final public int ROOT = 3;
66 static final public int SET = 4;
67
68 public MEMNode() {
69 this.type = ROOT;
70 }
71
72 public MEMNode(int type, Object object, MEMNode parent) {
73 this.userObject = object;
74 this.parent = parent;
75 this.type = type;
76 this.text = null;
77 }
78
79 public Enumeration children() {
80 if(children == null) {
81 mapChildren();
82 }
83 return children.elements();
84 }
85
86 public boolean getAllowsChildren() {
87 return true;
88 }
89
90 public TreeNode getChildAt(int index) {
91 if(children == null) {
92 mapChildren();
93 }
94 return (TreeNode) children.get(index);
95 }
96 public int getChildCount() {
97 if(children == null) {
98 mapChildren();
99 }
100 return children.size();
101 }
102 public ElementWrapper getElement() {
103 ElementWrapper result = null;
104 if(type == ELEMENT) {
105 result = (ElementWrapper) userObject;
106 }
107 return result;
108 }
109 public int getIndex(TreeNode node) {
110 if(children == null) {
111 mapChildren();
112 }
113 return children.indexOf(node);
114 }
115 public AttributeTableModel getModel() {
116 return model;
117 }
118 public TreeNode getParent() {
119 return parent;
120 }
121 public MetadataSet getSet() {
122 MetadataSet result = null;
123 if(type == SET) {
124 result = (MetadataSet) userObject;
125 }
126 return result;
127 }
128 public int getType() {
129 return type;
130 }
131 public boolean isLeaf() {
132 if(children == null) {
133 mapChildren();
134 }
135 return children.size() == 0;
136 }
137 public void setModel(AttributeTableModel model) {
138 this.model = model;
139 }
140
141 public String toString() {
142 if(text == null) {
143 if(userObject != null) {
144 if(userObject instanceof ElementWrapper) {
145 text = ((ElementWrapper)userObject).getName();
146 }
147 // 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
148 else if(type == COLLECTION) {
149 try {
150 boolean is_descendant = false;
151 File source_path = new File((String)userObject);
152 File collect_path;
153 if (Gatherer.GS3) {
154 collect_path = new File(Utility.getCollectDir(Configuration.gsdl3_path, Configuration.site_name));
155 } else {
156 collect_path = new File(Utility.getCollectDir(Configuration.gsdl_path));
157 }
158 File current_path = source_path;
159 while(!is_descendant && current_path != null) {
160 is_descendant = current_path.equals(collect_path);
161 current_path = current_path.getParentFile();
162 }
163 current_path = null;
164 collect_path = null;
165 // We've either found we are a descendant, or run out of path
166 if(is_descendant) {
167 // Create a basic config class
168 File config_file = new File(source_path, Utility.CONFIG_FILE);
169 if (config_file.exists()) {
170 BasicCollectionConfiguration config = new BasicCollectionConfiguration(config_file);
171 text = config.toString();
172 config = null;
173 } else {
174 text = source_path.getAbsolutePath();
175 }
176 config_file = null;
177 }
178 else {
179 text = source_path.getAbsolutePath();
180 }
181 }
182 catch(Exception exception) {
183 DebugStream.println("Exception in MEMNode.toString() - exception is unexpected");
184 DebugStream.printStackTrace(exception);
185 text = userObject.toString();
186 }
187 }
188 else {
189 text = userObject.toString();
190 }
191 }
192 else {
193 text = "error";
194 }
195 }
196 return text;
197 }
198 private void mapChildren() {
199 ///ystem.err.println("Mapping the children of " + this);
200 children = new Vector();
201 // How we build children depends on the node type
202 switch(type) {
203 case PROFILER: // Add the collections as children
204 ArrayList a = Gatherer.c_man.msm.profiler.getCollections();
205 for(int i = 0; i < a.size(); i++) {
206 children.add(new MEMNode(COLLECTION, a.get(i), this));
207 }
208 a = null;
209 break;
210 case ROOT:
211 Vector v = Gatherer.c_man.msm.getSets();
212 for(int i = 0; i < v.size(); i++) {
213 children.add(new MEMNode(SET, v.get(i), this));
214 }
215 v = null;
216 // Add the profile set.
217 children.add(new MEMNode(PROFILER, Dictionary.get("MEM.Profiles"), this));
218 break;
219 case SET: // Add the elements as children
220 MetadataSet set = (MetadataSet) userObject;
221 NodeList elements = set.getElements();
222 set = null;
223 for(int i = 0; i < elements.getLength(); i++) {
224 children.add(new MEMNode(ELEMENT, new ElementWrapper((Element) elements.item(i)), this));
225 }
226 elements = null;
227 break;
228 case COLLECTION:
229 case ELEMENT:
230 default:
231 }
232 }
233}
Note: See TracBrowser for help on using the repository browser.