Package hu.u_szeged.nbo.client.ui

Source Code of hu.u_szeged.nbo.client.ui.NBOClientMainWindow

package hu.u_szeged.nbo.client.ui;

import hu.u_szeged.nbo.client.ui.components.logger.ClientLoggerComponent;
import hu.u_szeged.nbo.client.ui.components.perspective.Perspective;
import hu.u_szeged.nbo.client.ui.components.perspective.ResourceAllocationPerspective;
import hu.u_szeged.nbo.client.ui.components.perspective.SettingsPerspective;
import hu.u_szeged.nbo.client.ui.components.perspective.TransportationTaskPerspective;
import hu.u_szeged.nbo.client.ui.components.perspective.UpdateClientPerspective;
import hu.u_szeged.nbo.client.ui.components.problemtree.ProblemBrowserTree;
import hu.u_szeged.nbo.client.ui.components.statusbar.ExtendedStatusLineManager;
import hu.u_szeged.nbo.client.ui.components.statusbar.StatusBar;
import hu.u_szeged.nbo.client.ui.event.EventCentral;
import hu.u_szeged.nbo.client.ui.thread.StatusBarRefresherThread;
import hu.u_szeged.nbo.client.util.log.ClientLogManager;
import hu.u_szeged.nbo.client.util.lowlevel.ConfigurationManager;
import hu.u_szeged.nbo.client.util.ui.ImageManager;

import java.io.File;
import java.util.Date;

import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.CoolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.action.ToolBarContributionItem;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

public class NBOClientMainWindow extends ApplicationWindow {
  private static NBOClientMainWindow mainWindow;

  private Composite mainContainer;
  private Composite loggerContainer;
  private Composite workspace;

  private ResourceAllocationPerspective spPerspective;
  private SettingsPerspective settingsPerspective;
  private TransportationTaskPerspective ttPerspective;
  private UpdateClientPerspective updateClientPerspective;
  private Perspective actualPerspective;

  private ClientLoggerComponent logger;

  private MenuManager mainMenu;
  private MenuManager fileMenu;
  private MenuManager editMenu;
  private MenuManager viewMenu;
  private MenuManager problemMenu;
  private MenuManager helpMenu;

  private EventCentral events;

  private ImageManager imageManager;

  private GridLayout mainLayout;
  private StackLayout workspaceStack;
  private GridData workspaceStackLayoutData;

  private ProblemBrowserTree problemTree;

  private StatusBar statusBar;

  public NBOClientMainWindow() {
    super(null);

    this.events = new EventCentral(this);
    this.imageManager = new ImageManager();

    this.createComponents();
    this.create();
    this.getShell().setMinimumSize(780, 680);
    this.getShell().setMaximized(Boolean.parseBoolean(ConfigurationManager.settings.get("maximize")));

    ClientLogManager.setLoggerComponent(logger);
    ClientLogManager.addUserLog("NBO Client started.", false);
   
    statusBar = (StatusBar)this.getStatusLineManager().getControl();
    statusBar.setDate(new Date());
    statusBar.setUpdatesLabel();
    statusBar.setUser(ConfigurationManager.settings.get("username"));

    new StatusBarRefresherThread(this).start();
  }

  protected MenuManager createMenuManager() {
    this.mainMenu = super.createMenuManager();
    this.fileMenu = new MenuManager("&File");
    this.editMenu = new MenuManager("&Edit");
    this.viewMenu = new MenuManager("&View");
    this.problemMenu = new MenuManager("&Problem");
    this.helpMenu = new MenuManager("&Help");

    this.fileMenu.add(events.newFileAct);
    this.fileMenu.add(events.openProblemAct);
    this.fileMenu.add(events.saveProblemAct);
    this.fileMenu.add(new Separator());
    this.fileMenu.add(events.settingsAct);
    this.fileMenu.add(new Separator());
    this.fileMenu.add(events.refreshTTasksAct);
    this.fileMenu.add(new Separator());
    this.fileMenu.add(events.exitAppAct);

    this.problemMenu.add(events.solveProblemAct);
    this.problemMenu.add(events.clearProblemAct);
    this.problemMenu.add(events.closeFileAct);  

    this.helpMenu.add(events.aboutAppAct);
    this.helpMenu.add(new Separator());
    this.helpMenu.add(events.updatesAct);

    this.mainMenu.add(this.fileMenu);
    this.mainMenu.add(this.editMenu);
    this.mainMenu.add(this.viewMenu);
    this.mainMenu.add(this.problemMenu);
    this.mainMenu.add(this.helpMenu);

    return this.mainMenu;
  }

  protected CoolBarManager createCoolBarManager(int style) {
    CoolBarManager coolBarManager = new CoolBarManager(style);
    coolBarManager.setLockLayout(false);               
    int toolBarStyle = SWT.FLAT | SWT.WRAP;

    /* Create "File" group */
    ToolBarManager toolBarManager = new ToolBarManager(toolBarStyle);
    ToolBarContributionItem toolItem = new ToolBarContributionItem(toolBarManager);

    ActionContributionItem newFileItem = new ActionContributionItem(events.newFileAct);
    ActionContributionItem openFileItem = new ActionContributionItem(events.openProblemAct);
    ActionContributionItem saveFileItem = new ActionContributionItem(events.saveProblemAct);

    toolBarManager.add(newFileItem);
    toolBarManager.add(openFileItem);
    toolBarManager.add(saveFileItem);
    coolBarManager.add(toolItem);

    /* Create "Solution" group */
    toolBarManager = new ToolBarManager(toolBarStyle);
    toolItem = new ToolBarContributionItem(toolBarManager);

    ActionContributionItem solveItem = new ActionContributionItem(events.solveProblemAct);

    toolBarManager.add(solveItem);
    coolBarManager.add(toolItem);

    return coolBarManager;
  }

  protected Control createContents(Composite parent) {
    this.mainLayout = new GridLayout(4, true);
    this.workspaceStackLayoutData = new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1);
    this.workspaceStack = new StackLayout();

    this.mainContainer = new Composite(parent, SWT.NONE);
    this.mainContainer.setLayout(mainLayout);
    this.mainContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 4, 2));

    this.problemTree = new ProblemBrowserTree(mainContainer, this);

    this.workspace = new Composite(this.mainContainer, SWT.NONE);
    this.workspace.setLayout(this.workspaceStack);
    this.workspace.setLayoutData(this.workspaceStackLayoutData);

    this.loggerContainer = new Composite(mainContainer, SWT.NONE);
    this.logger = new ClientLoggerComponent(loggerContainer, this);

    this.spPerspective = new ResourceAllocationPerspective(this);
    this.ttPerspective = new TransportationTaskPerspective(this);
    this.settingsPerspective = new SettingsPerspective(this);
   
    this.updateClientPerspective = new UpdateClientPerspective(this);

    if (!new File("lastSession.settings").exists()) {
      this.settingsPerspective.getContent().setSelection(0);
      this.changePerspective(this.settingsPerspective);
    }

    else
      this.changePerspective(this.spPerspective)
    }

    return this.mainContainer;
  }

  protected void createComponents() {
    this.addMenuBar();
    this.addCoolBar(SWT.FLAT);
    this.addStatusLine()
  }

  protected StatusLineManager createStatusLineManager() {
    StatusLineManager slm = new ExtendedStatusLineManager();

    return slm;
  }

  protected void configureShell(Shell shell) {
    super.configureShell(shell);       

    this.setShellStyle(SWT.MIN | SWT.MAX | SWT.RESIZE);
    shell.setText("NBO Client 0.1");     
    shell.setImage(new Image(null, "img/nbo.png"));
  }

  public void changePerspective(Perspective perspective) {
    if (perspective instanceof ResourceAllocationPerspective) {
      if (this.actualPerspective != this.spPerspective) {
        this.workspaceStack.topControl = this.spPerspective.getContent();

        this.actualPerspective = perspective;
      }
    }
   
    if (perspective instanceof TransportationTaskPerspective) {
      if (this.actualPerspective != this.ttPerspective) {
        this.workspaceStack.topControl = this.ttPerspective.getContent();

        this.actualPerspective = perspective;
      }
    }

    if (perspective instanceof SettingsPerspective) {
      if (this.actualPerspective != this.settingsPerspective) {
        this.workspaceStack.topControl = this.settingsPerspective.getContent();

        this.actualPerspective = perspective;
      }
    }

    if (perspective instanceof UpdateClientPerspective) {
      if (this.actualPerspective != this.updateClientPerspective) {
        this.workspaceStack.topControl = this.updateClientPerspective.getContent();

        this.actualPerspective = perspective;
      }
    }


    this.workspace.layout();
  }

  public boolean close() {
    ClientLogManager.addUserLog("Cleaning up...", false);

    /*    Problem root = (Problem)getProblemBrowserTree().getRoot();

    System.out.println("close() " + root.getProblems().toArray()[1]);

    for (ResourceAllocation raProblem: ((Problem)root.getProblems().toArray()[1]).getRAProblems()) {
      if (raProblem.isStarted()) {
        raProblem.getSolverThread().stopSolving();
        SolutionMonitorThread.monitors.get(raProblem.getName()).stopMonitoring();

        System.out.println("for: " + raProblem);

        if (!raProblem.isSentToServer()) {
          System.out.println("not sent: " + raProblem);
          raProblem.clearStates();
        }
      }
    }

    try {Logger.addUserLog( " Cleaning up...");
      Thread.sleep(1000);
    }

    catch (InterruptedException e) { }
     */   
    ConfigurationManager.saveSettings();
   
    if (Boolean.parseBoolean(ConfigurationManager.settings.get("save_on_exit"))) {
      ConfigurationManager.saveProblems();
    }

    return super.close();
  }

  public ResourceAllocationPerspective getRAPerspective() {
    return this.spPerspective;
  }
 
  public TransportationTaskPerspective getTTPerspective() {
    return this.ttPerspective;
  }

  public SettingsPerspective getSPerspective() {
    return this.settingsPerspective;
  }

  public UpdateClientPerspective getUpdateClientPerspective() {
    return this.updateClientPerspective;
  }

  public Composite getWorkspace() {
    return this.workspace;
  }   

  public ProblemBrowserTree getProblemBrowserTree() {
    return this.problemTree;
  }

  public EventCentral getEventCentral() {
    return this.events;
  }

  public ClientLoggerComponent getLogger() {
    return this.logger;
  }

  public StatusBar getStatusBar() {
    return statusBar;
  }

  public ImageManager getImageManager() {
    return this.imageManager;
  }

  public static NBOClientMainWindow getInstance() {
    return mainWindow;
  }

  public static void main(String[] args) {
    ConfigurationManager.getSettings();
    ConfigurationManager.getProblems();

    mainWindow = new NBOClientMainWindow();
    mainWindow.setBlockOnOpen(true);      
    mainWindow.open();

    System.exit(0);
  }
}
TOP

Related Classes of hu.u_szeged.nbo.client.ui.NBOClientMainWindow

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.