Commit | Line | Data |
---|---|---|
6ce136da JL |
1 | /* |
2 | GNU Lesser General Public License | |
3 | ||
4 | UserInputDialog | |
5 | Copyright (C) 2000 Howard Kistler | |
6 | ||
7 | This library is free software; you can redistribute it and/or | |
8 | modify it under the terms of the GNU Lesser General Public | |
9 | License as published by the Free Software Foundation; either | |
10 | version 2.1 of the License, or (at your option) any later version. | |
11 | ||
12 | This library is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public | |
18 | License along with this library; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | */ | |
21 | ||
22 | package com.hexidec.ekit.component; | |
23 | ||
24 | import java.awt.Frame; | |
25 | import java.awt.event.ActionEvent; | |
26 | import java.awt.event.ActionListener; | |
27 | import java.awt.event.WindowAdapter; | |
28 | import java.awt.event.WindowEvent; | |
29 | import java.beans.PropertyChangeEvent; | |
30 | import java.beans.PropertyChangeListener; | |
31 | import javax.swing.JDialog; | |
32 | import javax.swing.JOptionPane; | |
33 | import javax.swing.JTextField; | |
34 | ||
35 | import com.hexidec.util.Translatrix; | |
36 | ||
37 | /** Class for providing a dialog that lets the user specify values for tag attributes | |
38 | */ | |
39 | public 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 |