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