Package gri.tasks.gui

Source Code of gri.tasks.gui.TestShell$SimpleExitAction

/*
* File: TestShell.java
* Author: Daniel Rogers
* Created on Apr 11, 2008
*
*/
package gri.tasks.gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import javax.swing.*;

import gri.tasks.AsyncTaskController;
import gri.tasks.managers.JobManager;
import gri.tasks.managers.JobInfo;
import gri.tasks.managers.JobManagerEntry;
import gri.tasks.managers.BasicJobManager;
import gri.tasks.managers.TaskCollection;
import gri.tasks.managers.JobManagerSet;

import gri.tasks.gui.util.JobEntry;
import gri.tasks.gui.widgets.InputParameterWidgetFactory;
import gri.tasks.gui.widgets.OutputParameterWidgetFactory;
import gri.tasks.gui.widgets.ParameterWidgetFactory;
import gri.tasks.gui.status.StatusListFrame;

import dan.swing.event.DefaultButtonUpdater;
import dan.swing.menus.FlexibleMenuBar;

import java.util.List;

/**
* Utility class for running a graphical application exposing
* tasks from a JobManager.
*
* @author dan.rogers
*/
public class TestShell {

  ParameterWidgetFactory inputWidgetFactory;
  ParameterWidgetFactory outputWidgetFactory;

  // ------------------------------------------------ Constructors

  public TestShell() {
    inputWidgetFactory = new InputParameterWidgetFactory();
    outputWidgetFactory = new OutputParameterWidgetFactory();
  }

  // -------------------------------------------------- Accessors

  public void setInputWidgetFactory(ParameterWidgetFactory factory) {
    this.inputWidgetFactory = factory;
  }
  public void setOutputWidgetFactory(ParameterWidgetFactory factory) {
    this.outputWidgetFactory = factory;
  }

  // --------------------------------------------- Implementation

  public void runTest(JobManagerSet jobManagers) {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch(Exception e) {
      e.printStackTrace();
    }

    //panel:
    MainPanel panel = new MainPanel(jobManagers);
    panel.setInputWidgetFactory(this.inputWidgetFactory);

    StatusDisplayListener submitListener = new StatusDisplayListener();
    JobOutputViewer jobViewer = new JobOutputViewer(outputWidgetFactory);
    submitListener.setJobViewer(jobViewer);

    panel.addSubmitListener(submitListener);

    //frame:
    JFrame frame = new JFrame("Test");

    //menu bar:
    FlexibleMenuBar menuBar = new FlexibleMenuBar();

    menuBar.add("File");
    menuBar.addSeparator("File");

    //Action exitAction = new SimpleExitAction();
    Action exitAction = new SafeExitAction(panel, submitListener.getStatusFrame());
    JMenuItem mnuiExit = new JMenuItem(exitAction);
    mnuiExit.setText("Exit");
    menuBar.add("File", mnuiExit);

    JMenuItem mnuiPrintJobs = new JMenuItem(new PrintJobsAction(jobManagers));
    mnuiPrintJobs.setText("Print Jobs");
    menuBar.add("Test", mnuiPrintJobs);

    JMenuItem mnuiShowJobs = new JMenuItem(new ShowFrameAction(submitListener.getStatusFrame()));
    mnuiShowJobs.setText("Show Jobs");
    menuBar.add("Test", mnuiShowJobs);

    frame.setJMenuBar(menuBar);

    addJobs(jobManagers, submitListener);

    //display:
    frame.getContentPane().add(panel, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    frame.addWindowListener(new WindowCloseListener(exitAction));
    frame.pack();
   
    //enable default button setting:
    DefaultButtonUpdater updater = new DefaultButtonUpdater(frame);
    panel.setDefaultButtonChangeListener(updater);

    frame.setVisible(true);
  }
  public void runTest(JobManager jobManager) {
    JobManagerSet managers = new JobManagerSet();
    managers.addJobManager("My Job Manager", jobManager);
    runTest(managers);
  }
  public void runTest(TaskCollection tasks) {
    runTest(new BasicJobManager(tasks));
  }

  protected void addJobs(JobManagerSet jobManagers, StatusDisplayListener display) {
    display.setAutoDisplay(false);

    List entries = jobManagers.getJobManagerEntries();
    JobManagerEntry entry;
    for (int i=0; i<entries.size(); i++) {
      entry = (JobManagerEntry)entries.get(i);
      JobManager jobManager = entry.getJobManager();

      JobInfo [] jobInfo = jobManager.listJobs();
      for (int j=0; j<jobInfo.length; j++) {
        JobEntry job = new JobEntry(
            jobInfo[j].getTaskId(),
            jobInfo[j].getTaskId(),
            jobManager);

        display.jobSubmitted(job);
        //display.addJob(jobEntry);
      }
    }

    display.setAutoDisplay(true);
  }

  // ----------------------------------------------------- Actions

  /**
   * Listener to respond window closing events and fire a particular
   * action.
   */
  class WindowCloseListener extends WindowAdapter {
    Action exitAction;

    public WindowCloseListener(Action exitAction) {
      this.exitAction = exitAction;
    }

    public void windowClosing(WindowEvent e) {
      ActionEvent aEvent = new ActionEvent(e.getSource(), ActionEvent.ACTION_PERFORMED, "close");
      exitAction.actionPerformed(aEvent);
    }
  }

  /**
   * Action which calls System.exit(0) to end graphical applications.
   */
  class SimpleExitAction extends AbstractAction {

    public void actionPerformed(ActionEvent e) {
      System.exit(0);
    }

  }

  /**
   * Action which will check for unfreed jobs on our status display
   * panel.  If unfreed jobs are found we will ask the user if we
   * can free these jobs before exiting.
   */
  class SafeExitAction extends AbstractAction {

    Component parent;
    StatusListFrame statusFrame;

    public SafeExitAction(Component parent, StatusListFrame statusFrame) {
      this.parent = parent;
      this.statusFrame = statusFrame;
    }

    public void actionPerformed(ActionEvent e) {

      //kill running jobs (if indicated):
      if (statusFrame.hasRunningJobs()) {
        int choice = JOptionPane.showConfirmDialog(parent, "Some jobs are still running.  Do you want to cancel these jobs?");
        if (choice == JOptionPane.CANCEL_OPTION)
          return;

        if (choice == JOptionPane.OK_OPTION) {
          JobEntry [] jobEntries = statusFrame.listJobs();
          for (int i=0; i<jobEntries.length; i++) {
            AsyncTaskController controller = jobEntries[i].getController();
            if (!controller.isComplete())
              controller.kill();
          }
        }
      }

      //free unfreed jobs (if indicated):
      JobEntry [] jobEntries = statusFrame.listJobs();
      if (jobEntries.length > 0) {
        int choice = JOptionPane.showConfirmDialog(parent, "Jobs still exist that may be consuming system resources.  Free these jobs before exiting?");
        if (choice == JOptionPane.CANCEL_OPTION)
          return;

        else if (choice == JOptionPane.OK_OPTION) {
          for (int i=0; i<jobEntries.length; i++)
            jobEntries[i].free();
        }
      }

      System.exit(0);
    }

  }

  class ShowFrameAction extends AbstractAction {

    JFrame frame;

    public ShowFrameAction(JFrame frame) {
      this.frame = frame;
    }

    public void actionPerformed(ActionEvent e) {
      if (!frame.isVisible())
        frame.setVisible(true);
    }

  }

  class PrintJobsAction extends AbstractAction {

    JobManagerSet jobManagers;

    public PrintJobsAction(JobManagerSet jobManagers) {
      this.jobManagers = jobManagers;
    }

    public void actionPerformed(ActionEvent e) {
      System.out.println("=========================");
      System.out.println("Jobs");
      System.out.println("=========================");
      System.out.println();

      List entries = jobManagers.getJobManagerEntries();
      JobManagerEntry entry;
      for (int i=0; i<entries.size(); i++) {
        entry = (JobManagerEntry)entries.get(i);
        JobManager jobManager = entry.getJobManager();

        System.out.println(entry.getDisplayName());
        System.out.println("-------------------------");
        JobInfo [] jobs = jobManager.listJobs();
        for (int j=0; j<jobs.length; j++) {
          System.out.println(jobs[j].getJobId() + " " + jobs[j].getStatus());

        }
        System.out.println();
      }
    }

  }



}
TOP

Related Classes of gri.tasks.gui.TestShell$SimpleExitAction

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.