Revision 1947 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.dynform/org.gvsig.tools.dynform.spi/src/main/java/org/gvsig/tools/dynform/spi/dynformfield/AbstractJDynFormFieldWithValueList.java

View differences:

AbstractJDynFormFieldWithValueList.java
28 28
import java.awt.event.KeyAdapter;
29 29
import java.awt.event.KeyEvent;
30 30
import java.util.Objects;
31
import javax.swing.BorderFactory;
31 32
import javax.swing.ComboBoxModel;
32 33
import javax.swing.DefaultComboBoxModel;
33 34

  
......
35 36
import javax.swing.JComponent;
36 37
import javax.swing.JLabel;
37 38
import javax.swing.JTextField;
39
import javax.swing.UIManager;
38 40
import javax.swing.text.JTextComponent;
39 41
import org.apache.commons.lang3.StringUtils;
40 42

  
......
43 45
import org.gvsig.tools.dynform.JDynFormField;
44 46
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
45 47
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory;
48
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.TAG_DYNFORM_DROPDOWN;
46 49
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField.IllegalFieldValue;
47 50
import org.gvsig.tools.dynobject.DynObjectValueItem;
48 51
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
......
71 74
    }
72 75

  
73 76
    protected JTextField getJTextField() {
74
        if( this.contents instanceof JTextField ) {
77
        if (this.contents instanceof JTextField) {
75 78
            return (JTextField) this.contents;
76 79
        }
77 80
        return null;
......
88 91
    @Override
89 92
    public void initComponent() {
90 93
        DynObjectValueItem[] availableValues = this.getAvailableValues();
91
        if (availableValues == null) {
94
        ComponentsFactory theFactory = this.getComponentsFactory();
95
        if (theFactory.containsJComboBox(this.getDefinition(), null)) {
96
            JComboBox combo = this.getComponentsFactory().getJComboBox(this.getDefinition(), null);
97
            this.dropDown = ToolsSwingLocator.getToolsSwingManager().createDropDown(combo);
98
            this.dropDown.setModel(new DefaultComboBoxModel(availableValues));
99
            this.contents = combo;
100
        } else if (theFactory.containsJLabel(this.getDefinition(), "Ddn")) {
101
            JLabel label = this.getComponentsFactory().getJLabel(this.getDefinition(), "Ddn");
102
            this.dropDown = ToolsSwingLocator.getToolsSwingManager().createDropDown(label);
103
            this.dropDown.setModel(new DefaultComboBoxModel(availableValues));
104
            this.dropDown.setVisibleDropdownArrow(true);
105
            this.contents = label;
106
        } else if (theFactory.containsJTextField(this.getDefinition(), null)) {
107
            this.contents = theFactory.getJTextField(this.getDefinition(), null);
92 108
            this.dropDown = null;
93
            if( this.getComponentsFactory().containsJComboBox(this.getDefinition(), null) ) {
94
                JComboBox combo = this.getComponentsFactory().getJComboBox(this.getDefinition(), null);
95
                this.contents = (JComponent) combo.getEditor().getEditorComponent();
96
            } else {
109
        } else {
110
            if (availableValues == null) {
111
                this.dropDown = null;
97 112
                this.contents = this.getComponentsFactory().getJTextField(this.getDefinition(), null);
98
            }
99
            this.contents.addFocusListener(this);
100
            if (this.getDefinition().isReadOnly()) {
101
                this.getJTextField().setEditable(false);
102
            }
103
        } else {
104
            ComponentsFactory theFactory = this.getComponentsFactory();
105
            if( theFactory.containsJComboBox(this.getDefinition(), null) ) {
106
                JComboBox combo = this.getComponentsFactory().getJComboBox(this.getDefinition(), null);
107
                this.dropDown = ToolsSwingLocator.getToolsSwingManager().createDropDown(combo);
108
                this.dropDown.setModel(new DefaultComboBoxModel(availableValues));
109
                this.contents = combo;
110
            } else if( theFactory.containsJLabel(this.getDefinition(), "DDn") ) {
111
                JLabel label = this.getComponentsFactory().getJLabel(this.getDefinition(), "DDn");
112
                this.dropDown = ToolsSwingLocator.getToolsSwingManager().createDropDown(label);
113
                this.dropDown.setModel(new DefaultComboBoxModel(availableValues));
114
                this.contents = label;
115 113
            } else {
116
                this.contents = theFactory.getJTextField(this.getDefinition(), null);
117
                this.dropDown = null;
114
                String dropdownType = this.getTagValueAsString(TAG_DYNFORM_DROPDOWN,
115
                        availableValues.length <= 20 ? "label" : "combo"
116
                ).toLowerCase().trim();
117
                switch (dropdownType) {
118
                    case "label": {
119
                        JLabel label = this.getComponentsFactory().getJLabel(this.getDefinition(), "Ddn");
120
                        this.dropDown = ToolsSwingLocator.getToolsSwingManager().createDropDown(label);
121
                        this.dropDown.setModel(new DefaultComboBoxModel(availableValues));
122
                        this.dropDown.setVisibleDropdownArrow(true);
123
                        label.setBorder(BorderFactory.createLineBorder(UIManager.getColor("TextField.darkShadow")));
124
                        label.setForeground(UIManager.getColor("TextField.foreground"));
125
                        label.setBackground(UIManager.getColor("TextField.background"));
126
                        label.setOpaque(true);
127
                        this.contents = label;
128
                        break;
129
                    }
130
                    default:
131
                    case "combo": {
132
                        JComboBox combo = this.getComponentsFactory().getJComboBox(this.getDefinition(), null);
133
                        this.dropDown = ToolsSwingLocator.getToolsSwingManager().createDropDown(combo);
134
                        this.dropDown.setModel(new DefaultComboBoxModel(availableValues));
135
                        this.contents = combo;
136
                        break;
137
                    }
138
                }
118 139
            }
119
            if (availableValues.length > 0) {
120
                this.getDropDown().setSelectedIndex(0);
121
            }
122
            this.contents.addFocusListener(this);
123
            if (this.getDefinition().isReadOnly()) {
124
                this.getDropDown().setReadOnly(true);
125
            }
126 140
        }
141
        this.contents.addFocusListener(this);
142
        this.setReadOnly(readOnly); // Forzamos a actualiza rel estado del component
127 143
        this.setValue(this.assignedValue);
128 144
    }
129 145

  
146
    @Override
147
    public void setReadOnly(boolean readonly) {
148
        if (!readonly && this.isForcedReadOnly()) {
149
            readonly = true;
150
        }
151
        if (this.dropDown == null) {
152
            super.setReadOnly(readonly);
153
        } else {
154
            this.readOnly = readonly;
155
            if (jlabel != null) {
156
                this.jlabel.setEnabled(!readonly);
157
            }
158
            this.dropDown.setReadOnly(readonly);
159
        }
160
    }
161

  
130 162
    protected String getValueFromJComponent() {
131 163
        String s;
132
        if( this.dropDown==null ) {
164
        if (this.dropDown == null) {
133 165
            JTextField jtext = this.getJTextField();
134
            if( jtext == null ) {
166
            if (jtext == null) {
135 167
                s = null;
136 168
            } else {
137 169
                s = jtext.getText();
138 170
            }
139 171
        } else {
140 172
            DynObjectValueItem value = (DynObjectValueItem) this.dropDown.getSelectedItem();
141
            if( value == null ) {
173
            if (value == null) {
142 174
                s = null;
143 175
            } else {
144 176
                s = Objects.toString(value.getValue(), null);
145 177
            }
146 178
        }
147
        if (StringUtils.isEmpty(s) && this.translateEmptyToNull()) {
179
        if (StringUtils.isBlank(s) && this.translateEmptyToNull()) {
148 180
            return null;
149 181
        }
150 182
        return s;
......
152 184

  
153 185
    @Override
154 186
    public void setValue(Object value) {
155
        if( this.dropDown==null ) {
187
        if (this.dropDown == null) {
156 188
            JTextField jtext = this.getJTextField();
157
            if( jtext!=null ) {
189
            if (jtext != null) {
158 190
                String s;
159 191
                if (value == null) {
160 192
                    this.setTranslateEmptyToNull(true);
......
177 209
                jtext.setText(s);
178 210
            }
179 211
        } else {
180
            ComboBoxModel model = this.dropDown.getModel();
181
            for (int i = 0; i < model.getSize(); i++) {
182
                DynObjectValueItem item = (DynObjectValueItem) model.getElementAt(i);
183
                if ( item!=null && Objects.equals(item.getValue(),value) ) {
184
                    this.dropDown.setSelectedIndex(i);
185
                    break;
212
            if( value == null ) {
213
                this.dropDown.setSelectedIndex(-1);
214
            } else {
215
                ComboBoxModel model = this.dropDown.getModel();
216
                if (model != null) {
217
                    for (int i = 0; i < model.getSize(); i++) {
218
                        DynObjectValueItem item = (DynObjectValueItem) model.getElementAt(i);
219
                        if (item != null && Objects.equals(item.getValue(), value)) {
220
                            this.dropDown.setSelectedIndex(i);
221
                            break;
222
                        }
223
                    }
186 224
                }
187 225
            }
188 226
        }

Also available in: Unified diff