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: STextCellEditor.java,v 1.6 2002/09/05 15:41:49 ludovicc Exp $
37 */
38 package org.scopemvc.view.swing;
39
40
41 import java.awt.event.MouseEvent;
42 import java.util.EventObject;
43 import javax.swing.AbstractCellEditor;
44 import javax.swing.JTextField;
45 import javax.swing.text.JTextComponent;
46 import org.apache.commons.logging.Log;
47 import org.apache.commons.logging.LogFactory;
48 import org.scopemvc.util.convertor.StringConvertor;
49
50 /***
51 * Base class for cell editors based on Swing's <code>JTextComponent</code>. It
52 * is validating editor - it means, that {@link #stopCellEditing
53 * stopCellEditing} can return <code>false</code>. <p>
54 *
55 * Difference between a Swing's and this cell editor is that Swing always
56 * returns String value, whereas <code>STextCellEditor</code> uses {@link
57 * org.scopemvc.util.convertor.StringConvertor StringConvertor} to return object
58 * of desired type.</p> <p>
59 *
60 * <i>Note:</i> This editor fixes in unbelievable simple way Sun's editor
61 * unpleasant bug - when You start cell editing with keyboard, not mouse, the
62 * text field has no cursor (in 1.3.1 and 1.4 beta) </p>
63 *
64 * @author <A HREF="mailto:daniel.michalik@autel.cz">Daniel Michalik</A>
65 * @created 05 September 2002
66 * @version $Revision: 1.6 $ $Date: 2002/09/05 15:41:49 $
67 */
68 public abstract class STextCellEditor extends AbstractCellEditor {
69
70 private final static Log LOG = LogFactory.getLog(STextCellEditor.class);
71 /***
72 * TODO: describe of the Field
73 */
74 protected JTextComponent component;
75
76 private StringConvertor convertor;
77 private ValidationHelper validationHelper;
78 private Object value;
79 private int clickCountToStart = 2;
80
81 /***
82 * Creates new SDefaultCellRenderer
83 *
84 * @param inConvertor TODO: Describe the Parameter
85 * @throws IllegalArgumentException TODO: Describe the Exception
86 */
87 public STextCellEditor(StringConvertor inConvertor)
88 throws IllegalArgumentException {
89 if (inConvertor == null) {
90 throw new IllegalArgumentException("Passed StringConvertor "
91 + "cannot be null");
92 }
93 convertor = inConvertor;
94 component = createTextComponent();
95 if (component == null) {
96 throw new IllegalStateException("Method createTextComponent "
97 + "cannot return null component");
98 }
99 validationHelper = new ValidationHelper(component);
100 }
101
102 /***
103 * Gets the string convertor
104 *
105 * @return The stringConvertor value
106 */
107 public final StringConvertor getStringConvertor() {
108 return convertor;
109 }
110
111 /***
112 * Gets the cell editor value
113 *
114 * @return The cellEditorValue value
115 */
116 public final Object getCellEditorValue() {
117 return value;
118 }
119
120 /***
121 * ClickCountToStart controls the number of clicks required to start
122 * editing. Default value is 2.
123 *
124 * @return The clickCountToStart value
125 */
126 public final int getClickCountToStart() {
127 return clickCountToStart;
128 }
129
130 /***
131 * Editable for mause click events if click count is equals or greater then
132 * {@link #getClickCountToStart() getClickCountToStart()}. For all other
133 * event types returns true.
134 *
135 * @param anEvent TODO: Describe the Parameter
136 * @return The cellEditable value
137 */
138 public boolean isCellEditable(EventObject anEvent) {
139 if (anEvent instanceof MouseEvent) {
140 return ((MouseEvent) anEvent).getClickCount() >= clickCountToStart;
141 }
142 return true;
143 }
144
145 /***
146 * Specifies the number of clicks needed to start editing.
147 *
148 * @param count an int specifying the number of clicks needed to start
149 * editing
150 * @see #getClickCountToStart
151 */
152 public final void setClickCountToStart(int count) {
153 clickCountToStart = count;
154 }
155
156 /***
157 * TODO: document the method
158 */
159 public final void cancelCellEditing() {
160 if (LOG.isDebugEnabled()) {
161 LOG.debug("cancel Editing");
162 }
163 value = null;
164 super.cancelCellEditing();
165 }
166
167 /***
168 * TODO: document the method
169 *
170 * @return TODO: Describe the Return Value
171 */
172 public final boolean stopCellEditing() {
173 if (LOG.isDebugEnabled()) {
174 LOG.debug("stop Editing");
175 }
176 boolean result = obtainValue();
177 super.stopCellEditing();
178 return result;
179 }
180
181 /***
182 * Returns true.
183 *
184 * @return The validating value
185 */
186 protected boolean isValidating() {
187 return true;
188 }
189
190 /***
191 * Sets value for editing. Value is converted into String with <code>StringConvertor</code>
192 * . For use in inherited classes.
193 *
194 * @param inValue The new value value
195 * @throws IllegalArgumentException if converted cannot convert passed
196 * value.
197 */
198 protected final void setValue(Object inValue)
199 throws IllegalArgumentException {
200 component.setText(convertor.valueAsString(inValue));
201 }
202
203 /***
204 * TODO: document the method
205 *
206 * @return TODO: Describe the Return Value
207 */
208 protected JTextComponent createTextComponent() {
209 JTextComponent c =
210 new JTextField() {
211 public void addNotify() {
212 super.addNotify();
213 requestFocus();
214 }
215 };
216 return c;
217 }
218
219 private boolean obtainValue() {
220 try {
221 value = convertor.stringAsValue(component.getText());
222 validationHelper.validationSuccess();
223 if (LOG.isDebugEnabled()) {
224 LOG.debug("cell validation succeed");
225 }
226 return true;
227 } catch (IllegalArgumentException ex) {
228 value = null;
229 if (LOG.isDebugEnabled()) {
230 LOG.debug("cell validation failed");
231 }
232 validationHelper.validationFailed(ex);
233 return !isValidating();
234 }
235 }
236
237 }
238
This page was automatically generated by Maven