package com.c2kernel.gui.tabs.outcome.form;
import java.util.ArrayList;
import java.util.Enumeration;
import javax.swing.table.AbstractTableModel;
import org.exolab.castor.xml.schema.*;
import org.w3c.dom.*;
import com.c2kernel.gui.tabs.outcome.OutcomeException;
import com.c2kernel.utils.Language;
import com.c2kernel.utils.Logger;
public class DimensionTableModel extends AbstractTableModel {
ElementDecl model;
ArrayList columnHeadings = new ArrayList();
ArrayList columnClasses = new ArrayList();
ArrayList columnDecls = new ArrayList();
ArrayList colReadOnly = new ArrayList();
ArrayList colHelp = new ArrayList();
ArrayList rows = new ArrayList();
ArrayList elements = new ArrayList();
boolean readOnly;
public DimensionTableModel(ElementDecl model, boolean readOnly) throws StructuralException {
XMLType modelContent = model.getType();
this.model = model;
this.readOnly = readOnly;
// use text node for simple types
if (modelContent.isSimpleType()) {
SimpleType elementType = (SimpleType)modelContent;
SimpleType baseType = elementType.getBuiltInBaseType();
addColumn(model.getName(), baseType, baseType.getTypeCode(), new Boolean(model.getFixedValue() != null));
}
else if (modelContent.isComplexType()) { // if complex type, process child elements
ComplexType elementType = (ComplexType)modelContent;
// find out if a CDATA type is used for this complex type
XMLType baseType = elementType.getBaseType();
while (!(baseType instanceof SimpleType) && baseType != null) {
baseType = baseType.getBaseType();
}
if (baseType != null) {
int typeCode = ((SimpleType)baseType).getTypeCode();
addColumn(model.getName(), baseType, typeCode, new Boolean(model.getFixedValue() != null));
}
// process attributes
for (Enumeration e = elementType.getAttributeDecls(); e.hasMoreElements();) {
AttributeDecl thisAttr = (AttributeDecl)e.nextElement();
addColumn(thisAttr.getName(), thisAttr, thisAttr.getSimpleType().getTypeCode(), new Boolean(thisAttr.isFixed()));
}
// enumerate child elements
enumerateElements(elementType);
}
}
public synchronized void addColumn(String heading, Annotated decl, int typeCode, Boolean readOnly) {
Logger.msg(8, "Column "+heading+" contains "+decl.getClass().getName()+" readOnly="+readOnly.toString());
columnHeadings.add(heading);
columnDecls.add(decl);
columnClasses.add(OutcomeStructure.getJavaClass(typeCode));
colReadOnly.add(readOnly);
// read help
String helpText;
if (decl instanceof SimpleType)
helpText = OutcomeStructure.extractHelp(model);
else
helpText = OutcomeStructure.extractHelp(decl);
if (helpText.length() == 0)
helpText = ""+Language.translate("No help is available for this cell")+"";
colHelp.add(helpText);
}
public void enumerateElements(ContentModelGroup group) throws StructuralException {
for (Enumeration childElements = group.enumerate(); childElements.hasMoreElements(); ) {
Particle thisParticle = (Particle)childElements.nextElement();
String extraHeader = "";
if (thisParticle instanceof Group) {
Group thisGroup = (Group)thisParticle;
Order order = thisGroup.getOrder();
if (order == Order.sequence || order == Order.all)
enumerateElements(thisGroup);
else // we only support sequences in data structures such as these
throw new StructuralException("Element "+thisGroup.getName()+". Expecting sequence or all. Got "+thisGroup.getOrder());
}
else if (thisParticle instanceof ElementDecl) {
ElementDecl thisElement = (ElementDecl)thisParticle;
int typeCode = SimpleTypesFactory.INVALID_TYPE;
//make sure not too complex
if (thisElement.getType() != null) {
if (thisElement.getType().isComplexType()) {
ComplexType elementType = (ComplexType)thisElement.getType();
if (elementType.getParticleCount() > 0 ||
thisElement.getMaxOccurs() > 1)
throw new StructuralException("Too deep for a table");
for (Enumeration attrs = elementType.getAttributeDecls(); attrs.hasMoreElements();) {
AttributeDecl thisAttr = (AttributeDecl)attrs.nextElement();
if (!thisAttr.isFixed())
throw new StructuralException("Non-fixed attributes of child elements not supported in tables.");
else
extraHeader=extraHeader+" ("+thisAttr.getName()+":"+(thisAttr.getFixedValue()!=null?thisAttr.getFixedValue():thisAttr.getDefaultValue())+")";
}
// find type
XMLType parentType = thisElement.getType();
while (!(parentType instanceof SimpleType) && parentType != null) {
parentType = parentType.getBaseType();
if (parentType != null) typeCode = ((SimpleType)parentType).getTypeCode();
}
}
else
typeCode = ((SimpleType)thisElement.getType()).getTypeCode();
}
//add to list
addColumn(thisElement.getName()+extraHeader, thisElement, typeCode, new Boolean(thisElement.getFixedValue() != null));
}
else throw new StructuralException("Particle "+thisParticle.getClass()+" not implemented");
}
}
public void addInstance(Element myElement, int index) throws OutcomeException {
if (index == -1) index = elements.size();
Object[] newRow = new Object[columnHeadings.size()];
for (int i=0; i