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
55 import org.apache.commons.lang3.StringUtils;
56
57 import javax.swing.*;
58 import java.text.DateFormat;
59 import java.text.ParseException;
60 import java.util.Date;
61 public class DateComponentFormatter extends JFormattedTextField.AbstractFormatter {
62
63 private static final long serialVersionUID = 5997312768041129127L;
64
65 private final AbstractComponentFormat componentFormat;
66
67 /**
68 * <p>Constructor for DateComponentFormatter.</p>
69 */
70 public DateComponentFormatter() {
71 AbstractComponentFormat componentFormat;
72 try {
73 componentFormat = ComponentFormatDefaults.getInstance().clone();
74 } catch (CloneNotSupportedException e) {
75 componentFormat = ComponentFormatDefaults.getInstance();
76 }
77 this.componentFormat = componentFormat;
78 }
79
80 /** {@inheritDoc} */
81 @Override
82 public String valueToString(Object value) {
83 if (value instanceof Date) {
84 Date date = (Date) value;
85 DateFormat format = componentFormat.getFormat(AbstractComponentFormat.Key.OUTPUT_DATE_FIELD);
86 return format.format(date);
87 }
88 return "";
89 }
90
91 /** {@inheritDoc} */
92 @Override
93 public Object stringToValue(String text) throws ParseException {
94 if (StringUtils.isBlank(text)) {
95 return null;
96 }
97 DateFormat format = componentFormat.getFormat(AbstractComponentFormat.Key.INPUT_DATE_FIELD);
98 return format.parse(text);
99 }
100
101 /**
102 * <p>setFormat.</p>
103 *
104 * @param formatKey a {@link AbstractComponentFormat.Key} object.
105 * @param format a {@link java.text.DateFormat} object.
106 */
107 public void setFormat(AbstractComponentFormat.Key formatKey, DateFormat format) {
108 componentFormat.setFormat(formatKey, format);
109 }
110 }