Package net.sf.arianne.marboard.client.gui

Source Code of net.sf.arianne.marboard.client.gui.MainWindow

package net.sf.arianne.marboard.client.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;

import marauroa.client.ClientFramework;
import net.sf.arianne.marboard.client.core.CurrentBoard;
import net.sf.arianne.marboard.client.gui.component.DrawingArea;
import net.sf.arianne.marboard.client.gui.dialog.Log4jSwingAppender;
import net.sf.arianne.marboard.client.gui.menu.MenuBuilder;
import net.sf.arianne.marboard.client.gui.toolbar.ToolBarBuilder;

import org.apache.log4j.Logger;

/**
* The main window of Marboard
*
* @author hendrik
*/
public class MainWindow {
  private static Logger logger = Logger.getLogger(MainWindow.class);

  private JFrame frame;
  private DrawingArea drawingArea;
  private CurrentBoard board;
  private ClientFramework client;
  boolean keepRunning = true;
  private BoardState boardState;

  /**
   * creates a new MainWindow
   *
   * @param client ClientFramework
   * @param board the whiteboard with the shape information
   */
  public MainWindow(ClientFramework client, CurrentBoard board) {
    this.client = client;
    this.board = board;
    this.boardState = new BoardState();
  }

  /**
   * creates and displays the main window
   */
  public void createWindow() {
    nativeLookAndFeel();

    frame = new JFrame();

    Logger.getRootLogger().addAppender(new Log4jSwingAppender());
    Log4jSwingAppender.setMainFrame(frame);

    // title bar
    frame.setTitle("Marboard");
    frame.setIconImage(new ImageIcon(getClass().getResource("marboard-icon.png")).getImage());

    // window operation handlers
    frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    frame.addWindowListener(new WindowEventHandler(this));

    // size and layout
    frame.setSize(new Dimension(640, 440));
    frame.setLayout(new BorderLayout());

    // drawing area
    drawingArea = new DrawingArea(board, boardState);

    // pannel for the toolbar and drawing area
    JPanel panel = new JPanel(new BorderLayout());
    panel.add(new ToolBarBuilder().createToolBar(boardState,this),BorderLayout.NORTH);
    panel.add(drawingArea, BorderLayout.CENTER);
    frame.add(panel);

    // menu par
    frame.setJMenuBar(new MenuBuilder(this, board, boardState).create());

    // and finally make it visible
    frame.setVisible(true);
  }

  /**
   * tries to use the native look and feel of the plattform.
   */
  private void nativeLookAndFeel() {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (UnsupportedLookAndFeelException e) {
      logger.warn("Unable to load native look and feel", e);
    } catch (ClassNotFoundException e) {
      logger.warn("Unable to load native look and feel", e);
    } catch (InstantiationException e) {
      logger.warn("Unable to load native look and feel", e);
    } catch (IllegalAccessException e) {
      logger.warn("Unable to load native look and feel", e);
    }   
  }
 
  /**
   * the main "game" loop
   */
  public void loop() {
    while (keepRunning) {
      client.loop(0);
      try {
        Thread.sleep(1);
      } catch (InterruptedException e) {
        logger.error(e, e);
      }
    }

    // clean up
    frame.dispose();
    try {
      client.logout();
    } catch (Exception e) {
      logger.error(e, e);
    }

  }

  /**
   * closes the client
   */
  public void quit() {
    keepRunning = false;
  }

  /**
   * gets the main frame
   *
   * @return JFrame
   */
  public JFrame getMainFrame() {
    return frame;
  }

  /**
   * gets the drawing area
   *
   * @return DrawingArea
   */
  public DrawingArea getDrawingArea() {
    return drawingArea;
  }
}
TOP

Related Classes of net.sf.arianne.marboard.client.gui.MainWindow

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.