1 package org.argosfields.wizard;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.argosfields.multi.client.GameClient;
6 import org.argosfields.multi.client.IGameClient;
7 import org.argosfields.persistence.Account;
8 import org.argosfields.resource.ResourceManager;
9 import org.argosfields.service.CreateAccountException;
10 import org.argosfields.service.CreateAccountNotAllowedException;
11 import org.argosfields.service.DuplicateAccountException;
12 import org.argosfields.service.ILoginManager;
13 import org.argosfields.service.LoginException;
14 import org.argosfields.spring.ClientContextHelper;
15 import org.eclipse.jface.dialogs.MessageDialog;
16 import org.eclipse.jface.preference.IPreferenceStore;
17 import org.eclipse.jface.preference.JFacePreferences;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.jface.wizard.IWizardPage;
20 import org.eclipse.jface.wizard.Wizard;
21
22 /***
23 * AccountWizard.java
24 * @author Xavier Cho
25 * @version $Revision: 1.8 $ $Date: 2004/04/17 08:59:17 $
26 */
27 public class AccountWizard extends Wizard {
28
29 private static Log log = LogFactory.getLog(AccountWizard.class);
30
31 /***
32 * @see org.eclipse.jface.wizard.IWizard#addPages()
33 */
34 public void addPages() {
35 ResourceManager resources = ResourceManager.getInstance();
36
37 String pageName = resources.getString("wizard.account.page1.title");
38
39 ImageDescriptor image = resources
40 .getImageDescriptor("wizard.account.page1.image");
41
42 setWindowTitle(pageName);
43 setDefaultPageImageDescriptor(image);
44
45 IWizardPage page = new AccountWizardPage1(pageName);
46 page.setTitle(pageName);
47
48 addPage(page);
49 }
50
51 public boolean performFinish() {
52 AccountWizardPage1 page = (AccountWizardPage1) getPages()[0];
53
54 Account account = page.getAccount();
55 String hostAddress = page.getHostAddress();
56
57 int index = hostAddress.lastIndexOf(":");
58 int port = -1;
59
60 if (index == -1) {
61 ResourceManager resources = ResourceManager.getInstance();
62 page.setErrorMessage(resources.getString("error.invalid.host"));
63
64 return false;
65 }
66
67 try {
68 port = Integer.parseInt(hostAddress.substring(index + 1));
69 hostAddress = hostAddress.substring(0, index);
70 } catch (NumberFormatException e) {
71 ResourceManager resources = ResourceManager.getInstance();
72 page.setErrorMessage(resources.getString("error.invalid.host"));
73
74 return false;
75 }
76
77 if (log.isInfoEnabled()) {
78 log.info("Creating a new account at the remote host :");
79 log.info(" - host : " + hostAddress);
80 log.info(" - port : " + port);
81 }
82
83 IPreferenceStore preferences = JFacePreferences.getPreferenceStore();
84 preferences.setValue("remote.server.host", hostAddress);
85 preferences.setValue("remote.server.port", port);
86
87 ClientContextHelper helper = ClientContextHelper.getInstance();
88
89 ILoginManager manager = helper.getLoginManager();
90
91 String sessionId = null;
92
93 ResourceManager resources = ResourceManager.getInstance();
94 String errorMessage = null;
95
96 try {
97 manager.createAccount(account);
98 } catch (CreateAccountNotAllowedException e) {
99 errorMessage = resources.getString("error.create.not.allowed");
100 } catch (DuplicateAccountException e) {
101 errorMessage = resources.getString("error.duplicate.user");
102 } catch (CreateAccountException e) {
103 errorMessage = resources.getString("error.account.create");
104 } catch (LoginException e) {
105 errorMessage = e.getMessage();
106
107 if (errorMessage != null) {
108 errorMessage = resources.getString(errorMessage);
109 }
110
111 if (errorMessage == null) {
112 errorMessage = resources.getString("error.login");
113 }
114 }
115
116 if (errorMessage != null) {
117 String title = resources.getString("dialog.title.warning");
118 String message = resources.getString("error.duplicate.user");
119
120 MessageDialog.openWarning(getShell(), title, message);
121
122 return false;
123 }
124
125 IGameClient client = GameClient.getInstance();
126 client.startSession(hostAddress, port, sessionId);
127
128 if (log.isInfoEnabled()) {
129 log.info("Successfully logged in to the remote host.");
130 }
131
132 return true;
133 }
134 }
This page was automatically generated by Maven