KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > l10n > DefaultDateFormatProvider


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 /*
32  * Created on Dec 23, 2004
33  */

34 package com.nightlabs.l10n;
35
36 import java.text.DateFormat JavaDoc;
37 import java.text.DateFormatSymbols JavaDoc;
38 import java.text.SimpleDateFormat JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.Locale JavaDoc;
41 import java.util.Map JavaDoc;
42
43 import com.nightlabs.config.Config;
44
45 /**
46  * @author Marco Schulze - marco at nightlabs dot de
47  * @author Marc Klinger - marc at nightlabs dot de (API documentation fixes)
48  */

49 public class DefaultDateFormatProvider implements DateFormatProvider
50 {
51     protected Config config;
52     protected DefaultDateFormatCfMod defaultDateFormatCfMod;
53     protected String JavaDoc isoLanguage;
54     protected String JavaDoc isoCountry;
55
56     public DefaultDateFormatProvider()
57     {
58     }
59
60     /**
61      * @see com.nightlabs.l10n.DateFormatProvider#init(Config, String, String)
62      */

63     public void init(Config config, String JavaDoc isoLanguage, String JavaDoc isoCountry)
64     {
65         try {
66             this.config = config;
67             this.isoLanguage = isoLanguage;
68             this.isoCountry = isoCountry;
69             this.defaultDateFormatCfMod = (DefaultDateFormatCfMod) ConfigUtil.createConfigModule(config, DefaultDateFormatCfMod.class, isoLanguage, isoCountry);
70         } catch (RuntimeException JavaDoc x) {
71             throw x;
72         } catch (Exception JavaDoc x) {
73             throw new RuntimeException JavaDoc(x);
74         }
75     }
76
77     /**
78      * key: Long flags
79      * value: SimpleDateFormat dateFormat
80      */

81     protected Map JavaDoc dateFormatsByFlags = new HashMap JavaDoc();
82
83     /**
84      * @see com.nightlabs.l10n.DateFormatProvider#getDateFormat(long)
85      */

86     public DateFormat JavaDoc getDateFormat(long flags)
87     {
88         Long JavaDoc flagsL = new Long JavaDoc(flags);
89         SimpleDateFormat JavaDoc sdf;
90         synchronized (dateFormatsByFlags) {
91             sdf = (SimpleDateFormat JavaDoc) dateFormatsByFlags.get(flagsL);
92         }
93         if (sdf == null) {
94             String JavaDoc datePattern = null;
95             String JavaDoc timePattern = null;
96
97             if ((flags & DATE_LONG) == DATE_LONG && (flags & DATE_WEEKDAY) == DATE_WEEKDAY)
98                 datePattern = defaultDateFormatCfMod.getDateLongWeekday();
99             else if ((flags & DATE_LONG) == DATE_LONG)
100                 datePattern = defaultDateFormatCfMod.getDateLong();
101             else if ((flags & DATE_SHORT) == DATE_SHORT && (flags & DATE_WEEKDAY) == DATE_WEEKDAY)
102                 datePattern = defaultDateFormatCfMod.getDateShortWeekday();
103             else if ((flags & DATE_SHORT) == DATE_SHORT)
104                 datePattern = defaultDateFormatCfMod.getDateShort();
105
106             if ((flags & TIME_MSEC) == TIME_MSEC)
107                 timePattern = defaultDateFormatCfMod.getTimeHMSms();
108             else if ((flags & TIME_SEC) == TIME_SEC)
109                 timePattern = defaultDateFormatCfMod.getTimeHMS();
110             else if ((flags & TIME) == TIME)
111                 timePattern = defaultDateFormatCfMod.getTimeHM();
112
113             String JavaDoc pattern;
114             if (datePattern != null && timePattern != null)
115                 pattern = datePattern + ' ' + timePattern;
116             else if (datePattern != null)
117                 pattern = datePattern;
118             else
119                 pattern = timePattern;
120
121             if (pattern == null)
122                 throw new IllegalArgumentException JavaDoc("It seems the flags were not producing a senseful pattern!");
123
124             DateFormatSymbols JavaDoc dfs = new DateFormatSymbols JavaDoc(Locale.getDefault());
125             dfs.setAmPmStrings(defaultDateFormatCfMod.getAmPmStrings());
126             dfs.setEras(defaultDateFormatCfMod.getEras());
127             dfs.setMonths(defaultDateFormatCfMod.getMonthsLong());
128             dfs.setShortMonths(defaultDateFormatCfMod.getMonthsShort());
129             dfs.setWeekdays(defaultDateFormatCfMod.getWeekDaysLong());
130             dfs.setShortWeekdays(defaultDateFormatCfMod.getWeekDaysShort());
131             sdf = new SimpleDateFormat JavaDoc(pattern, dfs);
132
133             synchronized (dateFormatsByFlags) {
134                 dateFormatsByFlags.put(flagsL, sdf);
135             }
136         }
137         return sdf;
138     }
139
140 }
141
Popular Tags