001 // Copyright (c) 2001 Hursh Jain (http://www.mollypages.org)
002 // The Molly framework is freely distributable under the terms of an
003 // MIT-style license. For details, see the molly pages web site at:
004 // http://www.mollypages.org/. Use, modify, have fun !
005
006 package fc.web.forms;
007
008 import javax.servlet.*;
009 import javax.servlet.http.*;
010 import java.io.*;
011 import java.util.*;
012
013 import fc.jdbc.*;
014 import fc.io.*;
015 import fc.util.*;
016
017 /**
018 Keeps track of a set of related/grouped fields in a form.
019 <p>
020 A fieldref is is useful when a set of fields are created
021 programmatically from a database table and need to be
022 retrieved from the form (but we don't know the names of
023 those fields since they were created dynamically -- however,
024 those fields can be added both to the form and to a
025 fieldref and obtained via then the name of the fieldref
026 (whose name we <b>do</b> know.)
027 <p>
028 <b>Note</b>: Any fields contained in this object must also
029 be added directly to the form.
030
031 @author hursh jain
032 **/
033 public final class FieldRef
034 {
035 String name;
036 Object obj;
037
038 /**
039 Creates a new field with the specified name. All fieldref's
040 in a particular form must have a unique name.
041 **/
042 public FieldRef(String name)
043 {
044 this.name = name;
045 }
046
047 /**
048 Creates a new field with the specified name and the
049 specified object (which may be any data structure like a
050 list, map or whatever containing references to fields in
051 the form).
052 **/
053 public FieldRef(String name, Object obj)
054 {
055 this.name = name;
056 this.obj = obj;
057 }
058
059
060 /**
061 Adds an object to this fieldref. The object will typically
062 be some datastructure that contains references to fields
063 in the form.
064 */
065 public void add(Object obj)
066 {
067 this.obj = obj;
068 }
069
070 /**
071 Returns the data object contained in this fieldref as a List.
072 */
073 public List getList() {
074 return (List) obj;
075 }
076
077
078 /**
079 Returns the data object contained in this fieldref as a Map.
080 */
081 public Map getMap() {
082 return (Map) obj;
083 }
084
085 /**
086 Returns the data object contained in this fieldref.
087 */
088 public Object getObject()
089 {
090 return obj;
091 }
092
093 String getName() {
094 return name;
095 }
096
097 public String toString() {
098 StringBuffer buf = new StringBuffer();
099 buf.append("FieldRef: ").append(name);
100 buf.append("; Contains: \n");
101 buf.append(obj);
102 return buf.toString();
103 }
104
105 } //~class Text