001: /*
002: Launch4j (http://launch4j.sourceforge.net/)
003: Cross-platform Java application wrapper for creating Windows native executables.
004:
005: Copyright (c) 2004, 2007 Grzegorz Kowal
006:
007: All rights reserved.
008:
009: Redistribution and use in source and binary forms, with or without modification,
010: are permitted provided that the following conditions are met:
011:
012: * Redistributions of source code must retain the above copyright notice,
013: this list of conditions and the following disclaimer.
014: * Redistributions in binary form must reproduce the above copyright notice,
015: this list of conditions and the following disclaimer in the documentation
016: and/or other materials provided with the distribution.
017: * Neither the name of the Launch4j nor the names of its contributors
018: may be used to endorse or promote products derived from this software without
019: specific prior written permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
022: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
023: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
024: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
025: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
026: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
027: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
028: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
029: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
030: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
031: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
032: */
033:
034: /*
035: * Created on Sep 3, 2005
036: */
037: package net.sf.launch4j.binding;
038:
039: import java.awt.Color;
040: import java.awt.event.ActionEvent;
041: import java.awt.event.ActionListener;
042: import java.util.ArrayList;
043: import java.util.List;
044:
045: import javax.swing.JTextArea;
046: import javax.swing.JToggleButton;
047:
048: import org.apache.commons.beanutils.BeanUtils;
049: import org.apache.commons.beanutils.PropertyUtils;
050:
051: /**
052: * @author Copyright (C) 2005 Grzegorz Kowal
053: */
054: public class OptJTextAreaBinding implements Binding, ActionListener {
055: private final String _property;
056: private final String _stateProperty;
057: private final JToggleButton _button;
058: private final JTextArea _textArea;
059: private final Color _validColor;
060:
061: public OptJTextAreaBinding(String property, String stateProperty,
062: JToggleButton button, JTextArea textArea) {
063: if (property == null || button == null || textArea == null) {
064: throw new NullPointerException();
065: }
066: if (property.equals("")) {
067: throw new IllegalArgumentException();
068: }
069: _property = property;
070: _stateProperty = stateProperty;
071: _button = button;
072: _textArea = textArea;
073: _validColor = _textArea.getBackground();
074: button.addActionListener(this );
075: }
076:
077: public String getProperty() {
078: return _property;
079: }
080:
081: public void clear(IValidatable bean) {
082: put(bean);
083: }
084:
085: public void put(IValidatable bean) {
086: try {
087: boolean selected = "true".equals(BeanUtils.getProperty(
088: bean, _stateProperty));
089: _button.setSelected(selected);
090: _textArea.setEnabled(selected);
091: List list = (List) PropertyUtils.getProperty(bean,
092: _property);
093: StringBuffer sb = new StringBuffer();
094: if (list != null) {
095: for (int i = 0; i < list.size(); i++) {
096: sb.append(list.get(i));
097: if (i < list.size() - 1) {
098: sb.append("\n");
099: }
100: }
101: }
102: _textArea.setText(sb.toString());
103: } catch (Exception e) {
104: throw new BindingException(e);
105: }
106: }
107:
108: public void get(IValidatable bean) {
109: try {
110: String text = _textArea.getText();
111: if (_button.isSelected() && !text.equals("")) {
112: String[] items = text.split("\n");
113: List list = new ArrayList();
114: for (int i = 0; i < items.length; i++) {
115: list.add(items[i]);
116: }
117: PropertyUtils.setProperty(bean, _property, list);
118: } else {
119: PropertyUtils.setProperty(bean, _property, null);
120: }
121: } catch (Exception e) {
122: throw new BindingException(e);
123: }
124: }
125:
126: public void markValid() {
127: _textArea.setBackground(_validColor);
128: _textArea.requestFocusInWindow();
129: }
130:
131: public void markInvalid() {
132: _textArea.setBackground(Binding.INVALID_COLOR);
133: }
134:
135: public void setEnabled(boolean enabled) {
136: _textArea.setEnabled(enabled);
137: }
138:
139: public void actionPerformed(ActionEvent e) {
140: _textArea.setEnabled(_button.isSelected());
141: }
142: }
|