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.JTextPane;
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 TextPane 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 TextPane(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 JTextPane();
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 scrollPane.setMinimumSize(new Dimension(10,10));
139 }
140 if (isImmidiate()) {
141 if (this.immidiateListener == null) {
142 IXuluxListener listener = getPart().getFieldEventHandler(this);
143 if (listener != null) {
144 this.immidiateListener = (PrePostFieldListener) listener;
145 } else {
146 this.immidiateListener = new PrePostFieldListener(this);
147 }
148 }
149 }
150 if (isVisible()) {
151 if (focusListener == null) {
152 IXuluxListener listener = getPart().getFieldEventHandler(this);
153 if (listener == null) {
154 focusListener = (PrePostFieldListener) listener;
155 } else {
156 focusListener = new PrePostFieldListener(this);
157 }
158 textComponent.addFocusListener(focusListener);
159 }
160 }
161 refresh();
162 processInit();
163 this.setValueCalled = false;
164 }
165
166
167
168
169 public void refresh() {
170 isRefreshing = true;
171 initialize();
172 if (getProperty("content-type") != null) {
173 ((JTextPane) textComponent).setContentType(getProperty("content-type"));
174 }
175 textComponent.setEnabled(isEnabled());
176 textComponent.setVisible(isVisible());
177 textComponent.setPreferredSize(this.size);
178 if (getProperty("editable") != null) {
179 textComponent.setEditable(BooleanUtils.toBoolean(getProperty("editable")));
180 }
181 initializeValue();
182 String backgroundColor = null;
183 if (isRequired() && isEnabled()) {
184 backgroundColor = getProperty("required-background-color");
185 } else if (!isEnabled()) {
186 backgroundColor = getProperty("disabled-background-color");
187 } else {
188 backgroundColor = getProperty("default-background-color");
189 }
190 if (backgroundColor != null) {
191 textComponent.setBackground(ColorUtils.getSwingColor(backgroundColor));
192 }
193 String foreGroundColor = null;
194 if (isRequired() && isEnabled()) {
195 foreGroundColor = getProperty("required-foreground-color");
196 } else if (!isEnabled()) {
197 foreGroundColor = getProperty("disabled-foreground-color");
198 if (foreGroundColor != null) {
199 textComponent.setDisabledTextColor(ColorUtils.getSwingColor(foreGroundColor));
200 foreGroundColor = null;
201 }
202 }
203 if (foreGroundColor == null) {
204 foreGroundColor = getProperty("default-foreground-color");
205 }
206 if (foreGroundColor != null) {
207 textComponent.setForeground(ColorUtils.getSwingColor(foreGroundColor));
208 }
209 String border = getProperty("border");
210 if (border != null) {
211 String borderThickness = getProperty("border-thickness");
212 String borderColor = getProperty("border-color");
213 int thickness = 1;
214 if (borderThickness != null) {
215 try {
216 thickness = Integer.parseInt(borderThickness);
217 } catch (NumberFormatException nfe) {
218 System.out.println("invalid borderthickness, value is " + borderThickness + ", but should be a number ");
219 }
220 }
221 Color bColor = null;
222 if (borderColor != null) {
223 bColor = ColorUtils.getSwingColor(borderColor);
224 } else {
225
226 if (getParent() != null) {
227 bColor = ((JComponent) getParent().getNativeWidget()).getForeground();
228 } else {
229 bColor = Color.black;
230 }
231 }
232 if (border.equalsIgnoreCase("line")) {
233 textComponent.setBorder(new LineBorder(bColor, thickness));
234 }
235 }
236 textComponent.repaint();
237 textComponent.setCaretPosition(0);
238 isRefreshing = false;
239 }
240 }