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;
|
---|
28 | import java.awt.event.ActionEvent;
|
---|
29 | import java.awt.event.ActionListener;
|
---|
30 |
|
---|
31 | import javax.swing.JTextArea;
|
---|
32 | import javax.swing.JToggleButton;
|
---|
33 |
|
---|
34 | import org.apache.commons.beanutils.BeanUtils;
|
---|
35 | import org.apache.commons.beanutils.PropertyUtils;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * @author Copyright (C) 2005 Grzegorz Kowal
|
---|
39 | */
|
---|
40 | public class OptListBinding implements Binding, ActionListener {
|
---|
41 | private final String _property;
|
---|
42 | private final String _stateProperty;
|
---|
43 | private final JToggleButton _button;
|
---|
44 | private final JTextArea _textArea;
|
---|
45 | private final Color _validColor;
|
---|
46 |
|
---|
47 | public OptListBinding(String property, String stateProperty,
|
---|
48 | JToggleButton button, JTextArea textArea) {
|
---|
49 | if (property == null || button == null || textArea == null) {
|
---|
50 | throw new NullPointerException();
|
---|
51 | }
|
---|
52 | if (property.equals("")) {
|
---|
53 | throw new IllegalArgumentException();
|
---|
54 | }
|
---|
55 | _property = property;
|
---|
56 | _stateProperty = stateProperty;
|
---|
57 | _button = button;
|
---|
58 | _textArea = textArea;
|
---|
59 | _validColor = _textArea.getBackground();
|
---|
60 | button.addActionListener(this);
|
---|
61 | }
|
---|
62 |
|
---|
63 | public String getProperty() {
|
---|
64 | return _property;
|
---|
65 | }
|
---|
66 |
|
---|
67 | public void clear(IValidatable bean) {
|
---|
68 | put(bean);
|
---|
69 | }
|
---|
70 |
|
---|
71 | public void put(IValidatable bean) {
|
---|
72 | try {
|
---|
73 | boolean selected = "true".equals(BeanUtils.getProperty(bean, _stateProperty));
|
---|
74 | _button.setSelected(selected);
|
---|
75 | _textArea.setEnabled(selected);
|
---|
76 | String[] items = (String[]) PropertyUtils.getProperty(bean, _property);
|
---|
77 | StringBuffer sb = new StringBuffer();
|
---|
78 | for (int i = 0; i < items.length; i++) {
|
---|
79 | sb.append(items[i]);
|
---|
80 | if (i < items.length - 1) {
|
---|
81 | sb.append("\n");
|
---|
82 | }
|
---|
83 | }
|
---|
84 | _textArea.setText(sb.toString());
|
---|
85 | } catch (Exception e) {
|
---|
86 | throw new BindingException(e);
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | public void get(IValidatable bean) {
|
---|
91 | try {
|
---|
92 | String[] items = _textArea.getText().split("\n");
|
---|
93 | PropertyUtils.setProperty(bean, _property, _button.isSelected() ? items : null);
|
---|
94 | } catch (Exception 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 | public void actionPerformed(ActionEvent e) {
|
---|
113 | _textArea.setEnabled(_button.isSelected());
|
---|
114 | }
|
---|
115 | }
|
---|