1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Contributor(s):
*
* The Original Software is NetBeans. The Initial Developer of the Original
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
* Microsystems, Inc. All Rights Reserved.
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder.
*/
package org.netbeans.validation.api.ui.swing;
import java.awt.Component;
import org.netbeans.validation.api.ui.*;
import java.awt.EventQueue;
import java.awt.Point;
import javax.swing.AbstractButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.Popup;
import javax.swing.text.JTextComponent;
import org.netbeans.validation.api.Problem;
import org.netbeans.validation.api.Validator;
import org.netbeans.validation.api.ValidatorUtils;
/**
* {@link ValidationGroup} subclass specialized for handling Swing
* components. This subclass has {@code add}-methods for adding
* GUI-components for common Swing cases. There are also a method for
* getting the {@link SwingComponentDecorationFactory} used by this
* SwingValidationGroup to create decorations for the separate
* GUI-components added to the group. A custom {@code SwingComponentDecorationFactory}
* can be specified when creating the {@code SwingValidationGroup}.
*
* <p> For components this library supports out-of-the-box such as
* <code>JTextField</code>s or <code>JComboBox</code>es, simply call
* one of the <code>add()</code> methods with your component and
* validators. For validating your own components or ones this class
* doesn't have methods for, you implement {@link ValidationListener}s, and add them
* to the {@code ValidationGroup} using the the method
* {@link ValidationGroup#addItem(org.netbeans.validation.api.ui.ValidationItem, boolean) }
*/
public final class SwingValidationGroup extends ValidationGroup {
private final SwingComponentDecorationFactory decorator;
private SwingValidationGroup(GroupValidator additionalGroupValidation, SwingComponentDecorationFactory decorator, ValidationUI... ui) {
super(additionalGroupValidation, ui);
if (ui == null) {
throw new NullPointerException();
}
this.decorator = ( decorator!=null ? decorator : SwingComponentDecorationFactory.getDefault() );
}
public static SwingValidationGroup create(ValidationUI... ui) {
assert EventQueue.isDispatchThread() : "Must be called on event thread";
return new SwingValidationGroup(null, null, ui);
}
/**
* Creates a {@code SwingValidationGroup}.
*
* Will use a {@code SwingComponentDecorationFactory} returned by {@link SwingComponentDecorationFactory#getDefault() } to modify the appearance of
* subsequently added components (to show that there is a problem with a
* component's content). To instead use a custom {@code SwingComponentDecorationFactory}, call
* {@link #create(org.netbeans.validation.api.ui.GroupValidator, org.netbeans.validation.api.ui.swing.SwingComponentDecorationFactory, org.netbeans.validation.api.ui.ValidationUI[]) }
*
* @param ui Zero or more {@code ValidationUI}:s. Will be used by the {@code SwingValidationGroup} to show the leading problem (if any)
*/
public static SwingValidationGroup create(GroupValidator additionalGroupValidation, ValidationUI... ui) {
assert EventQueue.isDispatchThread() : "Must be called on event thread";
return new SwingValidationGroup(additionalGroupValidation, null, ui);
}
/**
* Creates a {@code SwingValidationGroup}.
* @param additionalGroupValidation may be null
* @param ui Zero or more {@code ValidationUI}:s. Will all be used by the
* {@code SwingValidationGroup} to show the leading problem (if any)
* @param decorator A decorator to be used to modify the appearance of
* subsequently added components (to show that there is a problem with a
* component's content).
*/
public static SwingValidationGroup create(GroupValidator additionalGroupValidation, SwingComponentDecorationFactory decorator, ValidationUI... ui) {
assert EventQueue.isDispatchThread() : "Must be called on event thread";
return new SwingValidationGroup(additionalGroupValidation, decorator, ui);
}
/**
* Gets the currently set component decorator used to modify
* components appearance (to show that there is a problem with a
* component's content).
* @return decorator A decorator. May not be null.
*/
final SwingComponentDecorationFactory getComponentDecorationFactory() {
return decorator;
}
@Override
protected final <T> ValidationUI decorationFor (T comp) {
ValidationUI dec = comp instanceof JComponent ?
this.getComponentDecorationFactory().decorationFor((JComponent) comp) :
ValidationUI.NO_OP;
return dec;
}
/**
* Add a text component to be validated using the passed validators.
*
* <p> When a problem occurs, the created ValidationListener will
* use a {@link ValidationUI} created by this {@code ValidationGroup} to decorate
* the component.
*
* <p> <b>Note:</b> All methods in this class must be called from
* the AWT Event Dispatch thread, or assertion errors will be
* thrown. Manipulating components on other threads is not safe.
*
* <p> Swing {@code Document}s (the model used by JTextComponent)
* are thread-safe, and can be modified from other threads. In
* the case that a text component validator receives an event on
* another thread, validation will be scheduled for later,
* <i>on</i> the event thread.
*
* @param comp A text component such as a <code>JTextField</code>
* @param validators One or more Validators
*/
public final void add(JTextComponent comp, Validator<String>... validators) {
assert EventQueue.isDispatchThread() : "Must be called on event thread";
assert validators.length > 0 : "Empty validator array";
Validator<String> merged = ValidatorUtils.merge(validators);
ValidationListener<JTextComponent> vl = ValidationListenerFactory.createValidationListener(comp,
ValidationStrategy.DEFAULT,
this.getComponentDecorationFactory().decorationFor(comp),
merged);
this.addItem (vl, false);
}
/**
* Add a text component to be validated using the passed validator.
*
* <p> When a problem occurs, the created ValidationListener will
* use a {@link ValidationUI} created by this {@code ValidationGroup} to decorate
* the component.
*
* <p> <b>Note:</b> All methods in this class must be called from
* the AWT Event Dispatch thread, or assertion errors will be
* thrown. Manipulating components on other threads is not safe.
*
* <p> Swing {@code Document}s (the model used by JTextComponent)
* are thread-safe, and can be modified from other threads. In
* the case that a text component validator receives an event on
* another thread, validation will be scheduled for later,
* <i>on</i> the event thread.
* <p>Unlike {@link #add(JTextComponent,Validator...)}, calling this method does not trigger warnings under {@code -Xlint:unchecked}.
* If you wish to add more than one validator, simply add the result of {@link ValidatorUtils#merge(Validator,Validator)}.
* @param comp A text component such as a <code>JTextField</code>
* @param validator a validator
*/
public final void add(JTextComponent comp, Validator<String> validator) {
assert EventQueue.isDispatchThread() : "Must be called on event thread";
ValidationListener<JTextComponent> vl = ValidationListenerFactory.createValidationListener(comp,
ValidationStrategy.DEFAULT,
this.getComponentDecorationFactory().decorationFor(comp),
validator);
this.addItem (vl, false);
}
/**
* Add a combo box to be validated using the passed validators
*
* <p> When a problem occurs, the created {@link ValidationListener} will
* use a {@link ValidationUI} created by this {@code ValidationGroup} to decorate
* the component.
*
* <p> <b>Note:</b> All methods in this class must be called from
* the AWT Event Dispatch thread, or assertion errors will be
* thrown. Manipulating components on other threads is not safe.
*
* @param box A combo box component
* @param validators One or more Validators
*/
public final void add(JComboBox box, Validator<String>... validators) {
assert EventQueue.isDispatchThread() : "Must be called on event thread";
this.addItem(ValidationListenerFactory.createValidationListener(box, ValidationStrategy.DEFAULT, ValidationUI.NO_OP, ValidatorUtils.<String>merge(validators)), false);
}
/**
* Add a combo box to be validated using the passed validator.
*
* <p> When a problem occurs, the created {@link ValidationListener} will
* use a {@link ValidationUI} created by this {@code ValidationGroup} to decorate
* the component.
*
* <p> <b>Note:</b> All methods in this class must be called from
* the AWT Event Dispatch thread, or assertion errors will be
* thrown. Manipulating components on other threads is not safe.
* <p>Unlike {@link #add(JComboBox,Validator...)}, calling this method does not trigger warnings under {@code -Xlint:unchecked}.
* If you wish to add more than one validator, simply add the result of {@link ValidatorUtils#merge(Validator,Validator)}.
* @param box A combo box component
* @param validator a validator
*/
public final void add(JComboBox box, Validator<String> validator) {
assert EventQueue.isDispatchThread() : "Must be called on event thread";
this.addItem(ValidationListenerFactory.createValidationListener(box, ValidationStrategy.DEFAULT, ValidationUI.NO_OP, validator), false);
}
/**
* Add a JList to be validated using the passed validators
*
* <p> When a problem occurs, the created {@link ValidationListener} will
* use a {@link ValidationUI} created by this {@code ValidationGroup} to decorate
* the component.
*
* <p> <b>Note:</b> All methods in this class must be called from
* the AWT Event Dispatch thread, or assertion errors will be
* thrown. Manipulating components on other threads is not safe.
*
* @param list A JList component
* @param validators One or more Validators
*/
public final void add(JList list, Validator<Integer[]>... validators) {
assert EventQueue.isDispatchThread() : "Must be called on event thread";
this.addItem(ValidationListenerFactory.createValidationListener(list, ValidationStrategy.DEFAULT, this.getComponentDecorationFactory().decorationFor(list), ValidatorUtils.merge(validators)), false);
}
/**
* Add a JList to be validated using the passed validator.
*
* <p> When a problem occurs, the created {@link ValidationListener} will
* use a {@link ValidationUI} created by this {@code ValidationGroup} to decorate
* the component.
*
* <p> <b>Note:</b> All methods in this class must be called from
* the AWT Event Dispatch thread, or assertion errors will be
* thrown. Manipulating components on other threads is not safe.
* <p>Unlike {@link #add(JList,Validator...)}, calling this method does not trigger warnings under {@code -Xlint:unchecked}.
* If you wish to add more than one validator, simply add the result of {@link ValidatorUtils#merge(Validator,Validator)}.
* @param list A JList component
* @param validator a validator
*/
public final void add(JList list, Validator<Integer[]> validator) {
assert EventQueue.isDispatchThread() : "Must be called on event thread";
this.addItem(ValidationListenerFactory.createValidationListener(list, ValidationStrategy.DEFAULT, this.getComponentDecorationFactory().decorationFor(list), validator), false);
}
/**
* Add a validator of button models - typically to see if any are selected.
*
* <p> <b>Note:</b> All methods in this class must be called from
* the AWT Event Dispatch thread, or assertion errors will be
* thrown. Manipulating components on other threads is not safe.
*
* @param buttons The buttons
* @param validators A number of Validators
*/
public final void add(final AbstractButton[] buttons, Validator<Integer[]>... validators) {
assert EventQueue.isDispatchThread() : "Must be called on event thread";
this.addItem(ValidationListenerFactory.createValidationListener(buttons, ValidationStrategy.DEFAULT, ValidationUI.NO_OP, ValidatorUtils.merge(validators)), false);
}
/**
* Add a validator of button models - typically to see if any are selected.
*
* <p> <b>Note:</b> All methods in this class must be called from
* the AWT Event Dispatch thread, or assertion errors will be
* thrown. Manipulating components on other threads is not safe.
* <p>Unlike {@link #add(AbstractButton[],Validator...)}, calling this method does not trigger warnings under {@code -Xlint:unchecked}.
* If you wish to add more than one validator, simply add the result of {@link ValidatorUtils#merge(Validator,Validator)}.
* @param buttons The buttons
* @param validator a validator
*/
public final void add(final AbstractButton[] buttons, Validator<Integer[]> validator) {
assert EventQueue.isDispatchThread() : "Must be called on event thread";
this.addItem(ValidationListenerFactory.createValidationListener(buttons, ValidationStrategy.DEFAULT, ValidationUI.NO_OP, validator), false);
}
/**
* Create a label which will show the current problem if any, which
* can be added to a panel that uses validation
*
* @return A JLabel
*/
public final JComponent createProblemLabel() {
assert EventQueue.isDispatchThread() : "Must be called on event thread";
final MultilineLabel result = new MultilineLabel();
addUI(result.createUI());
return result;
}
/**
* Create a Popup which can be shown over a component to display what the
* problem is. The resulting popup will be word-wrapped and effort will be
* made to ensure it fits on-screen in the case of lengthy error messages.
*
* @param problem The problem to show
* @param target The target component
* @param relativeLocation The coordinates where the popup should appear,
* <i>in the coordinate space of the target component, not the screen</i>.
* @return A popup. Generally, use the returned popup once and get a new
* one if you want to show a message again. The returned popup will take
* care of hiding itself on component hierarchy changes.
*/
static Popup createProblemPopup (Problem problem, Component target, Point relativeLocation) {
return MultilineLabelUI.showPopup(problem, target, relativeLocation.x, relativeLocation.y);
}
/**
* Client property which can be set to provide a component's name
* for use in validation messages. If not set, the component's
* <code>getName()</code> method is used.
*/
private static final String CLIENT_PROP_NAME = "_name";
/**
* Get a string name for a component using the following strategy:
* <ol>
* <li>Check <code>jc.getClientProperty(CLIENT_PROP_NAME)</code></li>
* <li>If that returned null, call <code>jc.getName()</code>
* </ol>
* @param jc The component
* @return its name, if any, or null
*/
public static String nameForComponent(JComponent jc) {
String result = (String) jc.getClientProperty(CLIENT_PROP_NAME);
if (result == null) {
result = jc.getName();
}
return result;
}
public static void setComponentName(JComponent comp, String localizedName) {
comp.putClientProperty (CLIENT_PROP_NAME, localizedName);
}
}
|