KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > launch4j > binding > JTextAreaBinding


1 /*
2     Launch4j (http://launch4j.sourceforge.net/)
3     Cross-platform Java application wrapper for creating Windows native executables.
4
5     Copyright (C) 2004, 2006 Grzegorz Kowal
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */

21
22 /*
23  * Created on Jun 14, 2006
24  */

25 package net.sf.launch4j.binding;
26
27 import java.awt.Color JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30
31 import javax.swing.JTextArea JavaDoc;
32
33 import org.apache.commons.beanutils.PropertyUtils;
34
35 /**
36  * @author Copyright (C) 2006 Grzegorz Kowal
37  */

38 public class JTextAreaBinding implements Binding {
39     private final String JavaDoc _property;
40     private final JTextArea JavaDoc _textArea;
41     private final Color JavaDoc _validColor;
42
43     public JTextAreaBinding(String JavaDoc property, JTextArea JavaDoc textArea) {
44         if (property == null || textArea == null) {
45             throw new NullPointerException JavaDoc();
46         }
47         if (property.equals("")) {
48             throw new IllegalArgumentException JavaDoc();
49         }
50         _property = property;
51         _textArea = textArea;
52         _validColor = _textArea.getBackground();
53     }
54
55     public String JavaDoc getProperty() {
56         return _property;
57     }
58
59     public void clear(IValidatable bean) {
60         put(bean);
61     }
62
63     public void put(IValidatable bean) {
64         try {
65             List JavaDoc list = (List JavaDoc) PropertyUtils.getProperty(bean, _property);
66             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
67             if (list != null) {
68                 for (int i = 0; i < list.size(); i++) {
69                     sb.append(list.get(i));
70                     if (i < list.size() - 1) {
71                         sb.append("\n");
72                     }
73                 }
74             }
75             _textArea.setText(sb.toString());
76         } catch (Exception JavaDoc e) {
77             throw new BindingException(e);
78         }
79     }
80
81     public void get(IValidatable bean) {
82         try {
83             String JavaDoc text = _textArea.getText();
84             if (!text.equals("")) {
85                 String JavaDoc[] items = text.split("\n");
86                 List JavaDoc list = new ArrayList JavaDoc();
87                 for (int i = 0; i < items.length; i++) {
88                     list.add(items[i]);
89                 }
90                 PropertyUtils.setProperty(bean, _property, list);
91             } else {
92                 PropertyUtils.setProperty(bean, _property, null);
93             }
94         } catch (Exception JavaDoc e) {
95             throw new BindingException(e);
96         }
97     }
98
99     public void markValid() {
100         _textArea.setBackground(_validColor);
101         _textArea.requestFocusInWindow();
102     }
103
104     public void markInvalid() {
105         _textArea.setBackground(Binding.INVALID_COLOR);
106     }
107     
108     public void setEnabled(boolean enabled) {
109         _textArea.setEnabled(enabled);
110     }
111 }
112
Popular Tags