1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package net.sf.magicproject.ui.wizard;
21
22 import java.awt.Dimension;
23 import java.awt.event.ActionEvent;
24 import java.net.InetAddress;
25
26 import javax.swing.JCheckBox;
27 import javax.swing.JComboBox;
28 import javax.swing.JLabel;
29 import javax.swing.JOptionPane;
30 import javax.swing.JPanel;
31 import javax.swing.JTextField;
32
33 import net.sf.magicproject.deckbuilder.DeckConstraint;
34 import net.sf.magicproject.network.ConnectionManager;
35 import net.sf.magicproject.network.StartingOption;
36 import net.sf.magicproject.tools.Configuration;
37 import net.sf.magicproject.ui.MagicUIComponents;
38 import net.sf.magicproject.ui.component.LoaderConsole;
39 import net.sf.magicproject.ui.i18n.LanguageManager;
40
41 import org.mortbay.util.Password;
42
43 /***
44 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
45 * @since 0.81
46 */
47 public class Server extends Network {
48
49 /***
50 * Creates a new instance of Client <br>
51 */
52 public Server() {
53 super(LanguageManager.getString("wiz_server.title"), LanguageManager
54 .getString("wiz_server.description"), LanguageManager
55 .getString("wiz_server.ok"), 460, 380);
56
57 try {
58 passwordTxt.setText(Password.deobfuscate(Configuration.getString(
59 "serverPassword", "")));
60 } catch (StringIndexOutOfBoundsException sioobe) {
61 System.out
62 .println("Error while trying to deobfuscate the server password");
63 }
64 try {
65
66
67
68
69 ipTxt.setText(InetAddress.getLocalHost().getHostAddress());
70 } catch (Exception e1) {
71
72 }
73 portTxt.setToolTipText("<html>"
74 + LanguageManager.getString("wiz_network.port.tooltip"));
75 ipTxt.setEditable(false);
76 ipTxt.setToolTipText("<html>"
77 + LanguageManager.getString("wiz_server.ip.tooltip"));
78
79
80 JPanel gameNamePanel = setSizes(new JLabel(LanguageManager
81 .getString("wiz_server.gamename")
82 + " : "));
83 gameNameTxt = new JTextField(Configuration.getString("gameName", "My Game"));
84 gameNameTxt.setToolTipText("<html>"
85 + LanguageManager.getString("wiz_server.gamename.tooltip"));
86 gameNameTxt.setMaximumSize(new Dimension(UNBOUNDED_TXT_SIZE,
87 MAXIMAL_TXT_SIZE));
88 addCheckValidity(gameNameTxt);
89 gameNamePanel.add(gameNameTxt);
90 gameParamPanel.add(gameNamePanel, 1);
91
92
93 JPanel whoStartsPanel = setSizes(new JLabel(LanguageManager
94 .getString("wiz_server.whoStarts")
95 + " : "));
96 whoStarts = new JComboBox();
97 whoStarts
98 .setMaximumSize(new Dimension(UNBOUNDED_TXT_SIZE, MAXIMAL_TXT_SIZE));
99 whoStarts.setEditable(false);
100 for (StartingOption option : StartingOption.values()) {
101 whoStarts.addItem(option.getLocaleValue());
102 }
103 whoStarts.setSelectedIndex(Configuration.getInt("whoStarts",
104 StartingOption.random.ordinal()));
105 whoStartsPanel.add(whoStarts);
106 gameParamPanel.add(whoStartsPanel);
107
108
109 useMana = new JCheckBox(LanguageManager.getString("wiz_server.allowmana"),
110 Configuration.getBoolean("useMana", true));
111 useMana.setToolTipText("<html>"
112 + LanguageManager.getString("wiz_server.allowmana.tooltip")
113 + "<br><br>" + MagicUIComponents.HTML_ICON_TIP
114 + LanguageManager.getString("wiz_server.allowmana.tooltip.tip"));
115 gameParamPanel.add(useMana);
116
117
118 opponentResponse = new JCheckBox(LanguageManager
119 .getString("wiz_server.allowopponentresponse"), Configuration
120 .getBoolean("opponentResponse", true));
121 opponentResponse.setToolTipText("<html>"
122 + LanguageManager.getString("wiz_server.allowopponentresponse.tooltip")
123 + "<br><br>"
124 + MagicUIComponents.HTML_ICON_TIP
125 + LanguageManager
126 .getString("wiz_server.allowopponentresponse.tooltip.tip"));
127 gameParamPanel.add(opponentResponse);
128 checkValidity();
129 }
130
131 @Override
132 protected boolean checkValidity() {
133 if (!super.checkValidity()) {
134 return false;
135 }
136 if (gameNameTxt.getText().length() == 0) {
137 wizardInfo.resetWarning(LanguageManager
138 .getString("wiz_server.gamename.missed"));
139 return false;
140 }
141 return true;
142 }
143
144 @Override
145 public void actionPerformed(ActionEvent event) {
146 Object source = event.getSource();
147 if (source == okBtn) {
148
149
150
151 try {
152
153 Object value = portTxt.getValue();
154 int port = Integer.parseInt(value == null ? portTxt.getText() : portTxt
155 .getValue().toString());
156 if (port < MINIMAL_PORT || port > MAXIMAL_PORT) {
157
158 JOptionPane.showMessageDialog(this, LanguageManager
159 .getString("wiz_network.port.invalid"), LanguageManager
160 .getString("error"), JOptionPane.WARNING_MESSAGE);
161 return;
162 }
163
164
165 String gameName = gameNameTxt.getText().trim();
166 if (gameName.length() == 0) {
167 JOptionPane.showMessageDialog(this, LanguageManager
168 .getString("wiz_server.gamename.missed"), LanguageManager
169 .getString("error"), JOptionPane.WARNING_MESSAGE);
170 dispose();
171 return;
172 }
173 if (!validateDeck()) {
174 return;
175 }
176
177 setVisible(false);
178
179
180 Configuration.setProperty("port", port);
181 Configuration.setProperty("gameName", gameName);
182 Configuration.setProperty("decks.constraint",
183 ((DeckConstraint) constraintList.getSelectedItem()).getName());
184 ConnectionManager.closeConnexions();
185 Configuration.setProperty("serverPassword", Password
186 .obfuscate(new String(passwordTxt.getPassword())));
187 Configuration.setProperty("useMana", useMana.isSelected());
188 Configuration.setProperty("opponentResponse", opponentResponse
189 .isSelected());
190 Configuration.setProperty("whoStarts", whoStarts.getSelectedIndex());
191
192
193 LoaderConsole.launchLoader();
194 ConnectionManager.server = new net.sf.magicproject.network.Server(deck,
195 passwordTxt.getPassword());
196 ConnectionManager.server.start();
197 } catch (Exception e1) {
198 ConnectionManager.closeConnexions();
199 JOptionPane.showMessageDialog(this, LanguageManager
200 .getString("wiz_network.unknownpb")
201 + " : " + e1.getMessage(), LanguageManager
202 .getString("wiz_network.unknownpb"), JOptionPane.WARNING_MESSAGE);
203 return;
204 }
205 setVisible(false);
206 dispose();
207 } else {
208 super.actionPerformed(event);
209 }
210 }
211
212 /***
213 * The "use mana" option.
214 */
215 private final JCheckBox useMana;
216
217 /***
218 * The opponent response option.
219 */
220 private final JCheckBox opponentResponse;
221
222 /***
223 * The starting player option.
224 */
225 private final JComboBox whoStarts;
226
227 /***
228 * The game name text.
229 */
230 private final JTextField gameNameTxt;
231 }