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 @ 1881

History | View | Annotate | Download (10.3 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.net.URL;
32
import java.text.DateFormat;
33
import java.text.SimpleDateFormat;
34
import java.util.Date;
35
import java.util.Locale;
36
import javax.swing.ImageIcon;
37

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

    
45
import org.freixas.jcalendar.DateEvent;
46
import org.freixas.jcalendar.DateListener;
47
import org.freixas.jcalendar.JCalendar;
48
import org.gvsig.tools.dynform.DynFormFieldDefinition;
49

    
50
import org.gvsig.tools.dynform.JDynFormField;
51
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
52
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory;
53
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory;
54
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField;
55
import org.gvsig.tools.dynform.spi.dynformfield.CustomSpinnerDateModel;
56
import org.gvsig.tools.dynform.spi.dynformfield.JCustomSpinner;
57
import org.gvsig.tools.dynobject.DynObjectValueItem;
58
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
59

    
60
/**
61
 * @author gvSIG team
62
 *
63
 */
64
public class JDynFormFieldDate extends AbstractJDynFormField implements JDynFormField, FocusListener {
65

    
66
    protected Object assignedValue = null;
67
    private JCalendar jcalendar = null;
68
    private JSpinner timeSpinner = null;
69
    private JButton button = null;
70
    private boolean readonly;
71
    private static final String DATE_PANEL_NAME = "Date panel";
72

    
73
    public JDynFormFieldDate(
74
            DynFormSPIManager serviceManager,
75
            ComponentsFactory componentsFactory,
76
            JDynFormFieldFactory factory,
77
            DynFormFieldDefinition definition,
78
            Object value
79
    ) {
80
        super(serviceManager, componentsFactory, factory, definition, value);
81
        this.assignedValue = value;
82
    }
83

    
84
    public void setReadOnly(boolean readonly) {
85
        this.readonly = readonly;
86
        if (this.contents != null) {
87
            if (this.readonly) {
88
                this.timeSpinner.setEnabled(false);
89
                this.button.setEnabled(false);
90
            } else {
91
                this.timeSpinner.setEnabled(true);
92
                this.button.setEnabled(true);
93
            }
94
        }
95
    }
96

    
97
    public Object getAssignedValue() {
98
        return this.assignedValue;
99
    }
100

    
101
    protected JSpinner getJSpinner() {
102
        return (JSpinner) this.timeSpinner;
103
    }
104

    
105
    protected JButton getJButton() {
106
        return (JButton) this.button;
107
    }
108

    
109
    protected JCalendar getJCalendar() {
110
        if (this.jcalendar == null) {
111
            this.jcalendar = new JCalendar();
112
            this.jcalendar.addDateListener(new DateListener() {
113
                public void dateChanged(DateEvent arg0) {
114
                    getJSpinner().setValue(getJCalendar().getDate());
115
                }
116
            });
117
        }
118
        return (JCalendar) this.jcalendar;
119
    }
120

    
121
    public void initComponent() {
122
        DynObjectValueItem[] availableValues = this.getDefinition().getAvailableValues();
123
        if (availableValues == null) {
124
            this.contents = new JPanel();
125
            this.contents.setName(DATE_PANEL_NAME);
126
            this.contents.setLayout(new BorderLayout());
127
            timeSpinner = this.getComponentsFactory().getJSpinner(this.getDefinition(), null);
128
            CustomSpinnerDateModel model = new CustomSpinnerDateModel(true);
129
            timeSpinner.setModel(model);
130
            DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
131
            String pattern = "dd-MM-yyyy";
132
            try {
133
                SimpleDateFormat sdf = (SimpleDateFormat) dateFormat;
134
                pattern = sdf.toPattern();
135
            } catch (ClassCastException e) {
136
                LOGGER.warn("Can't determinate date format pattern, using default pattern:" + pattern);
137
            }
138

    
139
            NullDateEditor timeEditor = new NullDateEditor(timeSpinner, pattern);
140
            timeSpinner.setEditor(timeEditor);
141
//            timeEditor.dismiss(timeSpinner);
142
            button = this.getComponentsFactory().getJButton(this.getDefinition(), null);
143
            if( button.getIcon()==null ) {
144
                URL imageResource = this.getClass().getClassLoader().getResource("org/gvsig/tools/swing/picker/picker-date.png");
145
                if (imageResource != null) {
146
                    button.setIcon(new ImageIcon(imageResource));
147
                }
148
            }
149
            if( button.getText().trim().equals("...") ) {
150
                button.setText("");
151
            }
152
//            button.setText("\u2026");
153
            button.addActionListener(new ActionListener() {
154
                public void actionPerformed(ActionEvent event) {
155
                    JPopupMenu menu = new JPopupMenu();
156
                    menu.add(getJCalendar());
157
                    Date value = (Date) getJSpinner().getValue();
158
                    if (value != null) {
159
                        getJCalendar().setDate(value);
160
                    }
161
                    JComponent thisComp = (JComponent) event.getSource();
162
                    menu.show(thisComp, 0, thisComp.getY() + 22);
163
                    setValue(getJCalendar().getDate());
164
                }
165
            });
166

    
167
            if (this.getDefinition().isReadOnly()) {
168
                this.getJSpinner().setEnabled(false);
169
                this.getJButton().setEnabled(false);
170
            }
171
            this.getJSpinner().addFocusListener(this);
172

    
173
            if( !this.getComponentsFactory().containsJSpinner(this.getDefinition(), null) ) {
174
                this.contents.add(timeSpinner, BorderLayout.CENTER);
175
                this.contents.add(button, BorderLayout.LINE_END);
176
            }
177
        }
178
        this.setValue(this.assignedValue);
179
    }
180

    
181
    public void setValue(Object value) {
182
        if (value == null) {
183
            value = this.getDefinition().getDefaultValue();
184
            if (value == null) {
185
                value = null;
186
            }
187
        } else {
188
            try {
189
                this.getDefinition().validate(value);
190
                this.problemIndicator().clear();
191
            } catch (DynFieldValidateException e) {
192
                this.problemIndicator().set(e.getLocalizedMessage());
193
            }
194
        }
195
        if (this.contents instanceof JPanel && this.contents.getName().equalsIgnoreCase(DATE_PANEL_NAME)) {
196
            try {
197
                this.getJSpinner().setValue(value);
198
            } catch (Exception ex) {
199

    
200
            }
201
        }
202
        this.assignedValue = value;
203
    }
204

    
205
    /*
206
         * Métodos específicos de cada tipo de datos
207
     */
208
    public Object getValue() {
209
        Object value = null;
210
        if (this.contents instanceof JPanel && this.contents.getName().equalsIgnoreCase(DATE_PANEL_NAME)) {
211
            value = getJSpinner().getValue();
212
        }
213
        try {
214
            this.getDefinition().validate(value);
215
            this.problemIndicator().clear();
216
        } catch (DynFieldValidateException e) {
217
            throw new IllegalFieldValue(this, e.getLocalizedMessage());
218
        }
219
        return value;
220
    }
221

    
222
    @SuppressWarnings("unused")
223
    public boolean hasValidValue() {
224
        try {
225
            Object value = this.getValue();
226
        } catch (Exception e) {
227
            return false;
228
        }
229
        return true;
230
    }
231

    
232
    public void focusGained(FocusEvent arg0) {
233
        fireFieldEnterEvent();
234
        this.problemIndicator().restore();
235
    }
236

    
237
    public void focusLost(FocusEvent arg0) {
238
        if (this.hasValidValue()) {
239
            this.problemIndicator().clear();
240
        } else {
241
            try {
242
                Object value = this.getValue();
243
            } catch (Exception e) {
244
                this.problemIndicator().set(e.getLocalizedMessage());
245
            }
246
        }
247
        fireFieldExitEvent();
248
    }
249

    
250
    public void clear() {
251
        setValue(null);
252
//            Object value = this.getDefinition().getDefaultValue();
253
//            if( value != null ) {
254
//                if (!(value instanceof Date)){
255
//                    try {
256
//                        value = DateFormat.getInstance().parse(value.toString());
257
//                    } catch (ParseException e) {
258
//                        this.problemIndicator().set(e.getLocalizedMessage());
259
//                    }
260
//                }
261
//            } else {
262
//                value = "";
263
//            }
264
//            try {
265
//                this.timeSpinner.setValue(value);
266
//            } catch (RuntimeException e) {
267
//                this.timeSpinner.setValue(getJCalendar().getDate());
268
//            }
269
    }
270

    
271
    private class NullDateEditor extends JSpinner.DateEditor {
272

    
273
        /**
274
         *
275
         */
276
        private static final long serialVersionUID = -522934985228645970L;
277

    
278
        /**
279
         * @param jspinner
280
         * @param pattern
281
         *
282
         */
283
        public NullDateEditor(JSpinner jspinner, String pattern) {
284
            super(jspinner, pattern);
285
        }
286

    
287
        public NullDateEditor(JSpinner jspinner) {
288
            super(jspinner);
289
        }
290

    
291
        public void stateChanged(ChangeEvent e) {
292
            Object value = getSpinner().getValue();
293

    
294
            String text = "";
295
            if (value != null) {
296
                text = getFormat().format((Date) value);
297
            }
298
            getTextField().setText(text);
299
        }
300
    }
301
}