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

History | View | Annotate | Download (8.08 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.BorderLayout;
27
import java.awt.Cursor;
28
import java.awt.event.ActionEvent;
29
import java.awt.event.FocusEvent;
30
import java.awt.event.FocusListener;
31
import java.sql.Timestamp;
32
import java.util.Date;
33
import java.util.Objects;
34
import javax.swing.JButton;
35
import javax.swing.JComponent;
36
import javax.swing.JPanel;
37
import javax.swing.JPopupMenu;
38
import javax.swing.text.JTextComponent;
39
import org.apache.commons.lang3.StringUtils;
40
import org.freixas.jcalendar.DateEvent;
41
import org.freixas.jcalendar.JCalendar;
42
import org.gvsig.tools.dynform.DynFormFieldDefinition;
43
import org.gvsig.tools.dynform.JDynFormField;
44
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
45
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField;
46
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory;
47
import org.gvsig.tools.dynobject.DynObjectValueItem;
48
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
49
import org.gvsig.tools.swing.api.ToolsSwingLocator;
50
import org.gvsig.tools.swing.api.ToolsSwingManager;
51

    
52
public class JDynFormFieldTimestamp extends AbstractJDynFormField implements JDynFormField, FocusListener {
53

    
54
    protected Object assignedValue = null;
55
    private JCalendar jcalendar = null;
56
    private JButton button = null;
57
    private boolean readonly = false;
58
    private JTextComponent txtTimestamp;
59

    
60
    public JDynFormFieldTimestamp(
61
            DynFormSPIManager serviceManager,
62
            DynFormSPIManager.ComponentsFactory componentsFactory,
63
            JDynFormFieldFactory factory,
64
            DynFormFieldDefinition definition,
65
            Object value
66
    ) {
67
        super(serviceManager, componentsFactory, factory, definition, value);
68
        this.assignedValue = value;
69
    }
70

    
71
    @Override
72
    public void setReadOnly(boolean readonly) {
73
        this.readonly = readonly;
74
        if (this.contents != null) {
75
            if (readonly) {
76
                this.txtTimestamp.setEditable(false);
77
                this.button.setEnabled(false);
78
            } else {
79
                this.txtTimestamp.setEditable(true);
80
                this.button.setEnabled(true);
81
            }
82
        }
83
    }
84

    
85
    @Override
86
    public Object getAssignedValue() {
87
        return this.assignedValue;
88
    }
89

    
90
    protected JButton getJButton() {
91
        return (JButton) this.button;
92
    }
93

    
94
    protected JCalendar getJCalendar() {
95
        if (this.jcalendar == null) {
96
            this.jcalendar = new JCalendar();
97
            this.jcalendar.addDateListener((DateEvent arg0) -> {
98
                Timestamp currentDate = (Timestamp) getValue();
99
                if(currentDate == null){
100
                    currentDate = new Timestamp(new Date().getTime());
101
                }
102
                Date newDate = getJCalendar().getDate();
103
                
104
                currentDate.setDate(newDate.getDate());
105
                currentDate.setMonth(newDate.getMonth());
106
                currentDate.setYear(newDate.getYear());
107
                txtTimestamp.setText(StringUtils.rightPad(currentDate.toString(),29,'0'));
108
                try {
109
                    this.getDefinition().validate(currentDate);
110
                    this.problemIndicator().clear();
111
                } catch (DynFieldValidateException e) {
112
                    this.problemIndicator().set(e.getLocalizedMessage());
113
                }
114
                fireFieldChangedEvent();
115
            });
116
        }
117
        return (JCalendar) this.jcalendar;
118
    }
119

    
120
    @Override
121
    public void initComponent() {
122
        DynObjectValueItem[] availableValues = this.getDefinition().getAvailableValues();
123
        if (availableValues == null) {
124
            ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
125
            this.contents = new JPanel();
126
            this.contents.setLayout(new BorderLayout());
127

    
128
            this.txtTimestamp = this.getComponentsFactory().getJTextField(this.getDefinition(), null);
129
            toolsSwingManager.addClearButton(this.txtTimestamp);
130
            toolsSwingManager.setDefaultPopupMenu(this.txtTimestamp);
131
            button = new JButton();
132
            button.addActionListener((ActionEvent event) -> {
133
                JPopupMenu menu = new JPopupMenu();
134
                menu.add(getJCalendar());
135
                
136
                JComponent thisComp = (JComponent) event.getSource();
137
                menu.show(thisComp, 0, thisComp.getY() + 22);
138
            });
139
            button.setIcon(this.getIcon("picker-date"));            
140
            button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
141

    
142
            this.contents.addFocusListener(this);
143
            if (this.getDefinition().isReadOnly()) {
144
                this.txtTimestamp.setEnabled(false);
145
                this.getJButton().setEnabled(false);
146
            }
147

    
148
            this.contents.add(txtTimestamp, BorderLayout.CENTER);
149
            this.contents.add(button, BorderLayout.LINE_END);
150
        }
151
        this.setValue(this.assignedValue);
152
    }
153

    
154
    @Override
155
    public void setValue(Object value) {
156
        if (value == null) {
157
            this.txtTimestamp.setText("");
158
        } else {
159
            this.txtTimestamp.setText(StringUtils.rightPad(((Timestamp)value).toString(),29,'0'));
160
        }
161
        try {
162
            this.getDefinition().validate(value);
163
            this.problemIndicator().clear();
164
        } catch (DynFieldValidateException e) {
165
            this.problemIndicator().set(e.getLocalizedMessage());
166
        }
167
        this.assignedValue = value;
168
    }
169

    
170
    @Override
171
    public Object getValue() {
172
        try {
173
            String ss = this.txtTimestamp.getText();
174
            if( StringUtils.isBlank(ss) ) {
175
                return null;
176
            }
177
            Object value = Timestamp.valueOf(ss);
178
            this.getDefinition().validate(value);
179
            this.problemIndicator().clear();
180
            return value;
181
        } catch (DynFieldValidateException e) {
182
            throw new IllegalFieldValue(this, e.getLocalizedMessage());
183
        }
184
    }
185

    
186
    @SuppressWarnings("unused")
187
    @Override
188
    public boolean hasValidValue() {
189
        try {
190
            Object value = this.getValue();
191
        } catch (Exception e) {
192
            return false;
193
        }
194
        return true;
195
    }
196

    
197
    @Override
198
    public void focusGained(FocusEvent arg0) {
199
        fireFieldEnterEvent();
200
        this.problemIndicator().restore();
201
    }
202

    
203
    @Override
204
    public void focusLost(FocusEvent arg0) {
205
        if (this.hasValidValue()) {
206
            this.problemIndicator().clear();
207
        } else {
208
            try {
209
                Object value = this.getValue();
210
            } catch (Exception e) {
211
                this.problemIndicator().set(e.getLocalizedMessage());
212
            }
213
        }
214
        fireFieldExitEvent();
215
    }
216

    
217
    @Override
218
    public boolean isModified() {
219
        
220
        Timestamp assigned = (Timestamp) getAssignedValue();
221
        String ss = this.txtTimestamp.getText();
222
        if (StringUtils.isBlank(ss)) {
223
            return assigned != null;
224
        }
225

    
226
        try {
227
            Timestamp value = Timestamp.valueOf(ss);
228
            return !Objects.equals(value, assigned);
229
        } catch (Exception ex) {
230
            return false;
231
        }
232
    }
233
    
234
    
235
}