Import of Ekit 0.9h
[old-projects.git] / ekit / com / hexidec / ekit / component / UserInputDialog.java
CommitLineData
6ce136da
JL
1/*
2GNU Lesser General Public License
3
4UserInputDialog
5Copyright (C) 2000 Howard Kistler
6
7This library is free software; you can redistribute it and/or
8modify it under the terms of the GNU Lesser General Public
9License as published by the Free Software Foundation; either
10version 2.1 of the License, or (at your option) any later version.
11
12This library is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public
18License along with this library; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20*/
21
22package com.hexidec.ekit.component;
23
24import java.awt.Frame;
25import java.awt.event.ActionEvent;
26import java.awt.event.ActionListener;
27import java.awt.event.WindowAdapter;
28import java.awt.event.WindowEvent;
29import java.beans.PropertyChangeEvent;
30import java.beans.PropertyChangeListener;
31import javax.swing.JDialog;
32import javax.swing.JOptionPane;
33import javax.swing.JTextField;
34
35import com.hexidec.util.Translatrix;
36
37/** Class for providing a dialog that lets the user specify values for tag attributes
38 */
39public class UserInputDialog extends JDialog
40{
41
42 private String inputText = new String();
43 private JOptionPane jOptionPane;
44
45 public UserInputDialog(Frame parent, String title, boolean bModal, String attribName, String defaultText)
46 {
47 super(parent, title, bModal);
48 final JTextField jtxfInput = new JTextField(32);
49 jtxfInput.setText(defaultText);
50 Object[] panelContents = { attribName, jtxfInput };
51 final Object[] buttonLabels = { Translatrix.getTranslationString("DialogAccept"), Translatrix.getTranslationString("DialogCancel") };
52
53 jOptionPane = new JOptionPane(panelContents, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, buttonLabels, buttonLabels[0]);
54 setContentPane(jOptionPane);
55 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
56
57 addWindowListener(new WindowAdapter() {
58 public void windowClosing(WindowEvent we)
59 {
60 jOptionPane.setValue(new Integer(JOptionPane.CLOSED_OPTION));
61 }
62 });
63
64 jtxfInput.addActionListener(new ActionListener() {
65 public void actionPerformed(ActionEvent e)
66 {
67 jOptionPane.setValue(buttonLabels[0]);
68 }
69 });
70
71 jOptionPane.addPropertyChangeListener(new PropertyChangeListener() {
72 public void propertyChange(PropertyChangeEvent e)
73 {
74 String prop = e.getPropertyName();
75 if(isVisible()
76 && (e.getSource() == jOptionPane)
77 && (prop.equals(JOptionPane.VALUE_PROPERTY) || prop.equals(JOptionPane.INPUT_VALUE_PROPERTY)))
78 {
79 Object value = jOptionPane.getValue();
80 if(value == JOptionPane.UNINITIALIZED_VALUE)
81 {
82 return;
83 }
84 jOptionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
85 if(value.equals(buttonLabels[0]))
86 {
87 inputText = jtxfInput.getText();
88 setVisible(false);
89 }
90 else
91 {
92 inputText = null;
93 setVisible(false);
94 }
95 }
96 }
97 });
98 this.pack();
99 this.show();
100 jtxfInput.requestFocus();
101 }
102
103 public UserInputDialog(Frame parent, String title, boolean bModal, String attribName)
104 {
105 this(parent, title, bModal, attribName, "");
106 }
107
108 public String getInputText()
109 {
110 return inputText;
111 }
112}
113