View Javadoc

1   /*
2      Copyright 2002-2006 Martin van den Bemt
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
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   * The swing textare widget.
34   *
35   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
36   * @version $Id: TextArea.java,v 1.1 2005/12/18 12:58:18 mvdb Exp $
37   */
38  public class TextArea extends Entry {
39  
40      /**
41       * the focuslistener
42       */
43      private PrePostFieldListener focusListener;
44      /**
45       * the immidiatelistener
46       */
47      private PrePostFieldListener immidiateListener;
48      /**
49       * the size
50       */
51      private Dimension size;
52  
53      /**
54       * The scrollpane for the textarea
55       */
56      private JScrollPane scrollPane;
57  
58      /**
59       * @param name the textarea name
60       */
61      public TextArea(String name) {
62          super(name);
63      }
64  
65      /**
66       * @see org.xulux.gui.XuluxWidget#destroy()
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       * @see org.xulux.nyx.gui.XuluxWidget#getNativeWidget()
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      * @see org.xulux.nyx.gui.XuluxWidget#initialize()
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      * @see org.xulux.nyx.gui.XuluxWidget#refresh()
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                 // we default to black border color
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 //        ((JTextArea) textComponent).setEditable(BooleanUtils.toBoolean(getProperty("editable")));
238         textComponent.repaint();
239         textComponent.setCaretPosition(0);
240         isRefreshing = false;
241     }
242 }