1 package org.sapia.regis.forms; 2 3 import java.util.ArrayList ; 4 import java.util.Collections ; 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Map ; 9 10 import org.sapia.regis.RWNode; 11 import org.sapia.regis.type.BuiltinType; 12 13 public class Form { 14 15 private Map _fields = new HashMap (); 16 private String _type; 17 18 public Form(String type){ 19 _type = type; 20 } 21 public String getType(){ 22 return _type; 23 } 24 25 public Field createField(BuiltinType type, String name){ 26 Field f = new Field(type, name); 27 if(_fields.containsKey(name)){ 28 throw new IllegalArgumentException ("Field already defined for: " + name); 29 } 30 f.setDisplayOrder(_fields.size()); 31 _fields.put(name, f); 32 return f; 33 } 34 35 public void addField(Field f){ 36 if(f.getName() == null){ 37 throw new IllegalArgumentException ("Field name not set"); 38 } 39 if(_fields.containsKey(f.getName())){ 40 throw new IllegalArgumentException ("Field already defined for: " + f.getName()); 41 } 42 _fields.put(f.getName(), f); 43 } 44 45 public List getFields(){ 46 List fields = new ArrayList (_fields.values()); 47 Collections.sort(fields); 48 return fields; 49 } 50 51 public Field getField(String name){ 52 Field f = (Field)_fields.get(name); 53 if(f != null){ 54 return f; 55 } 56 throw new IllegalArgumentException ("No field for: " + name); 57 } 58 59 public boolean containsField(String name){ 60 return _fields.containsKey(name); 61 } 62 63 public FormInstance getInstance(RWNode node){ 64 return new FormInstance(this, node); 65 } 66 67 void assign(RWNode instance, Map props){ 68 for (Iterator i = _fields.values().iterator(); i.hasNext();) { 69 Field f = (Field)i.next(); 70 f.set(instance, props); 71 } 72 } 73 74 75 } 76 | Popular Tags |