Example #1
0
  private Slice showNewSliceDialog() {
    Object[] allProjects = mainGUI.getProjectsArray();

    if (allProjects.length == 0) {
      JOptionPane.showMessageDialog(
          this, "Please first create a project.", "Create Slice", JOptionPane.INFORMATION_MESSAGE);
      return null;
    }

    String[] projectNames = new String[allProjects.length];
    for (int i = 0; i < projectNames.length; i++) projectNames[i] = ((Project) allProjects[i]).name;

    JPanel infoPanel = new JPanel();
    infoPanel.setLayout(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.insets = new Insets(10, 0, 0, 0); // top padding
    c.weightx = 0.1;
    c.gridx = 0;
    c.gridy = 0;
    infoPanel.add(new JLabel("Name:"), c);
    c.gridy = 1;
    infoPanel.add(new JLabel("Project:"), c);
    c.gridy = 2;
    infoPanel.add(new JLabel("Description:"), c);

    JComboBox projectBox = new JComboBox(projectNames);
    JTextField name = new JTextField("");
    AbstractDocument doc = (AbstractDocument) name.getDocument();
    doc.setDocumentFilter(new PatternFilter("^[a-zA-Z0-9][-a-zA-Z0-9]{0,18}"));

    name.setColumns(30);
    JTextArea desc = new JTextArea("");

    c.weightx = 0.9;
    c.gridx = 1;
    c.gridy = 0;
    infoPanel.add(name, c);
    c.gridy = 1;
    infoPanel.add(projectBox, c);
    c.gridy = 2;
    c.ipady = 100;
    infoPanel.add(new JScrollPane(desc), c);

    int response;
    do {
      response =
          JOptionPane.showConfirmDialog(
              this,
              infoPanel,
              "New Slice",
              JOptionPane.OK_CANCEL_OPTION,
              JOptionPane.PLAIN_MESSAGE);
      name.setBackground(name.getText().trim().length() == 0 ? Color.pink : Color.white);
      desc.setBackground(desc.getText().trim().length() == 0 ? Color.pink : Color.white);
    } while ((name.getText().length() == 0 || desc.getText().length() == 0)
        && response == JOptionPane.OK_OPTION);

    if (response != JOptionPane.OK_OPTION) return null;
    else {
      Slice d = new Slice();
      d.name = name.getText().trim();
      d.desc = desc.getText().trim();
      d.urnProject = ((Project) allProjects[projectBox.getSelectedIndex()]).urn;
      return d;
    }
  }