Ejemplo n.º 1
0
  // Method to open a file for sequential read-only.
  private void openFile() {
    // Define local primitive(s).
    String contents = new String();

    // If a file is open, prompt to save the file before dismissing content
    // and reference.
    if (!file.toString().equals("")) {
      // Close the existing file.
      closeFile();
    } // End of if file not null or not equal to a zero String.

    // Open a file.
    fileName = FileIO.findFile(this);

    // If a file name is returned.
    if (fileName != null) {
      // Read file.
      contents = FileIO.openFile(this, fileName);

      // Verify that the JTextAreaPanel is empty.
      if (sender.isTextAreaEmpty()) {
        // Set JTextAreaPanel.
        sender.setText(contents);

      } // End of if JTextAreaPanel is empty.
      else {
        // Reset JTextAreaPanel by replacing the range.
        sender.replaceRange(contents, 0, sender.getText().length());
      } // End of else JTextAreaPanel is not empty.

      // Set file open flag to true.
      fileOpen = true;
    } // End of if a fileName is selected.
  } // End of openFile method.
Ejemplo n.º 2
0
  // This method starts from the ButtonListener class but ends up in the Ballot class, updating the
  // file of each Ballot
  public void updateBallots() {
    FileIO nullFile =
        new FileIO(); // need to create this just to gain access to the filename of the ballots file
    FileIO ballotFile = new FileIO(nullFile.getBallotsFileName());
    ArrayList<Ballot> ballots = ballotFile.createBallots();

    for (Ballot ballot : ballots) {
      ballot.updateFile();
    }
  }
Ejemplo n.º 3
0
 // Initialize the buttons ArrayList because there the program messes up if the voter doesn't vote
 // for anything, the buttons array
 // will be null.
 public void initializeButtons() {
   FileIO nullFile =
       new FileIO(); // need to create this just to gain access to the filename of the ballots file
   FileIO ballotFile = new FileIO(nullFile.getBallotsFileName());
   _ballots = ballotFile.createBallots();
   buttons = new ArrayList<JToggleButton>();
   for (Ballot ballot : _ballots) {
     JToggleButton[] ballotButtons = ballot.getButtons();
     for (int i = 0; i < ballotButtons.length; i++) {
       buttons.add(ballotButtons[i]);
     }
   }
 }
Ejemplo n.º 4
0
  // Grab the number of buttons from all of the ballots in the program.
  // This will define the votes array, which stores all of the true and false values...
  // as to whether the button is really selected or not (because button.isSelected() won't work for
  // me for some reason).
  public void setVotesLength() {
    FileIO nullFile =
        new FileIO(); // need to create this just to gain access to the filename of the ballots file
    FileIO ballotsFile = new FileIO(nullFile.getBallotsFileName());
    ArrayList<Ballot> throwAwayList = ballotsFile.createBallots();
    int numButtons = 0;

    for (Ballot ballot : throwAwayList) {
      numButtons += ballot.getNumButtons();
    }

    votes = new boolean[numButtons];
  }
Ejemplo n.º 5
0
  // Method to saveAsFile().
  private void saveAsFile() {
    // Set a file.
    fileName = FileIO.nameFile(this);

    // If a file name is selected.
    if (fileName != null) {
      // Try block to throw a custom exception.
      try {
        // Save file.
        FileIO.saveFile(this, fileName, sender.getText());

      } // End of try to get message.
      catch (BufferEmptyException e) // Catch InvalidFileReference.
      {
        // Capture the stack trace into a String.
        setExceptionString(e.fillInStackTrace());

        // Pass message to the JOptionPane manager.
        setExceptionPane(
            "There is nothing in the sender panel to write.\n\n"
                + "Do you want to see the stack trace?");
      } // End of catch on BufferEmptyException.
    } // End of if a fileName is selected.
  } // End of saveAsFile() method.
Ejemplo n.º 6
0
  // Update the true/false value of every button in the program, and set every value to its
  // respective position in the votes array
  public void updateButtons(String text) {
    FileIO nullFile =
        new FileIO(); // need to create this just to gain access to the filename of the ballots file
    FileIO ballotFile = new FileIO(nullFile.getBallotsFileName());
    _ballots = ballotFile.createBallots();
    buttons = new ArrayList<JToggleButton>();
    for (Ballot ballot : _ballots) {
      JToggleButton[] ballotButtons = ballot.getButtons();
      for (int i = 0; i < ballotButtons.length; i++) {
        buttons.add(ballotButtons[i]);
      }
    }

    int numButtons = 0;
    for (Ballot ballot : _ballots) {
      numButtons += ballot.getButtons().length;
    }

    for (int i = 0; i < numButtons; i++) {
      if (buttons.get(i).getText().equals(text)) {
        votes[i] = true;
      }
    }
  }
Ejemplo n.º 7
0
  // This is where all the action happens.  If an action is performed (button press for example), it
  // goes through this method
  // that takes an ActionEvent, which is created at the time of the action.
  public void actionPerformed(ActionEvent e) {
    // Try to cast the ActionEvent source as JToggleButton (if successful, the button is a ballot
    // choice)
    try {
      JToggleButton theButton = (JToggleButton) e.getSource();
      String buttonText = theButton.getText();

      if (theButton.isSelected()) {
        theButton.setForeground(Color.RED);
        switchButton = true;
        switchButtonText = buttonText;
        updateButtons(
            buttonText); // switches this button's respective vote position to 1 (true), meaning it
                         // is selected
      } else if (!theButton.isSelected()) {
        theButton.setForeground(Color.BLACK);
        unvoteButton = true;
        unvoteButtonText = buttonText;
      }
      // If the program can't cast the source to a JToggleButton, cast it as a JButton, which is
      // 100% what the source will be at this point.
      // This button will be one of two things: the login button or the cast vote button.
    } catch (Exception exception) {
      JButton theButton = (JButton) e.getSource();

      String buttonText = theButton.getText();

      // If the user hits the Login button, check if the ID is valid.
      // If the ID is not valid, don't let them vote.  However, if it is valid, disable login and
      // enable everything else.
      if (buttonText.equals("Login")) {
        String voterID =
            JOptionPane.showInputDialog(theButton, "Enter your voter ID: ", "Login", 3);
        FileIO votersFile = new FileIO("voters.txt");
        ArrayList<Voter> voters = votersFile.createVoters();

        if (votersFile.checkValidID(voterID)) {
          Voter theVoter = voters.get(votersFile.getVoterIndex(voterID));
          if (theVoter.getVotedStatus().equals("false")) {
            setVoterID(voterID);
          } else {
            JOptionPane.showMessageDialog(
                theButton, "You already voted " + theVoter.getVoterName() + "!");
          }
        } else {
          JOptionPane.showMessageDialog(theButton, "Invalid ID!", "Error", 0);
        }
        // If the user hits the cast vote button, update the respective ballot files and disable
        // every button except
        // for the login button.
      } else if (buttonText.equals("Cast vote")) {
        int confirm = JOptionPane.showConfirmDialog(theButton, "Are you sure?");
        FileIO votersFile = new FileIO("voters.txt");
        ArrayList<Voter> voters = votersFile.createVoters();
        Voter theVoter = voters.get(votersFile.getVoterIndex(voterID));

        if (confirm == 0) {
          JOptionPane.showMessageDialog(theButton, "Thanks for voting!");
          updateBallots();
          votersFile.updateVoterFile(theVoter.getVoterID());
          setVoterID("-1");
          votersFile.initializeVoters();
        }
      }
    }
  }