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

History | View | Annotate | Download (10.5 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
    @Override
85
    public void setReadOnly(boolean readonly) {
86
        JComponent jlabel = this.getJLabel();
87
        if( jlabel !=null ) {
88
            jlabel.setEnabled(!readonly);
89
        }
90
        this.readonly = readonly;
91
        if (this.contents != null) {
92
            if (this.readonly) {
93
                this.timeSpinner.setEnabled(false);
94
                this.button.setEnabled(false);
95
            } else {
96
                this.timeSpinner.setEnabled(true);
97
                this.button.setEnabled(true);
98
            }
99
        }
100
    }
101

    
102
    public Object getAssignedValue() {
103
        return this.assignedValue;
104
    }
105

    
106
    protected JSpinner getJSpinner() {
107
        return (JSpinner) this.timeSpinner;
108
    }
109

    
110
    protected JButton getJButton() {
111
        return (JButton) this.button;
112
    }
113

    
114
    protected JCalendar getJCalendar() {
115
        if (this.jcalendar == null) {
116
            this.jcalendar = new JCalendar();
117
            this.jcalendar.addDateListener(new DateListener() {
118
                public void dateChanged(DateEvent arg0) {
119
                    getJSpinner().setValue(getJCalendar().getDate());
120
                }
121
            });
122
        }
123
        return (JCalendar) this.jcalendar;
124
    }
125

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

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

    
172
            if (this.getDefinition().isReadOnly()) {
173
                this.getJSpinner().setEnabled(false);
174
                this.getJButton().setEnabled(false);
175
            }
176
            this.getJSpinner().addFocusListener(this);
177

    
178
            if( !this.getComponentsFactory().containsJSpinner(this.getDefinition(), null) ) {
179
                this.contents.add(timeSpinner, BorderLayout.CENTER);
180
                this.contents.add(button, BorderLayout.LINE_END);
181
            }
182
        }
183
        this.setValue(this.assignedValue);
184
    }
185

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

    
205
            }
206
        }
207
        this.assignedValue = value;
208
    }
209

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

    
227
    @SuppressWarnings("unused")
228
    public boolean hasValidValue() {
229
        try {
230
            Object value = this.getValue();
231
        } catch (Exception e) {
232
            return false;
233
        }
234
        return true;
235
    }
236

    
237
    public void focusGained(FocusEvent arg0) {
238
        fireFieldEnterEvent();
239
        this.problemIndicator().restore();
240
    }
241

    
242
    public void focusLost(FocusEvent arg0) {
243
        if (this.hasValidValue()) {
244
            this.problemIndicator().clear();
245
        } else {
246
            try {
247
                Object value = this.getValue();
248
            } catch (Exception e) {
249
                this.problemIndicator().set(e.getLocalizedMessage());
250
            }
251
        }
252
        fireFieldExitEvent();
253
    }
254

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

    
276
    private class NullDateEditor extends JSpinner.DateEditor {
277

    
278
        /**
279
         *
280
         */
281
        private static final long serialVersionUID = -522934985228645970L;
282

    
283
        /**
284
         * @param jspinner
285
         * @param pattern
286
         *
287
         */
288
        public NullDateEditor(JSpinner jspinner, String pattern) {
289
            super(jspinner, pattern);
290
        }
291

    
292
        public NullDateEditor(JSpinner jspinner) {
293
            super(jspinner);
294
        }
295

    
296
        public void stateChanged(ChangeEvent e) {
297
            Object value = getSpinner().getValue();
298

    
299
            String text = "";
300
            if (value != null) {
301
                text = getFormat().format((Date) value);
302
            }
303
            getTextField().setText(text);
304
        }
305
    }
306
}