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

History | View | Annotate | Download (4.46 KB)

1
package org.gvsig.tools.dynform.services.dynformfield.Date;
2

    
3
import java.awt.BorderLayout;
4
import java.awt.event.ActionEvent;
5
import java.awt.event.ActionListener;
6
import java.awt.event.FocusEvent;
7
import java.awt.event.FocusListener;
8

    
9
import javax.swing.JButton;
10
import javax.swing.JCheckBox;
11
import javax.swing.JComponent;
12
import javax.swing.JPanel;
13
import javax.swing.JPopupMenu;
14
import javax.swing.JSpinner;
15

    
16
import org.freixas.jcalendar.DateEvent;
17
import org.freixas.jcalendar.DateListener;
18
import org.freixas.jcalendar.JCalendar;
19
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField;
20
import org.gvsig.tools.dynform.spi.dynformfield.JCustomSpinner;
21
import org.gvsig.tools.dynforms.JDynFormField;
22
import org.gvsig.tools.dynobject.DynObject;
23
import org.gvsig.tools.dynobject.DynObjectValueItem;
24
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
25
import org.gvsig.tools.service.spi.ServiceManager;
26

    
27
public class JDynFormFieldDate extends AbstractJDynFormField implements JDynFormField, FocusListener {
28
        
29
        protected Object assignedValue  = null;
30
        private JCalendar jcalendar = null;
31
        private JCustomSpinner timeSpinner = null;
32
        private JButton button = null;
33
        
34
        public JDynFormFieldDate(DynObject parameters,
35
                        ServiceManager serviceManager) {
36
                super(parameters, serviceManager);
37
                this.assignedValue = this.getParameterValue();
38
        }
39

    
40
        public Object getAssignedValue() {
41
                return this.assignedValue;
42
        }
43

    
44
        protected JSpinner getJSpinner() {
45
                return (JSpinner) this.timeSpinner;
46
        }
47
        
48
        protected JButton getJButton() {
49
                return (JButton) this.button;
50
        }
51
        
52
        protected JCalendar getJCalendar() {
53
                if(this.jcalendar==null){
54
                        this.jcalendar = new JCalendar();
55
                        this.jcalendar.addDateListener(new DateListener() {
56
                                public void dateChanged(DateEvent arg0) {
57
                                        getJSpinner().setValue(getJCalendar().getDate());
58
                                }
59
                        });
60
                }
61
                return (JCalendar) this.jcalendar;
62
        }
63
        
64
                        
65
        public void initComponent() {
66
                DynObjectValueItem[] availableValues = this.getDefinition().getAvailableValues();
67
                if( availableValues==null ) {
68
                        this.contents = new JPanel();
69
                        this.contents.setLayout(new BorderLayout());
70
                        
71
                        //timeSpinner = new JSpinner( new SpinnerDateModel() );
72
                        timeSpinner = new JCustomSpinner();
73
                        JSpinner.DateEditor timeEditor = new JSpinner.DateEditor(timeSpinner, "dd-MM-yyyy");
74
                        timeSpinner.setEditor(timeEditor);
75
                        //timeEditor.dismiss(timeSpinner);
76
                        button  = new JButton("Choose a date...");
77
                        button.addActionListener(new ActionListener() {
78
                                public void actionPerformed(ActionEvent event) {
79
                                        JPopupMenu menu = new JPopupMenu();
80
                                        menu.add(getJCalendar());
81
                                        
82
                                        JComponent thisComp = (JComponent)event.getSource();
83
                                        menu.show(thisComp, 0, thisComp.getY()+22);
84
                                }
85
                        });
86
                        
87

    
88
                        
89
                        this.contents.addFocusListener(this);
90
                        if(this.getDefinition().isReadOnly()) {
91
                                this.getJSpinner().setEnabled(false);
92
                                this.getJButton().setEnabled(false);
93
                        }
94
                        
95
                        this.contents.add(timeSpinner,BorderLayout.CENTER);
96
                        this.contents.add(button,BorderLayout.LINE_END);
97
                }
98
                this.setValue(this.assignedValue);
99
        }
100
        
101
        public void setValue(Object value) {
102
                if( value == null ) {
103
                        value = this.getDefinition().getDefaultValue();
104
                        if( value == null ) {
105
                                value = null;
106
                        }
107
                } else {
108
                        try {
109
                                this.getDefinition().validate(value);
110
                                this.problemIndicator().clear();
111
                        } catch (DynFieldValidateException e) {
112
                                this.problemIndicator().set(e.getLocalizedMessage());
113
                        }
114
                }
115
                if( this.contents instanceof JSpinner) {
116
                        this.getJSpinner().setValue(value);
117
                } 
118
                this.assignedValue = value;
119
        }
120
        
121
        /* 
122
         * Métodos específicos de cada tipo de datos
123
         */
124
        
125
        public Object getValue() {
126
                Object value = null;
127
                if( this.contents instanceof JCheckBox ) {
128
                        value = getJSpinner().getValue();
129
                }
130
                try {
131
                        this.getDefinition().validate(value);
132
                        this.problemIndicator().clear();
133
                } catch (DynFieldValidateException e) {
134
                        throw new IllegalFieldValue(this, e.getLocalizedMessage());
135
                }
136
                return value;
137
        }
138

    
139
        
140
        @SuppressWarnings("unused")
141
        public boolean hasValidValue() {
142
                try {
143
                        Object value = this.getValue();
144
                } catch(Exception e) {
145
                        return false;
146
                }
147
                return true;
148
        }
149

    
150
        public void focusGained(FocusEvent arg0) {
151
                fireFieldEnterEvent();
152
                this.problemIndicator().restore();
153
        }
154

    
155
        public void focusLost(FocusEvent arg0) {
156
                if( this.hasValidValue() ) {
157
                        this.problemIndicator().clear();
158
                } else {
159
                        try {
160
                                Object value = this.getValue();
161
                        } catch(Exception e) {
162
                                this.problemIndicator().set(e.getLocalizedMessage());
163
                        }
164
                }
165
                fireFieldExitEvent();
166
        }
167

    
168
}