1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.magicproject.ui.component;
20
21 import java.awt.Container;
22 import java.awt.Dimension;
23 import java.awt.event.ActionEvent;
24 import java.awt.event.ActionListener;
25 import java.awt.event.WindowEvent;
26
27 import javax.swing.BoxLayout;
28 import javax.swing.JButton;
29 import javax.swing.JDialog;
30 import javax.swing.JLabel;
31 import javax.swing.JTextField;
32 import javax.swing.WindowConstants;
33
34 import net.sf.magicproject.token.IdConst;
35 import net.sf.magicproject.tools.Picture;
36 import net.sf.magicproject.ui.MagicUIComponents;
37 import net.sf.magicproject.ui.ToolKit;
38 import net.sf.magicproject.ui.i18n.LanguageManager;
39
40 /***
41 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
42 */
43 public final class JPrompt extends JDialog implements ActionListener {
44
45 /***
46 * Create a new instance of JPrompt
47 */
48 private JPrompt() {
49 super(MagicUIComponents.magicForm);
50 setModal(true);
51
52 final Container con = this.getContentPane();
53 con.setLayout(new BoxLayout(con, BoxLayout.X_AXIS));
54 pressme = new JButton(LanguageManager.getString("ok"));
55 pressme.setMnemonic('o');
56 pressme.addActionListener(this);
57 cancel = new JButton(LanguageManager.getString("cancel"));
58 cancel.addActionListener(this);
59 answer = new JTextField();
60 answer.setMaximumSize(new Dimension(32000, 20));
61 pressme.setPreferredSize(new Dimension(50, 20));
62
63 final JLabel promptLbl = new JLabel(LanguageManager.getString("entLoc"));
64 con.add(promptLbl);
65 con.add(answer);
66 con.add(pressme);
67 setTitle(LanguageManager.getString("webLoc"));
68 setBounds(100, 100, 350, 70);
69 answer.requestFocus();
70 getRootPane().setDefaultButton(pressme);
71 setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
72 ToolKit.addCancelByEscapeKey(this, cancel);
73 }
74
75 public void actionPerformed(ActionEvent event) {
76 Object source = event.getSource();
77 if (source == pressme) {
78 value = answer.getText();
79 if (value.length() > 0 && value.charAt(value.length() - 1) == '/') {
80 value = value.substring(0, value.length() - 1);
81 }
82 setVisible(false);
83 } else if (source == cancel) {
84 value = null;
85 setVisible(false);
86 }
87 }
88
89 @Override
90 protected void processWindowEvent(WindowEvent e) {
91 super.processWindowEvent(e);
92 if (e.getID() == WindowEvent.WINDOW_CLOSING) {
93 cancel.doClick();
94 }
95 }
96
97 /***
98 * Create the one instance of this class. The 'static final' are not used for
99 * the 'instance' field, since this class contains object with textes
100 * depending on current language.
101 *
102 * @return the unique instance of this class.
103 */
104 public static JPrompt getInstance() {
105 if (instance == null) {
106 instance = new JPrompt();
107 }
108 return instance;
109 }
110
111 @Override
112 public void setVisible(boolean visible) {
113 try {
114 if (visible) {
115 MagicUIComponents.magicForm.setIconImage(Picture
116 .loadImage(IdConst.IMAGES_DIR + "mp_wiz.gif"));
117 } else {
118 MagicUIComponents.magicForm.setIconImage(Picture
119 .loadImage(IdConst.IMAGES_DIR + "mp.gif"));
120 }
121 } catch (Exception e) {
122
123 }
124 super.setVisible(visible);
125 }
126
127 /***
128 * The instance of JPrompt
129 */
130 private static JPrompt instance;
131
132 /***
133 * The ok [default] button
134 */
135 private final JButton pressme;
136
137 /***
138 * The cancel button
139 */
140 private final JButton cancel;
141
142 /***
143 * The current value
144 */
145 public static String value = "";
146
147 /***
148 * The text field containing the last answer
149 */
150 public final JTextField answer;
151
152 }