blob: e1bc35c15e79aae9594690d76cd34237bfe05ff5 (
plain)
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
|
package com.c2kernel.gui.tabs.outcome.form;
import java.awt.Component;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import org.exolab.castor.xml.schema.ElementDecl;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import com.c2kernel.gui.tabs.outcome.OutcomeException;
import com.c2kernel.gui.tabs.outcome.form.field.StringEditField;
import com.c2kernel.utils.Logger;
public class Field extends OutcomeStructure {
StringEditField myElementPanel = null;
AttributeList myAttributes;
JLabel tagName;
Text textNode;
boolean fixed;
public static final JPanel nullPanel = new JPanel();
public Field(ElementDecl model, boolean readOnly, HelpPane helpPane) {
super(model, readOnly, helpPane);
try {
myElementPanel = StringEditField.getEditField(model);
Logger.msg(6, "Field type: "+myElementPanel.getClass().getName());
myElementPanel.setHelp(helpPane, help);
if (readOnly) myElementPanel.setEditable(false);
} catch (StructuralException e) { // no base type for field - only attributes
myElementPanel = null;
}
myAttributes = new AttributeList(model, readOnly, helpPane);
tagName = new JLabel(model.getName());
tagName.setVerticalAlignment(SwingConstants.BOTTOM);
}
public JComponent getLabel() {
return tagName;
}
public Component getCData() {
if (myElementPanel == null)
return nullPanel;
return myElementPanel.getControl();
}
public JComponent getAttributes() {
return myAttributes;
}
public void addStructure(OutcomeStructure newElement) throws StructuralException {
throw new StructuralException("Field "+model.getName()+" cannot have child structures");
}
public void addInstance(Element myElement, Document parentDoc) throws OutcomeException {
Logger.msg(6, "Accepting Field "+myElement.getTagName());
if (this.myElement != null) throw new CardinalException("Field "+this.getName()+" cannot repeat");
this.myElement = myElement;
try {
if (myElementPanel == null)
Logger.error("Field should be empty. Discarding contents.");
else {
if (myElement.hasChildNodes())
textNode = (Text)myElement.getFirstChild();
else {
textNode = parentDoc.createTextNode(getDefaultValue());
myElement.appendChild(textNode);
}
myElementPanel.setData(textNode);
}
} catch (ClassCastException ex) {
throw new StructuralException("First child node of Field " + this.getName() + " was not Text: "+myElement.getFirstChild().getNodeType());
}
myAttributes.setInstance(myElement);
}
// check if valid
public String validateStructure() {
myAttributes.validateAttributes();
if (myElementPanel != null) myElementPanel.updateNode();
Text contents = (Text)myElement.getFirstChild();
if (!myElement.hasAttributes() && model.getMinOccurs() < 1 &&
(contents == null || contents.getData().length() == 0))
// empty - should remove if optional
myElement.getParentNode().removeChild(myElement);
return null;
}
public Element initNew(Document parent) {
Logger.msg(6, "Creating Field "+this.getName());
// make a new Element
myElement = parent.createElement(this.getName());
// see if there is a default/fixed value
if (myElementPanel != null) {
// populate
String defaultVal = readOnly?"":getDefaultValue();
textNode = parent.createTextNode(defaultVal);
myElement.appendChild(textNode);
myElementPanel.setData(textNode);
}
// set up attributes
myAttributes.initNew(myElement);
return myElement;
}
private String getDefaultValue() {
String defaultValue = model.getFixedValue();
if (defaultValue == null) defaultValue = model.getDefaultValue();
if (defaultValue == null) defaultValue = myElementPanel.getDefaultValue();
return defaultValue;
}
public void grabFocus() {
if (myElementPanel != null)
myElementPanel.grabFocus();
else
myAttributes.grabFocus();
}
}
|