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