KickJava   Java API By Example, From Geeks To Geeks.

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


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 Sep 3, 2005
24  */

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

42 public class OptJTextAreaBinding implements Binding, ActionListener JavaDoc {
43     private final String JavaDoc _property;
44     private final String JavaDoc _stateProperty;
45     private final JToggleButton JavaDoc _button;
46     private final JTextArea JavaDoc _textArea;
47     private final Color JavaDoc _validColor;
48
49     public OptJTextAreaBinding(String JavaDoc property, String JavaDoc stateProperty,
50             JToggleButton JavaDoc button, JTextArea JavaDoc textArea) {
51         if (property == null || button == null || textArea == null) {
52             throw new NullPointerException JavaDoc();
53         }
54         if (property.equals("")) {
55             throw new IllegalArgumentException JavaDoc();
56         }
57         _property = property;
58         _stateProperty = stateProperty;
59         _button = button;
60         _textArea = textArea;
61         _validColor = _textArea.getBackground();
62         button.addActionListener(this);
63     }
64
65     public String JavaDoc getProperty() {
66         return _property;
67     }
68
69     public void clear(IValidatable bean) {
70         put(bean);
71     }
72
73     public void put(IValidatable bean) {
74         try {
75             boolean selected = "true".equals(BeanUtils.getProperty(bean,
76                     _stateProperty));
77             _button.setSelected(selected);
78             _textArea.setEnabled(selected);
79             List JavaDoc list = (List JavaDoc) PropertyUtils.getProperty(bean, _property);
80             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
81             if (list != null) {
82                 for (int i = 0; i < list.size(); i++) {
83                     sb.append(list.get(i));
84                     if (i < list.size() - 1) {
85                         sb.append("\n");
86                     }
87                 }
88             }
89             _textArea.setText(sb.toString());
90         } catch (Exception JavaDoc e) {
91             throw new BindingException(e);
92         }
93     }
94
95     public void get(IValidatable bean) {
96         try {
97             String JavaDoc text = _textArea.getText();
98             if (_button.isSelected() && !text.equals("")) {
99                 String JavaDoc[] items = text.split("\n");
100                 List JavaDoc list = new ArrayList JavaDoc();
101                 for (int i = 0; i < items.length; i++) {
102                     list.add(items[i]);
103                 }
104                 PropertyUtils.setProperty(bean, _property, list);
105             } else {
106                 PropertyUtils.setProperty(bean, _property, null);
107             }
108         } catch (Exception JavaDoc e) {
109             throw new BindingException(e);
110         }
111     }
112
113     public void markValid() {
114         _textArea.setBackground(_validColor);
115         _textArea.requestFocusInWindow();
116     }
117
118     public void markInvalid() {
119         _textArea.setBackground(Binding.INVALID_COLOR);
120     }
121     
122     public void setEnabled(boolean enabled) {
123         _textArea.setEnabled(enabled);
124     }
125
126     public void actionPerformed(ActionEvent JavaDoc e) {
127         _textArea.setEnabled(_button.isSelected());
128     }
129 }
130
Popular Tags