123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- /*
- * Copyright (c) 2006-2013 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.addons.ui_swing.components.validating;
-
- import com.dmdirc.addons.ui_swing.UIUtilities;
- import com.dmdirc.ui.IconManager;
- import com.dmdirc.util.validators.ValidationResponse;
- import com.dmdirc.util.validators.Validator;
-
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.event.KeyListener;
- import java.awt.event.MouseListener;
-
- import javax.swing.Icon;
- import javax.swing.JComponent;
- import javax.swing.JLabel;
- import javax.swing.JTextField;
- import javax.swing.TransferHandler;
- import javax.swing.event.DocumentEvent;
- import javax.swing.event.DocumentListener;
- import javax.swing.text.BadLocationException;
- import javax.swing.text.Document;
-
- import net.miginfocom.swing.MigLayout;
-
- /**
- * Validating Text field.
- */
- public class ValidatingJTextField extends JComponent implements DocumentListener {
-
- /**
- * A version number for this class. It should be changed whenever the class
- * structure is changed (or anything else that would prevent serialized
- * objects being unserialized with the new class).
- */
- private static final long serialVersionUID = 1;
- /** TextField. */
- private final JTextField textField;
- /** Validator. */
- private Validator<String> validator;
- /** Error icon. */
- private final JLabel errorIcon;
-
- /**
- * Instantiates a new Validating text field.
- *
- * @param iconManager Icon manager
- * @param validator Validator instance
- */
- public ValidatingJTextField(final IconManager iconManager,
- final Validator<String> validator) {
- this(iconManager, new JTextField(), validator);
- }
- /**
- * Instantiates a new Validating text field.
- *
- * @param iconManager Icon manager
- * @param textField JTextField to wrap
- * @param validator Validator instance
- */
- public ValidatingJTextField(final IconManager iconManager,
- final JTextField textField, final Validator<String> validator) {
- this(iconManager.getIcon("input-error"), textField, validator);
- }
-
- /**
- * Instantiates a new Validating text field.
- *
- * @param icon Icon to show on error
- * @param textField JTextField to wrap
- * @param validator Validator instance
- */
- public ValidatingJTextField(final Icon icon,
- final JTextField textField, final Validator<String> validator) {
- super();
- this.textField = textField;
- this.validator = validator;
- errorIcon = new JLabel(icon);
-
- setLayout(new MigLayout("fill, ins 0, hidemode 3, gap 0"));
- add(textField, "grow, pushx");
- add(errorIcon);
-
- checkError();
-
- textField.getDocument().addDocumentListener(this);
- }
-
- /**
- * Updates the validator for this textfield.
- *
- * @param validator new validator
- */
- public void setValidator(final Validator<String> validator) {
- this.validator = validator;
- checkError();
- }
-
- /**
- * Checks the text for errors and sets the error state accordingly.
- */
- public void checkError() {
- if (textField.isEnabled()) {
- final ValidationResponse vr =
- validator.validate(textField.getText());
- errorIcon.setToolTipText(vr.getFailureReason());
- firePropertyChange("validationResult", !errorIcon.isVisible(), !vr.isFailure());
- errorIcon.setVisible(vr.isFailure());
- } else {
- firePropertyChange("validationResult", !errorIcon.isVisible(), true);
- errorIcon.setVisible(false);
- }
- }
-
- /**
- * Checks if the text validates.
- *
- * @see com.dmdirc.ui.swing.components.validating.Validator#validate(Object)
- *
- * @return true iif the text validates
- */
- public boolean validateText() {
- if (textField.isEnabled()) {
- return !validator.validate(getText()).isFailure();
- } else {
- return true;
- }
- }
-
- /** {@inheritDoc} */
- @Override
- public void changedUpdate(final DocumentEvent e) {
- checkError();
- }
-
- /** {@inheritDoc} */
- @Override
- public void insertUpdate(final DocumentEvent e) {
- checkError();
- }
-
- /** {@inheritDoc} */
- @Override
- public void removeUpdate(final DocumentEvent e) {
- checkError();
- }
-
- /** {@inheritDoc} */
- @Override
- public void setToolTipText(final String text) {
- textField.setToolTipText(text);
- }
-
- /** {@inheritDoc} */
- @Override
- public void setEnabled(final boolean enabled) {
- textField.setEnabled(enabled);
- checkError();
- }
-
- /** {@inheritDoc} */
- @Override
- public void requestFocus() {
- textField.requestFocus();
- }
-
- /** {@inheritDoc} */
- @Override
- public boolean requestFocusInWindow() {
- return textField.requestFocusInWindow();
- }
-
- /**
- * Sets the text in the textfield.
- *
- * @see javax.swing.JTextField#setText(String)
- *
- * @param t Text to set
- */
- public void setText(final String t) {
- textField.setText(t);
- }
-
- /**
- * Sets the selection start.
- *
- * @see javax.swing.JTextField#setSelectionStart(int)
- *
- * @param selectionStart Start of the selection
- */
- public void setSelectionStart(final int selectionStart) {
- textField.setSelectionStart(selectionStart);
- }
-
- /**
- * Sets the selection end.
- *
- * @see javax.swing.JTextField#setSelectionEnd(int)
- *
- * @param selectionEnd End of the selection
- */
- public void setSelectionEnd(final int selectionEnd) {
- textField.setSelectionEnd(selectionEnd);
- }
-
- /**
- * Sets whether the component is editable.
- *
- * @see javax.swing.JTextField#setEditable(boolean)
- *
- * @param b editable state for the component
- */
- public void setEditable(final boolean b) {
- textField.setEditable(b);
- }
-
- /**
- * Selects all text in the textfield.
- *
- * @see javax.swing.JTextField#selectAll()
- */
- public void selectAll() {
- textField.selectAll();
- }
-
- /**
- * Selects the specified text in the textfield.
- *
- * @see javax.swing.JTextField#select(int, int)
- *
- * @param selectionStart Selection start
- * @param selectionEnd Selection end
- */
- public void select(final int selectionStart, final int selectionEnd) {
- textField.select(selectionStart, selectionEnd);
- }
-
- /**
- * Replaces the textfields selection with the specified content.
- *
- * @see javax.swing.JTextField#replaceSelection(String)
- *
- * @param content Text to replace selection with
- */
- public void replaceSelection(final String content) {
- textField.replaceSelection(content);
- }
-
- /**
- * Paste's the system clipboard into the textfield.
- *
- * @see javax.swing.JTextField#paste()
- */
- public void paste() {
- textField.paste();
- }
-
- /**
- * Checks if the textfield is editable.
- *
- * @see javax.swing.JTextField#isEditable()
- *
- * @return true iif the textfield is editable
- */
- public boolean isEditable() {
- return textField.isEditable();
- }
-
- /**
- * Returns the text in the textfield.
- *
- * @see javax.swing.JTextField#getText()
- *
- * @return Textfield content
- */
- public String getText() {
- return textField.getText();
- }
-
- /**
- * Returns the specified section of text in the textfield.
- *
- * @see javax.swing.JTextField#getText(int, int)
- *
- * @param offs Start offset
- * @param len section length
- *
- * @return Specified textfield content
- *
- * @throws javax.swing.text.BadLocationException
- */
- public String getText(final int offs, final int len) throws BadLocationException {
- return textField.getText(offs, len);
- }
-
- /**
- * Returns the start of the selection in the textfield.
- *
- * @see javax.swing.JTextField#getSelectionStart()
- *
- * @return Selection start
- */
- public int getSelectionStart() {
- return textField.getSelectionStart();
- }
-
- /**
- * Returns the end of the textfield selection.
- *
- * @see javax.swing.JTextField#getSelectionEnd()
- *
- * @return Selection end
- */
- public int getSelectionEnd() {
- return textField.getSelectionEnd();
- }
-
- /**
- * Returns the selected text in the textfield.
- *
- * @see javax.swing.JTextField#getSelectedText()
- *
- * @return Selected text
- */
- public String getSelectedText() {
- return textField.getSelectedText();
- }
-
- /**
- * Returns the textfield's document.
- *
- * @see javax.swing.JTextField#getDocument()
- *
- * @return Textfield's document
- */
- public Document getDocument() {
- return textField.getDocument();
- }
-
- /**
- * Cuts the selected text from the textfield into the clipboard.
- *
- * @see javax.swing.JTextField#cut()
- */
- public void cut() {
- textField.cut();
- }
-
- /**
- * Copies the selected text from the textfield into the clipboard.
- *
- * @see javax.swing.JTextField#copy()
- */
- public void copy() {
- textField.copy();
- }
-
- /**
- * Returns the font for the textfield.
- *
- * @see javax.swing.JTextField#copy()
- */
- @Override
- public Font getFont() {
- return textField.getFont();
- }
-
- /** {@inheritDoc} */
- @Override
- public synchronized void addKeyListener(final KeyListener l) {
- textField.addKeyListener(l);
- }
-
- /** {@inheritDoc} */
- @Override
- public synchronized void removeKeyListener(final KeyListener l) {
- textField.removeKeyListener(l);
- }
-
- /** {@inheritDoc} */
- @Override
- public synchronized void addMouseListener(final MouseListener l) {
- textField.addMouseListener(l);
- }
-
- /** {@inheritDoc} */
- @Override
- public synchronized void removeMouseListener(final MouseListener l) {
- textField.removeMouseListener(l);
- }
-
- /**
- * Sets the drag enabled property on the textfield.
- *
- * @param enabled Enabled?
- */
- public void setDragEnabled(final boolean enabled) {
- textField.setDragEnabled(enabled);
- }
-
- /** {@inheritDoc} */
- @Override
- public void setTransferHandler(final TransferHandler newHandler) {
- textField.setTransferHandler(newHandler);
- }
-
- /**
- * Returns the validator used by this text field.
- *
- * @since 0.6.3m1
- *
- * @return This field's validator
- */
- public Validator<String> getValidator() {
- return validator;
- }
-
- /**
- * Returns the text field used by this validating text field.
- *
- * @since 0.6.3m1
- *
- * @return This field's text field
- */
- public JTextField getTextField() {
- return textField;
- }
-
- /**
- * Sets the caret colour to the specified coloour.
- *
- * @param optionColour Colour for the caret
- */
- public void setCaretColor(final Color optionColour) {
- UIUtilities.invokeLater(new Runnable() {
-
- /** {@inheritDoc} */
- @Override
- public void run() {
- textField.setCaretColor(optionColour);
- }
- });
- }
-
- /**
- * Sets the foreground colour to the specified coloour.
- *
- * @param optionColour Colour for the foreground
- */
- @Override
- public void setForeground(final Color optionColour) {
- UIUtilities.invokeLater(new Runnable() {
-
- /** {@inheritDoc} */
- @Override
- public void run() {
- textField.setForeground(optionColour);
- }
- });
- }
-
- /**
- * Sets the disabled text colour to the specified coloour.
- *
- * @param optionColour Colour for the disabled text
- */
- public void setDisabledTextColour(final Color optionColour) {
- UIUtilities.invokeLater(new Runnable() {
-
- /** {@inheritDoc} */
- @Override
- public void run() {
- textField.setDisabledTextColor(optionColour);
- }
- });
- }
-
- /**
- * Sets the background colour to the specified coloour.
- *
- * @param optionColour Colour for the caret
- */
- @Override
- public void setBackground(final Color optionColour) {
- UIUtilities.invokeLater(new Runnable() {
-
- /** {@inheritDoc} */
- @Override
- public void run() {
- textField.setBackground(optionColour);
- }
- });
- }
- }
|