Package settlers.ui.server

Source Code of settlers.ui.server.ServerGUI$RestoreAction

package settlers.ui.server;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.BlockingQueue;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.KeyStroke;

import settlers.networking.ServerHandeler;

public class ServerGUI implements Runnable {

  static JFrame uiFrame = new JFrame("Settler Server");
  static private JMenuBar menuBar;
  private ServerHandeler serverHandeler = new ServerHandeler();
  private JTabbedPane gameTabs;
  MakeGamePanel makeGamePanel = new MakeGamePanel();

  TrayIcon trayIcon;

  public static void main(String[] args) {
    (new Thread(new ServerGUI())).start();
  }

  public void init() {
    makeMenu();

    makeGamePanel.createGameButton
        .addActionListener(new CreateGameAction());

    gameTabs = new JTabbedPane();
    gameTabs.addTab("+", makeGamePanel);

    gameTabs.setTabPlacement(javax.swing.JTabbedPane.BOTTOM);

    uiFrame.setLayout(new BorderLayout());
    uiFrame.setJMenuBar(menuBar);
    uiFrame.setPreferredSize(new Dimension(400, 250));
    uiFrame.add(gameTabs, BorderLayout.CENTER);
    uiFrame.pack();
    uiFrame.setLocationRelativeTo(null); // center on screen
    uiFrame.setVisible(true);

    uiFrame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        if (SystemTray.isSupported() && serverHandeler.hasGame())
          // when there is a system tray we can hide the window and
          // later the user can restore it from the tray
          uiFrame.setVisible(false);
        else
          // otherwise exit on close
          quit();
      }
    });

    makeTrayIcon();
  }

  private void makeTrayManu(PopupMenu popup) {
    MenuItem quitItem = new MenuItem("Quit");
    quitItem.addActionListener(new QuitAction());
    popup.add(quitItem);

    MenuItem restoreItem = new MenuItem("Restore");
    restoreItem.addActionListener(new RestoreAction());
    popup.add(restoreItem);
  }

  private void makeMenu() {
    JMenu gameMenu, helpMenu;

    menuBar = new JMenuBar();

    gameMenu = new JMenu("Server");
    gameMenu.setMnemonic(KeyEvent.VK_G);

    JMenuItem newGameItem = new JMenuItem("New Game");
    newGameItem.setMnemonic(KeyEvent.VK_N);
    newGameItem.setAccelerator(KeyStroke.getKeyStroke("control N"));

    JMenuItem quitItem = new JMenuItem("Quit");
    quitItem.setMnemonic(KeyEvent.VK_Q);
    quitItem.setAccelerator(KeyStroke.getKeyStroke("control Q"));

    gameMenu.add(newGameItem);
    gameMenu.add(quitItem);

    helpMenu = new JMenu("Help");
    helpMenu.setMnemonic(KeyEvent.VK_H);

    JMenuItem helpContentsItem = new JMenuItem("Help Contents");
    helpContentsItem.setAccelerator(KeyStroke.getKeyStroke("F1"));
    helpMenu.add(helpContentsItem);

    menuBar.add(gameMenu);
    menuBar.add(helpMenu);

    quitItem.addActionListener(new QuitAction());
    newGameItem.addActionListener(new OpenNewGameMaker());
  }

  class QuitAction implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      if (serverHandeler.hasGame()) {
        int answer = JOptionPane.showConfirmDialog(uiFrame,
            QUIT_WARNING, QUIT_WARNING_TITLE,
            JOptionPane.YES_NO_OPTION);
        if (answer == JOptionPane.YES_OPTION) {
          quit();
        }
      } else {
        quit();
      }
    }
  }

  class CreateGameAction implements ActionListener {
    public void actionPerformed(ActionEvent arg0) {
      try {
        int port = makeGamePanel.getPort();
        if (port < 0 || port > 65536) {
          JOptionPane.showMessageDialog(makeGamePanel,
              "Port must be a number 0 to 65536 inclusive.");
          return;
        }
        ServerPanel newGamePanel = new ServerPanel();
        BlockingQueue<String> newServerOutQueue = serverHandeler
            .newGame(port, makeGamePanel.getNumberPlayers());
        gameTabs.add(newGamePanel, makeGamePanel.getGameName());
        (new Thread(new ServerListener(newServerOutQueue, newGamePanel)))
            .start();
      } catch (NumberFormatException e) {
        JOptionPane
            .showMessageDialog(makeGamePanel,
                "Port was not a number. Port must be a number 0 to 65536 inclusive.");
      }
    }
  }

  class ServerListener implements Runnable {
    BlockingQueue<String> queue;
    ServerPanel serverPanel;

    ServerListener(BlockingQueue<String> queue, ServerPanel serverPanel) {
      this.queue = queue;
      this.serverPanel = serverPanel;
    }

    public void run() {
      try {
        while (true) {
          serverPanel.addMessage(queue.take());
        }
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }

  public void quit() {
    System.exit(0);
  }

  public boolean gameRunning() {
    return true;
  }

  class RestoreAction implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      uiFrame.setVisible(true);
    }
  }

  class OpenNewGameMaker implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      gameTabs.setSelectedIndex(0);
    }
  }

  public void processGameCommand(String command) {
    String[] temp = command.split(" ");
    System.out.println(Arrays.toString(temp));
  }

  public void run() {
    init();
  }

  private void makeTrayIcon() {
    if (SystemTray.isSupported()) {

      SystemTray tray = SystemTray.getSystemTray();

      Image image;
      try {
        image = ImageIO.read(new File(
            "assets\\themes\\default\\tray.png"));
      } catch (IOException e1) {
        image = new BufferedImage(1, 1, BufferedImage.OPAQUE);
      }

      PopupMenu popup = new PopupMenu();
      makeTrayManu(popup);

      trayIcon = new TrayIcon(image, "Settler Server", popup);
      trayIcon.setImageAutoSize(true);

      try {
        tray.add(trayIcon);
      } catch (AWTException e) {
        System.err.println("TrayIcon could not be added.");
      }

    } else {
      System.out.println("System Tray not supported.");
    }
  }

  private static final String QUIT_WARNING = "Quitting will stop all games you are hosting. Really Quit?";
  private static final String QUIT_WARNING_TITLE = "Really Quit?";
}
TOP

Related Classes of settlers.ui.server.ServerGUI$RestoreAction

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.