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 Jun 14, 2006
036: */
037: package net.sf.launch4j.binding;
038:
039: import java.awt.Color;
040: import java.util.ArrayList;
041: import java.util.List;
042:
043: import javax.swing.JTextArea;
044:
045: import org.apache.commons.beanutils.PropertyUtils;
046:
047: /**
048: * @author Copyright (C) 2006 Grzegorz Kowal
049: */
050: public class JTextAreaBinding implements Binding {
051: private final String _property;
052: private final JTextArea _textArea;
053: private final Color _validColor;
054:
055: public JTextAreaBinding(String property, JTextArea textArea) {
056: if (property == null || textArea == null) {
057: throw new NullPointerException();
058: }
059: if (property.equals("")) {
060: throw new IllegalArgumentException();
061: }
062: _property = property;
063: _textArea = textArea;
064: _validColor = _textArea.getBackground();
065: }
066:
067: public String getProperty() {
068: return _property;
069: }
070:
071: public void clear(IValidatable bean) {
072: put(bean);
073: }
074:
075: public void put(IValidatable bean) {
076: try {
077: List list = (List) PropertyUtils.getProperty(bean,
078: _property);
079: StringBuffer sb = new StringBuffer();
080: if (list != null) {
081: for (int i = 0; i < list.size(); i++) {
082: sb.append(list.get(i));
083: if (i < list.size() - 1) {
084: sb.append("\n");
085: }
086: }
087: }
088: _textArea.setText(sb.toString());
089: } catch (Exception e) {
090: throw new BindingException(e);
091: }
092: }
093:
094: public void get(IValidatable bean) {
095: try {
096: String text = _textArea.getText();
097: if (!text.equals("")) {
098: String[] items = text.split("\n");
099: List list = new ArrayList();
100: for (int i = 0; i < items.length; i++) {
101: list.add(items[i]);
102: }
103: PropertyUtils.setProperty(bean, _property, list);
104: } else {
105: PropertyUtils.setProperty(bean, _property, null);
106: }
107: } catch (Exception e) {
108: throw new BindingException(e);
109: }
110: }
111:
112: public void markValid() {
113: _textArea.setBackground(_validColor);
114: _textArea.requestFocusInWindow();
115: }
116:
117: public void markInvalid() {
118: _textArea.setBackground(Binding.INVALID_COLOR);
119: }
120:
121: public void setEnabled(boolean enabled) {
122: _textArea.setEnabled(enabled);
123: }
124: }
|