From 254ee6f47eebfc00462c10756a92066e82cc1a96 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Tue, 21 Jun 2011 15:46:02 +0200 Subject: Initial commit --- .../com/c2kernel/gui/tabs/outcome/form/Field.java | 137 +++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100755 source/com/c2kernel/gui/tabs/outcome/form/Field.java (limited to 'source/com/c2kernel/gui/tabs/outcome/form/Field.java') diff --git a/source/com/c2kernel/gui/tabs/outcome/form/Field.java b/source/com/c2kernel/gui/tabs/outcome/form/Field.java new file mode 100755 index 0000000..e1bc35c --- /dev/null +++ b/source/com/c2kernel/gui/tabs/outcome/form/Field.java @@ -0,0 +1,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(); + } + +} -- cgit v1.2.3