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

View differences:

JDynFormFieldTimestamp.java
40 40
import org.freixas.jcalendar.DateEvent;
41 41
import org.freixas.jcalendar.DateListener;
42 42
import org.freixas.jcalendar.JCalendar;
43
import org.gvsig.tools.dynform.DynFormFieldDefinition;
43 44
import org.gvsig.tools.dynform.JDynFormField;
45
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
46
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory;
44 47
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField;
45 48
import org.gvsig.tools.dynform.spi.dynformfield.JCustomSpinner;
46
import org.gvsig.tools.dynobject.DynObject;
47 49
import org.gvsig.tools.dynobject.DynObjectValueItem;
48 50
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
49
import org.gvsig.tools.service.spi.ServiceManager;
50 51

  
51 52
public class JDynFormFieldTimestamp extends AbstractJDynFormField implements JDynFormField, FocusListener {
52
	
53
	protected Object assignedValue  = null;
54
	private JCalendar jcalendar = null;
55
	private JCustomSpinner timeSpinner = null;
56
	private JButton button = null;
57
	private boolean readonly = false;
58
	
59
	public JDynFormFieldTimestamp(DynObject parameters,
60
			ServiceManager serviceManager) {
61
		super(parameters, serviceManager);
62
		this.assignedValue = this.getParameterValue();
63
	}
64 53

  
65
	public void setReadOnly(boolean readonly) {
66
		this.readonly  = readonly;
67
		if( this.contents != null ) {
68
			if( readonly ) {
69
				this.timeSpinner.setEditable(false);
70
				this.button.setEnabled(false);
71
			} else {
72
				this.timeSpinner.setEditable(true);
73
				this.button.setEnabled(true);
74
			}
75
		}
76
	}
77
	
78
	public Object getAssignedValue() {
79
		return this.assignedValue;
80
	}
54
    protected Object assignedValue = null;
55
    private JCalendar jcalendar = null;
56
    private JCustomSpinner timeSpinner = null;
57
    private JButton button = null;
58
    private boolean readonly = false;
81 59

  
82
	protected JSpinner getJSpinner() {
83
		return (JSpinner) this.timeSpinner;
84
	}
85
	
86
	protected JButton getJButton() {
87
		return (JButton) this.button;
88
	}
89
	
90
	protected JCalendar getJCalendar() {
91
		if(this.jcalendar==null){
92
			this.jcalendar = new JCalendar();
93
			this.jcalendar.addDateListener(new DateListener() {
94
				public void dateChanged(DateEvent arg0) {
95
					Date currentDate = (Date) getJSpinner().getValue();
96
					Date newDate = getJCalendar().getDate();
97
					
98
					newDate.setHours(currentDate.getHours());
99
					newDate.setMinutes(currentDate.getMinutes());
100
					newDate.setSeconds(currentDate.getSeconds());
101
					
102
					getJSpinner().setValue(newDate);
103
					
104
				}
105
			});
106
		}
107
		return (JCalendar) this.jcalendar;
108
	}
109
	
110
			
111
	public void initComponent() {
112
		DynObjectValueItem[] availableValues = this.getDefinition().getAvailableValues();
113
		if( availableValues==null ) {
114
			this.contents = new JPanel();
115
			this.contents.setLayout(new BorderLayout());
116
			
117
			timeSpinner = new JCustomSpinner(this.getLabel());
118
			JSpinner.DateEditor timeEditor = new JSpinner.DateEditor(timeSpinner, "dd-MM-yyyy  HH:mm:ss");
119
			timeSpinner.setEditor(timeEditor);
120
			button  = new JButton("...");
121
			button.addActionListener(new ActionListener() {
122
				public void actionPerformed(ActionEvent event) {
123
					JPopupMenu menu = new JPopupMenu();
124
					menu.add(getJCalendar());
125
					
126
					JComponent thisComp = (JComponent)event.getSource();
127
					menu.show(thisComp, 0, thisComp.getY()+22);
128
				}
129
			});
130
			
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
    }
131 70

  
132
			
133
			this.contents.addFocusListener(this);
134
			if(this.getDefinition().isReadOnly()) {
135
				this.getJSpinner().setEnabled(false);
136
				this.getJButton().setEnabled(false);
137
			}
138
			
139
			this.contents.add(timeSpinner,BorderLayout.CENTER);
140
			this.contents.add(button,BorderLayout.LINE_END);
141
		}
142
		this.setValue(this.assignedValue);
143
	}
144
	
145
	public void setValue(Object value) {
146
		if( value == null ) {
147
			value = this.getDefinition().getDefaultValue();
148
			if( value == null ) {
149
				value = new Date();
150
			}
151
		} else {
152
			try {
153
				this.getDefinition().validate(value);
154
				this.problemIndicator().clear();
155
			} catch (DynFieldValidateException e) {
156
				this.problemIndicator().set(e.getLocalizedMessage());
157
			}
158
		}
159
		if( this.contents instanceof JSpinner) {
160
			this.getJSpinner().setValue(value);
161
		} 
162
		this.assignedValue = value;
163
	}
164
	
165
	/* 
71
    public void setReadOnly(boolean readonly) {
72
        this.readonly = readonly;
73
        if (this.contents != null) {
74
            if (readonly) {
75
                this.timeSpinner.setEditable(false);
76
                this.button.setEnabled(false);
77
            } else {
78
                this.timeSpinner.setEditable(true);
79
                this.button.setEnabled(true);
80
            }
81
        }
82
    }
83

  
84
    public Object getAssignedValue() {
85
        return this.assignedValue;
86
    }
87

  
88
    protected JSpinner getJSpinner() {
89
        return (JSpinner) this.timeSpinner;
90
    }
91

  
92
    protected JButton getJButton() {
93
        return (JButton) this.button;
94
    }
95

  
96
    protected JCalendar getJCalendar() {
97
        if (this.jcalendar == null) {
98
            this.jcalendar = new JCalendar();
99
            this.jcalendar.addDateListener(new DateListener() {
100
                public void dateChanged(DateEvent arg0) {
101
                    Date currentDate = (Date) getJSpinner().getValue();
102
                    Date newDate = getJCalendar().getDate();
103

  
104
                    newDate.setHours(currentDate.getHours());
105
                    newDate.setMinutes(currentDate.getMinutes());
106
                    newDate.setSeconds(currentDate.getSeconds());
107

  
108
                    getJSpinner().setValue(newDate);
109

  
110
                }
111
            });
112
        }
113
        return (JCalendar) this.jcalendar;
114
    }
115

  
116
    public void initComponent() {
117
        DynObjectValueItem[] availableValues = this.getDefinition().getAvailableValues();
118
        if (availableValues == null) {
119
            this.contents = new JPanel();
120
            this.contents.setLayout(new BorderLayout());
121

  
122
            timeSpinner = new JCustomSpinner(this.getLabel());
123
            JSpinner.DateEditor timeEditor = new JSpinner.DateEditor(timeSpinner, "dd-MM-yyyy  HH:mm:ss");
124
            timeSpinner.setEditor(timeEditor);
125
            button = new JButton("...");
126
            button.addActionListener(new ActionListener() {
127
                public void actionPerformed(ActionEvent event) {
128
                    JPopupMenu menu = new JPopupMenu();
129
                    menu.add(getJCalendar());
130

  
131
                    JComponent thisComp = (JComponent) event.getSource();
132
                    menu.show(thisComp, 0, thisComp.getY() + 22);
133
                }
134
            });
135

  
136
            this.contents.addFocusListener(this);
137
            if (this.getDefinition().isReadOnly()) {
138
                this.getJSpinner().setEnabled(false);
139
                this.getJButton().setEnabled(false);
140
            }
141

  
142
            this.contents.add(timeSpinner, BorderLayout.CENTER);
143
            this.contents.add(button, BorderLayout.LINE_END);
144
        }
145
        this.setValue(this.assignedValue);
146
    }
147

  
148
    public void setValue(Object value) {
149
        if (value == null) {
150
            value = this.getDefinition().getDefaultValue();
151
            if (value == null) {
152
                value = new Date();
153
            }
154
        } else {
155
            try {
156
                this.getDefinition().validate(value);
157
                this.problemIndicator().clear();
158
            } catch (DynFieldValidateException e) {
159
                this.problemIndicator().set(e.getLocalizedMessage());
160
            }
161
        }
162
        if (this.contents instanceof JSpinner) {
163
            this.getJSpinner().setValue(value);
164
        }
165
        this.assignedValue = value;
166
    }
167

  
168
    /* 
166 169
	 * Métodos específicos de cada tipo de datos
167
	 */
168
	
169
	public Object getValue() {
170
		Object value = null;
171
		if( this.contents instanceof JCheckBox ) {
172
			value = getJSpinner().getValue();
173
		}
174
		try {
175
			this.getDefinition().validate(value);
176
			this.problemIndicator().clear();
177
		} catch (DynFieldValidateException e) {
178
			throw new IllegalFieldValue(this, e.getLocalizedMessage());
179
		}
180
		return value;
181
	}
170
     */
171
    public Object getValue() {
172
        Object value = null;
173
        if (this.contents instanceof JCheckBox) {
174
            value = getJSpinner().getValue();
175
        }
176
        try {
177
            this.getDefinition().validate(value);
178
            this.problemIndicator().clear();
179
        } catch (DynFieldValidateException e) {
180
            throw new IllegalFieldValue(this, e.getLocalizedMessage());
181
        }
182
        return value;
183
    }
182 184

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

  
194
	public void focusGained(FocusEvent arg0) {
195
		fireFieldEnterEvent();
196
		this.problemIndicator().restore();
197
	}
195
    public void focusGained(FocusEvent arg0) {
196
        fireFieldEnterEvent();
197
        this.problemIndicator().restore();
198
    }
198 199

  
199
	public void focusLost(FocusEvent arg0) {
200
		if( this.hasValidValue() ) {
201
			this.problemIndicator().clear();
202
		} else {
203
			try {
204
				Object value = this.getValue();
205
			} catch(Exception e) {
206
				this.problemIndicator().set(e.getLocalizedMessage());
207
			}
208
		}
209
		fireFieldExitEvent();
210
	}
200
    public void focusLost(FocusEvent arg0) {
201
        if (this.hasValidValue()) {
202
            this.problemIndicator().clear();
203
        } else {
204
            try {
205
                Object value = this.getValue();
206
            } catch (Exception e) {
207
                this.problemIndicator().set(e.getLocalizedMessage());
208
            }
209
        }
210
        fireFieldExitEvent();
211
    }
211 212

  
212 213
}

Also available in: Unified diff