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 / Text / JDynFormFieldText.java @ 2548

History | View | Annotate | Download (4.41 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.Text;
25

    
26
import java.util.Objects;
27
import javax.swing.JTextArea;
28
import javax.swing.event.DocumentEvent;
29
import javax.swing.event.DocumentListener;
30
import org.apache.commons.lang3.StringUtils;
31
import org.gvsig.tools.dynform.DynFormFieldDefinition;
32
import org.gvsig.tools.dynform.JDynFormField;
33
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
34
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory.ScrolledComponent;
35
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField;
36
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory;
37

    
38
public class JDynFormFieldText extends AbstractJDynFormField implements JDynFormField {
39

    
40
    private final int CHAR_ROW_LIMIT = 3;
41
    private String assignedValue = null;
42
    private ScrolledComponent<JTextArea> textArea;
43

    
44
    public JDynFormFieldText(
45
            DynFormSPIManager serviceManager,
46
            DynFormSPIManager.ComponentsFactory componentsFactory,
47
            JDynFormFieldFactory factory,
48
            DynFormFieldDefinition definition,
49
            Object value
50
    ) {
51
        super(serviceManager, componentsFactory, factory, definition, value);
52
        this.assignedValue = Objects.toString(value, null);
53
    }
54

    
55
    @Override
56
    public Object getAssignedValue() {
57
        return this.assignedValue;
58
    }
59

    
60
    private JTextArea getJTextArea() {
61
        return this.textArea.getComponent();
62
    }
63

    
64
    @Override
65
    public void initComponent() {
66
        ScrolledComponent<JTextArea> text = this.getComponentsFactory().getJTextArea(this.getDefinition(), null);
67
        this.textArea = text;
68
        text.getComponent().getDocument().addDocumentListener(new DocumentListener() {
69
            @Override
70
            public void insertUpdate(DocumentEvent e) {
71
                fireFieldChangedEvent();
72
            }
73

    
74
            @Override
75
            public void removeUpdate(DocumentEvent e) {
76
                fireFieldChangedEvent();
77
            }
78

    
79
            @Override
80
            public void changedUpdate(DocumentEvent e) {
81
                fireFieldChangedEvent();
82
            }
83
        });
84
        this.contents = this.textArea.getScrollPane();
85
        this.getJTextArea().setLineWrap(true);
86
        this.getJTextArea().setWrapStyleWord(true);
87

    
88
        this.getJTextArea().setRows(getTagValueAsInt(DynFormSPIManager.TAG_DYNFORM_ROWS, CHAR_ROW_LIMIT));
89
        if (this.getDefinition().isReadOnly()) {
90
            this.getJTextArea().setEditable(false);
91
        }
92
        this.getJTextArea().addFocusListener(this);
93
        this.setValue(this.assignedValue);
94
    }
95

    
96
    @Override
97
    public void setValue(Object value) {
98
        String s = Objects.toString(value, "");
99
        this.assignedValue = Objects.toString(value, null);
100
        this.getJTextArea().setText(s);
101
        this.fixPreferredWidth(this.getJTextArea());
102
        this.setTranslateEmptyToNull(value == null);
103
    }
104

    
105
    @Override
106
    public Object getValue() {
107
        String s = this.getJTextArea().getText();
108
        if (this.translateEmptyToNull() && StringUtils.isBlank(s)) {
109
            return null;
110
        }
111
        return s;
112
    }
113

    
114
    @Override
115
    public boolean hasValidValue() {
116
        return true;
117
    }
118

    
119
    @Override
120
    public boolean isModified() {
121
        String value = this.getJTextArea().getText();
122
        String assigned = (String) getAssignedValue();
123
        if (StringUtils.isBlank(value)) {
124
            return StringUtils.isNotBlank(assigned);
125
        }
126
        return !Objects.equals(value, assigned);
127
    }
128

    
129

    
130
}