summaryrefslogtreecommitdiff
path: root/source/com/c2kernel/gui/tabs/outcome/form/field/StringEditField.java
blob: 310ee2e884ee4398a65f673c1927a4235880fff2 (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
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
package com.c2kernel.gui.tabs.outcome.form.field;
import java.awt.Component;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Enumeration;

import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

import org.exolab.castor.types.AnyNode;
import org.exolab.castor.xml.schema.Annotation;
import org.exolab.castor.xml.schema.AppInfo;
import org.exolab.castor.xml.schema.AttributeDecl;
import org.exolab.castor.xml.schema.ElementDecl;
import org.exolab.castor.xml.schema.Facet;
import org.exolab.castor.xml.schema.SimpleType;
import org.exolab.castor.xml.schema.Structure;
import org.exolab.castor.xml.schema.XMLType;
import org.exolab.castor.xml.schema.simpletypes.ListType;
import org.w3c.dom.Attr;
import org.w3c.dom.Node;
import org.w3c.dom.Text;

import com.c2kernel.gui.DomainKeyConsumer;
import com.c2kernel.gui.MainFrame;
import com.c2kernel.gui.tabs.outcome.OutcomeException;
import com.c2kernel.gui.tabs.outcome.form.HelpPane;
import com.c2kernel.gui.tabs.outcome.form.OutcomeStructure;
import com.c2kernel.gui.tabs.outcome.form.StructuralException;
import com.c2kernel.lookup.DomainPath;

/** Superclass for the entry field for Field and AttributeList.
  */
public class StringEditField implements FocusListener, DomainKeyConsumer {

    Node data;
    Structure model;
    protected SimpleType content;
    HelpPane helpPane;
    String helpText;
    protected JTextComponent field;
    
    boolean isValid = true;
    boolean editable = true;
    String name;

    
    public StringEditField() { 
        field = makeTextField();
        if (field != null)
            field.addFocusListener(this);
    }
    
    private static StringEditField getFieldForType(SimpleType type) {
        // handle lists special
        if (type instanceof ListType) 
            return new ArrayEditField(type.getBuiltInBaseType());

        // is a combobox
        if (type.hasFacet(Facet.ENUMERATION))
            return new ComboField(type, null);
        //find LOVscript
        Enumeration e = type.getAnnotations();
        while (e.hasMoreElements()) {
            Annotation note = (Annotation)e.nextElement();
            for (Enumeration f = note.getAppInfo(); f.hasMoreElements();) {
                AppInfo thisAppInfo = (AppInfo)f.nextElement();
                for (Enumeration g = thisAppInfo.getObjects(); g.hasMoreElements();) {
                    AnyNode appInfoNode = (AnyNode)g.nextElement();
                    if (appInfoNode.getLocalName().equals("ScriptList")
                            || appInfoNode.getLocalName().equals("LDAPList")) {
                        return new ComboField(type, appInfoNode);
                    }
                }
            }
        }
        // find info on length before we go to the base type
        long length = -1;
        if (type.getLength()!=null) length = type.getLength().longValue();
        else if (type.getMaxLength()!=null) length = type.getMaxLength().longValue();
        else if (type.getMinLength()!=null) length = type.getMinLength().longValue();
        
        // find base type if derived
        if (!(type.isBuiltInType()))
            type = type.getBuiltInBaseType(); 
        // else derive the class
        Class contentClass = OutcomeStructure.getJavaClass(type.getTypeCode());
        // disable list edits for the moment
        if (contentClass.equals(Boolean.class))
            return new BooleanEditField();
        else if (contentClass.equals(BigInteger.class))
            return new IntegerEditField();
        else if (contentClass.equals(BigDecimal.class))
            return new DecimalEditField();
        else if (contentClass.equals(ImageIcon.class))
            return new ImageEditField();        
        else if (length > 60)
            return new LongStringEditField();
        else return new StringEditField();
    }

    public static StringEditField getEditField(AttributeDecl model) throws StructuralException {
        StringEditField newField = getFieldForType(model.getSimpleType());
        newField.setDecl(model);
        return newField;
    }
    
    public static StringEditField getEditField(ElementDecl model) throws StructuralException {
        try {
            XMLType baseType = model.getType();
            while (!(baseType instanceof SimpleType))
                baseType = baseType.getBaseType();
            StringEditField newField = getFieldForType((SimpleType)baseType);
            newField.setDecl(model);
            return newField;
        } catch (Exception ex) {
            throw new StructuralException("No type defined in model");
        }
    }
    
    public void setDecl(AttributeDecl model) throws StructuralException {
        this.model=model;
        this.content=model.getSimpleType();
        this.name = model.getName();
        if (model.isFixed()) setEditable(false);
    }

    public void setDecl(ElementDecl model) throws StructuralException {
        this.model=model;
        this.name = model.getName();
        XMLType type = model.getType();
        
        // derive base type
        if (type.isSimpleType())
            this.content = (SimpleType)type;
        else
            this.content = (SimpleType)(type.getBaseType());

        if (this.content == null) throw new StructuralException("No declared base type of element");

        // 
        if (model.getFixedValue() != null) setEditable(false);
        
    }

    public void setData(Attr newData) throws StructuralException {
        if (!(newData.getName().equals(name)))
            throw new StructuralException("Tried to add a "+newData.getName()+" into a "+name+" attribute.");

        this.data = newData;
        setText(newData.getValue());
    }

    public void setData(Text newData) {
        String contents = newData.getData();
        this.data = newData;
        setText(contents);
    }

    public void setData(String newData) throws OutcomeException {
        if (data == null) throw new OutcomeException("No node exists");
        setText(newData);
        updateNode();

    }

    public Structure getModel() {
        return model;
    }

    public String getName() {
        return name;
    }

    public Node getData() {
        return data;
    }
    
    public String getDefaultValue() {
        return "";
    }
    
	public void setHelp(HelpPane helpPane, String helpText) {
		this.helpPane = helpPane;
		this.helpText = helpText;
	}

    public void focusLost(FocusEvent e) {
        if (MainFrame.itemFinder != null)
            MainFrame.itemFinder.clearConsumer(this);
        updateNode();
    }

    public void focusGained(FocusEvent e) {
    	helpPane.setHelp(name, helpText);
    	if (editable && MainFrame.itemFinder != null)
      		MainFrame.itemFinder.setConsumer(this, "Insert");    
    }

    public void updateNode() {
    	if (data == null) return;
        if (data instanceof Text) {
            ((Text)data).setData(getText());
        }
        else { //attribute
            ((Attr)data).setValue(getText());
        }
    }

    /**
     * Read domkey from barcode input
     */
    public void push(DomainPath key) {
        setText(key.getName());
    }

    /**
     * Read string from barcode input
     */
    public void push(String key) {
        setText(key);
    }
    
	public void setEditable(boolean editable) {
		this.editable = editable;
        if (field != null)
            field.setEditable(editable);
	}
	
    public String getText() {
        return field.getText();
    }

    public void setText(String text) {
        field.setText(text);
    }
    
    public JTextComponent makeTextField() {
        return new JTextField();
    }
    
    public Component getControl() {
        return field;
    }
    
    public void grabFocus() {
        getControl().requestFocus();
    }
}