View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.log4j.lf5.viewer;
    18
    19  import javax.swing.*;
    20  import java.awt.*;
    21  import java.awt.event.ActionEvent;
    22  import java.awt.event.ActionListener;
    23  import java.awt.event.KeyAdapter;
    24  import java.awt.event.KeyEvent;
    25
    26  /**
    27   * LogFactor5InputDialog
    28   *
    29   * Creates a popup input dialog box so that users can enter
    30   * a URL to open a log file from.
    31   *
    32   * @author Richard Hurst
    33   * @author Brad Marlborough
    34   */
    35
    36  // Contributed by ThoughtWorks Inc.
    37
    38  public class LogFactor5InputDialog extends LogFactor5Dialog {
    39    //--------------------------------------------------------------------------
    40    //   Constants:
    41    //--------------------------------------------------------------------------
    42    public static final int SIZE = 30;
    43    //--------------------------------------------------------------------------
    44    //   Protected Variables:
    45    //--------------------------------------------------------------------------
    46
    47    //--------------------------------------------------------------------------
    48    //   Private Variables:
    49    //--------------------------------------------------------------------------
    50    private JTextField _textField;
    51    //--------------------------------------------------------------------------
    52    //   Constructors:
    53    //--------------------------------------------------------------------------
    54
    55    /**
    56     * Configures an input dialog box using a defualt size for the text field.
    57     * param jframe the frame where the dialog will be loaded from.
    58     * param title the title of the dialog box.
    59     * param label the label to be put in the dialog box.
    60     */
    61    public LogFactor5InputDialog(JFrame jframe, String title, String label) {
    62      this(jframe, title, label, SIZE);
    63    }
    64
    65    /**
    66     * Configures an input dialog box.
    67     * param jframe the frame where the dialog will be loaded from.
    68     * param title the title of the dialog box.
    69     * param label the label to be put in the dialog box.
    70     * param size the size of the text field.
    71     */
    72    public LogFactor5InputDialog(JFrame jframe, String title, String label,
    73        int size) {
    74      super(jframe, title, true);
    75
    76      JPanel bottom = new JPanel();
    77      bottom.setLayout(new FlowLayout());
    78
    79      JPanel main = new JPanel();
    80      main.setLayout(new FlowLayout());
    81      main.add(new JLabel(label));
    82      _textField = new JTextField(size);
    83      main.add(_textField);
    84
    85      addKeyListener(new KeyAdapter() {
    86        public void keyPressed(KeyEvent e) {
    87          if (e.getKeyCode() == KeyEvent.VK_ENTER) {
    88            hide();
    89          }
    90        }
    91      });
    92
    93      JButton ok = new JButton("Ok");
    94      ok.addActionListener(new ActionListener() {
    95        public void actionPerformed(ActionEvent e) {
    96          hide();
    97        }
    98      });
    99
    100     JButton cancel = new JButton("Cancel");
    101     cancel.addActionListener(new ActionListener() {
    102       public void actionPerformed(ActionEvent e) {
    103         hide();
    104         // set the text field to blank just in case
    105         // a file was selected before the Cancel
    106         // button was pressed.
    107         _textField.setText("");
    108       }
    109     });
    110
    111     bottom.add(ok);
    112     bottom.add(cancel);
    113     getContentPane().add(main, BorderLayout.CENTER);
    114     getContentPane().add(bottom, BorderLayout.SOUTH);
    115     pack();
    116     centerWindow(this);
    117     show();
    118   }
    119
    120   //--------------------------------------------------------------------------
    121   //   Public Methods:
    122   //--------------------------------------------------------------------------
    123   public String getText() {
    124     String s = _textField.getText();
    125
    126     if (s != null && s.trim().length() == 0) {
    127       return null;
    128     }
    129
    130     return s;
    131
    132   }
    133
    134   //--------------------------------------------------------------------------
    135   //   Protected Methods:
    136   //--------------------------------------------------------------------------
    137
    138   //--------------------------------------------------------------------------
    139   //   Private Methods:
    140   //--------------------------------------------------------------------------
    141
    142   //--------------------------------------------------------------------------
    143   //   Nested Top-Level Classes or Interfaces
    144   //--------------------------------------------------------------------------
    145 }