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: SModelAction.java,v 1.2 2002/09/13 17:03:50 ludovicc Exp $
37 */
38 package org.scopemvc.view.swing;
39
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42 import org.scopemvc.core.Control;
43 import org.scopemvc.core.PropertyView;
44 import org.scopemvc.core.Selector;
45 import org.scopemvc.view.util.ModelBindable;
46
47 /***
48 * <P>
49 *
50 * An SAction that is bound to a property and performs a test on the value of
51 * the property to determine its active state. </P> <P>
52 *
53 * A comparable object is used to perform the test on the view value. This
54 * action is active when the comparable object returns a value greater than 0
55 * when passed the view value in its {@link Comparable#compareTo compareTo()}
56 * method.</P> <P>
57 *
58 * Note: it is convenient to use the Comparable interface to perform tests
59 * because it is already implemented in many places (natural ordering). For
60 * example, to have this SModelAction enabled when the view value is an Integer
61 * less than 1, then do: <br>
62 * <code>setValueTest(new Integer(1))</code> <br>
63 * because <code>new Integer(1).compareTo(value)</code> will return 1 if value
64 * is an Integer less than 1</P> <P>
65 *
66 * If the comparison fails because of an exception coming from the Comparable
67 * test, then this action is disabled. </P>
68 *
69 * @author <a href="mailto:ludovicc@users.sourceforge.net>Ludovic Claude</a>
70 * @created 18 June 2002
71 * @version $Revision: 1.2 $ $Date: 2002/09/13 17:03:50 $
72 */
73 public class SModelAction extends SAction implements ModelBindable, PropertyView, Refreshable {
74
75 private static final Log LOG = LogFactory.getLog(SModelAction.class);
76
77 /***
78 * Helper to manage model to view binding.
79 */
80 private SwingBoundModel boundModel = new SwingBoundModel(this);
81
82 // --------------------- shownModel -----------------------
83
84 /***
85 * The model object that this component presents, which may be a property of
86 * the bound model if a Selector is specified.
87 */
88 private Object shownModel;
89
90 private Comparable valueTest;
91
92 /***
93 * Constructor for the ModelAction object. <BR>
94 * It defines a test that will activate the action if the view value is not
95 * null.
96 *
97 * @param inControlID The control ID to be issued by this Action
98 */
99 public SModelAction(String inControlID) {
100 super(inControlID);
101 setValueTest(new NotNullComparable());
102 }
103
104 /***
105 * Constructor for the SModelAction object. <BR>
106 * It defines a test that will activate the action if the view value is not
107 * null.
108 *
109 * @param inControlID The control ID to be issued by this Action
110 * @param inView The view owning this Action; its bound controller will
111 * receive the Controls issued by this Action.
112 * @param inSelector The selector for the property
113 */
114 public SModelAction(String inControlID, SwingView inView, Selector inSelector) {
115 this(inControlID, inView, inSelector, new NotNullComparable());
116 }
117
118 /***
119 * Constructor for the SModelAction object.
120 *
121 * @param inControlID The control ID to be issued by this Action
122 * @param inView The view owning this Action; its bound controller will
123 * receive the Controls issued by this Action.
124 * @param inSelector The selector for the property
125 * @param inValueTest The test for the enabled state. If the compareTo()
126 * method returns a value greater than 0, then this action is active.
127 */
128 public SModelAction(String inControlID, SwingView inView, Selector inSelector, Comparable inValueTest) {
129 super(inControlID, inView);
130 setSelector(inSelector);
131 setValueTest(inValueTest);
132 }
133
134 // -------------- implement View ------------------
135
136 /***
137 * Gets the bound model
138 *
139 * @return The boundModel value
140 */
141 public final Object getBoundModel() {
142 return boundModel.getBoundModel();
143 }
144
145 /***
146 * Gets the selector
147 *
148 * @return The selector value
149 */
150 public final Selector getSelector() {
151 return boundModel.getSelector();
152 }
153
154 /***
155 * Get the current value (what would be set as a property of the bound model
156 * object) being presented on the View.
157 *
158 * @return property's value from the UI.
159 */
160 public final Object getViewValue() {
161 return shownModel;
162 }
163
164 /***
165 * Returns the Comparable used to test the view value.
166 *
167 * @return The valueTest value
168 */
169 public Comparable getValueTest() {
170 return valueTest;
171 }
172
173 /***
174 * Sets the selector
175 *
176 * @param inSelector The new selector value
177 */
178 public final void setSelector(Selector inSelector) {
179 boundModel.setSelector(inSelector);
180 }
181
182 /***
183 * Sets the selector string
184 *
185 * @param inSelectorString The new selectorString value
186 */
187 public final void setSelectorString(String inSelectorString) {
188 boundModel.setSelectorString(inSelectorString);
189 }
190
191 /***
192 * Sets the Comparable used to test the view value. <BR>
193 * This action is enabled when the compareTo() method of the test returns a
194 * value greater than 0 when the passed value is the model value bound to
195 * this SModelAction.
196 *
197 * @param inValueTest The new valueTest value
198 */
199 public void setValueTest(Comparable inValueTest) {
200 valueTest = inValueTest;
201 updateEnabledState();
202 }
203
204 /***
205 * Sets the bound model
206 *
207 * @param inModel The new boundModel value
208 */
209 public void setBoundModel(Object inModel) {
210 boundModel.setBoundModel(inModel);
211 }
212
213 // --------------------- Implement ModelBindable ----------------------
214
215 /***
216 * Use the passed property value and read-only state to update the View.
217 * <BR>
218 * Ignores inReadOnly.
219 *
220 * @param inValue The new value of the property in the bound model
221 * @param inReadOnly The new read-only state of the property
222 */
223 public void updateFromProperty(Object inValue, boolean inReadOnly) {
224 if (LOG.isDebugEnabled()) {
225 LOG.debug("updateFromProperty: " + inValue + ", " + inReadOnly);
226 }
227
228 setShownModel(inValue);
229 }
230
231 /***
232 * Validation failed while getting a value from View into the bound model
233 * object. <BR>
234 * Does nothing here.
235 *
236 * @param inException The exception causing the validation failure
237 */
238 public void validationFailed(Exception inException) {
239 // noop
240 }
241
242 /***
243 * Clear previous validation failure. <BR>
244 * Does nothing here.
245 */
246 public void validationSuccess() {
247 // noop
248 }
249
250
251 // ------------------ Refreshable -------------------------
252
253 /***
254 * Update the widget with the current state of the bound model.
255 */
256 public void refresh() {
257 Object propertyValue = boundModel.getPropertyValue();
258 boolean propertyReadOnly = boundModel.getPropertyReadOnly();
259 updateFromProperty(propertyValue, propertyReadOnly);
260 }
261
262
263 /***
264 * Now overwrite the firing of the control to include the additional
265 * information of the model
266 *
267 * @return The Control to fire
268 */
269 protected Control createControl() {
270 Control returnValue = super.createControl();
271 if (LOG.isDebugEnabled()) {
272 LOG.debug("createControl: Creating the control " + returnValue + "with value " + this.shownModel);
273 }
274
275 returnValue.setParameter(this.shownModel);
276 return returnValue;
277 }
278
279 /***
280 * Called internally from updateFromProperty(). Issues a
281 * CHANGE_MODEL_CONTROL_ID Control to notify parent Controller of the
282 * change.
283 *
284 * @param inModel The new shownModel value
285 */
286 private void setShownModel(Object inModel) {
287
288 if (shownModel == inModel) {
289 return;
290 }
291 shownModel = inModel;
292 updateEnabledState();
293 }
294
295 private void updateEnabledState() {
296 try {
297 if (LOG.isDebugEnabled()) {
298 LOG.debug("updateEnabledState: Testing " + shownModel + " with test " + valueTest + ", result: " + valueTest.compareTo(shownModel));
299 }
300 this.setEnabled(valueTest.compareTo(shownModel) > 0);
301 } catch (NullPointerException ex) {
302 LOG.info("NPE when testing the view value with the valueTest. Assuming that this action: " + this.getName() + " is disabled");
303 setEnabled(false);
304 } catch (Exception ex) {
305 LOG.warn("Could not test the view value with the valueTest", ex);
306 setEnabled(false);
307 }
308 }
309
310 /***
311 * Test that returns 1 if the value is not null.
312 *
313 * @author lclaude
314 * @created 12 September 2002
315 */
316 static class NotNullComparable implements Comparable {
317 /***
318 * Test if the object is not null
319 *
320 * @param o The object to test
321 * @return 1 if the object is not null, 0 otherwise
322 */
323 public int compareTo(Object o) {
324 return (o == null) ? 0 : 1;
325 }
326
327 /***
328 * Returns a string representation
329 *
330 * @return a string representation
331 */
332 public String toString() {
333 return "NotNullComparable";
334 }
335 }
336 }
337
This page was automatically generated by Maven