예제 #1
0
  /** @see ComboBoxInputable */
  public void insertIntoComboBox(String text) {

    this.currentContactName = text;

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

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

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

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

      institutionJTextArea.setEnabled(true);
      contactInfoJTextField.setEnabled(true);
      institutionJTextArea.setEditable(true);
      contactInfoJTextField.setEditable(true);

      readContactsFromFile();

    } 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();
    }
  }
예제 #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
예제 #3
0
  /**
   * Updates the contact information shown, or creates a new contact if the first row is selected.
   *
   * @param evt
   */
  private void namesJComboBoxActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_namesJComboBoxActionPerformed

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

    String selectedContactName = (String) namesJComboBox.getSelectedItem();

    boolean cancel = false;

    if (valuesChanged) {
      int value =
          JOptionPane.showConfirmDialog(
              this,
              "The contact has been changed. Do you want to save the changes?",
              "Contact Changed",
              JOptionPane.YES_NO_CANCEL_OPTION);

      if (value == JOptionPane.YES_OPTION) {

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

        try {

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

          bw.write("Name: " + lastSelectedContact + "\n");
          bw.write("E-mail: " + contactInfoJTextField.getText() + "\n");
          bw.write("Institution: " + institutionJTextArea.getText() + "\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();
        }
      } else if (value == JOptionPane.CANCEL_OPTION) {
        cancel = true;
      }
    }

    if (!cancel) {

      lastSelectedContact = "" + namesJComboBox.getSelectedItem();

      contactInfoJTextField.setText("");
      institutionJTextArea.setText("");

      if (namesJComboBox.getSelectedIndex() == 0) {

        institutionJTextArea.setEnabled(false);
        contactInfoJTextField.setEnabled(false);
        institutionJTextArea.setEditable(false);
        contactInfoJTextField.setEditable(false);

      } else if (namesJComboBox.getSelectedIndex() == namesJComboBox.getItemCount() - 1) {

        ComboBoxInputDialog input = new ComboBoxInputDialog(this, this, true);
        input.setTitle("Create New Contact");
        input.setBorderTitle("New Contact");
        input.setVisible(true);

      } else {

        institutionJTextArea.setEnabled(true);
        contactInfoJTextField.setEnabled(true);
        institutionJTextArea.setEditable(true);
        contactInfoJTextField.setEditable(true);

        selectedContactName = contactPath + selectedContactName + ".con";

        try {
          String temp, institution = "";

          FileReader f = new FileReader(selectedContactName);
          BufferedReader b = new BufferedReader(f);

          b.readLine();
          temp = b.readLine();
          contactInfoJTextField.setText(temp.substring(temp.indexOf(": ") + 2));

          temp = b.readLine();

          while (temp != null) {
            if (temp.indexOf(": ") != -1) {
              institution += temp.substring(temp.indexOf(": ") + 2) + ", ";
            } else {
              institution += temp + ", ";
            }

            temp = b.readLine();
          }

          if (institution.endsWith(", ")) {
            institution = institution.substring(0, institution.length() - 2);
          }

          institutionJTextArea.setText(institution);

          institutionJTextArea.setEnabled(true);
          contactInfoJTextField.setEnabled(true);
          institutionJTextArea.setEditable(true);
          contactInfoJTextField.setEditable(true);

          b.close();
          f.close();

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

      valuesChanged = false;
    } else {
      namesJComboBox.setSelectedItem(lastSelectedContact);
    }

    mandatoryFieldsCheck();

    this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  } // GEN-LAST:event_namesJComboBoxActionPerformed
예제 #4
0
  /**
   * Read all contacts from file and insert the contact names into the names combo box. Then inserts
   * the information about the selected contact.
   */
  public void readContactsFromFile() {

    contactPath = "" + this.getClass().getProtectionDomain().getCodeSource().getLocation();
    contactPath = contactPath.substring(5, contactPath.lastIndexOf("/"));
    contactPath =
        contactPath.substring(0, contactPath.lastIndexOf("/") + 1) + "Properties/Contacts/";
    contactPath = contactPath.replace("%20", " ");

    File file = new File(contactPath);
    String tempContactName = null;

    try {

      if (!file.exists()) {
        file.mkdir();
      }

      File[] contactFiles = file.listFiles();
      FileReader f;
      BufferedReader b;

      Vector contactNames = new Vector();

      for (int i = 0; i < contactFiles.length; i++) {

        if (contactFiles[i].getAbsolutePath().endsWith(".con")) {

          f = new FileReader(contactFiles[i]);
          b = new BufferedReader(f);

          tempContactName = b.readLine();
          tempContactName = tempContactName.substring(tempContactName.indexOf(": ") + 2);
          contactNames.add(tempContactName);

          b.close();
          f.close();
        }
      }

      java.util.Collections.sort(contactNames);

      contactNames.add("   Create a New Contact...");
      contactNames.insertElementAt("- Please Select a Contact -", 0);
      namesJComboBox.setModel(new DefaultComboBoxModel(contactNames));

      lastSelectedContact = "" + namesJComboBox.getSelectedItem();

      if (currentContactName != null) {
        namesJComboBox.setSelectedItem(currentContactName);
      }

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

    valuesChanged = false;
  }