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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
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<String> columnHeadings = new ArrayList<String>();
ArrayList<Class<?>> columnClasses = new ArrayList<Class<?>>();
ArrayList<Annotated> columnDecls = new ArrayList<Annotated>();
ArrayList<Boolean> colReadOnly = new ArrayList<Boolean>();
ArrayList<String> colHelp = new ArrayList<String>();
ArrayList<Object[]> rows = new ArrayList<Object[]>();
ArrayList<Element> elements = new ArrayList<Element>();
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 = "<i>"+Language.translate("No help is available for this cell")+"</i>";
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<columnDecls.size(); i++) {
if (columnDecls.get(i) instanceof ElementDecl) { // sub element - get the node from it
ElementDecl thisElementDecl = (ElementDecl)columnDecls.get(i);
NodeList childElements = myElement.getElementsByTagName(thisElementDecl.getName());
switch (childElements.getLength()) {
case 1: // element exists - read the contents
Element childElement = (Element)childElements.item(0);
if (childElement.hasChildNodes()) {
Node thisNode = childElement.getFirstChild();
if (thisNode.getNodeType() == Node.TEXT_NODE)
newRow[i] = OutcomeStructure.getTypedValue(((Text)thisNode).getData(), (Class)columnClasses.get(i));
else
throw new StructuralException("First child of Field " + thisElementDecl.getName() + " was not Text. (NodeType:"+thisNode.getNodeType()+")");
}
else { // create text node
newRow[i] = this.setupDefaultElement(thisElementDecl, childElement, (Class)columnClasses.get(i));
}
break;
case 0: // element is missing - create it
Element newElement = myElement.getOwnerDocument().createElement(thisElementDecl.getName());
myElement.appendChild(newElement); //TODO: not in the right place in sequence. should insert it
newRow[i] = setupDefaultElement(thisElementDecl, newElement, (Class)columnClasses.get(i));
break;
default:
throw new CardinalException("Element "+thisElementDecl.getName()+" appeared more than once.");
}
}
else if (columnDecls.get(i) instanceof AttributeDecl) { //attribute
AttributeDecl thisAttrDecl = (AttributeDecl)columnDecls.get(i);
newRow[i] = OutcomeStructure.getTypedValue(myElement.getAttribute(thisAttrDecl.getName()), (Class)columnClasses.get(i));
}
else { // first child node
Node thisNode = myElement.getFirstChild();
if (thisNode == null) {
thisNode = myElement.getOwnerDocument().createTextNode("");
myElement.appendChild(thisNode);
}
if (thisNode.getNodeType() == Node.TEXT_NODE || thisNode.getNodeType() == Node.CDATA_SECTION_NODE)
newRow[i] = OutcomeStructure.getTypedValue(((Text)thisNode).getData(), (Class)columnClasses.get(i));
else
throw new StructuralException("First child of Column " + myElement.getTagName() + " was not Text");
}
}
elements.add(index, myElement);
rows.add(index, newRow);
fireTableRowsInserted(index, index);
}
public Class<?> getColumnClass(int columnIndex) {
return columnClasses.get(columnIndex);
}
public String getColumnName(int columnIndex) {
return (String)columnHeadings.get(columnIndex);
}
public int getRowCount() {
return rows.size();
}
public int getColumnCount() {
return columnHeadings.size();
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
boolean isReadOnly = readOnly || ((Boolean)colReadOnly.get(columnIndex)).booleanValue();
return !isReadOnly;
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
Object[] thisRow = (Object[])rows.get(rowIndex);
thisRow[columnIndex]=aValue;
Element myElement = (Element)elements.get(rowIndex);
// update node
if (columnDecls.get(columnIndex) instanceof ElementDecl) { // sub element
ElementDecl thisDecl = (ElementDecl)columnDecls.get(columnIndex);
NodeList childElements = myElement.getElementsByTagName(thisDecl.getName());
// depend on one element with a Text child - this should have been enforced on init.
Text childNode = (Text)(childElements.item(0).getFirstChild());
childNode.setData(aValue.toString());
}
else if (columnDecls.get(columnIndex) instanceof AttributeDecl) { //attribute
AttributeDecl thisDecl = (AttributeDecl) columnDecls.get(columnIndex);
myElement.setAttribute(thisDecl.getName(), aValue.toString());
}
else { // first child node
Text textNode = (Text)myElement.getFirstChild();
textNode.setData(aValue.toString());
}
fireTableCellUpdated(rowIndex, columnIndex);
}
public Element removeRow(int rowIndex) {
Element elementToGo = (Element)elements.get(rowIndex);
elements.remove(rowIndex);
rows.remove(rowIndex);
fireTableRowsDeleted(rowIndex,rowIndex);
return elementToGo;
}
public Object setupDefaultElement(ElementDecl thisDecl, Element parent, Class type) {
Object newValue;
String defaultValue = thisDecl.getFixedValue();
if (defaultValue == null)
defaultValue = thisDecl.getDefaultValue();
if (readOnly)
newValue = "";
else
newValue = OutcomeStructure.getTypedValue(defaultValue, type);
Text newNode = parent.getOwnerDocument().createTextNode(newValue.toString());
parent.appendChild(newNode);
// fixed attributes
try {
ComplexType content = (ComplexType)thisDecl.getType();
for (Enumeration attrs = content.getAttributeDecls(); attrs.hasMoreElements();) {
AttributeDecl thisAttr = (AttributeDecl)attrs.nextElement();
parent.setAttribute(thisAttr.getName(), thisAttr.getFixedValue()!=null?thisAttr.getFixedValue():thisAttr.getDefaultValue());
}
} catch (ClassCastException ex) { } // only complex types have attributes
return newValue;
}
public Object getValueAt(int rowIndex, int columnIndex) {
Object[] thisRow = (Object[])rows.get(rowIndex);
if (!(getColumnClass(columnIndex).equals(thisRow[columnIndex].getClass())))
Logger.warning(thisRow[columnIndex]+" should be "+getColumnClass(columnIndex)+" is a "+thisRow[columnIndex].getClass().getName());
return thisRow[columnIndex];
}
public String validateStructure() { // remove empty rows
for (int j=0; j < rows.size(); j++) {
Object[] elems = (Object[])rows.get(j);
boolean empty = true;
for (int i = 0; i < elems.length && empty; i++)
empty &= OutcomeStructure.isEmpty(elems[i]);
if (empty)
if (model.getMinOccurs() < rows.size())
removeRow(j);
else
return "Too many empty rows in table "+model.getName();
}
return null;
}
public Element initNew(Document parent, int index) {
if (index == -1) index = elements.size();
Object[] newRow = new Object[columnHeadings.size()];
Element myElement = parent.createElement(model.getName());
for (int i=0; i<columnDecls.size(); i++) {
if (columnDecls.get(i) instanceof ElementDecl) { // sub element
ElementDecl childElementDecl = (ElementDecl)columnDecls.get(i);
Element childElement = parent.createElement(childElementDecl.getName());
Object newValue = setupDefaultElement(childElementDecl, childElement, (Class)columnClasses.get(i));
myElement.appendChild(childElement);
newRow[i] = newValue;
}
else if (columnDecls.get(i) instanceof AttributeDecl) { //attribute
AttributeDecl thisAttrDecl = (AttributeDecl)columnDecls.get(i);
String newValue = thisAttrDecl.getFixedValue()!=null?thisAttrDecl.getFixedValue():thisAttrDecl.getDefaultValue();
newRow[i] = OutcomeStructure.getTypedValue(newValue, (Class)columnClasses.get(i));
myElement.setAttribute(thisAttrDecl.getName(), newRow[i].toString());
}
else { // first child node
newRow[i] = setupDefaultElement(model, myElement, (Class)columnClasses.get(i));
}
}
elements.add(index,myElement);
rows.add(index, newRow);
fireTableRowsInserted(index,index);
return myElement;
}
public String getHelp(int i) {
return (String)colHelp.get(i);
}
}
|