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 / Timestamp / JDynFormFieldTimestamp.java @ 3033

History | View | Annotate | Download (11.2 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.Timestamp;
25

    
26
import java.awt.GridBagConstraints;
27
import java.awt.GridBagLayout;
28
import java.awt.event.ActionEvent;
29
import java.awt.event.FocusEvent;
30
import java.awt.event.FocusListener;
31
import java.awt.event.MouseAdapter;
32
import java.awt.event.MouseEvent;
33
import java.sql.Timestamp;
34
import java.util.Date;
35
import java.util.Objects;
36
import javax.swing.JButton;
37
import javax.swing.JComponent;
38
import javax.swing.JPanel;
39
import javax.swing.JPopupMenu;
40
import javax.swing.event.DocumentEvent;
41
import javax.swing.event.DocumentListener;
42
import javax.swing.text.JTextComponent;
43
import org.apache.commons.lang3.StringUtils;
44
import org.freixas.jcalendar.DateEvent;
45
import org.freixas.jcalendar.JCalendar;
46
import org.gvsig.tools.dataTypes.CoercionException;
47
import org.gvsig.tools.dynform.DynFormFieldDefinition;
48
import org.gvsig.tools.dynform.JDynFormField;
49
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
50
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField;
51
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory;
52
import org.gvsig.tools.dynobject.DynObjectValueItem;
53
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
54
import org.gvsig.tools.swing.api.ToolsSwingLocator;
55
import org.gvsig.tools.swing.api.ToolsSwingManager;
56
import org.gvsig.tools.swing.api.ToolsSwingUtils;
57

    
58
public class JDynFormFieldTimestamp extends AbstractJDynFormField implements JDynFormField {
59

    
60
    protected Object assignedValue = null;
61
    private JCalendar jcalendar = null;
62
    private JButton button = null;
63
    private boolean readonly = false;
64
    private JTextComponent txtTimestamp;
65
    private final DocumentListener documentListener;
66
    private final FocusListener focusListener;
67

    
68
    public JDynFormFieldTimestamp(
69
            DynFormSPIManager serviceManager,
70
            DynFormSPIManager.ComponentsFactory componentsFactory,
71
            JDynFormFieldFactory factory,
72
            DynFormFieldDefinition definition,
73
            Object value
74
    ) {
75
        super(serviceManager, componentsFactory, factory, definition, value);
76
        this.assignedValue = value;
77
        this.focusListener = new FocusListener() {
78
            @Override
79
            public void focusGained(FocusEvent e) {
80
                fireFieldEnterEvent();
81
            }
82

    
83
            @Override
84
            public void focusLost(FocusEvent e) {
85
                if (hasValidValue()) {
86
                    problemIndicator().clear();
87
                } else {
88
                    try {
89
                        Object value = getValue();
90
                    } catch (Exception ex) {
91
                        problemIndicator().set(ex.getLocalizedMessage());
92
                    }
93
                }
94
                fireFieldExitEvent();
95
            }
96
        };
97
        this.documentListener = new DocumentListener() {
98
            @Override
99
            public void insertUpdate(DocumentEvent e) {
100
//                updateProblemIndicator();
101
                fireFieldChangedEvent();
102
            }
103

    
104
            @Override
105
            public void removeUpdate(DocumentEvent e) {
106
//                updateProblemIndicator();
107
                fireFieldChangedEvent();
108
            }
109

    
110
            @Override
111
            public void changedUpdate(DocumentEvent e) {
112
//                updateProblemIndicator();
113
                fireFieldChangedEvent();
114
            }
115
        };
116
    }
117

    
118
    @Override
119
    public void setReadOnly(boolean readonly) {
120
        JComponent theJlabel = this.getJLabel();
121
        if( this.jlabel != null ) {
122
            this.jlabel.setEnabled(!readonly);
123
        } else if( theJlabel !=null ) {
124
            theJlabel.setEnabled(!readonly);
125
        }
126
        this.readonly = readonly;
127
        if (this.contents != null) {
128
            if (readonly) {
129
                this.txtTimestamp.setEditable(false);
130
                this.button.setEnabled(false);
131
            } else {
132
                this.txtTimestamp.setEditable(true);
133
                this.button.setEnabled(true);
134
            }
135
        }
136
        this.setReadOnlyButtonsOfEvents(readonly);
137
    }
138

    
139
    @Override
140
    public Object getAssignedValue() {
141
        return this.assignedValue;
142
    }
143

    
144
    protected JButton getJButton() {
145
        return (JButton) this.button;
146
    }
147

    
148
    protected JCalendar getJCalendar() {
149
        if (this.jcalendar == null) {
150
            this.jcalendar = new JCalendar(JCalendar.DISPLAY_DATE | JCalendar.DISPLAY_TIME, false);   
151
            this.jcalendar.setNullAllowed(true);
152
            this.jcalendar.addDateListener((DateEvent arg0) -> {
153
                doJCalendarChanged();
154
            });
155
        }
156
        return (JCalendar) this.jcalendar;
157
    }
158

    
159
    private void doJCalendarChanged() {
160
        Timestamp currentDate = null;
161
        try {
162
            currentDate = (Timestamp) getValue();
163
        } catch(Exception ex) {
164
            LOGGER.debug("Can't get current value",ex);
165
        }
166
        if(currentDate == null){
167
            currentDate = new Timestamp(new Date().getTime());
168
        }
169
        Date newDate = getJCalendar().getDate();
170
        if( newDate == null ) {
171
            return;
172
        }
173

    
174
        currentDate.setDate(newDate.getDate());
175
        currentDate.setMonth(newDate.getMonth());
176
        currentDate.setYear(newDate.getYear());
177
        txtTimestamp.setText(StringUtils.rightPad(currentDate.toString(),29,'0'));
178
        try {
179
            this.getDefinition().validate(currentDate);
180
            this.problemIndicator().clear();
181
        } catch (DynFieldValidateException e) {
182
            this.problemIndicator().set(e.getLocalizedMessage());
183
        }
184
        fireFieldChangedEvent();
185
    }
186
    
187
    @Override
188
    public void initComponent() {
189
        DynObjectValueItem[] availableValues = this.getDefinition().getAvailableValues();
190
        if (availableValues == null) {
191
            ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
192
            this.contents = new JPanel();
193

    
194
            this.txtTimestamp = this.getComponentsFactory().getJTextField(this.getDefinition(), null);
195
            this.txtTimestamp.removeFocusListener(focusListener);
196
            this.txtTimestamp.getDocument().removeDocumentListener(documentListener);
197
            
198
            button = this.getComponentsFactory().getJButton(this.getDefinition(), null);
199
            if( button == null ) {
200
                button = new JButton();
201
            }
202

    
203
            toolsSwingManager.addClearButton(this.txtTimestamp);
204
            toolsSwingManager.setDefaultPopupMenu(this.txtTimestamp);
205
            this.txtTimestamp.addFocusListener(focusListener);
206
            this.txtTimestamp.getDocument().addDocumentListener(documentListener);
207
            
208
            ToolsSwingUtils.configurePickersButton(
209
                    button, 
210
                    "_Show_calendar", 
211
                    "picker-date", 
212
                    (ActionEvent e) -> { doShowCalendar(); },
213
                    focusListener
214
            );                
215
            
216
            this.contents.addFocusListener(this);
217
            if (this.getDefinition().isReadOnly()) {
218
                this.txtTimestamp.setEnabled(false);
219
                this.getJButton().setEnabled(false);
220
            }
221

    
222
            this.contents.setLayout(new GridBagLayout());
223
            GridBagConstraints c = new GridBagConstraints();
224
            c.fill = GridBagConstraints.HORIZONTAL;
225
            c.ipadx = 4;
226
            c.ipady = 1;
227
            c.gridx = 1;
228
            c.gridy = 0;
229
            c.weightx = 1;
230
            this.contents.add(this.txtTimestamp, c);
231
            c.fill = GridBagConstraints.NONE;
232
            c.ipadx = 4;
233
            c.ipady = 1;
234
            c.gridx = 2;
235
            c.gridy = 0;
236
            c.weightx = 0;
237
            this.contents.add(this.button, c);
238
        }
239
        this.setValue(this.assignedValue);
240
    }
241

    
242
    private void doShowCalendar() {
243
        fireFieldEnterEvent();
244
        JPopupMenu menu = new JPopupMenu();        
245
        try {
246
            Date v = (Date) this.getValue();
247
            getJCalendar().setDate(v);
248
        } catch(Exception ex) {
249
            LOGGER.debug("Can't asign date to calendar");
250
            getJCalendar().setDate(null);
251
        }        
252
        menu.add(getJCalendar());
253

    
254
        menu.show(txtTimestamp, 0, txtTimestamp.getY() + 22);
255
    }
256
    
257
    @Override
258
    public void setValue(Object value) {
259
        if (value == null) {
260
            this.txtTimestamp.setText("");
261
        } else {
262
            this.txtTimestamp.setText(StringUtils.rightPad(((Timestamp)value).toString(),29,'0'));
263
        }
264
        try {
265
            this.getDefinition().validate(value);
266
            this.problemIndicator().clear();
267
        } catch (DynFieldValidateException e) {
268
            this.problemIndicator().set(e.getLocalizedMessage());
269
        }
270
        this.assignedValue = value;
271
    }
272

    
273
    @Override
274
    public Object getValue() {
275
        try {
276
            String ss = this.txtTimestamp.getText();
277
            if( StringUtils.isBlank(ss) ) {
278
                return null;
279
            }
280
            Object value = Timestamp.valueOf(ss);
281
            this.getDefinition().validate(value);
282
            this.problemIndicator().clear();
283
            return value;
284
        } catch (DynFieldValidateException e) {
285
            throw new IllegalFieldValue(this, e.getLocalizedMessage());
286
        }
287
    }
288

    
289
    @SuppressWarnings("unused")
290
    @Override
291
    public boolean hasValidValue() {
292
        try {
293
            Object value = this.getValue();
294
        } catch (Exception e) {
295
            return false;
296
        }
297
        return true;
298
    }
299

    
300
    @Override
301
    public boolean isModified() {
302
        
303
        Timestamp assigned = (Timestamp) getAssignedValue();
304
        String ss = this.txtTimestamp.getText();
305
        if (StringUtils.isBlank(ss)) {
306
            return assigned != null;
307
        }
308

    
309
        try {
310
            Timestamp value = Timestamp.valueOf(ss);
311
            return !Objects.equals(value, assigned);
312
        } catch (Exception ex) {
313
            return false;
314
        }
315
    }
316
    
317
    private void updateProblemIndicator() {
318
        try {
319
            Object value = this.getDefinition().coerce(getValue());
320
            this.getDefinition().validate(value);
321
            this.problemIndicator().clear();
322
        } catch (DynFieldValidateException ex) {
323
            this.problemIndicator().set(ex.getLocalizedMessageStack());
324
        } catch (CoercionException ex) {
325
            this.problemIndicator().set(ex.getLocalizedMessage());
326
        }
327
    }    
328
}