/**
*#########################################################################
*
* A component of the Gatherer application, part of the Greenstone digital
* library suite from the New Zealand Digital Library Project at the
* University of Waikato, New Zealand.
*
*
*
* Author: John Thompson, Greenstone Digital Library, University of Waikato
*
*
*
* Copyright (C) 1999 New Zealand Digital Library Project
*
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*########################################################################
*/
package org.greenstone.gatherer.mem;
import java.io.*;
import java.util.*;
import javax.swing.tree.*;
import org.greenstone.gatherer.Configuration;
import org.greenstone.gatherer.Dictionary;
import org.greenstone.gatherer.Gatherer;
import org.greenstone.gatherer.collection.BasicCollectionConfiguration;
import org.greenstone.gatherer.mem.AttributeTableModel;
import org.greenstone.gatherer.msm.ElementWrapper;
import org.greenstone.gatherer.msm.MetadataSet;
import org.greenstone.gatherer.msm.MetadataSetManager;
import org.greenstone.gatherer.util.Utility;
import org.w3c.dom.*;
/**
* @author John Thompson, Greenstone Digital Library, University of Waikato
* @version 2.3
*/
public class MEMNode
extends DefaultMutableTreeNode {
private AttributeTableModel model = null;
private int type = 0;
private String text = null;
static final public int COLLECTION = 0;
static final public int ELEMENT = 1;
static final public int PROFILER = 2;
static final public int ROOT = 3;
static final public int SET = 4;
public MEMNode() {
this.type = ROOT;
}
public MEMNode(int type, Object object, MEMNode parent) {
this.userObject = object;
this.parent = parent;
this.type = type;
this.text = null;
}
public Enumeration children() {
if(children == null) {
mapChildren();
}
return children.elements();
}
public boolean getAllowsChildren() {
return true;
}
public TreeNode getChildAt(int index) {
if(children == null) {
mapChildren();
}
return (TreeNode) children.get(index);
}
public int getChildCount() {
if(children == null) {
mapChildren();
}
return children.size();
}
public ElementWrapper getElement() {
ElementWrapper result = null;
if(type == ELEMENT) {
result = (ElementWrapper) userObject;
}
return result;
}
public int getIndex(TreeNode node) {
if(children == null) {
mapChildren();
}
return children.indexOf(node);
}
public AttributeTableModel getModel() {
return model;
}
public TreeNode getParent() {
return parent;
}
public MetadataSet getSet() {
MetadataSet result = null;
if(type == SET) {
result = (MetadataSet) userObject;
}
return result;
}
public int getType() {
return type;
}
public boolean isLeaf() {
if(children == null) {
mapChildren();
}
return children.size() == 0;
}
public void setModel(AttributeTableModel model) {
this.model = model;
}
public String toString() {
if(text == null) {
if(userObject != null) {
if(userObject instanceof ElementWrapper) {
text = ((ElementWrapper)userObject).getName();
}
// 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
else if(type == COLLECTION) {
try {
boolean is_descendant = false;
File source_path = new File((String)userObject);
File collect_path = new File(Utility.getCollectDir(Gatherer.config.gsdl_path));
File current_path = source_path;
while(!is_descendant && current_path != null) {
is_descendant = current_path.equals(collect_path);
current_path = current_path.getParentFile();
}
current_path = null;
collect_path = null;
// We've either found we are a descendant, or run out of path
if(is_descendant) {
// Create a basic config class
BasicCollectionConfiguration config = new BasicCollectionConfiguration(new File(source_path, Utility.CONFIG_FILE));
text = config.toString();
config = null;
}
else {
text = source_path.getAbsolutePath();
}
}
catch(Exception exception) {
Gatherer.println("Exception in MEMNode.toString() - exception is unexpected");
Gatherer.printStackTrace(exception);
text = userObject.toString();
}
}
else {
text = userObject.toString();
}
}
else {
text = "error";
}
}
return text;
}
private void mapChildren() {
///ystem.err.println("Mapping the children of " + this);
children = new Vector();
// How we build children depends on the node type
switch(type) {
case PROFILER: // Add the collections as children
ArrayList a = Gatherer.c_man.msm.profiler.getCollections();
for(int i = 0; i < a.size(); i++) {
children.add(new MEMNode(COLLECTION, a.get(i), this));
}
a = null;
break;
case ROOT:
Vector v = Gatherer.c_man.msm.getSets();
for(int i = 0; i < v.size(); i++) {
children.add(new MEMNode(SET, v.get(i), this));
}
v = null;
// Add the profile set.
children.add(new MEMNode(PROFILER, Dictionary.get("MEM.Profiles"), this));
break;
case SET: // Add the elements as children
MetadataSet set = (MetadataSet) userObject;
NodeList elements = set.getElements();
set = null;
for(int i = 0; i < elements.getLength(); i++) {
children.add(new MEMNode(ELEMENT, new ElementWrapper((Element) elements.item(i)), this));
}
elements = null;
break;
case COLLECTION:
case ELEMENT:
default:
}
}
}