View Javadoc
1   
2   /**
3    * Copyright 2004 Juan Heyns. All rights reserved.
4    * <p/>
5    * Redistribution and use in source and binary forms, with or without modification, are
6    * permitted provided that the following conditions are met:
7    * <p/>
8    * 1. Redistributions of source code must retain the above copyright notice, this list of
9    * conditions and the following disclaimer.
10   * <p/>
11   * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12   * of conditions and the following disclaimer in the documentation and/or other materials
13   * provided with the distribution.
14   * <p/>
15   * THIS SOFTWARE IS PROVIDED BY JUAN HEYNS ``AS IS'' AND ANY EXPRESS OR IMPLIED
16   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JUAN HEYNS OR
18   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24   * <p/>
25   * The views and conclusions contained in the software and documentation are those of the
26   * authors and should not be interpreted as representing official policies, either expressed
27   * or implied, of Juan Heyns.
28   */
29  package fr.ifremer.quadrige3.ui.swing.component.date;
30  
31  /*
32   * #%L
33   * Reef DB :: UI
34   * $Id:$
35   * $HeadURL:$
36   * %%
37   * Copyright (C) 2014 - 2015 Ifremer
38   * %%
39   * This program is free software: you can redistribute it and/or modify
40   * it under the terms of the GNU Affero General Public License as published by
41   * the Free Software Foundation, either version 3 of the License, or
42   * (at your option) any later version.
43   * 
44   * This program is distributed in the hope that it will be useful,
45   * but WITHOUT ANY WARRANTY; without even the implied warranty of
46   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
47   * GNU General Public License for more details.
48   * 
49   * You should have received a copy of the GNU Affero General Public License
50   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
51   * #L%
52   */
53  
54  import java.text.DateFormat;
55  import java.util.*;
56  public final class ComponentTextDefaults {
57  
58      private static ComponentTextDefaults instance;
59  
60      /**
61       * <p>Getter for the field <code>instance</code>.</p>
62       *
63       * @return a {@link ComponentTextDefaults} object.
64       */
65      public static ComponentTextDefaults getInstance() {
66          if (instance == null) {
67              instance = new ComponentTextDefaults();
68          }
69          return instance;
70      }
71  
72      public enum Key {
73          // General texts
74          TODAY("text.today", "general"),
75          MONTH("text.month", "general"),
76          YEAR("text.year", "general"),
77          CLEAR("text.clear", "general"),
78  
79          // Months of the year
80          JANUARY("text.january", "month", 0),
81          FEBRUARY("text.february", "month", 1),
82          MARCH("text.march", "month", 2),
83          APRIL("text.april", "month", 3),
84          MAY("text.may", "month", 4),
85          JUNE("text.june", "month", 5),
86          JULY("text.july", "month", 6),
87          AUGUST("text.august", "month", 7),
88          SEPTEMBER("text.september", "month", 8),
89          OCTOBER("text.october", "month", 9),
90          NOVEMBER("text.november", "month", 10),
91          DECEMBER("text.december", "month", 11),
92  
93          // Days of the week abbreviated where necessary
94          SUN("text.sun", "dow", 0),
95          MON("text.mon", "dow", 1),
96          TUE("text.tue", "dow", 2),
97          WED("text.wed", "dow", 3),
98          THU("text.thu", "dow", 4),
99          FRI("text.fri", "dow", 5),
100         SAT("text.sat", "dow", 6);
101 
102         private final String property;
103         private final String kind;
104         private Integer index;
105 
106         Key(String property, String kind) {
107             this.property = property;
108             this.kind = kind;
109         }
110 
111         Key(String property, String kind, Integer index) {
112             this.property = property;
113             this.kind = kind;
114             this.index = index;
115         }
116 
117         public String getProperty() {
118             return property;
119         }
120 
121         public String getKind() {
122             return kind;
123         }
124 
125         public Integer getIndex() {
126             return index;
127         }
128 
129         public static Key getMonthKey(int index) {
130             for (Key key : values()) {
131                 if ("month".equals(key.getKind()) && index == key.getIndex()) {
132                     return key;
133                 }
134             }
135             return null;
136         }
137 
138         public static Key getDowKey(int index) {
139             for (Key key : values()) {
140                 if ("dow".equals(key.getKind()) && index == key.getIndex()) {
141                     return key;
142                 }
143             }
144             return null;
145         }
146 
147     }
148 
149     private final Properties texts;
150 
151     /**
152      * Instantiated with the values which is default for the current locale.
153      */
154     private ComponentTextDefaults() {
155         texts = toProperties(ResourceBundle.getBundle("org.jdatepicker.i18n.Text", Locale.getDefault()));
156     }
157 
158     private Properties toProperties(ResourceBundle resource) {
159         Properties result = new Properties();
160         Enumeration<String> keys = resource.getKeys();
161         while (keys.hasMoreElements()) {
162             String key = keys.nextElement();
163             result.put(key, resource.getString(key));
164         }
165         return result;
166     }
167 
168     /**
169      * For general texts retrieve from the resource bundles.
170      *
171      * For months and day of the week use the SimpleDateFormat symbols. In most cases these are the correct ones, but
172      * we may want to override it, so if a text is specified then we will not consider the SimpleDateFormat symbols.
173      *
174      * @param key a {@link ComponentTextDefaults.Key} object.
175      * @return a {@link java.lang.String} object.
176      */
177     public String getText(Key key) {
178         String text = texts.getProperty(key.getProperty());
179         if (text == null && "month".equals(key.getKind())) {
180             Calendar c = Calendar.getInstance();
181             c.set(Calendar.MONTH, key.getIndex());
182             AbstractComponentFormat defaults = ComponentFormatDefaults.getInstance();
183             DateFormat monthFormat = defaults.getFormat(AbstractComponentFormat.Key.MONTH_SELECTOR);
184             text = monthFormat.format(c.getTime());
185         }
186         if (text == null && "dow".equals(key.getKind())) {
187             Calendar c = Calendar.getInstance();
188             c.set(Calendar.DAY_OF_WEEK, key.getIndex());
189             AbstractComponentFormat defaults = ComponentFormatDefaults.getInstance();
190             DateFormat dowFormat = defaults.getFormat(AbstractComponentFormat.Key.DOW_HEADER);
191             text = dowFormat.format(c.getTime());
192         }
193         return text;
194     }
195 
196     /**
197      * <p>setText.</p>
198      *
199      * @param key a {@link ComponentTextDefaults.Key} object.
200      * @param value a {@link java.lang.String} object.
201      */
202     public void setText(Key key, String value) {
203         texts.setProperty(key.getProperty(), value);
204     }
205 
206 }