Source Code Cross Referenced for SimpleDateConstraint.java in  » Ajax » zk » org » zkoss » zul » 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 » Ajax » zk » org.zkoss.zul 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* SimpleDateConstraint.java
002:
003:        {{IS_NOTE
004:        	Purpose:
005:        		
006:        	Description:
007:        		
008:        	History:
009:        		Tue Dec 25 12:06:30     2007, Created by tomyeh
010:        }}IS_NOTE
011:
012:        Copyright (C) 2007 Potix Corporation. All Rights Reserved.
013:
014:        {{IS_RIGHT
015:        	This program is distributed under GPL Version 2.0 in the hope that
016:        	it will be useful, but WITHOUT ANY WARRANTY.
017:        }}IS_RIGHT
018:         */
019:        package org.zkoss.zul;
020:
021:        import java.util.Date;
022:        import java.text.SimpleDateFormat;
023:        import java.text.ParseException;
024:
025:        import org.zkoss.mesg.Messages;
026:        import org.zkoss.util.Dates;
027:
028:        import org.zkoss.zk.ui.Component;
029:        import org.zkoss.zk.ui.UiException;
030:        import org.zkoss.zk.ui.WrongValueException;
031:
032:        import org.zkoss.zul.mesg.MZul;
033:
034:        /**
035:         * A simple date constraint.
036:         *
037:         * @author tomyeh
038:         * @since 3.0.2
039:         */
040:        public class SimpleDateConstraint extends SimpleConstraint {
041:            private static final SimpleDateFormat _df = new SimpleDateFormat(
042:                    "yyyyMMdd");
043:            private Date _beg, _end;
044:
045:            public SimpleDateConstraint(int flags) {
046:                super (flags);
047:                fixConstraint();
048:            }
049:
050:            /** Constraints a constraint.
051:             *
052:             * @param flags a combination of {@link #NO_POSITIVE}, {@link #NO_NEGATIVE},
053:             * {@link #NO_ZERO}, and so on.
054:             * @param errmsg the error message to display. Ignored if null or empty.
055:             */
056:            public SimpleDateConstraint(int flags, String errmsg) {
057:                super (flags, errmsg);
058:                fixConstraint();
059:            }
060:
061:            /** Constructs a regular-expression constraint.
062:             *
063:             * @param regex ignored if null or empty
064:             * @param errmsg the error message to display. Ignored if null or empty.
065:             */
066:            public SimpleDateConstraint(String regex, String errmsg) {
067:                super (regex, errmsg);
068:                fixConstraint();
069:            }
070:
071:            /** Constructs a constraint combining regular expression.
072:             *
073:             * @param flags a combination of {@link #NO_POSITIVE}, {@link #NO_NEGATIVE},
074:             * {@link #NO_ZERO}, and so on.
075:             * @param regex ignored if null or empty
076:             * @param errmsg the error message to display. Ignored if null or empty.
077:             */
078:            public SimpleDateConstraint(int flags, String regex, String errmsg) {
079:                super (flags, regex, errmsg);
080:                fixConstraint();
081:            }
082:
083:            /** Constructs a constraint with beginning and ending date.
084:             *
085:             * @param flags a combination of {@link #NO_POSITIVE}, {@link #NO_NEGATIVE},
086:             * {@link #NO_ZERO}, and so on.
087:             * @param begin the beginning date, or null if no constraint at the beginning
088:             * date.
089:             * @param end the ending date, or null if no constraint at the ending
090:             * date.
091:             * @param errmsg the error message to display. Ignored if null or empty.
092:             */
093:            public SimpleDateConstraint(int flags, Date begin, Date end,
094:                    String errmsg) {
095:                super (flags, errmsg);
096:                _beg = begin;
097:                _end = end;
098:                fixConstraint();
099:            }
100:
101:            /** Constructs a constraint with a list of constraints separated by comma.
102:             *
103:             * @param constraint a list of constraints separated by comma.
104:             * Example: "between 20071012 and 20071223", "before 20080103"
105:             */
106:            public SimpleDateConstraint(String constraint) {
107:                super (constraint);
108:                fixConstraint();
109:            }
110:
111:            private void fixConstraint() {
112:                if ((_flags & NO_FUTURE) != 0 && _end == null)
113:                    _end = Dates.today();
114:                if ((_flags & NO_PAST) != 0 && _beg == null)
115:                    _beg = Dates.today();
116:            }
117:
118:            /** Returns the beginning date, or null if there is no constraint of
119:             * the beginning date.
120:             */
121:            public Date getBeginDate() {
122:                return _beg;
123:            }
124:
125:            /** Returns the ending date, or null if therer is no constraint of
126:             * the ending date.
127:             */
128:            public Date getEndDate() {
129:                return _end;
130:            }
131:
132:            //super//
133:            protected int parseConstraint(String constraint) throws UiException {
134:                if (constraint.startsWith("between")) {
135:                    final int j = constraint.indexOf("and", 7);
136:                    if (j < 0)
137:                        throw new UiException("Constraint syntax error: "
138:                                + constraint);
139:                    _beg = parseDate(constraint.substring(7, j));
140:                    _end = parseDate(constraint.substring(j + 3));
141:                    if (_beg.compareTo(_end) > 0) {
142:                        final Date d = _beg;
143:                        _beg = _end;
144:                        _end = d;
145:                    }
146:                    return 0;
147:                } else if (constraint.startsWith("before")) {
148:                    _end = parseDate(constraint.substring(6));
149:                    return 0;
150:                } else if (constraint.startsWith("after")) {
151:                    _beg = parseDate(constraint.substring(5));
152:                    return 0;
153:                }
154:                return super .parseConstraint(constraint);
155:            }
156:
157:            private static Date parseDate(String val) throws UiException {
158:                try {
159:                    return _df.parse(val.trim());
160:                } catch (ParseException ex) {
161:                    throw new UiException("Not a date: " + val
162:                            + ". Format: yyyyMMdd", ex);
163:                }
164:            }
165:
166:            public void validate(Component comp, Object value)
167:                    throws WrongValueException {
168:                if (value instanceof  Date) {
169:                    final Date d = (Date) value;
170:                    if (_beg != null && _beg.compareTo(d) > 0)
171:                        throw outOfRangeValue(comp);
172:                    if (_end != null && _end.compareTo(d) < 0)
173:                        throw outOfRangeValue(comp);
174:                }
175:                super .validate(comp, value);
176:            }
177:
178:            private WrongValueException outOfRangeValue(Component comp) {
179:                final String errmsg = getErrorMessage(comp);
180:                return errmsg != null ? new WrongValueException(comp, errmsg)
181:                        : new WrongValueException(comp, MZul.OUT_OF_RANGE,
182:                                new Object[] { dateToString(comp, _beg),
183:                                        dateToString(comp, _end) });
184:            }
185:
186:            private static String dateToString(Component comp, Date d) {
187:                if (d == null)
188:                    return "";
189:                if (comp instanceof  Datebox)
190:                    return ((Datebox) comp).coerceToString(d);
191:                return _df.format(d);
192:            }
193:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.