ExtractDialog.java

package edu.udel.cis.vsl.tass.gui.impl;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;

import edu.udel.cis.vsl.tass.config.VerifyConfiguration;
import edu.udel.cis.vsl.tass.util.Strings;

/**
 * Extract mode dialog window.
 * 
 * @author Yi Wei
 * 
 */
public class ExtractDialog extends JDialog implements ActionListener {

  private JPanel contentPanel = null;

  // File selection
  private JLabel fileSelectionLabel = null;
  private JTextField fileSelection = null;
  private JFileChooser fileChooser = null;
  private JButton browseFile = null;
  private JPanel filePanel = null;
  private File sourceFile = null;

  // Number of processes
  private SpinnerNumberModel processNumberModel = null;
  private JLabel processNumberLabel = null;
  private JSpinner processNumberOption = null;
  private JPanel processNumberPanel = null;

  // Buttons
  private JButton extractButton = null;
  private JButton cancelButton = null;
  private JPanel buttonPanel = null;

  public static final long serialVersionUID = 3;
  
  public ExtractDialog() {
    initializeComponents();
  }

  private void initializeComponents() {

    this.setTitle("Extract Options");
    this.setSize(TassUserFrame.frameWidth / 2, TassUserFrame.frameHeight / 2);
    this.setResizable(false);
    this.setModal(true);
    this.setDefaultCloseOperation(HIDE_ON_CLOSE);
    this.contentPanel = new JPanel();
    this.add(this.contentPanel);
    this.contentPanel.setBorder(BorderFactory.createEtchedBorder());
    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints con = new GridBagConstraints();
    this.contentPanel.setLayout(layout);

    setUpFileSelection();
    con.gridy = 0;
    con.fill = GridBagConstraints.HORIZONTAL;
    con.gridwidth = GridBagConstraints.REMAINDER;
    this.contentPanel.add(this.filePanel, con);

    setUpProcessNumberOption();
    con.gridy = 2;
    this.contentPanel.add(this.processNumberPanel, con);

    setUpButtons();
    con.gridy = 4;
    this.contentPanel.add(this.buttonPanel, con);

    this.setVisible(true);
  }

  private void setUpFileSelection() {

    this.filePanel = new JPanel();

    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints con = new GridBagConstraints();

    this.filePanel.setLayout(layout);

    con.fill = GridBagConstraints.HORIZONTAL;
    con.gridwidth = GridBagConstraints.REMAINDER;
    this.fileSelectionLabel = new JLabel("Select Source File:");
    this.filePanel.add(this.fileSelectionLabel, con);

    this.fileSelection = new JTextField(38);
    this.fileSelectionLabel.setLabelFor(this.fileSelection);
    this.filePanel.add(this.fileSelection, con);

    this.browseFile = new JButton("Browse");
    con.anchor = GridBagConstraints.EAST;
    con.fill = GridBagConstraints.NONE;
    this.filePanel.add(this.browseFile, con);
    this.browseFile.addActionListener(this);
  }

  private void setUpProcessNumberOption() {
    this.processNumberModel = new SpinnerNumberModel();
    this.processNumberModel.setValue(1);
    this.processNumberModel.setMinimum(1);
    this.processNumberModel.setStepSize(1);

    this.processNumberLabel = new JLabel("Process Number:");

    this.processNumberOption = new JSpinner(this.processNumberModel);

    this.processNumberPanel = new JPanel();
    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints con = new GridBagConstraints();
    this.processNumberPanel.setLayout(layout);

    con.fill = GridBagConstraints.HORIZONTAL;
    con.gridwidth = GridBagConstraints.REMAINDER;
    this.processNumberPanel.add(this.processNumberLabel, con);
    this.processNumberLabel.setLabelFor(this.processNumberOption);

    con.fill = GridBagConstraints.BOTH;
    con.gridwidth = GridBagConstraints.REMAINDER;
    this.processNumberPanel.add(this.processNumberOption, con);

  }

  private void setUpButtons() {

    this.buttonPanel = new JPanel();
    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints con = new GridBagConstraints();
    this.buttonPanel.setLayout(layout);

    con.weightx = 0.5;
    this.extractButton = new JButton("Extract");
    this.extractButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        extractModel();
      }
    });
    this.buttonPanel.add(this.extractButton, con);

    this.cancelButton = new JButton("Cancel");
    this.cancelButton.addActionListener(this);
    this.buttonPanel.add(this.cancelButton, con);
  }
  
  private void extractModel() {
    VerifyConfiguration configuration = new VerifyConfiguration();

    if(this.sourceFile == null)
    {
      // If source file field is empty, give the reminder.
      if(this.fileSelection.getText().length() == 0) {
        JOptionPane.showMessageDialog(this, "Please type in the source file name", "Information", JOptionPane.INFORMATION_MESSAGE);
        return;
      }
      else {
        this.sourceFile = new File(this.fileSelection.getText());
        // Give the error message if source file doesn't exist.
        if(!this.sourceFile.exists()) {
          JOptionPane.showMessageDialog(this, "Source file \"" + this.fileSelection.getText() + "\" doesn't exist.", "Error", JOptionPane.ERROR_MESSAGE);
          return;
        }
      } 
    }
    configuration.setSourceFile(this.sourceFile);
    configuration.setModelName(Strings.rootName(this.sourceFile.getName()));
    
    configuration.setNumProcs((Integer)(this.processNumberOption.getValue()));
    configuration.setVerbose(true);
    
    TassUserFrame.setConfiguration(configuration);
    TassUserFrame.optionsSet();
    setVisible(false);
  }
  
  public void actionPerformed(ActionEvent event) {

    if (event.getSource().equals(this.browseFile)) {
      if (this.fileChooser == null) {
        this.fileChooser = new JFileChooser();
        this.fileChooser.addChoosableFileFilter(new SourceFilter());
        this.fileChooser.setAcceptAllFileFilterUsed(false);
        this.fileChooser.setDialogTitle("Select MiniMP source file");
      }
      int returnValue = this.fileChooser.showDialog(this, "Select");
      if (returnValue == JFileChooser.APPROVE_OPTION) {
        this.sourceFile = this.fileChooser.getSelectedFile();
        this.fileSelection.setText(this.sourceFile.getAbsolutePath());
      }
    } 
    else if(event.getSource().equals(this.cancelButton)) {
      this.setVisible(false);
    }
  }
}