00001 package dionarap;
00002
00003 import java.awt.BorderLayout;
00004 import java.awt.GridLayout;
00005 import java.awt.Point;
00006 import java.io.File;
00007 import java.io.IOException;
00008 import java.net.MalformedURLException;
00009 import java.net.URL;
00010 import java.util.Vector;
00011
00012 import javax.swing.JButton;
00013 import javax.swing.JDialog;
00014 import javax.swing.JEditorPane;
00015 import javax.swing.JFrame;
00016 import javax.swing.JPanel;
00017 import javax.swing.JScrollPane;
00018 import javax.swing.SwingUtilities;
00019 import javax.swing.UIManager;
00020
00021 import de.dionarap.leveleditor.gui.LevelEditor;
00022 import de.dionarap.leveleditor.model.GameSettingsModel;
00023 import de.fhwgt.dionarap.controller.DionaRapController;
00024 import de.fhwgt.dionarap.levelreader.LevelReader;
00025 import de.fhwgt.dionarap.model.data.DionaRapModel;
00026 import de.fhwgt.dionarap.model.data.MTConfiguration;
00027 import de.fhwgt.dionarap.model.objects.AbstractPawn;
00028
00048 public class Hauptfenster extends JFrame {
00049
00051 private static final long serialVersionUID = 4932458420387758006L;
00052
00054 private static String titel = "DionaRap - TM, RH";
00055
00057 private Spielfeld spielfeld;
00058
00060 private DionaRapModel model;
00061
00063 private DionaRapController controller;
00064
00066 private JDialog helpDialog = this.createHelpDialog();
00067
00069 private Navigator navigator = new Navigator(this);
00070
00072 private JDialog iconDialog = createDescriptionWindow();
00073
00075 private JDialog levelEditorDialog = createLevelEditorWindow();
00076
00078 private String levelname;
00079
00081 private Vector<String> levelnames;
00082
00084 public static final String levelDefaultName = "default";
00085
00087 public static final String levelPrefix = "level_";
00088
00089
00098 public Hauptfenster(Point position, String level) {
00099 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
00100
00102 this.setTitle(titel);
00103
00105 this.setResizable(false);
00106
00108 this.setLayout(new BorderLayout());
00109
00111 this.setLocation(position);
00112
00113 this.levelname = level;
00114 this.addComponentListener(new ListenerFenster(this.navigator));
00115 this.setupLevels();
00116 this.readLevel();
00117
00118 this.model.addModelChangedEventListener(new ListenerModel(this));
00119
00120 this.setJMenuBar(new MenuBar(this, new ListenerMenu(this)));
00121
00122 this.spielfeld = new Spielfeld( this.model.getGrid().getGridSizeX(), this.model.getGrid().getGridSizeY() );
00123 this.getContentPane().add(this.spielfeld);;
00124
00125 this.updateGame(GameStatus.game);
00126
00127 this.navigator.setNewGameButtonStatus(false);
00128 this.navigator.setVisible(true);
00129
00131 this.pack();
00132 this.setVisible(true);
00133 }
00134
00142 public static void main(String[] args) {
00143 new Hauptfenster(new Point(60, 60), Hauptfenster.levelDefaultName);
00144 }
00145
00146
00153 protected DionaRapController getController() {
00154 return this.controller;
00155 }
00156
00157
00164 protected DionaRapModel getModel() {
00165 return this.model;
00166 }
00167
00168
00176 private void setupLevels() {
00177 this.levelnames = new Vector<String>();
00178 this.levelnames.add(Hauptfenster.levelDefaultName);
00179 try {
00180 File f = new File(this.getDirectory("xml"));
00181 File[] files = f.listFiles();
00182 for (int i = 0; i < files.length; i++) {
00183 if (!files[i].isFile()) continue;
00184 if (!files[i].getName().startsWith(Hauptfenster.levelPrefix)) continue;
00185
00186 String filename = files[i].getName();
00187 filename = filename.substring(Hauptfenster.levelPrefix.length(), filename.length() - ".xml".length());
00188
00189 if (filename.equalsIgnoreCase(Hauptfenster.levelDefaultName)) {
00190 System.out.println("Reservierter Levelname " + filename);
00191 continue;
00192 }
00193
00194 this.levelnames.add(filename);
00195 }
00196 } catch (Exception e) {
00200 }
00201 }
00202
00203
00218 private void readLevel() {
00219 if (this.levelname.equals(Hauptfenster.levelDefaultName)) {
00220 this.model = new DionaRapModel();
00221 this.controller = new DionaRapController(this.model);
00222 this.controller.setMultiThreaded(getDefaultConfiguration());
00223 return;
00224 }
00225
00226 System.out.println("Lade Level: " + this.levelname);
00227 this.model = new DionaRapModel();
00228 this.controller = new DionaRapController(this.model);
00229 MTConfiguration mt = new MTConfiguration();
00230
00231 LevelReader lr = new LevelReader(mt, model);
00232 try {
00233 lr.readLevel(this.getLevelFilename(this.levelname));
00234 this.controller.setMultiThreaded(mt);
00235 } catch (Exception e) {
00236 System.out.println("Fehler beim Einlesen der XML-Leveldatei: " + e);
00237 }
00238 }
00239
00240
00247 private MTConfiguration getDefaultConfiguration() {
00248 MTConfiguration mt = new MTConfiguration();
00249 mt.setAlgorithmAStarActive(true);
00250 mt.setAvoidCollisionWithOpponent(true);
00251 mt.setAvoidCollisionWithObstacles(true);
00252 mt.setMinimumTime(800);
00253 mt.setShotGetsOwnThread(true);
00254 mt.setOpponentStartWaitTime(2000);
00255 mt.setOpponentWaitTime(2000);
00256 mt.setShotWaitTime(500);
00257 mt.setRandomOpponentWaitTime(false);
00258 mt.setDynamicOpponentWaitTime(false);
00259
00260 return mt;
00261 }
00262
00263
00269 private String getLevelFilename(String level) {
00270 return this.getDirectory("xml") + Hauptfenster.levelPrefix + level + ".xml";
00271 }
00272
00273
00279 private String getDirectory(String directory) {
00280 return System.getProperty("user.dir") + File.separator + directory + File.separator;
00281 }
00282
00283
00294 public void updateGame(GameStatus status) {
00295 if ((this.model == null) || (this.spielfeld == null)) return;
00296
00297 this.navigator.setPoints(this.model.getScore());
00298 this.spielfeld.cleanupSpielfeld();
00299 if (status != GameStatus.game) {
00300 this.navigator.setNewGameButtonStatus(true);
00301 }
00302 this.spielfeld.updateStatus(status);
00303 AbstractPawn[] pawns = this.model.getAllPawns();
00304 for (int i = 0; i < pawns.length; i++) {
00305 this.spielfeld.setPawn(pawns[i]);
00306 }
00307 }
00308
00314 public void showLevelEditor() {
00315 this.levelEditorDialog.setVisible(true);
00316 }
00317
00318
00324 private JDialog createLevelEditorWindow() {
00325 JDialog levDialog;
00326 levDialog = new JDialog();
00327
00328 JPanel panel = new JPanel();
00329 panel.add(new LevelEditor(new GameSettingsModel()));
00330 panel.setVisible(true);
00331
00332 levDialog.add(panel, BorderLayout.CENTER);
00333 levDialog.pack();
00334
00335 return levDialog;
00336 }
00337
00338
00344 public void showDescriptionWindow() {
00345 this.iconDialog.setVisible(true);
00346 }
00347
00348
00355 private JDialog createDescriptionWindow() {
00356 Artwork theme = Artwork.getTheme();
00357
00358 JDialog descDialog;
00359 descDialog = new JDialog();
00360 descDialog.setLayout(new BorderLayout());
00361
00362 JPanel panel = new JPanel();
00363 panel.setLayout(new GridLayout(1,3));
00364
00365 panel.add(new PaintGraphic("Spieler", theme.getPlayerIcon(5)));
00366 panel.add(new PaintGraphic("Gegner", theme.getOpponentIcon()));
00367 panel.add(new PaintGraphic("Hindernis", theme.getObstacleIcon()));
00368 panel.setVisible(true);
00369
00370 JButton descCloseButton = new JButton("Schlie§en");
00371 descCloseButton.setActionCommand(MenuBar.CMD_CLOSEWINDOW);
00372 descCloseButton.addActionListener(new ListenerDialog());
00373
00374 descDialog.add(panel, BorderLayout.CENTER);
00375 descDialog.add(descCloseButton, BorderLayout.SOUTH);
00376
00377 descDialog.pack();
00378
00379 return descDialog;
00380 }
00381
00382
00389 public void setArtwork(String theme) {
00390 Artwork.getTheme().setTheme(theme);
00391
00392 JDialog tmp = this.createDescriptionWindow();
00393 tmp.setLocation(this.iconDialog.getLocation());
00394 tmp.setVisible(this.iconDialog.isVisible());
00395 this.iconDialog.dispose();
00396 this.iconDialog = tmp;
00397 this.requestFocus();
00398
00399 this.updateGame(GameStatus.game);
00400 }
00401
00402
00410 public void setLookAndFeel(String lookAndFeel) {
00411 try {
00412 UIManager.setLookAndFeel(lookAndFeel);
00413 SwingUtilities.updateComponentTreeUI(this);
00414 SwingUtilities.updateComponentTreeUI(this.navigator);
00415 SwingUtilities.updateComponentTreeUI(this.helpDialog);
00416 SwingUtilities.updateComponentTreeUI(this.iconDialog);
00417 } catch (Exception e) {
00418 System.out.println("setLookAndFeel Exception " + e);
00419 }
00420
00421 this.pack();
00422 this.navigator.pack();
00423 }
00424
00425
00430 public String getCurrentLevel() {
00431 return this.levelname;
00432 }
00433
00434
00441 public void startNewGame(String level) {
00442 Point location = this.getLocation();
00443
00444 this.model.clear();
00445
00446 this.navigator.dispose();
00447 this.iconDialog.dispose();
00448 this.dispose();
00449
00450 new Hauptfenster(location, level);
00451 }
00452
00453
00460 private JDialog createHelpDialog() {
00461 JDialog dlg = new JDialog();
00462 dlg.setSize(800, 600);
00463 dlg.setTitle("Spielhilfe");
00464 dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
00465 dlg.setLayout(new BorderLayout());
00466
00467 JEditorPane editorPane = new JEditorPane();
00468 JScrollPane scrollPane = new JScrollPane(editorPane,
00469 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
00470 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
00471
00472 editorPane.setEditable(false);
00473
00474 try {
00475 editorPane.setPage(getHelpFile());
00476 }
00477 catch (IOException e) {
00478 e.printStackTrace();
00479 }
00480
00481 JButton helpCloseButton = new JButton("Schließen");
00482 helpCloseButton.setActionCommand(MenuBar.CMD_CLOSEWINDOW);
00483 helpCloseButton.addActionListener(new ListenerDialog());
00484
00485 dlg.add(scrollPane, BorderLayout.CENTER);
00486 dlg.add(helpCloseButton, BorderLayout.SOUTH);
00487
00488 return dlg;
00489 }
00490
00496 public void showHelpWindow() {
00497 this.helpDialog.setVisible(true);
00498 }
00499
00500
00506 private String getHelpDirectory() {
00507 return this.getDirectory("html");
00508 }
00509
00510
00518 private URL getHelpFile() {
00519 String helpFilename = this.getHelpDirectory() + "help.html";
00520 File helpFile = new File(helpFilename);
00521 URL helpURL = null;
00522 try {
00523 helpURL = helpFile.toURI().toURL();
00524 }
00525 catch (MalformedURLException e) {
00526 e.printStackTrace();
00527 }
00528 return helpURL;
00529 }
00530
00531
00537 public String[] getLevels() {
00538 String[] levelarray = new String[this.levelnames.size()];
00539 for (int i = 0; i < levelarray.length; i++) {
00540 levelarray[i] = this.levelnames.get(i);
00541 }
00542
00543 this.getTitle();
00544
00545 return levelarray;
00546 }
00547
00548
00554 public void toggleNavigator() {
00555 this.navigator.setVisible(!this.navigator.isVisible());
00556 }
00557
00558 }