File Source: StandardInputDialog.java

         /* 
    P/P   *  Method: com.dmdirc.addons.ui_swing.components.StandardInputDialog__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;
    24  
    25  import com.dmdirc.config.prefs.validator.ValidationResponse;
    26  import com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField;
    27  import com.dmdirc.config.prefs.validator.Validator;
    28  import com.dmdirc.addons.ui_swing.UIUtilities;
    29  import com.dmdirc.addons.ui_swing.components.text.TextLabel;
    30  
    31  import java.awt.Component;
    32  import java.awt.Window;
    33  import java.awt.event.ActionEvent;
    34  import java.awt.event.ActionListener;
    35  import java.awt.event.KeyEvent;
    36  import java.awt.event.WindowAdapter;
    37  import java.awt.event.WindowEvent;
    38  
    39  import javax.swing.JButton;
    40  import javax.swing.JComponent;
    41  import javax.swing.KeyStroke;
    42  import javax.swing.event.DocumentEvent;
    43  import javax.swing.event.DocumentListener;
    44  
    45  import net.miginfocom.swing.MigLayout;
    46  
    47  /**
    48   * Standard input dialog.
    49   */
         /* 
    P/P   *  Method: void access$100(StandardInputDialog)
          * 
          *  Preconditions:
          *    x0 != null
          *    x0.okButton != null
          *    x0.textField != null
          *    x0.validator != null
          */
    50  public abstract class StandardInputDialog extends StandardDialog {
    51  
    52      /** Validator. */
    53      private Validator<String> validator;
    54      /** Text field. */
    55      private ValidatingJTextField textField;
    56      /** Blurb label. */
    57      private TextLabel blurb;
    58      /** Message. */
    59      private String message;
    60  
    61      /**
    62       * Instantiates a new standard input dialog.
    63       * 
    64       * @param owner Dialog owner
    65       * @param modal modality type
    66       * @param title Dialog title
    67       * @param message Dialog message
    68       */
    69      public StandardInputDialog(Window owner, ModalityType modal,
    70              final String title,
    71              final String message) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.components.StandardInputDialog(Window, Dialog$ModalityType, String, String)
                  * 
                  *  Postconditions:
                  *    this.blurb == &amp;new TextLabel(initComponents#4)
                  *    this.cancelButton == One-of{&amp;new JButton(initComponents#2), &amp;new JButton(initComponents#1)}
                  *    this.cancelButton in Addr_Set{&amp;new JButton(initComponents#1),&amp;new JButton(initComponents#2)}
                  *    this.message == message
                  *    init'ed(this.message)
                  *    this.okButton == One-of{&amp;new JButton(initComponents#1), &amp;new JButton(initComponents#2)}
                  *    this.okButton in Addr_Set{&amp;new JButton(initComponents#1),&amp;new JButton(initComponents#2)}
                  *    this.textField == &amp;new ValidatingJTextField(initComponents#3)
                  *    this.validator == &amp;new StandardInputDialog$1(StandardInputDialog#1)
                  *    new JButton(initComponents#1) num objects == 1
                  *    ...
                  */
    72          this(owner, modal, title, message, new Validator<String>() {
    73  
    74              /** {@inheritDoc} */
    75              @Override
    76              public ValidationResponse validate(final String object) {
                         /* 
    P/P                   *  Method: ValidationResponse validate(String)
                          * 
                          *  Postconditions:
                          *    return_value == &amp;new ValidationResponse(validate#1)
                          *    new ValidationResponse(validate#1) num objects == 1
                          */
    77                  return new ValidationResponse();
    78              }
    79          });
    80      }
    81  
    82      /**
    83       * Instantiates a new standard input dialog.
    84       * 
    85       * @param owner Dialog owner
    86       * @param modal modality type
    87       * @param validator Textfield validator
    88       * @param title Dialog title
    89       * @param message Dialog message
    90       */
    91      public StandardInputDialog(Window owner, ModalityType modal,
    92              final String title, final String message,
    93              final Validator<String> validator) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.components.StandardInputDialog(Window, Dialog$ModalityType, String, String, Validator)
                  * 
                  *  Preconditions:
                  *    validator != null
                  * 
                  *  Postconditions:
                  *    this.blurb == &amp;new TextLabel(initComponents#4)
                  *    this.cancelButton == One-of{&amp;new JButton(initComponents#2), &amp;new JButton(initComponents#1)}
                  *    this.cancelButton in Addr_Set{&amp;new JButton(initComponents#1),&amp;new JButton(initComponents#2)}
                  *    this.message == message
                  *    init'ed(this.message)
                  *    this.okButton == One-of{&amp;new JButton(initComponents#1), &amp;new JButton(initComponents#2)}
                  *    this.okButton in Addr_Set{&amp;new JButton(initComponents#1),&amp;new JButton(initComponents#2)}
                  *    this.textField == &amp;new ValidatingJTextField(initComponents#3)
                  *    this.validator == validator
                  *    this.validator != null
                  *    ...
                  */
    94          super(owner, modal);
    95  
    96          this.validator = validator;
    97          this.message = message;
    98  
    99          setTitle(title);
   100          setDefaultCloseOperation(StandardInputDialog.DISPOSE_ON_CLOSE);
   101  
   102          initComponents();
   103          addListeners();
   104          layoutComponents();
   105      }
   106  
   107      /**
   108       * Called when the dialog's OK button is clicked.
   109       * 
   110       * @return whether the dialog can close
   111       */
   112      public abstract boolean save();
   113  
   114      /**
   115       * Called when the dialog's cancel button is clicked, or otherwise closed.
   116       */
   117      public abstract void cancelled();
   118  
   119      /**
   120       * Initialises the components.
   121       */
   122      private final void initComponents() {
                 /* 
    P/P           *  Method: void initComponents()
                  * 
                  *  Preconditions:
                  *    init'ed(this.message)
                  *    this.validator != null
                  * 
                  *  Postconditions:
                  *    this.blurb == &amp;new TextLabel(initComponents#4)
                  *    this.cancelButton == One-of{&amp;new JButton(initComponents#2), &amp;new JButton(initComponents#1)}
                  *    this.cancelButton in Addr_Set{&amp;new JButton(initComponents#1),&amp;new JButton(initComponents#2)}
                  *    this.okButton == One-of{&amp;new JButton(initComponents#1), &amp;new JButton(initComponents#2)}
                  *    this.okButton in Addr_Set{&amp;new JButton(initComponents#1),&amp;new JButton(initComponents#2)}
                  *    this.textField == &amp;new ValidatingJTextField(initComponents#3)
                  *    new JButton(initComponents#1) num objects == 1
                  *    new JButton(initComponents#2) num objects == 1
                  *    new TextLabel(initComponents#4) num objects == 1
                  *    new ValidatingJTextField(initComponents#3) num objects == 1
                  */
   123          orderButtons(new JButton(), new JButton());
   124          textField = new ValidatingJTextField(validator);
   125          blurb = new TextLabel(message);
   126          validateText();
   127      }
   128  
   129      /**
   130       * Adds the listeners
   131       */
   132      private final void addListeners() {
                 /* 
    P/P           *  Method: void addListeners()
                  * 
                  *  Preconditions:
                  *    this.cancelButton != null
                  *    this.okButton != null
                  *    this.textField != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.addons.ui_swing.components.StandardInputDialog:getRootPane(...)@197 != null
                  *    com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField:getDocument(...)@167 != null
                  */
   133          getOkButton().addActionListener(new ActionListener() {
   134  
   135              /** {@inheritDoc} */
   136              @Override
   137              public void actionPerformed(ActionEvent e) {
                         /* 
    P/P                   *  Method: void actionPerformed(ActionEvent)
                          * 
                          *  Preconditions:
                          *    this.textField != null
                          *    this.controller != null
                          *    this.controller.mainFrameCreated != null
                          *    (soft) this.controller.me != null
                          */
   138                  if (save()) {
   139                      dispose();
   140                  }
   141              }
   142          });
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.components.StandardInputDialog$3(StandardInputDialog)
                  */
   143          getCancelButton().addActionListener(new ActionListener() {
   144  
   145              /** {@inheritDoc} */
   146              @Override
   147              public void actionPerformed(ActionEvent e) {
                         /* 
    P/P                   *  Method: void actionPerformed(ActionEvent)
                          */
   148                  cancelled();
   149                  dispose();
   150              }
   151          });
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.components.StandardInputDialog$4(StandardInputDialog)
                  */
   152          addWindowListener(new WindowAdapter() {
   153  
   154              /** {@inheritDoc} */
   155              @Override
   156              public void windowOpened(WindowEvent e) {
                         /* 
    P/P                   *  Method: void windowOpened(WindowEvent)
                          * 
                          *  Preconditions:
                          *    this.textField != null
                          */
   157                  textField.requestFocusInWindow();
   158              }
   159  
   160              /** {@inheritDoc} */
   161              @Override
   162              public void windowClosed(WindowEvent e) {
                         /* 
    P/P                   *  Method: void windowClosed(WindowEvent)
                          */
   163                  cancelled();
   164              //dispose();
   165              }
   166          });
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.components.StandardInputDialog$5(StandardInputDialog)
                  */
   167          textField.getDocument().addDocumentListener(new DocumentListener() {
   168  
   169              /** {@inheritDoc} */
   170              @Override
   171              public void insertUpdate(DocumentEvent e) {
                         /* 
    P/P                   *  Method: void insertUpdate(DocumentEvent)
                          * 
                          *  Preconditions:
                          *    this.okButton != null
                          *    this.textField != null
                          *    this.validator != null
                          */
   172                  validateText();
   173              }
   174  
   175              /** {@inheritDoc} */
   176              @Override
   177              public void removeUpdate(DocumentEvent e) {
                         /* 
    P/P                   *  Method: void removeUpdate(DocumentEvent)
                          * 
                          *  Preconditions:
                          *    this.okButton != null
                          *    this.textField != null
                          *    this.validator != null
                          */
   178                  validateText();
   179              }
   180  
   181              /** {@inheritDoc} */
   182              @Override
   183              public void changedUpdate(DocumentEvent e) {
   184              //Ignore
                     /* 
    P/P               *  Method: void changedUpdate(DocumentEvent)
                      */
   185              }
   186          });
   187  
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.components.StandardInputDialog$6(StandardInputDialog)
                  */
   188          final ActionListener enterListener = new ActionListener() {
   189  
   190              /** {@inheritDoc} */
   191              @Override
   192              public void actionPerformed(final ActionEvent actionEvent) {
                         /* 
    P/P                   *  Method: void actionPerformed(ActionEvent)
                          * 
                          *  Presumptions:
                          *    com.dmdirc.addons.ui_swing.components.StandardInputDialog:getOkButton(...)@193 != null
                          */
   193                  getOkButton().doClick();
   194              }
   195          };
   196          final KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
   197          getRootPane().registerKeyboardAction(enterListener, enter,
   198                  JComponent.WHEN_IN_FOCUSED_WINDOW);
   199      }
   200  
   201      /**
   202       * Validates the change.
   203       */
   204      private void validateText() {
                 /* 
    P/P           *  Method: void validateText()
                  * 
                  *  Preconditions:
                  *    this.okButton != null
                  *    this.textField != null
                  *    this.validator != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.config.prefs.validator.Validator:validate(...)@205 != null
                  */
   205          getOkButton().setEnabled(!validator.validate(getText()).isFailure());
   206      }
   207  
   208      /**
   209       * Lays out the components.
   210       */
   211      private final void layoutComponents() {
                 /* 
    P/P           *  Method: void layoutComponents()
                  * 
                  *  Preconditions:
                  *    init'ed(this.blurb)
                  *    init'ed(this.textField)
                  *    (soft) init'ed(this.cancelButton)
                  *    (soft) init'ed(this.okButton)
                  */
   212          setLayout(new MigLayout("fill, wrap 1"));
   213  
   214          add(blurb, "growx");
   215          add(textField, "growx");
   216          add(getLeftButton(), "split 2, right");
   217          add(getRightButton(), "right");
   218      }
   219  
   220      /**
   221       * Displays the input dialog.
   222       */
   223      @Override
   224      public final void display() {
                 /* 
    P/P           *  Method: void display()
                  */
   225          display(getParent());
   226      }
   227  
   228      /**
   229       * Displays the input dialog.
   230       * 
   231       * @param parent Parent component
   232       */
   233      public final void display(final Component parent) {
                 /* 
    P/P           *  Method: void display(Component)
                  */
   234          UIUtilities.invokeLater(new Runnable() {
   235  
   236              /** {@inheritDoc} */
   237              @Override
   238              public void run() {
                         /* 
    P/P                   *  Method: void run()
                          */
   239                  pack();
   240                  setLocationRelativeTo(parent);
   241                  setVisible(true);
   242              }
   243          });
   244      }
   245  
   246      /**
   247       * Returns the text in the input field.
   248       * 
   249       * @return Input text
   250       */
   251      public final String getText() {
                 /* 
    P/P           *  Method: String getText()
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   252          return textField.getText();
   253      }
   254  
   255      /**
   256       * Sets the dialogs text to the specified text.
   257       * 
   258       * @param text New test
   259       */
   260      public final void setText(final String text) {
                 /* 
    P/P           *  Method: void setText(String)
                  * 
                  *  Preconditions:
                  *    this.textField != null
                  */
   261          textField.setText(text);
   262      }
   263  }








SofCheck Inspector Build Version : 2.17854
StandardInputDialog.java 2009-Jun-25 01:54:24
StandardInputDialog.class 2009-Sep-02 17:04:15
StandardInputDialog$1.class 2009-Sep-02 17:04:15
StandardInputDialog$2.class 2009-Sep-02 17:04:15
StandardInputDialog$3.class 2009-Sep-02 17:04:15
StandardInputDialog$4.class 2009-Sep-02 17:04:15
StandardInputDialog$5.class 2009-Sep-02 17:04:15
StandardInputDialog$6.class 2009-Sep-02 17:04:15
StandardInputDialog$7.class 2009-Sep-02 17:04:15