View Javadoc

1   /*
2    * Magic-Project is a turn based strategy simulator Copyright (C) 2003-2007 Fabrice
3    * Daugan
4    * 
5    * This program is free software; you can redistribute it and/or modify it under
6    * the terms of the GNU General Public License as published by the Free Software
7    * Foundation; either version 2 of the License, or (at your option) any later
8    * version.
9    * 
10   * This program is distributed in the hope that it will be useful, but WITHOUT
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13   * details.
14   * 
15   * You should have received a copy of the GNU General Public License along with
16   * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17   * Place, Suite 330, Boston, MA 02111-1307 USA
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  		// inherit main frame
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'); // associate hotkey
56  		pressme.addActionListener(this); // register button
57  		cancel = new JButton(LanguageManager.getString("cancel"));
58  		cancel.addActionListener(this); // cancel button
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); // position frame
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 			// IGNORING
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 }