|
1 /* |
|
2 * Copyright 2012 Tomas Zeman <tzeman@volny.cz> |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 package net.tz.lift.model |
|
17 |
|
18 import java.util.Calendar |
|
19 import net.liftweb.common._ |
|
20 import net.liftweb.http._ |
|
21 import net.liftweb.record._ |
|
22 import net.liftweb.record.field._ |
|
23 import net.liftweb.util._ |
|
24 import net.liftweb.util.Helpers._ |
|
25 import org.joda.time.{DateTime, DateMidnight} |
|
26 import org.joda.time.base.AbstractDateTime |
|
27 import org.joda.time.format.DateTimeFormat |
|
28 import scala.xml.{Elem, Text, Unparsed} |
|
29 |
|
30 /** |
|
31 * Joda date time field. Renders view in Czech format; renders form as 3 text |
|
32 * fields w/ css classes: <code>date</code>, <code>hour</code>, |
|
33 * <code>minute</code> to be rendered via CSS (date picker etc.). |
|
34 */ |
|
35 class JodaDateTimeField[T <: Record[T]](rec: T) extends DateTimeField(rec) { |
|
36 override def setFromAny(in: Any): Box[Calendar] = in match { |
|
37 case dt: AbstractDateTime => setBox(Full(dt.toGregorianCalendar)) |
|
38 case x => super.setFromAny(x) |
|
39 } |
|
40 |
|
41 def set(dt: AbstractDateTime): Calendar = set(dt.toGregorianCalendar) |
|
42 |
|
43 def dateTime = new DateTime(get) |
|
44 |
|
45 private def mdt = dateTime.toMutableDateTime |
|
46 |
|
47 override def asHtml = Text(AsDateTime(dateTime)) |
|
48 override def toForm = Full( |
|
49 SHtml.text(AsDateMidnight(dateTime.toDateMidnight), { s => |
|
50 val v = mdt |
|
51 AsDateMidnight(s) foreach { in => |
|
52 v.setYear(in.getYear) |
|
53 v.setMonthOfYear(in.getMonthOfYear) |
|
54 v.setDayOfMonth(in.getDayOfMonth) |
|
55 } |
|
56 set(v.toGregorianCalendar) }, "class" -> "date") ++ Unparsed(" ") ++ |
|
57 SHtml.selectObj[Int]((0 to 23) map { v => (v, "%02d".format(v)) }, |
|
58 Full(dateTime.getHourOfDay), { h => |
|
59 val v = mdt |
|
60 v.setHourOfDay(h) |
|
61 set(v.toGregorianCalendar) }, "class" -> "hour") ++ Text(":") ++ |
|
62 SHtml.selectObj[Int](Range(0, 60, minuteStep) map { v => |
|
63 (v, "%02d".format(v)) }, |
|
64 Full(dateTime.getMinuteOfHour / minuteStep * minuteStep), { m => |
|
65 val v = mdt |
|
66 v.setMinuteOfHour(m) |
|
67 set(v.toGregorianCalendar) }, "class" -> "minute") |
|
68 ) |
|
69 |
|
70 val minuteStep: Int = 5 |
|
71 } |
|
72 |
|
73 /** |
|
74 * Joda date field. |
|
75 */ |
|
76 class JodaDateMidnightField[T <: Record[T]](rec: T) extends DateTimeField(rec) { |
|
77 override def setFromAny(in: Any): Box[Calendar] = in match { |
|
78 case dt: AbstractDateTime => setBox(Full(dt.toGregorianCalendar)) |
|
79 case x => super.setFromAny(x) |
|
80 } |
|
81 |
|
82 def set(dt: AbstractDateTime): Calendar = set(dt.toGregorianCalendar) |
|
83 |
|
84 def date = new DateMidnight(get) |
|
85 |
|
86 override def asHtml = Text(AsDateMidnight(date)) |
|
87 override def toForm = Full(SHtml.text(AsDateMidnight(date), |
|
88 s => setBox(AsDateMidnight(s) map {_.toGregorianCalendar}), |
|
89 "class" -> "date")) |
|
90 } |
|
91 |
|
92 /** |
|
93 * Joda date-time converters. |
|
94 */ |
|
95 object AsDateTime { |
|
96 val fmt = DateTimeFormat.forPattern("dd.MM.yyyy HH:mm") |
|
97 |
|
98 def apply(d: DateTime): String = fmt.print(d) |
|
99 def apply(s: String): Box[DateTime] = tryo { fmt.parseDateTime(s) } |
|
100 } |
|
101 |
|
102 /** |
|
103 * Joda date converters. |
|
104 */ |
|
105 object AsDateMidnight { |
|
106 val fmt = DateTimeFormat.forPattern("dd.MM.yyyy") |
|
107 |
|
108 def apply(d: DateMidnight): String = fmt.print(d) |
|
109 def apply(s: String): Box[DateMidnight] = tryo { |
|
110 fmt.parseDateTime(s).toDateMidnight } |
|
111 } |
|
112 |
|
113 // vim: set ts=2 sw=2 et: |