Source Code Cross Referenced for FormField.java in  » Groupware » LibreSource » org » libresource » form » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Groupware » LibreSource » org.libresource.form 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * LibreSource Community
003:         * Copyright (C) 2004-2007 Artenum SARL / INRIA
004:         * http://www.libresource.org - contact@artenum.com
005:         *
006:         * This software is not a free software; you can modify it under the
007:         * LibreSource Enterprise user license but you can't redistribute it.
008:         * See licenses details in LSE-user-license.txt 
009:         *
010:         * Initial authors :
011:         *
012:         * Guillaume Bort / INRIA
013:         * Francois Charoy / Universite Nancy 2
014:         * Julien Forest / Artenum
015:         * Claude Godart / Universite Henry Poincare
016:         * Florent Jouille / INRIA
017:         * Sebastien Jourdain / INRIA / Artenum
018:         * Yves Lerumeur / Artenum
019:         * Pascal Molli / Universite Henry Poincare
020:         * Gerald Oster / INRIA
021:         * Mariarosa Penzi / Artenum
022:         * Gerard Sookahet / Artenum
023:         * Raphael Tani / INRIA
024:         *
025:         * Contributors :
026:         *
027:         * Stephane Bagnier / Artenum
028:         * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
029:         */package org.libresource.form;
030:
031:        import java.io.IOException;
032:        import java.io.ObjectInputStream;
033:        import java.io.ObjectOutputStream;
034:        import java.io.Serializable;
035:
036:        import java.util.ArrayList;
037:        import java.util.Iterator;
038:
039:        import javax.servlet.http.HttpServletRequest;
040:
041:        /**
042:         * Artenum Contribution
043:         * 
044:         * @author <a href="mailto:jourdain@artenum.com">Sebastien Jourdain</a> - <a
045:         *         href="http://www.artenum.com">Artenum</a>
046:         */
047:        public class FormField implements  Serializable, Cloneable {
048:            private static final long serialVersionUID = 1L;
049:            // Fields types
050:            public final static int FIELD_TYPE_TEXT_LINE = 1;
051:            public final static int FIELD_TYPE_TEXT_MULTI_LINE = 2;
052:            public final static int FIELD_TYPE_RADIO = 3;
053:            public final static int FIELD_TYPE_CHECK = 4;
054:            private String fieldId;
055:            private String fieldLabel;
056:            private boolean fieldRequired;
057:            private int type;
058:            private ArrayList<Option> options;
059:            private String value;
060:            private ArrayList<String> values;
061:            private String validationRegex;
062:
063:            public FormField(String fieldId) {
064:                this (fieldId, "", false, FIELD_TYPE_TEXT_LINE);
065:            }
066:
067:            public FormField(String fieldId, String fieldLabel,
068:                    boolean fieldRequired, int type) {
069:                options = new ArrayList<Option>();
070:                this .fieldId = fieldId.toLowerCase();
071:                this .fieldLabel = fieldLabel;
072:                this .fieldRequired = fieldRequired;
073:                this .type = type;
074:            }
075:
076:            public String getFieldId() {
077:                return fieldId;
078:            }
079:
080:            public void setFieldId(String fieldId) {
081:                this .fieldId = fieldId.toLowerCase();
082:            }
083:
084:            public String getFieldLabel() {
085:                return fieldLabel;
086:            }
087:
088:            public void setFieldLabel(String fieldLabel) {
089:                this .fieldLabel = fieldLabel;
090:            }
091:
092:            public boolean isFieldRequired() {
093:                return fieldRequired;
094:            }
095:
096:            public void setFieldRequired(boolean fieldRequired) {
097:                this .fieldRequired = fieldRequired;
098:            }
099:
100:            public ArrayList<Option> getOptions() {
101:                return options;
102:            }
103:
104:            public void setOptions(String options) {
105:                String[] lines = options.split("\n");
106:                clearOptions();
107:
108:                for (int i = 0; i < lines.length; i++) {
109:                    if (lines[i].trim().length() > 0) {
110:                        addOption(new Option(lines[i]));
111:                    }
112:                }
113:            }
114:
115:            public int getType() {
116:                return type;
117:            }
118:
119:            public void setType(int type) {
120:                this .type = type;
121:            }
122:
123:            public void addOption(Option option) {
124:                options.add(option);
125:            }
126:
127:            public void clearOptions() {
128:                options.clear();
129:            }
130:
131:            public int hashCode() {
132:                return 31 + ((fieldId == null) ? 0 : fieldId.hashCode());
133:            }
134:
135:            public boolean equals(Object obj) {
136:                if (this  == obj) {
137:                    return true;
138:                }
139:
140:                if (obj == null) {
141:                    return false;
142:                }
143:
144:                if (getClass() != obj.getClass()) {
145:                    return false;
146:                }
147:
148:                final FormField other = (FormField) obj;
149:
150:                if (fieldId == null) {
151:                    if (other.fieldId != null) {
152:                        return false;
153:                    }
154:                } else if (!fieldId.equals(other.fieldId)) {
155:                    return false;
156:                }
157:
158:                return true;
159:            }
160:
161:            public String getValue() {
162:                switch (type) {
163:                case FIELD_TYPE_TEXT_LINE:
164:                case FIELD_TYPE_TEXT_MULTI_LINE:
165:                case FIELD_TYPE_RADIO:
166:                    return value;
167:                case FIELD_TYPE_CHECK:
168:                    if (values != null)
169:                        return values.toString().substring(1,
170:                                values.toString().length() - 1);
171:                }
172:                return null;
173:            }
174:
175:            public ArrayList<String> getValues() {
176:                ArrayList<String> result = new ArrayList<String>();
177:                switch (type) {
178:                case FIELD_TYPE_TEXT_LINE:
179:                case FIELD_TYPE_TEXT_MULTI_LINE:
180:                case FIELD_TYPE_RADIO:
181:                    if (value != null)
182:                        result.add(value);
183:                case FIELD_TYPE_CHECK:
184:                    if (values != null)
185:                        result.addAll(values);
186:                }
187:                return result;
188:            }
189:
190:            public String toString() {
191:                StringBuffer result = new StringBuffer();
192:                result.append("\nfieldId: ");
193:                result.append(fieldId);
194:                result.append("\nfieldLabel: ");
195:                result.append(fieldLabel);
196:                result.append("\nfieldRequired: ");
197:                result.append(fieldRequired);
198:                result.append("\ntype: ");
199:                result.append("" + type);
200:                result.append("\noptions: ");
201:                result.append(options);
202:                result.append("\nvalue: " + value);
203:
204:                return result.toString();
205:            }
206:
207:            public String getHtmlInput() {
208:                StringBuffer result = new StringBuffer();
209:
210:                switch (type) {
211:                case FIELD_TYPE_TEXT_LINE:
212:                    result.append("<input type=\"text\" name=\"");
213:                    result.append(getFieldId());
214:                    result.append("\" size=\"30\" value=\"");
215:
216:                    if (value != null) {
217:                        result.append(value);
218:                    } else {
219:                        if (options.size() > 0) {
220:                            for (Iterator i = options.iterator(); i.hasNext();) {
221:                                Option option = (Option) i.next();
222:                                result.append(option.getLabel());
223:                            }
224:                        }
225:                    }
226:
227:                    result.append("\"/>&nbsp;");
228:
229:                    break;
230:
231:                case FIELD_TYPE_TEXT_MULTI_LINE:
232:                    result.append("<textarea name=\"");
233:                    result.append(getFieldId());
234:                    result.append("\" cols=\"50\" rows=\"5\">");
235:
236:                    if (value != null) {
237:                        result.append(value);
238:                    } else {
239:                        for (Iterator i = options.iterator(); i.hasNext();) {
240:                            Option option = (Option) i.next();
241:                            result.append(option.getLabel() + "\n");
242:                        }
243:                    }
244:
245:                    result.append("</textarea>&nbsp;");
246:
247:                    break;
248:
249:                case FIELD_TYPE_CHECK:
250:
251:                    int index = 0;
252:
253:                    for (Option option : options) {
254:                        result.append("<input type=\"checkbox\"");
255:
256:                        if ((values != null)
257:                                && (values.contains(option.getLabel()))) {
258:                            result.append(" checked=\"checked\" ");
259:                        } else if (option.isSelected()) {
260:                            result.append(" checked=\"checked\" ");
261:                        }
262:
263:                        result.append(" name=\"");
264:                        result.append(getFieldId() + "_" + index++);
265:                        result.append("\" value=\"true\" />");
266:                        result.append(option.getLabel());
267:                        result.append("<br/>");
268:                    }
269:
270:                    break;
271:
272:                case FIELD_TYPE_RADIO:
273:
274:                    for (Option option : options) {
275:                        result.append("<input type=\"radio\"");
276:
277:                        if ((value != null)
278:                                && (value.equals(option.getLabel()))) {
279:                            result.append(" checked=\"checked\" ");
280:                        } else if (option.isSelected()) {
281:                            System.out.println(">" + value + "< = >"
282:                                    + option.getLabel() + "<");
283:                            result.append(" checked=\"checked\" ");
284:                        }
285:
286:                        result.append(" name=\"");
287:                        result.append(getFieldId());
288:                        result.append("\" value=\"");
289:                        result.append(option.getLabel());
290:                        result.append("\" />");
291:                        result.append(option.getLabel());
292:                        result.append("<br/>");
293:                    }
294:
295:                    break;
296:                }
297:
298:                return result.toString();
299:            }
300:
301:            public String getHtmlValue(boolean printLabel, boolean printValue) {
302:                StringBuffer result = new StringBuffer();
303:
304:                if (printLabel) {
305:                    result.append("<b>");
306:                    result.append(getFieldLabel());
307:                    result.append("</b> : ");
308:                }
309:
310:                if (printValue) {
311:                    switch (type) {
312:                    case FIELD_TYPE_TEXT_LINE:
313:                    case FIELD_TYPE_TEXT_MULTI_LINE:
314:                    case FIELD_TYPE_RADIO:
315:                        result.append(getValue());
316:                        break;
317:                    case FIELD_TYPE_CHECK:
318:                        result.append("<ul>");
319:                        for (String value : getValues()) {
320:                            result.append("<li>");
321:                            result.append(value);
322:                            result.append("</li>");
323:                        }
324:                        result.append("</ul>");
325:                        break;
326:                    }
327:                }
328:
329:                return result.toString();
330:            }
331:
332:            public void updateValueFromRequest(HttpServletRequest request)
333:                    throws FormFieldRequiredException {
334:                resetValue();
335:                switch (type) {
336:                case FIELD_TYPE_TEXT_LINE:
337:                case FIELD_TYPE_TEXT_MULTI_LINE:
338:                case FIELD_TYPE_RADIO:
339:                    value = request.getParameter(getFieldId());
340:                    if (value != null)
341:                        value = value.trim();
342:                    if (isFieldRequired()
343:                            && ((value == null) || (value.length() == 0))) {
344:                        throw new FormFieldRequiredException(
345:                                "The required field " + getFieldLabel()
346:                                        + " is not provided.");
347:                    }
348:                    break;
349:                case FIELD_TYPE_CHECK:
350:                    int index = 0;
351:                    values = new ArrayList<String>();
352:                    for (Option option : options) {
353:                        if (request.getParameter(getFieldId() + "_" + index++) != null) {
354:                            values.add(option.getLabel());
355:                        }
356:                    }
357:                    if (isFieldRequired() && (values.size() == 0)) {
358:                        throw new FormFieldRequiredException(
359:                                "The required field " + getFieldLabel()
360:                                        + " is not provided.");
361:                    }
362:                    break;
363:                }
364:
365:            }
366:
367:            public void resetValue() {
368:                value = null;
369:                values = null;
370:            }
371:
372:            public Object clone() throws CloneNotSupportedException {
373:                return super .clone();
374:            }
375:
376:            public String getValidationRegex() {
377:                return validationRegex;
378:            }
379:
380:            public void setValidationRegex(String validationRegex) {
381:                this .validationRegex = validationRegex;
382:            }
383:
384:            private void writeObject(ObjectOutputStream out) throws IOException {
385:                // Write current version
386:                out.writeInt(2);
387:
388:                // Write data for V1
389:                out.writeObject(fieldId);
390:                out.writeObject(fieldLabel);
391:                out.writeBoolean(fieldRequired);
392:                out.writeInt(type);
393:                out.writeObject(options);
394:                out.writeObject(validationRegex);
395:                out.writeBoolean(value != null || values != null);
396:                if (value != null)
397:                    out.writeObject(value);
398:                if (values != null)
399:                    out.writeObject(values);
400:
401:            }
402:
403:            private void readObject(ObjectInputStream in) throws IOException,
404:                    ClassNotFoundException {
405:                int version = in.readInt();
406:                switch (version) {
407:                case 1:
408:                    fieldId = (String) in.readObject();
409:                    fieldLabel = (String) in.readObject();
410:                    fieldRequired = in.readBoolean();
411:                    type = in.readInt();
412:                    options = (ArrayList<Option>) in.readObject();
413:                    if (in.readBoolean()) {
414:                        Object obj = in.readObject();
415:                        if (obj instanceof  String)
416:                            value = (String) obj;
417:                        if (obj instanceof  ArrayList)
418:                            values = (ArrayList<String>) obj;
419:                    }
420:                    // To support V2 format
421:                    validationRegex = null;
422:                    break;
423:                case 2:
424:                    fieldId = (String) in.readObject();
425:                    fieldLabel = (String) in.readObject();
426:                    fieldRequired = in.readBoolean();
427:                    type = in.readInt();
428:                    options = (ArrayList<Option>) in.readObject();
429:                    validationRegex = (String) in.readObject();
430:                    if (in.readBoolean()) {
431:                        Object obj = in.readObject();
432:                        if (obj instanceof  String)
433:                            value = (String) obj;
434:                        if (obj instanceof  ArrayList)
435:                            values = (ArrayList<String>) obj;
436:                    }
437:                    break;
438:                }
439:            }
440:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.