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 Apr 30, 2005
|
---|
24 | */
|
---|
25 | package net.sf.launch4j.binding;
|
---|
26 |
|
---|
27 | import java.awt.Color;
|
---|
28 |
|
---|
29 | import javax.swing.text.JTextComponent;
|
---|
30 |
|
---|
31 | import org.apache.commons.beanutils.BeanUtils;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Handles JEditorPane, JTextArea, JTextField
|
---|
35 | *
|
---|
36 | * @author Copyright (C) 2005 Grzegorz Kowal
|
---|
37 | */
|
---|
38 | public class JTextComponentBinding implements Binding {
|
---|
39 | private final String _property;
|
---|
40 | private final JTextComponent _textComponent;
|
---|
41 | private final String _defaultValue;
|
---|
42 | private final Color _validColor;
|
---|
43 |
|
---|
44 | public JTextComponentBinding(String property, JTextComponent textComponent, String defaultValue) {
|
---|
45 | if (property == null || textComponent == null || defaultValue == null) {
|
---|
46 | throw new NullPointerException();
|
---|
47 | }
|
---|
48 | if (property.equals("")) {
|
---|
49 | throw new IllegalArgumentException();
|
---|
50 | }
|
---|
51 | _property = property;
|
---|
52 | _textComponent = textComponent;
|
---|
53 | _defaultValue = defaultValue;
|
---|
54 | _validColor = _textComponent.getBackground();
|
---|
55 | }
|
---|
56 |
|
---|
57 | public String getProperty() {
|
---|
58 | return _property;
|
---|
59 | }
|
---|
60 |
|
---|
61 | public void clear(IValidatable bean) {
|
---|
62 | _textComponent.setText(_defaultValue);
|
---|
63 | }
|
---|
64 |
|
---|
65 | public void put(IValidatable bean) {
|
---|
66 | try {
|
---|
67 | String s = BeanUtils.getProperty(bean, _property);
|
---|
68 | _textComponent.setText(s != null && !s.equals("0") ? s : ""); // XXX displays zeros as blank
|
---|
69 | } catch (Exception e) {
|
---|
70 | throw new BindingException(e);
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | public void get(IValidatable bean) {
|
---|
75 | try {
|
---|
76 | BeanUtils.setProperty(bean, _property, _textComponent.getText());
|
---|
77 | } catch (Exception e) {
|
---|
78 | throw new BindingException(e);
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | public void markValid() {
|
---|
83 | _textComponent.setBackground(_validColor);
|
---|
84 | _textComponent.requestFocusInWindow();
|
---|
85 | }
|
---|
86 |
|
---|
87 | public void markInvalid() {
|
---|
88 | _textComponent.setBackground(Binding.INVALID_COLOR);
|
---|
89 | }
|
---|
90 |
|
---|
91 | public void setEnabled(boolean enabled) {
|
---|
92 | _textComponent.setEnabled(enabled);
|
---|
93 | }
|
---|
94 | }
|
---|