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 @ 2487
History | View | Annotate | Download (4.33 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.awt.event.ContainerEvent; |
27 |
import java.awt.event.ContainerListener; |
28 |
import java.util.Objects; |
29 |
import javax.swing.JTextArea; |
30 |
import javax.swing.event.DocumentEvent; |
31 |
import javax.swing.event.DocumentListener; |
32 |
import org.apache.commons.lang3.StringUtils; |
33 |
import org.gvsig.tools.dynform.DynFormFieldDefinition; |
34 |
|
35 |
import org.gvsig.tools.dynform.JDynFormField; |
36 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
37 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory.ScrolledComponent; |
38 |
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory; |
39 |
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField; |
40 |
|
41 |
public class JDynFormFieldText extends AbstractJDynFormField implements JDynFormField { |
42 |
|
43 |
private final int CHAR_ROW_LIMIT = 3; |
44 |
private String assignedValue = null; |
45 |
private ScrolledComponent<JTextArea> textArea; |
46 |
|
47 |
public JDynFormFieldText(
|
48 |
DynFormSPIManager serviceManager, |
49 |
DynFormSPIManager.ComponentsFactory componentsFactory, |
50 |
JDynFormFieldFactory factory, |
51 |
DynFormFieldDefinition definition, |
52 |
Object value
|
53 |
) { |
54 |
super(serviceManager, componentsFactory, factory, definition, value);
|
55 |
this.assignedValue = Objects.toString(value, null); |
56 |
} |
57 |
|
58 |
@Override
|
59 |
public Object getAssignedValue() { |
60 |
return this.assignedValue; |
61 |
} |
62 |
|
63 |
private JTextArea getJTextArea() { |
64 |
return this.textArea.getComponent(); |
65 |
} |
66 |
|
67 |
@Override
|
68 |
public void initComponent() { |
69 |
ScrolledComponent<JTextArea> text = this.getComponentsFactory().getJTextArea(this.getDefinition(), null); |
70 |
this.textArea = text;
|
71 |
text.getComponent().getDocument().addDocumentListener(new DocumentListener() { |
72 |
@Override
|
73 |
public void insertUpdate(DocumentEvent e) { |
74 |
fireFieldChangedEvent(); |
75 |
} |
76 |
|
77 |
@Override
|
78 |
public void removeUpdate(DocumentEvent e) { |
79 |
fireFieldChangedEvent(); |
80 |
} |
81 |
|
82 |
@Override
|
83 |
public void changedUpdate(DocumentEvent e) { |
84 |
fireFieldChangedEvent(); |
85 |
} |
86 |
}); |
87 |
this.contents = this.textArea.getScrollPane(); |
88 |
this.getJTextArea().setLineWrap(true); |
89 |
this.getJTextArea().setWrapStyleWord(true); |
90 |
|
91 |
this.getJTextArea().setRows(getTagValueAsInt(DynFormSPIManager.TAG_DYNFORM_ROWS, CHAR_ROW_LIMIT));
|
92 |
if (this.getDefinition().isReadOnly()) { |
93 |
this.getJTextArea().setEditable(false); |
94 |
} |
95 |
this.getJTextArea().addFocusListener(this); |
96 |
this.setValue(this.assignedValue); |
97 |
} |
98 |
|
99 |
@Override
|
100 |
public void setValue(Object value) { |
101 |
String s = Objects.toString(value, ""); |
102 |
this.assignedValue = Objects.toString(value, null); |
103 |
this.getJTextArea().setText(s);
|
104 |
this.setTranslateEmptyToNull(value == null); |
105 |
} |
106 |
|
107 |
@Override
|
108 |
public Object getValue() { |
109 |
String s = this.getJTextArea().getText(); |
110 |
if (this.translateEmptyToNull() && StringUtils.isBlank(s)) { |
111 |
return null; |
112 |
} |
113 |
return s;
|
114 |
} |
115 |
|
116 |
@Override
|
117 |
public boolean hasValidValue() { |
118 |
return true; |
119 |
} |
120 |
|
121 |
// @Override
|
122 |
// public void clear() {
|
123 |
// Object value = this.getDefinition().getDefaultValue();
|
124 |
// String s = Objects.toString(value, "");
|
125 |
// this.getJTextArea().setText(s);
|
126 |
// }
|
127 |
} |