package org.cristalise.gui.tabs.outcome.form; import java.awt.Component; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingConstants; import org.cristalise.gui.tabs.outcome.OutcomeException; import org.cristalise.gui.tabs.outcome.form.field.StringEditField; import org.cristalise.kernel.utils.Logger; import org.exolab.castor.xml.schema.ElementDecl; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Text; 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); setupPanel(); } private void setupPanel() { GridBagLayout gridbag = new java.awt.GridBagLayout(); setLayout(gridbag); GridBagConstraints position = new GridBagConstraints(); position.anchor = GridBagConstraints.NORTHWEST; position.ipadx = 5; position.ipady = 5; position.insets = new Insets(5,5,0,0); position.gridwidth=1; position.gridy=0; position.gridx=0; position.weightx=2; position.weighty=0; position.fill=GridBagConstraints.NONE; gridbag.setConstraints(getLabel(), position); this.add(getLabel()); position.gridy++; position.weighty=1; position.fill = GridBagConstraints.HORIZONTAL; gridbag.setConstraints(getCData(), position); this.add(getCData()); position.gridx++; position.gridy--; position.gridheight=2; position.weightx=0; position.fill=GridBagConstraints.NONE; gridbag.setConstraints(getAttributes(), position); this.add(getAttributes()); position.gridx=0; position.gridheight=1; position.gridy++; } public JComponent getLabel() { return tagName; } public Component getCData() { if (myElementPanel == null) return nullPanel; return myElementPanel.getControl(); } public JComponent getAttributes() { return myAttributes; } @Override public void addStructure(OutcomeStructure newElement) throws StructuralException { throw new StructuralException("Field "+model.getName()+" cannot have child structures"); } @Override 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 @Override 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; } @Override 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; } @Override public void grabFocus() { if (myElementPanel != null) myElementPanel.grabFocus(); else myAttributes.grabFocus(); } }