Source code: com/xpn/xwiki/objects/classes/DateClass.java
1 /**
2 * ===================================================================
3 *
4 * Copyright (c) 2003 Ludovic Dubost, All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details, published at
15 * http://www.gnu.org/copyleft/gpl.html or in gpl.txt in the
16 * root folder of this distribution.
17 *
18 * Created by
19 * User: Ludovic Dubost
20 * Date: 9 déc. 2003
21 * Time: 13:58:38
22
23 */
24 package com.xpn.xwiki.objects.classes;
25
26 import com.xpn.xwiki.XWikiContext;
27 import com.xpn.xwiki.objects.*;
28 import com.xpn.xwiki.objects.meta.PropertyMetaClass;
29 import org.apache.ecs.xhtml.input;
30 import org.dom4j.Element;
31
32 import java.text.DateFormat;
33 import java.text.SimpleDateFormat;
34 import java.text.ParseException;
35 import java.util.Date;
36 import java.util.Locale;
37
38 public class DateClass extends PropertyClass {
39
40 public DateClass(PropertyMetaClass wclass) {
41 super(<font color=red>"date"font>, <font color=red>"Date"font>, wclass);
42 setSize(20);
43 setDateFormat(<font color=red>"dd/MM/yyyy HH:mm:ss"font>);
44 setEmptyIsToday(1);
45 }
46
47 public DateClass() {
48 this(null);
49 }
50
51 public int getSize() {
52 return getIntValue(<font color=red>"size"font>);
53 }
54
55 public void setSize(int size) {
56 setIntValue(<font color=red>"size"font>, size);
57 }
58
59 public int getEmptyIsToday() {
60 return getIntValue(<font color=red>"emptyIsToday"font>);
61 }
62
63 public void setEmptyIsToday(int emptyIsToday) {
64 setIntValue(<font color=red>"emptyIsToday"font>, emptyIsToday);
65 }
66
67 public String getDateFormat() {
68 return getStringValue(<font color=red>"dateFormat"font>);
69 }
70
71 public void setDateFormat(String dformat) {
72 setStringValue(<font color=red>"dateFormat"font>, dformat);
73 }
74
75 public BaseProperty fromString(String value) {
76 DateProperty property = new DateProperty();
77 property.setName(getName());
78
79 if ((value==null)||(value.equals(""))) {
80 property.setValue(new Date());
81 return property;
82 }
83
84 try {
85 SimpleDateFormat sdf = new SimpleDateFormat(getDateFormat());
86 property.setValue(sdf.parse(value));
87 } catch (ParseException e) {
88 return null;
89 }
90 return property;
91 }
92
93 public String toFormString(BaseProperty property) {
94 SimpleDateFormat sdf = new SimpleDateFormat(getDateFormat());
95 return sdf.format(property.getValue());
96 }
97
98 public BaseProperty newPropertyfromXML(Element ppcel) {
99 String value = ppcel.getText();
100 DateProperty property = new DateProperty();
101 property.setName(getName());
102
103 if ((value==null)||(value.equals(""))) {
104 property.setValue(new Date());
105 return property;
106 }
107
108 try {
109 SimpleDateFormat sdf = new SimpleDateFormat(<font color=red>"yyyy-MM-dd HH:mm:ss.S"font>);
110 property.setValue(sdf.parse(value));
111 } catch (ParseException e) {
112 try {
113 e.printStackTrace();
114 SimpleDateFormat sdf = new SimpleDateFormat(<font color=red>"EEE MMM d HH:mm:ss z yyyy"font>, Locale.US);
115 property.setValue(sdf.parse(value));
116 } catch (ParseException e2) {
117 e2.printStackTrace();
118 property.setValue(new Date());
119 }
120 }
121 return property;
122 }
123
124 public void displayView(StringBuffer buffer, String name, String prefix, BaseCollection object, XWikiContext context) {
125 BaseProperty prop = (BaseProperty) object.safeget(name);
126 buffer.append(toFormString(prop));
127 }
128
129 public void displayEdit(StringBuffer buffer, String name, String prefix, BaseCollection object, XWikiContext context) {
130 input input = new input();
131
132 BaseProperty prop = (BaseProperty) object.safeget(name);
133 if (prop!=null) input.setValue(toFormString(prop));
134
135 input.setType(<font color=red>"text"font>);
136 input.setName(prefix + name);
137 input.setSize(getSize());
138 buffer.append(input.toString());
139 }
140
141 }