File Source: ValidatingJTextField.java

         /* 
    P/P   *  Method: com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
     3   * 
     4   * Permission is hereby granted, free of charge, to any person obtaining a copy
     5   * of this software and associated documentation files (the "Software"), to deal
     6   * in the Software without restriction, including without limitation the rights
     7   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   * copies of the Software, and to permit persons to whom the Software is
     9   * furnished to do so, subject to the following conditions:
    10   * 
    11   * The above copyright notice and this permission notice shall be included in
    12   * all copies or substantial portions of the Software.
    13   * 
    14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   * SOFTWARE.
    21   */
    22  
    23  package com.dmdirc.addons.ui_swing.components.validating;
    24  
    25  import com.dmdirc.config.prefs.validator.Validator;
    26  import com.dmdirc.ui.IconManager;
    27  import com.dmdirc.config.prefs.validator.ValidationResponse;
    28  
    29  import java.awt.Font;
    30  import java.awt.event.KeyListener;
    31  
    32  import java.awt.event.MouseListener;
    33  import javax.swing.JComponent;
    34  import javax.swing.JLabel;
    35  import javax.swing.JTextField;
    36  import javax.swing.TransferHandler;
    37  import javax.swing.event.DocumentEvent;
    38  import javax.swing.event.DocumentListener;
    39  import javax.swing.text.BadLocationException;
    40  import javax.swing.text.Document;
    41  
    42  import net.miginfocom.swing.MigLayout;
    43  
    44  /**
    45   * Validating Text field.
    46   */
    47  public class ValidatingJTextField extends JComponent implements DocumentListener {
    48  
    49      /**
    50       * A version number for this class. It should be changed whenever the class
    51       * structure is changed (or anything else that would prevent serialized
    52       * objects being unserialized with the new class).
    53       */
    54      private static final long serialVersionUID = 1;
    55      /** TextField. */
    56      private final JTextField textField;
    57      /** Validator. */
    58      private Validator<String> validator;
    59      /** Error icon. */
    60      private final JLabel errorIcon;
    61  
    62      /**
    63       * Instantiates a new Validating text field.
    64       * 
    65       * @param validator Validator instance
    66       */
    67      public ValidatingJTextField(final Validator<String> validator) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField(Validator)
                  * 
                  *  Preconditions:
                  *    (soft) validator != null
                  * 
                  *  Postconditions:
                  *    this.errorIcon == &amp;new JLabel(ValidatingJTextField#1)
                  *    this.textField == &amp;new JTextField(ValidatingJTextField#1)
                  *    this.validator == validator
                  *    this.validator != null
                  *    new JLabel(ValidatingJTextField#1) num objects == 1
                  *    new JTextField(ValidatingJTextField#1) num objects == 1
                  */
    68          this(new JTextField(), validator);
    69      }
    70  
    71      /**
    72       * Instantiates a new Validating text field.
    73       * 
    74       * @param textField JTextField to wrap
    75       * @param validator Validator instance
    76       */
    77      public ValidatingJTextField(final JTextField textField,
    78              final Validator<String> validator) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField(JTextField, Validator)
                  * 
                  *  Preconditions:
                  *    textField != null
                  *    (soft) validator != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.ui.IconManager:getIconManager(...)@82 != null
                  *    javax.swing.JTextField:getDocument(...)@91 != null
                  * 
                  *  Postconditions:
                  *    this.errorIcon == &amp;new JLabel(ValidatingJTextField#1)
                  *    this.textField == textField
                  *    this.textField != null
                  *    this.validator == validator
                  *    this.validator != null
                  *    new JLabel(ValidatingJTextField#1) num objects == 1
                  */
    79          super();
    80          this.textField = textField;
    81          this.validator = validator;
    82          errorIcon =
    83                  new JLabel(IconManager.getIconManager().getIcon("input-error"));
    84  
    85          setLayout(new MigLayout("fill, ins 0, hidemode 3, gap 0"));
    86          add(textField, "grow, pushx");
    87          add(errorIcon);
    88  
    89          checkError();
    90  
    91          textField.getDocument().addDocumentListener(this);
    92      }
    93  
    94      /**
    95       * Checks the text for errors and sets the error state accordingly.
    96       */
    97      public void checkError() {
                 /* 
    P/P           *  Method: void checkError()
                  * 
                  *  Preconditions:
                  *    this.errorIcon != null
                  *    this.textField != null
                  *    (soft) this.validator != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.config.prefs.validator.Validator:validate(...)@99 != null
                  * 
                  *  Test Vectors:
                  *    javax.swing.JTextField:isEnabled(...)@98: {0}, {1}
                  */
    98          if (textField.isEnabled()) {
    99              final ValidationResponse vr =
   100                      validator.validate(textField.getText());
   101              errorIcon.setToolTipText(vr.getFailureReason());
   102              firePropertyChange("validationResult", !errorIcon.isVisible(), !vr.isFailure());
   103              errorIcon.setVisible(vr.isFailure());
   104          } else {
   105              firePropertyChange("validationResult", !errorIcon.isVisible(), true);
   106              errorIcon.setVisible(false);
   107          }
   108      }
   109  
   110      /**
   111       * Checks if the text validates.
   112       * 
   113       * @see com.dmdirc.ui.swing.components.validating.Validator#validate(Object)
   114       * 
   115       * @return true iif the text validates
   116       */
   117      public boolean validateText() {
                 /* 
    P/P           *  Method: bool validateText()
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  *    (soft) this.validator != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.config.prefs.validator.Validator:validate(...)@119 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    javax.swing.JTextField:isEnabled(...)@118: {0}, {1}
                  */
   118          if (textField.isEnabled()) {
   119              return !validator.validate(getText()).isFailure();
   120          } else {
   121              return true;
   122          }
   123      }
   124  
   125      /** {@inheritDoc} */
   126      @Override
   127      public void changedUpdate(final DocumentEvent e) {
                 /* 
    P/P           *  Method: void changedUpdate(DocumentEvent)
                  * 
                  *  Preconditions:
                  *    this.errorIcon != null
                  *    this.textField != null
                  *    (soft) this.validator != null
                  */
   128          checkError();
   129      }
   130  
   131      /** {@inheritDoc} */
   132      @Override
   133      public void insertUpdate(final DocumentEvent e) {
                 /* 
    P/P           *  Method: void insertUpdate(DocumentEvent)
                  * 
                  *  Preconditions:
                  *    this.errorIcon != null
                  *    this.textField != null
                  *    (soft) this.validator != null
                  */
   134          checkError();
   135      }
   136  
   137      /** {@inheritDoc} */
   138      @Override
   139      public void removeUpdate(final DocumentEvent e) {
                 /* 
    P/P           *  Method: void removeUpdate(DocumentEvent)
                  * 
                  *  Preconditions:
                  *    this.errorIcon != null
                  *    this.textField != null
                  *    (soft) this.validator != null
                  */
   140          checkError();
   141      }
   142  
   143      /** {@inheritDoc} */
   144      @Override
   145      public void setToolTipText(final String text) {
                 /* 
    P/P           *  Method: void setToolTipText(String)
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  */
   146          textField.setToolTipText(text);
   147      }
   148  
   149      /** {@inheritDoc} */
   150      @Override
   151      public void setEnabled(final boolean enabled) {
                 /* 
    P/P           *  Method: void setEnabled(bool)
                  * 
                  *  Preconditions:
                  *    this.errorIcon != null
                  *    this.textField != null
                  *    (soft) this.validator != null
                  */
   152          textField.setEnabled(enabled);
   153          checkError();
   154      }
   155      
   156      /** {@inheritDoc} */
   157      @Override
   158      public void requestFocus() {
                 /* 
    P/P           *  Method: void requestFocus()
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  */
   159          textField.requestFocus();
   160      }
   161  
   162      /** {@inheritDoc} */
   163      @Override
   164      public boolean requestFocusInWindow() {
                 /* 
    P/P           *  Method: bool requestFocusInWindow()
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   165          return textField.requestFocusInWindow();
   166      }
   167  
   168      /**
   169       * Sets the text in the textfield.
   170       * 
   171       * @see javax.swing.JTextField#setText(String)
   172       * 
   173       * @param t Text to set
   174       */
   175      public void setText(final String t) {
                 /* 
    P/P           *  Method: void setText(String)
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  */
   176          textField.setText(t);
   177      }
   178  
   179      /**
   180       * Sets the selection start.
   181       * 
   182       * @see javax.swing.JTextField#setSelectionStart(int)
   183       * 
   184       * @param selectionStart Start of the selection
   185       */
   186      public void setSelectionStart(final int selectionStart) {
                 /* 
    P/P           *  Method: void setSelectionStart(int)
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  */
   187          textField.setSelectionStart(selectionStart);
   188      }
   189  
   190      /**
   191       * Sets the selection end.
   192       * 
   193       * @see javax.swing.JTextField#setSelectionEnd(int)
   194       * 
   195       * @param selectionEnd End of the selection
   196       */
   197      public void setSelectionEnd(final int selectionEnd) {
                 /* 
    P/P           *  Method: void setSelectionEnd(int)
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  */
   198          textField.setSelectionEnd(selectionEnd);
   199      }
   200  
   201      /**
   202       * Sets whether the component is editable.
   203       * 
   204       * @see javax.swing.JTextField#setEditable(boolean)
   205       * 
   206       * @param b editable state for the component
   207       */
   208      public void setEditable(final boolean b) {
                 /* 
    P/P           *  Method: void setEditable(bool)
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  */
   209          textField.setEditable(b);
   210      }
   211  
   212      /**
   213       * Selects all text in the textfield.
   214       * 
   215       * @see javax.swing.JTextField#selectAll()
   216       */
   217      public void selectAll() {
                 /* 
    P/P           *  Method: void selectAll()
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  */
   218          textField.selectAll();
   219      }
   220  
   221      /**
   222       * Selects the specified text in the textfield.
   223       * 
   224       * @see javax.swing.JTextField#select(int, int)
   225       * 
   226       * @param selectionStart Selection start
   227       * @param selectionEnd Selection end
   228       */
   229      public void select(final int selectionStart, final int selectionEnd) {
                 /* 
    P/P           *  Method: void select(int, int)
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  */
   230          textField.select(selectionStart, selectionEnd);
   231      }
   232  
   233      /**
   234       * Replaces the textfields selection with the specified content.
   235       * 
   236       * @see javax.swing.JTextField#replaceSelection(String)
   237       * 
   238       * @param content Text to replace selection with
   239       */
   240      public void replaceSelection(final String content) {
                 /* 
    P/P           *  Method: void replaceSelection(String)
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  */
   241          textField.replaceSelection(content);
   242      }
   243  
   244      /**
   245       * Paste's the system clipboard into the textfield.
   246       * 
   247       * @see javax.swing.JTextField#paste()
   248       */
   249      public void paste() {
                 /* 
    P/P           *  Method: void paste()
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  */
   250          textField.paste();
   251      }
   252  
   253      /**
   254       * Checks if the textfield is editable.
   255       * 
   256       * @see javax.swing.JTextField#isEditable()
   257       * 
   258       * @return true iif the textfield is editable
   259       */
   260      public boolean isEditable() {
                 /* 
    P/P           *  Method: bool isEditable()
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   261          return textField.isEditable();
   262      }
   263  
   264      /**
   265       * Returns the text in the textfield.
   266       * 
   267       * @see javax.swing.JTextField#getText()
   268       * 
   269       * @return Textfield content
   270       */
   271      public String getText() {
                 /* 
    P/P           *  Method: String getText()
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   272          return textField.getText();
   273      }
   274  
   275      /**
   276       * Returns the specified section of text in the textfield.
   277       * 
   278       * @see javax.swing.JTextField#getText(int, int)
   279       * 
   280       * @param offs Start offset
   281       * @param len section length
   282       * 
   283       * @return Specified textfield content
   284       * 
   285       * @throws javax.swing.text.BadLocationException
   286       */
   287      public String getText(final int offs, final int len) throws BadLocationException {
                 /* 
    P/P           *  Method: String getText(int, int)
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   288          return textField.getText(offs, len);
   289      }
   290  
   291      /**
   292       * Returns the start of the selection in the textfield.
   293       * 
   294       * @see javax.swing.JTextField#getSelectionStart()
   295       * 
   296       * @return Selection start
   297       */
   298      public int getSelectionStart() {
                 /* 
    P/P           *  Method: int getSelectionStart()
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   299          return textField.getSelectionStart();
   300      }
   301  
   302      /**
   303       * Returns the end of the textfield selection.
   304       * 
   305       * @see javax.swing.JTextField#getSelectionEnd()
   306       * 
   307       * @return Selection end
   308       */
   309      public int getSelectionEnd() {
                 /* 
    P/P           *  Method: int getSelectionEnd()
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   310          return textField.getSelectionEnd();
   311      }
   312  
   313      /**
   314       * Returns the selected text in the textfield.
   315       * 
   316       * @see javax.swing.JTextField#getSelectedText()
   317       * 
   318       * @return Selected text
   319       */
   320      public String getSelectedText() {
                 /* 
    P/P           *  Method: String getSelectedText()
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   321          return textField.getSelectedText();
   322      }
   323  
   324      /**
   325       * Returns the textfield's document.
   326       * 
   327       * @see javax.swing.JTextField#getDocument()
   328       * 
   329       * @return Textfield's document
   330       */
   331      public Document getDocument() {
                 /* 
    P/P           *  Method: Document getDocument()
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   332          return textField.getDocument();
   333      }
   334  
   335      /**
   336       * Cuts the selected text from the textfield into the clipboard.
   337       * 
   338       * @see javax.swing.JTextField#cut()
   339       */
   340      public void cut() {
                 /* 
    P/P           *  Method: void cut()
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  */
   341          textField.cut();
   342      }
   343  
   344      /**
   345       * Copies the selected text from the textfield into the clipboard.
   346       * 
   347       * @see javax.swing.JTextField#copy()
   348       */
   349      public void copy() {
                 /* 
    P/P           *  Method: void copy()
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  */
   350          textField.copy();
   351      }
   352  
   353      /**
   354       * Returns the font for the textfield.
   355       * 
   356       * @see javax.swing.JTextField#copy()
   357       */
   358      @Override
   359      public Font getFont() {
                 /* 
    P/P           *  Method: Font getFont()
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   360          return textField.getFont();
   361      }
   362  
   363      /** {@inheritDoc} */
   364      @Override
   365      public synchronized void addKeyListener(final KeyListener l) {
                 /* 
    P/P           *  Method: void addKeyListener(KeyListener)
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  */
   366          textField.addKeyListener(l);
   367      }
   368  
   369      /** {@inheritDoc} */
   370      @Override
   371      public synchronized void removeKeyListener(final KeyListener l) {
                 /* 
    P/P           *  Method: void removeKeyListener(KeyListener)
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  */
   372          textField.removeKeyListener(l);
   373      }
   374  
   375      /** {@inheritDoc} */
   376      @Override
   377      public synchronized void addMouseListener(final MouseListener l) {
                 /* 
    P/P           *  Method: void addMouseListener(MouseListener)
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  */
   378          textField.addMouseListener(l);
   379      }
   380  
   381      /** {@inheritDoc} */
   382      @Override
   383      public synchronized void removeMouseListener(final MouseListener l) {
                 /* 
    P/P           *  Method: void removeMouseListener(MouseListener)
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  */
   384          textField.removeMouseListener(l);
   385      }
   386      
   387      /**
   388       * Sets the drag enabled property on the textfield.
   389       * 
   390       * @param enabled Enabled?
   391       */
   392      public void setDragEnabled(final boolean enabled) {
                 /* 
    P/P           *  Method: void setDragEnabled(bool)
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  */
   393          textField.setDragEnabled(enabled);
   394      }
   395      
   396      /** {@inheritDoc} */
   397      @Override
   398      public void setTransferHandler(final TransferHandler newHandler) {
                 /* 
    P/P           *  Method: void setTransferHandler(TransferHandler)
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  */
   399          textField.setTransferHandler(newHandler);
   400      }
   401  
   402      /**
   403       * Returns the validator used by this text field.
   404       * 
   405       * @since 0.6.3m1
   406       * 
   407       * @return This field's validator
   408       */
   409      public Validator<String> getValidator() {
                 /* 
    P/P           *  Method: Validator getValidator()
                  * 
                  *  Preconditions:
                  *    init'ed(this.validator)
                  * 
                  *  Postconditions:
                  *    return_value == this.validator
                  *    init'ed(return_value)
                  */
   410          return validator;
   411      }
   412  
   413      /**
   414       * Returns the text field used by this validating text field.
   415       *
   416       * @since 0.6.3m1
   417       *
   418       * @return This field's text field
   419       */
   420      public JTextField getTextField() {
                 /* 
    P/P           *  Method: JTextField getTextField()
                  * 
                  *  Postconditions:
                  *    return_value == this.textField
                  *    init'ed(return_value)
                  */
   421          return textField;
   422      }
   423  }








SofCheck Inspector Build Version : 2.17854
ValidatingJTextField.java 2009-Jun-25 01:54:24
ValidatingJTextField.class 2009-Sep-02 17:04:15