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: ValidationHelper.java,v 1.4 2002/09/05 15:41:50 ludovicc Exp $
37 */
38 package org.scopemvc.view.swing;
39
40
41 import java.awt.Color;
42 import java.awt.event.MouseEvent;
43 import javax.swing.JComponent;
44 import javax.swing.JToolTip;
45 import javax.swing.ToolTipManager;
46
47 /***
48 * <P>
49 *
50 * Handles {@link org.scopemvc.view.util.ModelBindable#validationFailed} and
51 * {@link org.scopemvc.view.util.ModelBindable#validationSuccess} by setting the
52 * background colour of the parent component to a new colour and being able to
53 * generate an "error tooltip" that is coloured and contains the localized error
54 * message from the validation failure. </P>
55 *
56 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net">Steve Meyfroidt</A>
57 * @created 05 September 2002
58 * @version $Revision: 1.4 $ $Date: 2002/09/05 15:41:50 $
59 */
60 public class ValidationHelper {
61
62 /***
63 * TODO: describe of the Field
64 */
65 public final static Color VALIDATION_FAILED_COLOR = Color.pink;
66
67 /***
68 * The localized error text from the exception that caused the validation
69 * failure.
70 */
71 private String validationError;
72
73 /***
74 * Owning JComponent of this helper.
75 */
76 private JComponent parent;
77
78 /***
79 * Keep the parent's tooltip text so we can restore it on validation
80 * success.
81 */
82 private String originalTooltipText;
83
84 /***
85 * Keep the parent's original background colour so it can be restored when
86 * validation succeeds.
87 */
88 private Color originalBackground;
89
90
91 /***
92 * @param inComponent parent JComponent.
93 */
94 public ValidationHelper(JComponent inComponent) {
95 parent = inComponent;
96 }
97
98
99 /***
100 * Signal that validation failed.
101 *
102 * @param inException what caused the validation failure. Should contain a
103 * user-readable message in getLocalizedMessage().
104 */
105 public void validationFailed(Exception inException) {
106 if (validationError == null) {
107 originalBackground = parent.getBackground();
108 originalTooltipText = parent.getToolTipText();
109 }
110 validationError = inException.getLocalizedMessage();
111
112 parent.setBackground(VALIDATION_FAILED_COLOR);
113 parent.setToolTipText(validationError);
114 clearTooltip();
115 }
116
117 // public void validationFailed(Exception inException) {
118 // validationError = inException.getLocalizedMessage();
119 // originalBackground = parent.getBackground();
120 // parent.setBackground(VALIDATION_FAILED_COLOR);
121 // originalTooltipText = parent.getToolTipText();
122 // parent.setToolTipText(validationError);
123 // clearTooltip();
124 // }
125
126
127
128 /***
129 * Signal that validation succeeded.
130 */
131 public void validationSuccess() {
132 if (validationError != null) {
133 clearTooltip();
134 parent.setBackground(originalBackground);
135 parent.setToolTipText(originalTooltipText);
136 validationError = null;
137 }
138 }
139
140
141 /***
142 * Parent should call this in its createTooltip() to allow helper to
143 * substitute an error tooltip.
144 *
145 * @param inTip normal tooltip created by parent.
146 * @return tooltip that may be an "error tooltip" rather than the passed
147 * default tooltip.
148 */
149 public JToolTip createToolTip(JToolTip inTip) {
150 if (inTip != null && validationError != null) {
151 inTip.setBackground(VALIDATION_FAILED_COLOR);
152 }
153 return inTip;
154 }
155
156
157 /***
158 * Marvellous hack to hide any shown tooltips.
159 */
160 private void clearTooltip() {
161 ToolTipManager.sharedInstance().mousePressed(new MouseEvent(parent, 0, 0, 0, 0, 0, 0, false));
162 }
163 }
164
This page was automatically generated by Maven