Ejemplo n.º 1
0
  /**
   * Opens a new NewContact dialog with a JDialog as a parent.
   *
   * @param frame the JDialog parent
   * @param contactFrame the frame where the contact details will be inserted
   * @param modal
   */
  public NewContact(JDialog frame, ContactInputable contactFrame, boolean modal) {
    super(frame, modal);
    this.contactFrame = contactFrame;

    setUpDialog();

    currentContactName = null;
    readContactsFromFile();

    setLocationRelativeTo(contactFrame.getWindow());
    setVisible(true);
  }
Ejemplo n.º 2
0
  /**
   * Clicking the add button inserts the new contact into the contact table in the
   * ExperimentProperies frame. Also writes the contact to file.
   *
   * @param evt
   */
  private void addJButtonActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_addJButtonActionPerformed

    this.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));

    boolean alreadyInTable = false;

    if (selectedRow == -1) {
      alreadyInTable =
          contactFrame.contactAlreadyInTable((String) namesJComboBox.getSelectedItem());
    }

    if (!alreadyInTable) {

      String tempInstitution = institutionJTextArea.getText();

      tempInstitution = tempInstitution.trim();

      tempInstitution = tempInstitution.replaceAll("\n", ", ");
      tempInstitution = tempInstitution.replaceAll(" , ", "");

      while (tempInstitution.lastIndexOf(",,") != -1) {
        tempInstitution = tempInstitution.replaceAll(",,", ",");
      }

      if (tempInstitution.endsWith(",")) {
        tempInstitution = tempInstitution.substring(0, tempInstitution.length() - 1);
      }

      String newName = contactPath + currentContactName + ".con";

      try {
        FileWriter r = new FileWriter(newName);
        BufferedWriter bw = new BufferedWriter(r);

        bw.write("Name: " + currentContactName + "\n");
        bw.write("E-mail: " + contactInfoJTextField.getText() + "\n");
        bw.write("Institution: " + tempInstitution + "\n");

        bw.close();
        r.close();

      } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(
            this,
            "The file " + newName + " could not be found.",
            "File Not Found",
            JOptionPane.ERROR_MESSAGE);
        Util.writeToErrorLog("Error when trying to save file: ");
        ex.printStackTrace();
      } catch (IOException ex) {
        JOptionPane.showMessageDialog(
            this,
            "An error occured when trying to save the file " + newName + ".",
            "File Error",
            JOptionPane.ERROR_MESSAGE);
        Util.writeToErrorLog("Error when trying to save file: ");
        ex.printStackTrace();
      }

      contactFrame.addContact(
          new ContactImpl(
              tempInstitution,
              (String) namesJComboBox.getSelectedItem(),
              contactInfoJTextField.getText()),
          selectedRow);

      this.setVisible(false);
      this.dispose();

      this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    }

    this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  } // GEN-LAST:event_addJButtonActionPerformed