Source code: org/dma/ihm/controller/Controller.java
1 package org.dma.ihm.controller;
2
3 import java.awt.event.*;
4 import java.io.*;
5 import java.net.*;
6 import java.text.MessageFormat;
7 import java.text.SimpleDateFormat;
8 import java.util.*;
9 import java.util.Locale;
10 import java.util.zip.*;
11 import javax.swing.UIManager;
12
13 import org.dma.ihm.controller.*;
14 import org.dma.ihm.controller.data.*;
15 import org.dma.ihm.controller.data.io.*;
16 import org.dma.ihm.game.*;
17 import org.dma.ihm.game.calendar.*;
18 import org.dma.ihm.game.calendar.events.*;
19 import org.dma.ihm.game.league.std.events.*;
20 import org.dma.ihm.game.match.*;
21 import org.dma.ihm.gui.*;
22
23 /**
24 * The Controller class is the "main class" of the game. It is also the
25 * interface between the game and the gui part.
26 *
27 * @author Bernhard von Gunten & Arik Dasen
28 * @created December 29, 2001
29 */
30 public class Controller {
31
32 /** TitleVersion */
33 public final static String TITLE_VERSION = "Ice Hockey Manager / Version 0.1.2";
34 /** The Game thread */
35 private static Game game = null;
36 /** The Scenario of this game */
37 private static Scenario scenario = null;
38 /** The user settings */
39 private static Settings settings = null;
40 /** The resources */
41 private static Resources resources = null;
42 /** Listeners on this controller */
43 private Vector listener;
44 /** Desktop of ihm */
45 private static Desktop desktop = null;
46 /** Helper to cycle trough multiple users for ONE event */
47 private static Vector stopBeforeGameDayUsers = null;
48 /** Helper to cycle trough multiple users for ONE event */
49 private static User currentStopBeforeGameDayUser = null;
50 /** Helper to cycle trough multiple users for ONE event */
51 private static GameCalendarStopBeforeGamedayEvent stopBeforeGamedayEvent = null;
52
53
54 /** Constructor for the Controller object */
55 private Controller() { }
56
57
58 /** Initialize the controller (and so the game) */
59 public static void init() {
60 try {
61 settings = new Settings();
62 resources = new Resources("", "", getUserSetting(settings.LANGUAGE), "");
63
64 try {
65 javax.swing.plaf.metal.MetalLookAndFeel.setCurrentTheme(new javax.swing.plaf.metal.DefaultMetalTheme());
66 UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
67 } catch (Exception ignored) {}
68
69 desktop = new Desktop();
70 showShortMessage("IHM", "Loading scenario ...");
71
72 // For faster startup load defaultscenario
73 if (true) {
74 // Load standard scenario
75 newGame(ScenarioXML.getScenarioFile(new File(settings.getIhmScenariosPath(), "ihm.xml")));
76 } else {
77 // Generate whole scenario from scracht and save XML to standard db
78 newGame(null);
79 exportScenario();
80 }
81
82 } catch (Exception e) {
83 /**
84 * @todo: improve
85 */
86 System.out.println(e.toString());
87 e.printStackTrace(System.out);
88 }
89 }
90
91
92 /** Description of the Method */
93 public static void exportScenario() {
94 // Create DB
95 scenario.saveDB(settings.getIhmDBPath(), "ihm");
96 // Create scneario, pointing to the ihm db
97 ScenarioFile scenarioFile = new ScenarioFile(new File(settings.getIhmScenariosPath(), "ihm.xml"), "ihm", true, "Default Scenario");
98 scenario.saveScenario(scenarioFile);
99 }
100
101
102 /**
103 * Description of the Method
104 *
105 * @param scenarioFile Description of the Parameter
106 */
107 public static void newGame(ScenarioFile scenarioFile) {
108
109 // Start game thread
110 game = new Game();
111
112 // load scenario
113 scenario = new Scenario(scenarioFile);
114
115 // der Listener ruft einfach nur entsprechenden Methoden des Controllers auf
116 game.addGameCalendarListener(
117 new GameCalendarListener() {
118 public void gameCalendarEventPerformed(GameCalendarEvent e) {
119 if (e instanceof GameCalendarStopBeforeGamedayEvent) {
120 doGameCalendarStopBeforeGameDayEvent((GameCalendarStopBeforeGamedayEvent) e);
121 }
122 if (e instanceof GameCalendarDisplayGuiEvent) {
123 doGameCalendarDisplayGuiEvent((GameCalendarDisplayGuiEvent) e);
124 }
125 if (e instanceof GameCalendarGameDayEvent) {
126 doGameCalendarGameDayEvent((GameCalendarGameDayEvent) e);
127 }
128
129 // StdLeague Events:
130 if (e instanceof StdGameCalendarLeaguePlayoffFinishedEvent) {
131 doStdGameCalendarLeaguePlayoffFinishedEvent((StdGameCalendarLeaguePlayoffFinishedEvent) e);
132 }
133 if (e instanceof StdGameCalendarPlayoffFinishedEvent) {
134 doStdGameCalendarPlayoffFinishedEvent((StdGameCalendarPlayoffFinishedEvent) e);
135 }
136 if (e instanceof StdGameCalendarPlayoutFinishedEvent) {
137 doStdGameCalendarPlayoutFinishedEvent((StdGameCalendarPlayoutFinishedEvent) e);
138 }
139 }
140 });
141
142 game.start();
143 }
144
145
146 /** Saves the scenario */
147 private static void saveScenario() {
148 String filename = new File(settings.getSavegamesPath(), "testscenario.ihm").getAbsolutePath();
149 ObjectOutputStream out;
150 try {
151 FileOutputStream fout = new FileOutputStream(filename);
152 GZIPOutputStream gzout = new GZIPOutputStream(fout);
153 // spart platz :-)
154 out = new ObjectOutputStream(gzout);
155 out.writeObject(scenario);
156 out.flush();
157 out.close();
158 } catch (Exception e) {
159 e.printStackTrace();
160 }
161 }
162
163
164 /** Load a scenario */
165 private static void loadScenario() {
166 String filename = new File(settings.getSavegamesPath(), "testscenario.ihm").getAbsolutePath();
167 ObjectInputStream in;
168 try {
169 FileInputStream fin = new FileInputStream(filename);
170 GZIPInputStream gzin = new GZIPInputStream(fin);
171 // spart platz :-)
172 in = new ObjectInputStream(gzin);
173 scenario = (Scenario) in.readObject();
174 in.close();
175 } catch (Exception e) {
176 e.printStackTrace();
177 }
178 }
179
180
181 /**
182 * Returns the scenario of this game
183 *
184 * @return The scenario of this game
185 */
186 public static Scenario getScenario() {
187 return scenario;
188 }
189
190
191 /**
192 * Gets the settings attribute of the Controller class
193 *
194 * @return The settings value
195 */
196 public static Settings getSettings() {
197 return settings;
198 }
199
200
201 /**
202 * Returns the desktop of this game
203 *
204 * @return The desktop of this game
205 */
206 public static Desktop getDesktop() {
207 return desktop;
208 }
209
210
211
212 /**
213 * Get Translation
214 *
215 * @param key The key of a translation looking for
216 * @return The translation or "@@ NO TRANSLATION
217 * @@" if key is not found
218 */
219 public static String getTranslation(String key) {
220 String result = resources.getTranslationsProperties().getProperty(key);
221 if (result == null) {
222 log("@@ NO TRANSLATION @@ / " + key);
223 return "@@TRANSLATION@@" + key;
224 }
225 return result;
226 }
227
228
229 /**
230 * User Setting lookup
231 *
232 * @param key Key
233 * @return The userSetting value
234 */
235 public static String getUserSetting(String key) {
236 return getUserSetting(key, null);
237 }
238
239
240 /**
241 * User Setting lookup
242 *
243 * @param key Key
244 * @param defaultValue Default value
245 * @return The userSetting value
246 */
247 public static String getUserSetting(String key, String defaultValue) {
248 String result = settings.getSettings().getProperty(key);
249 if (result == null) {
250 log("@@ NO USER SETTING @@ / " + key);
251 }
252 if (defaultValue != null) {
253 return defaultValue;
254 } else {
255 return result;
256 }
257 }
258
259
260 /**
261 * Gets the guiResource attribute of the Controller class
262 *
263 * @param file Description of the Parameter
264 * @param extension Description of the Parameter
265 * @return The guiResource value
266 */
267 public static URL getGuiResource(String file, String extension) {
268 URL result = null;
269 try {
270 result = resources.getGuiResource(file, extension);
271 if (result == null) {
272 log("@@ NO GUI RESOURCE FOUND @@ / " + file + extension);
273 }
274 } catch (Exception err) {
275 log(err.toString());
276 }
277
278 return result;
279 }
280
281
282 /**
283 * log message to file (would someone PLEASE take a look at log4j or jdk 1.4
284 * !!!!)
285 *
286 * @param msg The message for the log
287 */
288 public static void log(String msg) {
289 // Log to screen
290 String timestamp = "";
291 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS ");
292 timestamp = df.format(new Date());
293
294 // No std.io dump
295 System.out.println(timestamp + msg);
296
297 PrintWriter pw = null;
298 FileOutputStream fos = null;
299 try {
300 // Open FileOutputStream for append and PrintWriter with autoflush0
301 String filename = new File(settings.getLogsPath(), "ihm.log").getAbsolutePath();
302 fos = new FileOutputStream(filename, true);
303 pw = new PrintWriter(fos, true);
304 pw.write(timestamp + msg + "\n");
305 pw.close();
306 fos.close();
307 } catch (Exception ex) {
308 try {
309 pw.close();
310 } catch (Exception ignored) {}
311 try {
312 fos.close();
313 } catch (Exception ignored) {}
314 }
315 }
316
317
318
319 /*
320 ==============================================================================
321 METHODS CALLED BY THE GUI
322 ==============================================================================
323 */
324 /** Exits game and saves the scenario */
325 public static void exit() {
326 saveScenario();
327 System.exit(0);
328 }
329
330
331
332 /*
333 ==============================================================================
334 EVENTS CALLED BY GAME THREAD, IF NEEDS ONLINE IS SET IN CALENDAR EVENT
335 ==============================================================================
336 */
337 // ************************** STANDARD EVENTS ******************
338
339 /**
340 * Dear Mr. Big-Shot-Prgrammer. Please start your fancy 3D, dolby surround
341 * game engine here !
342 *
343 * @param e The gameCalendarGameDayEvent
344 */
345 private static void doGameCalendarGameDayEvent(GameCalendarGameDayEvent e) {
346 // Add a actionListener on desktop
347 desktop.addActionListener(
348 new java.awt.event.ActionListener() {
349 public void actionPerformed(ActionEvent e) {
350 desktop.removeActionListener(this);
351 // Remove action listener again
352 game.moveOn();
353 // Move on in the game thread
354 }
355 });
356
357 // Start the desktop
358 desktop.doGameCalendarGameDayEvent(e);
359 }
360
361
362 /**
363 * Special method that displays a message on the desktop without stoping the
364 * game thread
365 *
366 * @param title The title for the short message
367 * @param msg The message to show
368 */
369 public static void showShortMessage(String title, String msg) {
370 desktop.showShortMessage(title, msg);
371 try {
372 Thread.sleep(100);
373 // Stop everything for some time ;-)
374 } catch (Exception ignored) {}
375 }
376
377
378 /**
379 * Start the desktop for a single user and send some greetings ;-)
380 *
381 * @param e The GameCalendarDisplayGuiEvent
382 */
383 private static void doGameCalendarDisplayGuiEvent(GameCalendarDisplayGuiEvent e) {
384 // Add a actionListener on desktop
385 desktop.addActionListener(
386 new java.awt.event.ActionListener() {
387 public void actionPerformed(ActionEvent e) {
388 desktop.removeActionListener(this);
389 // Remove action listener again
390 game.moveOn();
391 // Move on in the game thread
392 }
393 });
394
395 // Start the desktop
396 desktop.doGameCalendarDisplayGuiEvent(e);
397 }
398
399
400 // ************************** STD LEAGUE EVENTS ******************
401
402
403 /**
404 * Show online Message from the StdLeague that LeaguePlayoffs are finished
405 *
406 * @param event The StdGameCalendarLeaguePlayoffFinishedEvent
407 */
408 public static void doStdGameCalendarLeaguePlayoffFinishedEvent(StdGameCalendarLeaguePlayoffFinishedEvent event) {
409 String title = event.getMessage()[0];
410 String msg = event.getMessage()[1];
411
412 // Add a actionListener on frame
413 desktop.addActionListener(
414 new java.awt.event.ActionListener() {
415 public void actionPerformed(ActionEvent e) {
416 desktop.removeActionListener(this);
417 // Remove action listener again
418 game.moveOn();
419 // Move on in the game thread
420 }
421 });
422 // Show message and wait for feedback trough the listener above !
423 desktop.showOnlineMessage(title, msg);
424 }
425
426
427 /**
428 * Show online Message from the StdLeague that the playoffs are finished
429 *
430 * @param event The StdGameCalendarPlayoffFinishedEvent
431 */
432 public static void doStdGameCalendarPlayoffFinishedEvent(StdGameCalendarPlayoffFinishedEvent event) {
433 String title = event.getMessage()[0];
434 String msg = event.getMessage()[1];
435
436 // Add a actionListener on frame
437 desktop.addActionListener(
438 new java.awt.event.ActionListener() {
439 public void actionPerformed(ActionEvent e) {
440 desktop.removeActionListener(this);
441 // Remove action listener again
442 game.moveOn();
443 // Move on in the game thread
444 }
445 });
446 // Show message and wait for feedback trough the listener above !
447 desktop.showOnlineMessage(title, msg);
448 }
449
450
451 /**
452 * Show online Message from the StdLeague that the playouts are finished
453 *
454 * @param event The StdGameCalendarPlayoutFinishedEvent
455 */
456 public static void doStdGameCalendarPlayoutFinishedEvent(StdGameCalendarPlayoutFinishedEvent event) {
457 String title = event.getMessage()[0];
458 String msg = event.getMessage()[1];
459
460 // Add a actionListener on frame
461 desktop.addActionListener(
462 new java.awt.event.ActionListener() {
463 public void actionPerformed(ActionEvent e) {
464 desktop.removeActionListener(this);
465 // Remove action listener again
466 game.moveOn();
467 // Move on in the game thread
468 }
469 });
470 // Show message and wait for feedback trough the listener above !
471 desktop.showOnlineMessage(title, msg);
472 }
473
474
475
476 // ************************** STOP BEFORE GAME DAY EVENT HANDLING ******************
477
478 /**
479 * This is a special Event, called before all GameDays. So Users could make
480 * changes.
481 *
482 * @param e The GameCalendarStopBeforeGameDayEvent
483 */
484 private static void doGameCalendarStopBeforeGameDayEvent(GameCalendarStopBeforeGamedayEvent e) {
485 stopBeforeGameDayUsers = (Vector) e.getInterestedUsers().clone();
486 currentStopBeforeGameDayUser = (User) stopBeforeGameDayUsers.get(0);
487 stopBeforeGamedayEvent = e;
488 cycleTroughUserBeforeGameDay();
489 }
490
491
492 /** Helper to cycle trough all users for a stop before game day event */
493 private static void cycleTroughUserBeforeGameDay() {
494 // In one sentence: This function cycles trough all users who are interested in gameDays
495 // and shows the desktop once for each of them ... pretty wild stuff
496
497 desktop.addActionListener(
498 new java.awt.event.ActionListener() {
499
500 private boolean moreUsersBeforeGameDay() {
501 if (stopBeforeGameDayUsers.lastElement().equals(currentStopBeforeGameDayUser)) {
502 stopBeforeGameDayUsers = null;
503 return false;
504 } else {
505 currentStopBeforeGameDayUser = (User) stopBeforeGameDayUsers.get(stopBeforeGameDayUsers.indexOf(currentStopBeforeGameDayUser) + 1);
506 return true;
507 }
508 }
509
510
511 public void actionPerformed(ActionEvent e) {
512 desktop.removeActionListener(this);
513 if (moreUsersBeforeGameDay()) {
514 cycleTroughUserBeforeGameDay();
515 } else {
516 game.moveOn();
517 }
518 }
519 });
520
521 desktop.doGameCalendarStopBeforeGameDayEvent(currentStopBeforeGameDayUser, stopBeforeGamedayEvent);
522
523 }
524
525 }