1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.xulux.guilayer.swing.widgets;
17
18 import java.awt.Color;
19 import java.awt.Container;
20 import java.awt.Dimension;
21
22 import javax.swing.JComponent;
23 import javax.swing.JScrollPane;
24 import javax.swing.JTextArea;
25 import javax.swing.border.LineBorder;
26
27 import org.xulux.api.gui.IXuluxListener;
28 import org.xulux.gui.utils.ColorUtils;
29 import org.xulux.guilayer.swing.listeners.PrePostFieldListener;
30 import org.xulux.utils.BooleanUtils;
31
32
33
34
35
36
37
38 public class TextArea extends Entry {
39
40
41
42
43 private PrePostFieldListener focusListener;
44
45
46
47 private PrePostFieldListener immidiateListener;
48
49
50
51 private Dimension size;
52
53
54
55
56 private JScrollPane scrollPane;
57
58
59
60
61 public TextArea(String name) {
62 super(name);
63 }
64
65
66
67
68 public void destroy() {
69 processDestroy();
70 removeAllRules();
71 if (textComponent != null) {
72 textComponent.removeAll();
73 Container container = textComponent.getParent();
74 if (container != null) {
75 container.remove(textComponent);
76 }
77 textComponent = null;
78 if (scrollPane != null) {
79 scrollPane.removeAll();
80 container = scrollPane.getParent();
81 if (container != null) {
82 container.remove(scrollPane);
83 }
84 }
85 }
86 getPart().removeWidget(this, this);
87 }
88
89
90
91
92 public Object getNativeWidget() {
93 if (!initialized) {
94 initialize();
95 }
96 if (scrollPane != null) {
97 return scrollPane;
98 }
99 return textComponent;
100 }
101
102
103
104
105 public void initialize() {
106 if (this.initialized) {
107 return;
108 }
109 this.initialized = true;
110 this.setValueCalled = true;
111 textComponent = new JTextArea();
112 if (getProperty("scrollbar") != null) {
113 scrollPane = new JScrollPane(textComponent);
114 String scrollBar = getProperty("scrollbar");
115 scrollBar = scrollBar.toLowerCase();
116 if (scrollBar.equals("vertical") || scrollBar.equals("both")) {
117 int policy = JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED;
118 String state = getProperty("scrollbar.state");
119 if ("never".equalsIgnoreCase(state)) {
120 policy = JScrollPane.VERTICAL_SCROLLBAR_NEVER;
121 } else if ("always".equalsIgnoreCase(state)) {
122 policy = JScrollPane.VERTICAL_SCROLLBAR_ALWAYS;
123 }
124 scrollPane.setVerticalScrollBarPolicy(policy);
125 }
126 if (scrollBar.equals("horizontal") || scrollBar.equals("both")) {
127 int policy = JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED;
128 String state = getProperty("scrollbar.state");
129 if ("never".equalsIgnoreCase(state)) {
130 policy = JScrollPane.HORIZONTAL_SCROLLBAR_NEVER;
131 } else if ("always".equalsIgnoreCase(state)) {
132 policy = JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS;
133 }
134 scrollPane.setHorizontalScrollBarPolicy(policy);
135 }
136 scrollPane.setSize(getRectangle().getRectangle().getSize());
137 scrollPane.setPreferredSize(getRectangle().getRectangle().getSize());
138 }
139 if (isImmidiate()) {
140 if (this.immidiateListener == null) {
141 IXuluxListener listener = getPart().getFieldEventHandler(this);
142 if (listener != null) {
143 this.immidiateListener = (PrePostFieldListener) listener;
144 } else {
145 this.immidiateListener = new PrePostFieldListener(this);
146 }
147 }
148 }
149 if (isVisible()) {
150 if (focusListener == null) {
151 boolean readOnly = BooleanUtils.toBoolean(getProperty("readonly"));
152 if (!readOnly) {
153 IXuluxListener listener = getPart().getFieldEventHandler(this);
154 if (listener == null) {
155 focusListener = (PrePostFieldListener) listener;
156 } else {
157 focusListener = new PrePostFieldListener(this);
158 }
159 textComponent.addFocusListener(focusListener);
160 }
161 }
162 }
163 if ("false".equalsIgnoreCase(getProperty("highlighter"))) {
164 textComponent.setHighlighter(null);
165 }
166 refresh();
167 processInit();
168 this.setValueCalled = false;
169 }
170
171
172
173
174 public void refresh() {
175 isRefreshing = true;
176 initialize();
177 textComponent.setEnabled(isEnabled());
178 textComponent.setVisible(isVisible());
179 textComponent.setPreferredSize(this.size);
180 initializeValue();
181 String backgroundColor = null;
182 if (isRequired() && isEnabled()) {
183 backgroundColor = getProperty("required-background-color");
184 } else if (!isEnabled()) {
185 backgroundColor = getProperty("disabled-background-color");
186 } else {
187 backgroundColor = getProperty("default-background-color");
188 }
189 if (backgroundColor != null) {
190 textComponent.setBackground(ColorUtils.getSwingColor(backgroundColor));
191 }
192 String foreGroundColor = null;
193 if (isRequired() && isEnabled()) {
194 foreGroundColor = getProperty("required-foreground-color");
195 } else if (!isEnabled()) {
196 foreGroundColor = getProperty("disabled-foreground-color");
197 if (foreGroundColor != null) {
198 textComponent.setDisabledTextColor(ColorUtils.getSwingColor(foreGroundColor));
199 foreGroundColor = null;
200 }
201 }
202 if (foreGroundColor == null) {
203 foreGroundColor = getProperty("default-foreground-color");
204 }
205 if (foreGroundColor != null) {
206 textComponent.setForeground(ColorUtils.getSwingColor(foreGroundColor));
207 }
208 String border = getProperty("border");
209 if (border != null) {
210 String borderThickness = getProperty("border-thickness");
211 String borderColor = getProperty("border-color");
212 int thickness = 1;
213 if (borderThickness != null) {
214 try {
215 thickness = Integer.parseInt(borderThickness);
216 } catch (NumberFormatException nfe) {
217 System.out.println("invalid borderthickness, value is " + borderThickness + ", but should be a number ");
218 }
219 }
220 Color bColor = null;
221 if (borderColor != null) {
222 bColor = ColorUtils.getSwingColor(borderColor);
223 } else {
224
225 if (getParent() != null) {
226 bColor = ((JComponent) getParent().getNativeWidget()).getForeground();
227 } else {
228 bColor = Color.black;
229 }
230 }
231 if (border.equalsIgnoreCase("line")) {
232 textComponent.setBorder(new LineBorder(bColor, thickness));
233 }
234 }
235 ((JTextArea) textComponent).setLineWrap(BooleanUtils.toBoolean(getProperty("linewrap")));
236 ((JTextArea) textComponent).setWrapStyleWord(BooleanUtils.toBoolean(getProperty("wordwrap")));
237
238 textComponent.repaint();
239 textComponent.setCaretPosition(0);
240 isRefreshing = false;
241 }
242 }