Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.dynform / org.gvsig.tools.dynform.services / src / main / java / org / gvsig / tools / dynform / services / dynformfield / Date / JDynFormFieldDate.java @ 1388

History | View | Annotate | Download (8.1 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.tools.dynform.services.dynformfield.Date;
25

    
26
import java.awt.BorderLayout;
27
import java.awt.event.ActionEvent;
28
import java.awt.event.ActionListener;
29
import java.awt.event.FocusEvent;
30
import java.awt.event.FocusListener;
31
import java.text.DateFormat;
32
import java.text.SimpleDateFormat;
33
import java.util.Date;
34
import java.util.Locale;
35

    
36
import javax.swing.JButton;
37
import javax.swing.JComponent;
38
import javax.swing.JPanel;
39
import javax.swing.JPopupMenu;
40
import javax.swing.JSpinner;
41
import javax.swing.event.ChangeEvent;
42

    
43
import org.freixas.jcalendar.DateEvent;
44
import org.freixas.jcalendar.DateListener;
45
import org.freixas.jcalendar.JCalendar;
46

    
47
import org.gvsig.tools.dynform.JDynFormField;
48
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField;
49
import org.gvsig.tools.dynform.spi.dynformfield.JCustomSpinner;
50
import org.gvsig.tools.dynobject.DynObject;
51
import org.gvsig.tools.dynobject.DynObjectValueItem;
52
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
53
import org.gvsig.tools.service.spi.ServiceManager;
54

    
55
/**
56
 * @author gvSIG team
57
 *
58
 */
59
public class JDynFormFieldDate extends AbstractJDynFormField implements JDynFormField, FocusListener {
60

    
61
        protected Object assignedValue  = null;
62
        private JCalendar jcalendar = null;
63
        private JCustomSpinner timeSpinner = null;
64
        private JButton button = null;
65
        private boolean readonly;
66
        private static final String DATE_PANEL_NAME = "Date panel";
67

    
68
        /**
69
         * @param parameters
70
         * @param serviceManager
71
         */
72
        public JDynFormFieldDate(DynObject parameters,
73
                        ServiceManager serviceManager) {
74
                super(parameters, serviceManager);
75
                this.assignedValue = this.getParameterValue();
76
        }
77

    
78
        public void setReadOnly(boolean readonly) {
79
                this.readonly = readonly;
80
                if( this.contents != null ) {
81
                        if( this.readonly ) {
82
                                this.timeSpinner.setEditable(false);
83
                                this.button.setEnabled(false);
84
                        } else {
85
                                this.timeSpinner.setEditable(true);
86
                                this.button.setEnabled(true);
87
                        }
88
                }
89
        }
90

    
91
        public Object getAssignedValue() {
92
                return this.assignedValue;
93
        }
94

    
95
        protected JSpinner getJSpinner() {
96
                return (JSpinner) this.timeSpinner;
97
        }
98

    
99
        protected JButton getJButton() {
100
                return (JButton) this.button;
101
        }
102

    
103
        protected JCalendar getJCalendar() {
104
                if(this.jcalendar==null){
105
                        this.jcalendar = new JCalendar();
106
                        this.jcalendar.addDateListener(new DateListener() {
107
                                public void dateChanged(DateEvent arg0) {
108
                                        getJSpinner().setValue(getJCalendar().getDate());
109
                                }
110
                        });
111
                }
112
                return (JCalendar) this.jcalendar;
113
        }
114

    
115

    
116
        public void initComponent() {
117
                DynObjectValueItem[] availableValues = this.getDefinition().getAvailableValues();
118
                if( availableValues==null ) {
119
                        this.contents = new JPanel();
120
                        this.contents.setName(DATE_PANEL_NAME);
121
                        this.contents.setLayout(new BorderLayout());
122

    
123
                        timeSpinner = new JCustomSpinner(this.getLabel(), true);
124
                        DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
125
                        String pattern = "dd-MM-yyyy";
126
                        try{
127
                            SimpleDateFormat sdf = (SimpleDateFormat)dateFormat;
128
                            pattern =sdf.toPattern();
129
                        } catch (ClassCastException e) {
130
                            logger.warn("Can't determinate date format pattern, using default pattern:"+pattern);
131
                        }
132

    
133
            NullDateEditor timeEditor = new NullDateEditor(timeSpinner, pattern);
134
                        timeSpinner.setEditor(timeEditor);
135
//                        timeEditor.dismiss(timeSpinner);
136
                        button  = new JButton("\u2026");
137
                        button.addActionListener(new ActionListener() {
138
                                public void actionPerformed(ActionEvent event) {
139
                                        JPopupMenu menu = new JPopupMenu();
140
                                        menu.add(getJCalendar());
141
                                        Date value = (Date)getJSpinner().getValue();
142
                                        if(value!=null){
143
                                            getJCalendar().setDate(value);
144
                                        }
145
                                        JComponent thisComp = (JComponent)event.getSource();
146
                                        menu.show(thisComp, 0, thisComp.getY()+22);
147
                                        setValue(getJCalendar().getDate());
148
                                }
149
                        });
150

    
151
                        this.contents.addFocusListener(this);
152
                        if(this.getDefinition().isReadOnly()) {
153
                                this.getJSpinner().setEnabled(false);
154
                                this.getJButton().setEnabled(false);
155
                        }
156

    
157
                        this.contents.add(timeSpinner,BorderLayout.CENTER);
158
                        this.contents.add(button,BorderLayout.LINE_END);
159
                }
160
                this.setValue(this.assignedValue);
161
        }
162

    
163
        public void setValue(Object value) {
164
                if( value == null ) {
165
                        value = this.getDefinition().getDefaultValue();
166
                        if( value == null ) {
167
                                value = null;
168
                        }
169
                } else {
170
                        try {
171
                                this.getDefinition().validate(value);
172
                                this.problemIndicator().clear();
173
                        } catch (DynFieldValidateException e) {
174
                                this.problemIndicator().set(e.getLocalizedMessage());
175
                        }
176
                }
177
        if (this.contents instanceof JPanel && this.contents.getName().equalsIgnoreCase(DATE_PANEL_NAME)) {
178
            this.getJSpinner().setValue(value);
179
        }
180
                this.assignedValue = value;
181
        }
182

    
183
        /*
184
         * Métodos específicos de cada tipo de datos
185
         */
186

    
187
        public Object getValue() {
188
                Object value = null;
189
                if( this.contents instanceof JPanel && this.contents.getName().equalsIgnoreCase(DATE_PANEL_NAME) ) {
190
                        value = getJSpinner().getValue();
191
                }
192
                try {
193
                        this.getDefinition().validate(value);
194
                        this.problemIndicator().clear();
195
                } catch (DynFieldValidateException e) {
196
                        throw new IllegalFieldValue(this, e.getLocalizedMessage());
197
                }
198
                return value;
199
        }
200

    
201

    
202
        @SuppressWarnings("unused")
203
        public boolean hasValidValue() {
204
                try {
205
                        Object value = this.getValue();
206
                } catch(Exception e) {
207
                        return false;
208
                }
209
                return true;
210
        }
211

    
212
        public void focusGained(FocusEvent arg0) {
213
                fireFieldEnterEvent();
214
                this.problemIndicator().restore();
215
        }
216

    
217
        public void focusLost(FocusEvent arg0) {
218
                if( this.hasValidValue() ) {
219
                        this.problemIndicator().clear();
220
                } else {
221
                        try {
222
                                Object value = this.getValue();
223
                        } catch(Exception e) {
224
                                this.problemIndicator().set(e.getLocalizedMessage());
225
                        }
226
                }
227
                fireFieldExitEvent();
228
        }
229

    
230
        public void clear() {
231
            setValue(null);
232
//            Object value = this.getDefinition().getDefaultValue();
233
//            if( value != null ) {
234
//                if (!(value instanceof Date)){
235
//                    try {
236
//                        value = DateFormat.getInstance().parse(value.toString());
237
//                    } catch (ParseException e) {
238
//                        this.problemIndicator().set(e.getLocalizedMessage());
239
//                    }
240
//                }
241
//            } else {
242
//                value = "";
243
//            }
244
//            try {
245
//                this.timeSpinner.setValue(value);
246
//            } catch (RuntimeException e) {
247
//                this.timeSpinner.setValue(getJCalendar().getDate());
248
//            }
249
        }
250

    
251

    
252
    private class NullDateEditor extends JSpinner.DateEditor {
253

    
254
        /**
255
         *
256
         */
257
        private static final long serialVersionUID = -522934985228645970L;
258

    
259
        /**
260
         * @param jspinner
261
         * @param pattern
262
         *
263
         */
264
        public NullDateEditor(JSpinner jspinner, String pattern) {
265
            super(jspinner, pattern);
266
        }
267

    
268
        public NullDateEditor(JSpinner jspinner) {
269
            super(jspinner);
270
        }
271

    
272
        public void stateChanged(ChangeEvent e) {
273
            Object value = getSpinner().getValue();
274

    
275
            String text = "";
276
            if (value != null) {
277
                text = getFormat().format((Date) value);
278
            }
279
            getTextField().setText(text);
280
        }
281
    }
282
}