Package de.gamobi.jkariam.gui

Source Code of de.gamobi.jkariam.gui.Controller

package de.gamobi.jkariam.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JOptionPane;
import javax.swing.UIManager;

import org.jdom.JDOMException;

import de.gamobi.jkariam.core.Account;
import de.gamobi.jkariam.core.City;

public class Controller implements ActionListener, KeyListener, MouseListener, WindowListener {
 
  private Account account;
 
  private MainWindow mainWindow;
 
  private boolean unsavedContent = false;
 
  public Controller() {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
      e.printStackTrace();
    }
   
    try {
      this.initialize();
    } catch (JDOMException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    this.mainWindow = new MainWindow(this);
    this.mainWindow.refresh();
  }
 
  public void createNewAccount() throws JDOMException, IOException {
    String accountName = "";
    while (accountName.equals("")) {
      accountName = (String) JOptionPane.showInputDialog("Geben Sie den Namen des Accounts ein:");
      if(accountName == null) {
        return;
      }
    }
   
    String server = "";
    while (server.equals("")) {
      server = (String) JOptionPane.showInputDialog("Geben Sie den Namen des Accounts ein:");
      if(server == null) {
        return;
      }
    }
   
    this.account = Account.createNewAccount(accountName, server);
  }
 
  public void exitApplication() {
    if(this.unsavedContent) {
      int choice = JOptionPane.showConfirmDialog(this.mainWindow, "Änderungen vor Beenden sichern?");
      if(choice == JOptionPane.NO_OPTION) {
        System.exit(0);
      } else if(choice == JOptionPane.YES_OPTION) {
        try {
          this.account.saveAccount();
        } catch (Exception e) {
          // TODO show error message
          e.printStackTrace();
        } finally {
          System.exit(0);
        }
      } else {
        // Do nothing and return to application...
      }
    } else {
      System.exit(0);
    }
  }
 
  public void setNewAttributeValue(String cityName, String attributeName, Object aValue) {
    this.account.getCityByName(cityName).setAttributeValue(attributeName, aValue);
  }
 
  private void createNewCity() {
    String cityName = "";
    while (cityName.equals("")) {
      cityName = (String) JOptionPane.showInputDialog("Geben Sie einen Name für die neue Stadt ein:");
      if(cityName == null) {
        return;
      }
    }
   
    City city;
    try {
      city = City.createNewCity(this.account.getPlayer(), cityName);
    } catch (Exception e) {
      e.printStackTrace();
      return;
    }
    this.account.getCities().add(city);
    this.mainWindow.refresh();
  }
 
  private void initialize() throws JDOMException, IOException {
    String option = "";
    String[] files = new File("saves").list();
    int i = 0;
   
    for(String str: files) {
      if(!str.startsWith(".")) i++;
    }
   
    String[] accounts = new String[i + 1];
    i = 0;
   
    for(String str: files) {
      if(!str.startsWith(".")) {
        accounts[i] = str;
        i++;
      }
    }
    accounts[i] = "Neu...";
   
    while (option.equals("")) {
      option = (String) JOptionPane.showInputDialog(null,
          "Welchen Account wollen Sie laden?", "Jkariam",
          JOptionPane.PLAIN_MESSAGE, null, accounts, accounts[0]);
      if(option == null) {
        System.exit(0);
      }
    }
   
    if(option.equals("Neu...")){
      this.createNewAccount();
    } else {
      this.account = new Account(option);
    }
  }

  /**
   * @return the account
   */
  public Account getAccount() {
    return account;
  }

  /**
   * @return the mainWindow
   */
  public MainWindow getMainWindow() {
    return mainWindow;
  }

  /**
   * @return the unsavedContent
   */
  public boolean isUnsavedContent() {
    return unsavedContent;
  }

  /**
   * @param unsavedContent the unsavedContent to set
   */
  public void setUnsavedContent(boolean unsavedContent) {
    this.unsavedContent = unsavedContent;
  }

  @Override
  public void windowActivated(WindowEvent arg0) {
    // TODO Auto-generated method stub
   
  }

  @Override
  public void windowClosed(WindowEvent arg0) {
    // TODO Auto-generated method stub
   
  }

  @Override
  public void windowClosing(WindowEvent arg0) {
    // TODO Auto-generated method stub
    this.exitApplication();
  }

  @Override
  public void windowDeactivated(WindowEvent arg0) {
    // TODO Auto-generated method stub
   
  }

  @Override
  public void windowDeiconified(WindowEvent arg0) {
    // TODO Auto-generated method stub
   
  }

  @Override
  public void windowIconified(WindowEvent arg0) {
    // TODO Auto-generated method stub
   
  }

  @Override
  public void windowOpened(WindowEvent arg0) {
    // TODO Auto-generated method stub
   
  }

  @Override
  public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub
   
  }

  @Override
  public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub
   
  }

  @Override
  public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub
   
  }

  @Override
  public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub
   
  }

  @Override
  public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub
   
  }

  @Override
  public void keyPressed(KeyEvent arg0) {
    // TODO Auto-generated method stub
   
  }

  @Override
  public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub
   
  }

  @Override
  public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub
   
  }

  @Override
  public void actionPerformed(ActionEvent arg0) {
    if(arg0.getActionCommand().equals("refresh")) this.mainWindow.refresh();
    else if(arg0.getActionCommand().equals("createNewCity")) this.createNewCity();
  }
}
TOP

Related Classes of de.gamobi.jkariam.gui.Controller

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.