1 /*
2 * Scope: a generic MVC framework.
3 * Copyright (c) 2000-2002, The Scope team
4 * All rights reserved.
5 *
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * Neither the name "Scope" nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 *
36 * $Id: SComboBoxEditor.java,v 1.9 2002/09/05 15:41:49 ludovicc Exp $
37 */
38 package org.scopemvc.view.swing;
39
40 import java.awt.Component;
41 import java.awt.event.ActionListener;
42 import javax.swing.ComboBoxEditor;
43 import javax.swing.JTextField;
44 import org.apache.commons.logging.Log;
45 import org.apache.commons.logging.LogFactory;
46 import org.scopemvc.util.convertor.StringConvertor;
47 import org.scopemvc.util.convertor.StringConvertors;
48
49 /***
50 * <P>
51 *
52 * Default combobox editor for SComboBox. It can be used for any value class for
53 * which a StringConvertor exists. The editor is created in SComboBox
54 * constructor, so it can be safely obtained with call <code>getEditor</code>.
55 * </P> <p>
56 *
57 * The StringConvertor is obtained from {@link
58 * org.scopemvc.util.convertor.StringConvertors StringConvertors} to match the
59 * type of object being edited. </p>
60 *
61 * @author <A HREF="mailto:daniel.michalik@autel.cz">Daniel Michalik</A>
62 * @created 05 September 2002
63 * @version $Revision: 1.9 $ $Date: 2002/09/05 15:41:49 $
64 */
65 public class SComboBoxEditor implements ComboBoxEditor {
66
67 private final static Log LOG = LogFactory.getLog(SComboBoxEditor.class);
68
69 private StringConvertor convertor;
70 private JTextField component;
71
72
73 /***
74 * Constructor for the SComboBoxEditor object
75 */
76 public SComboBoxEditor() {
77 component = new JTextField();
78 component.setBorder(null);
79 }
80
81
82 // public final StringConvertor getStringConvertor() {
83 // return convertor;
84 // }
85
86
87 /***
88 * Return the edited item.
89 *
90 * @return The item value
91 * @throws IllegalStateException TODO: Describe the Exception
92 */
93 public Object getItem() throws IllegalStateException {
94 Object result;
95 if (convertor == null) {
96 result = component.getText();
97 } else {
98 result = convertor.stringAsValue(component.getText());
99 }
100
101 if (LOG.isDebugEnabled()) {
102 if (result != null) {
103 LOG.debug("getItem: " + result + ", class: " + result.getClass());
104 } else {
105 LOG.debug("getItem: null ");
106 }
107 }
108
109 return result;
110 }
111
112
113 /***
114 * Return the component that should be added to the tree hierarchy for this
115 * editor
116 *
117 * @return The editorComponent value
118 */
119 public Component getEditorComponent() {
120 return component;
121 }
122
123
124 /***
125 * Sets the string convertor
126 *
127 * @param conv The new stringConvertor value
128 */
129 public final void setStringConvertor(StringConvertor conv) {
130 convertor = conv;
131 }
132
133
134 /***
135 * Set the item that should be edited. Cancel any editing if necessary
136 *
137 * @param anObject The new item value
138 */
139 public void setItem(Object anObject) {
140 if (LOG.isDebugEnabled()) {
141 if (anObject != null) {
142 LOG.debug("setItem " + anObject + ", class: " + anObject.getClass());
143 } else {
144 LOG.debug("setItem: null ");
145 }
146 }
147 createStringConvertor(anObject);
148 if (convertor == null) {
149 component.setText((anObject == null) ? "" : anObject.toString());
150 } else {
151 component.setText(convertor.valueAsString(anObject));
152 }
153 }
154
155
156 /***
157 * Add an ActionListener. An action event is generated when the edited item
158 * changes
159 *
160 * @param l The element to be added to the ActionListener attribute
161 */
162 public void addActionListener(ActionListener l) {
163 component.addActionListener(l);
164 }
165
166
167 /***
168 * Ask the editor to start editing and to select everything
169 */
170 public void selectAll() {
171 component.selectAll();
172 component.requestFocus();
173 }
174
175
176 /***
177 * Remove an ActionListener
178 *
179 * @param l TODO: Describe the Parameter
180 */
181 public void removeActionListener(ActionListener l) {
182 component.removeActionListener(l);
183 }
184
185
186 /***
187 * Returns current string convertor. If convertor was not set tries to
188 * obtain new one.
189 *
190 * @param value TODO: Describe the Parameter
191 */
192 private void createStringConvertor(Object value) {
193 if (convertor != null) {
194 return;
195 }
196 if (convertor == null) {
197 if (value == null) {
198 return;
199 }
200 convertor = StringConvertors.forClass(value.getClass());
201 if (LOG.isDebugEnabled()) {
202 LOG.debug("Resolving String convertor for value of class: "
203 + value.getClass());
204 LOG.debug("String convertor: " + convertor);
205 }
206 }
207 }
208 }
This page was automatically generated by Maven