View Javadoc
1 package org.argosfields; 2 3 import java.io.IOException; 4 import java.lang.reflect.Constructor; 5 import java.util.Collection; 6 import java.util.Iterator; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 import org.argosfields.action.ActionMap; 11 import org.argosfields.action.BaseAction; 12 import org.argosfields.multi.GameClientAdapter; 13 import org.argosfields.multi.client.GameClient; 14 import org.argosfields.multi.client.IGameClient; 15 import org.argosfields.multi.server.GameServer; 16 import org.argosfields.multi.server.IGameServer; 17 import org.argosfields.multi.server.ServerInfo; 18 import org.argosfields.preference.DefaultPreferenceStore; 19 import org.argosfields.resource.ResourceManager; 20 import org.argosfields.util.ExceptionHandler; 21 import org.argosfields.util.RunnableContextHelper; 22 import org.argosfields.widget.AboutTab; 23 import org.argosfields.widget.InformationPanel; 24 import org.argosfields.widget.MessagePanel; 25 import org.argosfields.widget.ServerInfoTab; 26 import org.eclipse.jface.action.MenuManager; 27 import org.eclipse.jface.action.ToolBarManager; 28 import org.eclipse.jface.preference.IPreferenceStore; 29 import org.eclipse.jface.preference.JFacePreferences; 30 import org.eclipse.jface.window.ApplicationWindow; 31 import org.eclipse.jface.window.Window; 32 import org.eclipse.swt.SWT; 33 import org.eclipse.swt.custom.SashForm; 34 import org.eclipse.swt.events.DisposeEvent; 35 import org.eclipse.swt.events.DisposeListener; 36 import org.eclipse.swt.graphics.Image; 37 import org.eclipse.swt.graphics.Point; 38 import org.eclipse.swt.layout.FillLayout; 39 import org.eclipse.swt.widgets.Composite; 40 import org.eclipse.swt.widgets.Control; 41 import org.eclipse.swt.widgets.Display; 42 import org.eclipse.swt.widgets.Shell; 43 import org.eclipse.swt.widgets.TabFolder; 44 import org.eclipse.swt.widgets.TabItem; 45 46 /*** 47 * ArgosFields.java 48 * @author Xavier Cho 49 * @version $Revision: 1.16 $ $Date: 2004/04/17 18:21:01 $ 50 */ 51 public class ArgosFields extends ApplicationWindow { 52 53 private static Log log = LogFactory.getLog(ArgosFields.class); 54 55 private ActionMap actionMap; 56 private TabFolder tabFolder; 57 private TabItem serverTab; 58 private InformationPanel infoPanel; 59 private MessagePanel messagePanel; 60 61 /*** 62 * Defualt constructor of ArgosFields class 63 */ 64 public ArgosFields() { 65 super(null); 66 } 67 68 /*** 69 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell) 70 */ 71 protected void configureShell(final Shell shell) { 72 if (log.isDebugEnabled()) { 73 log.debug("Configuring shell..."); 74 } 75 76 this.actionMap = createActionMap(); 77 78 ResourceManager resources = ResourceManager.getInstance(); 79 80 if (log.isDebugEnabled()) { 81 log.debug("Initializing preference store..."); 82 } 83 84 try { 85 IPreferenceStore preferences = new DefaultPreferenceStore(); 86 JFacePreferences.setPreferenceStore(preferences); 87 } catch (IOException e) { 88 String msg = resources.getString("error.preferences"); 89 ExceptionHandler.handleException(msg, e); 90 91 System.exit(-1); 92 } 93 94 setBlockOnOpen(true); 95 addMenuBar(); 96 addToolBar(SWT.FLAT); 97 addStatusLine(); 98 99 super.configureShell(shell); 100 101 Window.setExceptionHandler(new ExceptionHandler()); 102 103 RunnableContextHelper.initialize(this); 104 105 shell.setText(resources.getString("application.title")); 106 107 Image icon = resources.getImage("application.icon"); 108 if (icon != null) { 109 shell.setImage(icon); 110 } 111 112 Point size = getInitialSize(); 113 Point location = getInitialLocation(size); 114 115 shell.setBounds(location.x, location.y, size.x, size.y); 116 shell.addDisposeListener(new DisposeListener() { 117 118 public void widgetDisposed(final DisposeEvent event) { 119 handleDisposeEvent(); 120 } 121 }); 122 } 123 124 protected ActionMap createActionMap() { 125 if (log.isDebugEnabled()) { 126 log.debug("Initializing action map..."); 127 } 128 129 ActionMap map = new ActionMap(); 130 131 ResourceManager resources = ResourceManager.getInstance(); 132 133 Collection actions = resources.getActionNames(); 134 Iterator it = actions.iterator(); 135 136 while (it.hasNext()) { 137 String name = (String) it.next(); 138 139 try { 140 Constructor constructor = Class.forName(name).getConstructors()[0]; 141 142 Object[] args = new Object[] {this}; 143 144 BaseAction action = (BaseAction) constructor.newInstance(args); 145 146 map.put(action); 147 } catch (Exception e) { 148 log.warn("Unable to instantiate action class " + name, e); 149 } 150 } 151 152 if (log.isDebugEnabled()) { 153 log.debug("Loaded " + actions.size() + " action classes."); 154 } 155 156 return map; 157 } 158 159 /*** 160 * @see org.eclipse.jface.window.ApplicationWindow#createMenuManager() 161 */ 162 protected MenuManager createMenuManager() { 163 return new DefaultMenuManager(this); 164 } 165 166 /*** 167 * @see org.eclipse.jface.window.ApplicationWindow#createToolBarManager(int 168 * style) 169 */ 170 protected ToolBarManager createToolBarManager(final int style) { 171 return new DefaultToolBarManager(this); 172 } 173 174 /*** 175 * @see org.eclipse.jface.window.Window#createContents(org.eclipse.swt.widgets.Composite) 176 */ 177 protected Control createContents(final Composite parent) { 178 ResourceManager resources = ResourceManager.getInstance(); 179 Composite contents = new Composite(parent, SWT.NULL); 180 contents.setLayout(new FillLayout()); 181 182 SashForm horizontalSash = new SashForm(contents, SWT.HORIZONTAL); 183 SashForm verticalSash = new SashForm(horizontalSash, SWT.VERTICAL); 184 185 this.infoPanel = new InformationPanel(horizontalSash, SWT.BORDER); 186 infoPanel.setVisible(false); 187 188 horizontalSash.setWeights(new int[] {80, 20}); 189 190 this.tabFolder = new TabFolder(verticalSash, SWT.TOP); 191 192 TabItem tabItem1 = new TabItem(tabFolder, SWT.NONE); 193 tabItem1.setText(resources.getString("tab.about")); 194 tabItem1.setImage(resources.getImage("image.about")); 195 tabItem1.setControl(new AboutTab(tabFolder, SWT.NONE)); 196 197 this.messagePanel = new MessagePanel(verticalSash, SWT.NONE); 198 199 verticalSash.setWeights(new int[] {80, 20}); 200 201 messagePanel.setVisible(false); 202 203 IGameClient client = GameClient.getInstance(); 204 client.addGameClientListener(messagePanel); 205 client.addGameClientListener(new GameClientAdapter() { 206 207 public void sessionStarted(final ServerInfo info) { 208 ResourceManager resources = ResourceManager.getInstance(); 209 210 serverTab = new TabItem(tabFolder, SWT.NONE); 211 serverTab.setText(resources.getString("tab.server")); 212 serverTab.setImage(resources.getImage("image.server")); 213 serverTab.setControl(new ServerInfoTab(tabFolder, SWT.NONE, info)); 214 215 tabFolder.setSelection(1); 216 } 217 218 public void sessionEnded() { 219 if (serverTab != null) { 220 serverTab.dispose(); 221 serverTab = null; 222 } 223 } 224 }); 225 226 return contents; 227 } 228 229 /*** 230 * @see org.eclipse.jface.window.Window#getInitialLocation(org.eclipse.swt.graphics.Point) 231 */ 232 protected Point getInitialLocation(final Point initialSize) { 233 IPreferenceStore preferences = JFacePreferences.getPreferenceStore(); 234 235 int x = preferences.getInt("location.x"); 236 int y = preferences.getInt("location.y"); 237 238 return new Point(x, y); 239 } 240 241 /*** 242 * @see org.eclipse.jface.window.Window#getInitialSize() 243 */ 244 protected Point getInitialSize() { 245 IPreferenceStore preferences = JFacePreferences.getPreferenceStore(); 246 247 int x = preferences.getInt("size.width"); 248 int y = preferences.getInt("size.height"); 249 250 return new Point(x, y); 251 } 252 253 public void toggleMessagePanel() { 254 if (messagePanel != null) { 255 messagePanel.setVisible(!messagePanel.isVisible()); 256 messagePanel.getParent().layout(); 257 } 258 } 259 260 public void toggleInfoPanel() { 261 if (infoPanel != null) { 262 infoPanel.setVisible(!infoPanel.isVisible()); 263 infoPanel.getParent().layout(); 264 } 265 } 266 267 protected ActionMap getActionMap() { 268 return actionMap; 269 } 270 271 protected void handleDisposeEvent() { 272 Shell shell = getShell(); 273 274 Point location = shell.getLocation(); 275 Point size = shell.getSize(); 276 277 if (log.isInfoEnabled()) { 278 log.info("Saving user preferences..."); 279 } 280 281 DefaultPreferenceStore preferences = (DefaultPreferenceStore) JFacePreferences 282 .getPreferenceStore(); 283 284 preferences.setValue("location.x", location.x); 285 preferences.setValue("location.y", location.y); 286 preferences.setValue("size.width", size.x); 287 preferences.setValue("size.height", size.y); 288 289 try { 290 preferences.save(); 291 } catch (IOException e) { 292 if (log.isWarnEnabled()) { 293 String msg = "Failed to save user preferences data."; 294 log.warn(msg, e); 295 } 296 } 297 298 IGameClient client = GameClient.getInstance(); 299 if (client.isSessionStarted()) { 300 try { 301 client.endSession(); 302 } catch (Exception e) { 303 if (log.isWarnEnabled()) { 304 String msg = "Failed to disconnect from the game server."; 305 log.warn(msg, e); 306 } 307 } 308 } 309 310 IGameServer server = GameServer.getInstance(); 311 if (server.isRunning()) { 312 try { 313 server.stop(); 314 } catch (Exception e) { 315 if (log.isWarnEnabled()) { 316 String msg = "Failed to stop ArgosFields server."; 317 log.warn(msg, e); 318 } 319 } 320 } 321 } 322 323 public static void main(final String[] args) { 324 try { 325 ApplicationWindow application = new ArgosFields(); 326 application.open(); 327 } catch (Throwable t) { 328 if (log.isFatalEnabled()) { 329 String msg = "Unable to initialize the application."; 330 log.fatal(msg, t); 331 } 332 } finally { 333 Display.getCurrent().dispose(); 334 System.exit(0); 335 } 336 } 337 }

This page was automatically generated by Maven