Package jSimMacs.display.dialog

Source Code of jSimMacs.display.dialog.NewProjectDialog$WindowHandler

/**
*
*/
package jSimMacs.display.dialog;

import jSimMacs.data.Project;
import jSimMacs.data.ProjectType;
import jSimMacs.data.RemoteProject;
import jSimMacs.logic.ProjectLocation;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

/**
* @author sr
*
*/
public class NewProjectDialog extends JDialog implements ActionListener {

  private JLabel projectNameLabel = new JLabel("Project name: ");

  private JLabel sshHostnameLabel = new JLabel("Hostname: ");

  private JLabel sshPortLabel = new JLabel("Port: ");
 
  private JLabel sshUserLabel = new JLabel("User: ");

  //private JLabel sshPasswordLabel = new JLabel("Password: ");
 
  private JLabel sshPathLabel = new JLabel("Path: ");
 
  private JCheckBox mpiSupport = new JCheckBox("Enable MPI Libary");
 
  private JCheckBox sshCertificate = new JCheckBox("Use Certificate");

  private JTextField projectName;

  private JTextField sshHostname;

  private JTextField sshPort;
 
  private JTextField sshPath;
 
  private JTextField sshUser;

  //private JPasswordField sshPassword;

  private JPanel projectInputPanel;

  private JPanel projectTypPanel;

  private JPanel projectLocationPanel;
 
  private JPanel projectOptionPanel;

  private JPanel sshPanel;

  private JPanel buttonPanel;

  private JButton newButton = new JButton("New");

  private JButton cancelButton = new JButton("Cancel");

  private JRadioButton localProjectButton = new JRadioButton("Local Project");

  private JRadioButton remoteProjectButton = new JRadioButton("SSH Project");

  private DefaultListModel listModel = new DefaultListModel();

  private ProjectLocation projectLocation = ProjectLocation.LOCALE;
 
  private JList projectList;
 
  private boolean sshEnabled = false;

  public NewProjectDialog(Frame owner) {
    super(owner, true);
    projectName = new JTextField(30);
    sshHostname = new JTextField(20);
    sshPort = new JTextField(DialogConstants.SSHDEFAULTPORT, 20);
    sshUser = new JTextField(20);
    sshPath = new JTextField(20);
    //sshPassword = new JPasswordField(20);

    projectInputPanel = new JPanel();
    projectTypPanel = new JPanel();
    buttonPanel = new JPanel();
    projectLocationPanel = new JPanel(new GridLayout(0, 1));
    projectOptionPanel = new JPanel(new GridLayout(0,1));
    sshPanel = new JPanel(new GridLayout(0, 2, 5, 5));
    initDialog();
  }

  private void initDialog() {
    Container content = getContentPane();
    content.setLayout(new BorderLayout(5, 5));
    JPanel northPanel = new JPanel();
    northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.PAGE_AXIS));
    projectInputPanel.add(projectNameLabel);
    projectInputPanel.add(projectName);
   
    projectOptionPanel.add(mpiSupport);

    localProjectButton.setActionCommand(DialogConstants.LOCALPROJECT);
    remoteProjectButton.setActionCommand(DialogConstants.SSHPROJECT);
    localProjectButton.addActionListener(this);
    remoteProjectButton.addActionListener(this);
    ButtonGroup projectGroup = new ButtonGroup();
    projectGroup.add(localProjectButton);
    projectGroup.add(remoteProjectButton);
    localProjectButton.setSelected(true);
    projectLocationPanel.add(localProjectButton);
    projectLocationPanel.add(remoteProjectButton);
    projectLocationPanel.setBorder(BorderFactory
        .createTitledBorder("Project Location"));
   
    projectOptionPanel.setBorder(BorderFactory
        .createTitledBorder("Project Options"));

    sshPanel.add(sshHostnameLabel);
    sshPanel.add(sshHostname);
    sshPanel.add(sshPortLabel);
    sshPanel.add(sshPort);
    sshPanel.add(sshUserLabel);
    sshPanel.add(sshUser);
    //sshPanel.add(sshPasswordLabel);
    //sshPanel.add(sshPassword);
    sshPanel.add(sshPathLabel);
    sshPanel.add(sshPath);
    sshPanel.add(sshCertificate);
    sshPanel.setBorder(BorderFactory.createTitledBorder("SSH 2/SFTP"));

    setEnableSSHBox(false);

    // listModel.addElement("Empty project");
    // listModel.addElement("TCR-pMHC");
    projectList = new JList(listModel);
    projectList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    projectList.setLayoutOrientation(JList.VERTICAL);

    JScrollPane listScroller = new JScrollPane(projectList);
    projectTypPanel.setLayout(new BorderLayout());
    projectTypPanel.add(listScroller);
    projectTypPanel.setBorder(BorderFactory
        .createTitledBorder("Project Typ"));

    buttonPanel.setLayout(new FlowLayout());
    buttonPanel.add(newButton);
    buttonPanel.add(cancelButton);

    newButton.addActionListener(this);
    cancelButton.addActionListener(this);

    northPanel.add(projectInputPanel);
    northPanel.add(projectOptionPanel);
    northPanel.add(projectLocationPanel);
    northPanel.add(sshPanel);
    content.add(BorderLayout.NORTH, northPanel);
    content.add(BorderLayout.CENTER, projectTypPanel);
    content.add(BorderLayout.SOUTH, buttonPanel);
    this.addWindowListener(new WindowHandler());
  }

  public void actionPerformed(ActionEvent e) {
    Object es = e.getSource();
    if (es instanceof JButton) {
      if (es == cancelButton) {
        projectName.setText("");
        dispose();
      } else if (es == newButton) {

        dispose();
      }
    }
    else if (es instanceof JRadioButton) {
      if(e.getActionCommand().equals(DialogConstants.SSHPROJECT)){
        setEnableSSHBox(true);
        projectLocation = ProjectLocation.SSH;
      }
      else if(e.getActionCommand().equals(DialogConstants.LOCALPROJECT)){
        setEnableSSHBox(false);
        projectLocation = ProjectLocation.LOCALE;
      }
     
    }

  }
 
  private void setEnableSSHBox(boolean enabled){
    sshHostname.setEnabled(enabled);
    sshPort.setEnabled(enabled);
    sshUser.setEnabled(enabled);
    //sshPassword.setEnabled(enabled);
    sshPath.setEnabled(enabled);
    sshCertificate.setEnabled(enabled);
    sshEnabled = enabled;
  }

  /**
   * @return the projectName
   */
  public String getProjectName() {
    return projectName.getText();
  }

  /**
   * @return the listModel
   */
  public DefaultListModel getListModel() {
    return listModel;
  }

  /**
   * @param projectName
   *            the projectName to set
   */
  public void setProjectName(String projectName) {
    this.projectName.setText(projectName);
  }

  /**
   * @return the sshHostname
   */
  public String getSshHostname() {
    return sshHostname.getText();
  }

  /**
   * @return the sshPort
   */
  public String getSshPort() {
    return sshPort.getText();
  }

  /**
   * @return the sshPassword
   */
//  public char[] getSshPassword() {
//    return sshPassword.getPassword();
//  }

  /**
   * @return the sshPath
   */
  public String getSshPath() {
    String path = sshPath.getText();
    if (!path.endsWith("/")) {
      path += "/";
    }
    return path;
  }

  /**
   * @return the projectLocation
   */
  public ProjectLocation getProjectLocation() {
    return projectLocation;
  }
 
  /**
   * @return the sshUser
   */
  public String getSshUser() {
    return sshUser.getText();
  }
 
  public Project getProject(){
    Project project = null;
    if(sshEnabled){
      project = new RemoteProject(getProjectName());
      ((RemoteProject)project).setHostname(getSshHostname());
      ((RemoteProject)project).setPort(Integer.parseInt(getSshPort()));
      ((RemoteProject)project).setUsername(getSshUser());
      ((RemoteProject)project).setPath(getSshPath());
      ((RemoteProject)project).setUseCertificate(sshCertificate.isSelected());
    }
    else
      project = new Project(getProjectName());
    project.setType((ProjectType)projectList.getSelectedValue());
    project.setLocation(projectLocation);
    project.setUseMPILib(mpiSupport.isSelected());
    return project;
  }

  class WindowHandler extends WindowAdapter{

    /* (non-Javadoc)
     * @see java.awt.event.WindowAdapter#windowActivated(java.awt.event.WindowEvent)
     */
    @Override
    public void windowActivated(WindowEvent arg0) {
      projectList.setSelectedIndex(0);
    }

   
  }


}
TOP

Related Classes of jSimMacs.display.dialog.NewProjectDialog$WindowHandler

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.