Revision 854

View differences:

org.gvsig.tools/library/trunk/org.gvsig.tools/dynform/src/org/gvsig/tools/dynform/impl/dynformfield/string/JDynFormFieldString.java
1 1
package org.gvsig.tools.dynform.impl.dynformfield.string;
2 2

  
3
import java.awt.event.FocusEvent;
4
import java.awt.event.FocusListener;
5

  
6
import javax.swing.JComboBox;
7 3
import javax.swing.JTextField;
8 4

  
9
import org.gvsig.tools.dynform.api.JDynFormField;
10
import org.gvsig.tools.dynform.impl.dynformfield.AbstractJDynFormField;
11
import org.gvsig.tools.dynform.impl.dynformfield.JCustomTextField;
5
import org.gvsig.tools.dynform.impl.dynformfield.AbstractJDynFormFieldBase;
12 6
import org.gvsig.tools.dynobject.DynObject;
13
import org.gvsig.tools.dynobject.DynObjectValueItem;
14 7
import org.gvsig.tools.service.spi.ServiceManager;
15 8

  
16
@SuppressWarnings({"rawtypes", "unchecked"})
17
public class JDynFormFieldString extends AbstractJDynFormField  implements JDynFormField, FocusListener {
9
public class JDynFormFieldString extends AbstractJDynFormFieldBase {
18 10
	
19
	private String assignedValue  = null;
20 11
	
21 12
	public JDynFormFieldString(DynObject parameters,
22 13
			ServiceManager serviceManager) {
23 14
		super(parameters, serviceManager);
24
		this.assignedValue = (String) this.getParameterValue();
25
//		if(this.assignedValue == null){
26
//			if(this.hasDefaultParameterValue()){
27
//				this.assignedValue = (String) this.getDefaultParameterValue();
28
//			}
29
//		}
30 15
	}
31

  
32
	public Object getAssignedValue() {
33
		return this.assignedValue;
34
	}
35

  
36
	private JTextField getJTextField() {
37
		return (JTextField) this.contents;
38
	}
39 16
	
40
	private JComboBox getJComboBox() {
41
		return (JComboBox) this.contents;
17
	protected String getDefaultValue(){
18
		return "";
42 19
	}
43
	
44
	public void initComponent() {
45
		DynObjectValueItem[] availableValues = this.getDefinition().getAvailableValues();
46
		if( availableValues==null ) {
47
			this.contents = new JCustomTextField();
48
			this.contents.addFocusListener(this);
49
			if( this.getDefinition().isReadOnly() ) {
50
				this.getJTextField().setEditable(false);;
51
			}
52
		} else {
53
			this.contents = new JComboBox(availableValues);
54
			this.contents.addFocusListener(this);
55
			if( this.getDefinition().isReadOnly() ) {
56
				this.getJComboBox().setEditable(false);
57
			}
58
		}
59
		this.setValue(this.assignedValue);
60
	}
61
	
62
	public void setValue(Object value) {
63
		String s = null;
64
		if( value == null ) {
65
			value = this.getDefinition().getDefaultValue();
66
			if( value == null ) {
67
				s = "";
68
			} else {
69
				s = value.toString();
70
			}
71
		} else {
72
			s = value.toString();
73
		}
74
		if( this.contents instanceof JTextField ) {
75
			this.getJTextField().setText(s);
76
		} else {
77
			//if(this.assignedValue!=null)
78
			this.getJComboBox().getModel().setSelectedItem(s);
79
		}
80
	}
81
	
20

  
82 21
	public Object getValue() {
83 22
		Object value = null;
84 23
		String s = "";
......
100 39
		return value;
101 40
	}
102 41
	
103
	public boolean hasValidValue() {
104
		return true;
105
	}
106

  
107
	public void focusGained(FocusEvent arg0) {
108
		fireFieldEnterEvent();
109
	}
110

  
111
	public void focusLost(FocusEvent arg0) {
112
		fireFieldExitEvent();
113
	}
114
	
115 42
}
org.gvsig.tools/library/trunk/org.gvsig.tools/dynform/src/org/gvsig/tools/dynform/impl/dynformfield/AbstractJDynFormFieldBase.java
1
package org.gvsig.tools.dynform.impl.dynformfield;
2

  
3
import java.awt.event.FocusEvent;
4
import java.awt.event.FocusListener;
5

  
6
import javax.swing.JComboBox;
7
import javax.swing.JTextField;
8

  
9
import org.gvsig.tools.dynform.api.JDynFormField;
10
import org.gvsig.tools.dynobject.DynObject;
11
import org.gvsig.tools.dynobject.DynObjectValueItem;
12
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
13
import org.gvsig.tools.service.spi.ServiceManager;
14

  
15
public abstract class AbstractJDynFormFieldBase extends AbstractJDynFormField implements JDynFormField, FocusListener {
16
	
17
	protected Object assignedValue  = null;
18
	
19
	public AbstractJDynFormFieldBase(DynObject parameters,
20
			ServiceManager serviceManager) {
21
		super(parameters, serviceManager);
22
		this.assignedValue = this.getParameterValue();
23
	}
24

  
25
	public Object getAssignedValue() {
26
		return this.assignedValue;
27
	}
28

  
29
	protected JTextField getJTextField() {
30
		return (JTextField) this.contents;
31
	}
32
	
33
	protected JComboBox getJComboBox() {
34
		return (JComboBox) this.contents;
35
	}
36
		
37
	public void initComponent() {
38
		DynObjectValueItem[] availableValues = this.getDefinition().getAvailableValues();
39
		if( availableValues==null ) {
40
			this.contents = new JCustomTextField();
41
			this.contents.addFocusListener(this);
42
			if( this.getDefinition().isReadOnly() ) {
43
				this.getJTextField().setEditable(false);;
44
			}
45
		} else {
46
			this.contents = new JComboBox(availableValues);
47
			this.contents.addFocusListener(this);
48
			if( this.getDefinition().isReadOnly() ) {
49
				this.getJComboBox().setEditable(false);
50
			}
51
		}
52
		this.setValue(this.assignedValue);
53
	}
54
	
55
	public void setValue(Object value) {
56
		String s = null;
57
		if( value == null ) {
58
			value = this.getDefinition().getDefaultValue();
59
			if( value == null ) {
60
				s = this.getDefaultValue();
61
			} else {
62
				s = value.toString();
63
			}
64
		} else {
65
			s = value.toString();
66
			try {
67
				this.getDefinition().validate(value);
68
				this.problemIndicator().clear();
69
			} catch (DynFieldValidateException e) {
70
				this.problemIndicator().set(e.getLocalizedMessage());
71
			}
72
		}
73
		if( this.contents instanceof JTextField ) {
74
			this.getJTextField().setText(s);
75
		} else {
76
			//if(this.assignedValue!=null)
77
			boolean found=false;
78
			for(int i = 0; i< this.getJComboBox().getModel().getSize(); i++){
79
				if(this.getJComboBox().getModel().getElementAt(i).toString().equals(s)){
80
					found = true;
81
				}
82
			}
83
			if(found)
84
				this.getJComboBox().getModel().setSelectedItem(s);
85
		}
86
		this.assignedValue = value;
87
	}
88
	
89
	/* 
90
	 * Métodos específicos de cada tipo de datos
91
	 */
92
	protected abstract String getDefaultValue();
93
	
94
	public abstract Object getValue();
95

  
96
	
97
	@SuppressWarnings("unused")
98
	public boolean hasValidValue() {
99
		try {
100
			Object value = this.getValue();
101
		} catch(Exception e) {
102
			return false;
103
		}
104
		return true;
105
	}
106

  
107
	public void focusGained(FocusEvent arg0) {
108
		fireFieldEnterEvent();
109
		this.problemIndicator().restore();
110
	}
111

  
112
	public void focusLost(FocusEvent arg0) {
113
		if( this.hasValidValue() ) {
114
			this.problemIndicator().clear();
115
		} else {
116
			try {
117
				Object value = this.getValue();
118
			} catch(Exception e) {
119
				this.problemIndicator().set(e.getLocalizedMessage());
120
			}
121
		}
122
		fireFieldExitEvent();
123
	}
124

  
125
}
0 126

  
org.gvsig.tools/library/trunk/org.gvsig.tools/dynform/src/org/gvsig/tools/dynform/impl/dynformfield/integer/JDynFormFieldInteger.java
1 1
package org.gvsig.tools.dynform.impl.dynformfield.integer;
2 2

  
3
import java.awt.event.FocusEvent;
4
import java.awt.event.FocusListener;
5

  
6 3
import javax.swing.JComboBox;
7 4
import javax.swing.JTextField;
8 5

  
9
import org.gvsig.tools.dynform.api.JDynFormField;
10
import org.gvsig.tools.dynform.impl.dynformfield.AbstractJDynFormField;
11
import org.gvsig.tools.dynform.impl.dynformfield.JCustomTextField;
6
import org.gvsig.tools.dynform.impl.dynformfield.AbstractJDynFormFieldBase;
12 7
import org.gvsig.tools.dynobject.DynObject;
13 8
import org.gvsig.tools.dynobject.DynObjectValueItem;
14 9
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
15 10
import org.gvsig.tools.service.spi.ServiceManager;
16 11

  
17
public class JDynFormFieldInteger extends AbstractJDynFormField implements JDynFormField, FocusListener {
12
public class JDynFormFieldInteger extends AbstractJDynFormFieldBase {
18 13
	
19
	private Object assignedValue  = null;
20
	
21 14
	public JDynFormFieldInteger(DynObject parameters,
22 15
			ServiceManager serviceManager) {
23 16
		super(parameters, serviceManager);
24
		this.assignedValue = this.getParameterValue();
25 17
	}
26 18

  
27
	public Object getAssignedValue() {
28
		return this.assignedValue;
29
	}
30

  
31
	private JTextField getJTextField() {
32
		return (JTextField) this.contents;
33
	}
34
	
35
	private JComboBox getJComboBox() {
36
		return (JComboBox) this.contents;
37
	}
38

  
39
	
40
	public void initComponent() {
41
		DynObjectValueItem[] availableValues = this.getDefinition().getAvailableValues();
42
		if( availableValues==null ) {
43
			this.contents = new JCustomTextField();
44
			this.contents.addFocusListener(this);
45
			if( this.getDefinition().isReadOnly() ) {
46
				this.getJTextField().setEditable(false);;
47
			}
48
		} else {
49
			this.contents = new JComboBox(availableValues);
50
			this.contents.addFocusListener(this);
51
			if( this.getDefinition().isReadOnly() ) {
52
				this.getJComboBox().setEditable(false);
53
			}
54
		}
55
		this.setValue(this.assignedValue);
56
	}
57
	
58
	public void setValue(Object value) {
59
		String s = null;
60
		if( value == null ) {
61
			value = this.getDefinition().getDefaultValue();
62
			if( value == null ) {
63
				s = "0";
64
			} else {
65
				s = value.toString();
66
			}
67
		} else {
68
			s = value.toString();
69
			try {
70
				this.getDefinition().validate(value);
71
				this.problemIndicator().clear();
72
			} catch (DynFieldValidateException e) {
73
				this.problemIndicator().set(e.getLocalizedMessage());
74
			}
75
		}
76
		if( this.contents instanceof JTextField ) {
77
			this.getJTextField().setText(s);
78
		} else {
79
			// FIXME: Falta por implementar el set sobre el combo.
80
		}
81
		this.assignedValue = value;
82
	}
83
	
84 19
	public Object getValue() {
85 20
		Object value = null;
86 21
		if( this.contents instanceof JTextField ) {
......
111 46
		return value;
112 47
	}
113 48

  
114
	@SuppressWarnings("unused")
115
	public boolean hasValidValue() {
116
		try {
117
			Object value = this.getValue();
118
		} catch(Exception e) {
119
			return false;
120
		}
121
		return true;
49
	@Override
50
	protected String getDefaultValue() {
51
		return "0";
122 52
	}
123 53

  
124
	public void focusGained(FocusEvent arg0) {
125
		fireFieldEnterEvent();
126
		this.problemIndicator().restore();
127
	}
128

  
129
	public void focusLost(FocusEvent arg0) {
130
		if( this.hasValidValue() ) {
131
			this.problemIndicator().clear();
132
		} else {
133
			try {
134
				Object value = this.getValue();
135
			} catch(Exception e) {
136
				this.problemIndicator().set(e.getLocalizedMessage());
137
			}
138
		}
139
		fireFieldExitEvent();
140
	}
141

  
142 54
}

Also available in: Unified diff