001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.log4j.lf5.viewer;
018
019import javax.swing.*;
020import java.awt.*;
021import java.awt.event.ActionEvent;
022import java.awt.event.ActionListener;
023import java.awt.event.KeyAdapter;
024import java.awt.event.KeyEvent;
025
026/**
027 * LogFactor5InputDialog
028 *
029 * Creates a popup input dialog box so that users can enter
030 * a URL to open a log file from.
031 *
032 * @author Richard Hurst
033 * @author Brad Marlborough
034 */
035
036// Contributed by ThoughtWorks Inc.
037
038public class LogFactor5InputDialog extends LogFactor5Dialog {
039  //--------------------------------------------------------------------------
040  //   Constants:
041  //--------------------------------------------------------------------------
042  public static final int SIZE = 30;
043  //--------------------------------------------------------------------------
044  //   Protected Variables:
045  //--------------------------------------------------------------------------
046
047  //--------------------------------------------------------------------------
048  //   Private Variables:
049  //--------------------------------------------------------------------------
050  private JTextField _textField;
051  //--------------------------------------------------------------------------
052  //   Constructors:
053  //--------------------------------------------------------------------------
054
055  /**
056   * Configures an input dialog box using a defualt size for the text field.
057   * param jframe the frame where the dialog will be loaded from.
058   * param title the title of the dialog box.
059   * param label the label to be put in the dialog box.
060   */
061  public LogFactor5InputDialog(JFrame jframe, String title, String label) {
062    this(jframe, title, label, SIZE);
063  }
064
065  /**
066   * Configures an input dialog box.
067   * param jframe the frame where the dialog will be loaded from.
068   * param title the title of the dialog box.
069   * param label the label to be put in the dialog box.
070   * param size the size of the text field.
071   */
072  public LogFactor5InputDialog(JFrame jframe, String title, String label,
073      int size) {
074    super(jframe, title, true);
075
076    JPanel bottom = new JPanel();
077    bottom.setLayout(new FlowLayout());
078
079    JPanel main = new JPanel();
080    main.setLayout(new FlowLayout());
081    main.add(new JLabel(label));
082    _textField = new JTextField(size);
083    main.add(_textField);
084
085    addKeyListener(new KeyAdapter() {
086      public void keyPressed(KeyEvent e) {
087        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
088          hide();
089        }
090      }
091    });
092
093    JButton ok = new JButton("Ok");
094    ok.addActionListener(new ActionListener() {
095      public void actionPerformed(ActionEvent e) {
096        hide();
097      }
098    });
099
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}