예제 #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.
예제 #2
0
  // Method to closeFile().
  private void closeFile() {
    // Dismiss file reference.
    fileName = new File("");

    // If JTextAreaPanel contains text.
    if (!sender.isTextAreaEmpty()) {
      // Clear display area.
      sender.replaceRange(null, 0, sender.getText().length());
    } // End of if check if JTextAreaPanel contains text.
  } // End of closeFile() method.
예제 #3
0
  // Define method to return commands.
  private void buildFrame(int width, int height) {
    // Set JFrame title.
    setTitle("Communicator");

    // Initialize and set policies for JScrollPane(s).
    receiverScrollPane =
        setScrollPaneProperties(new JScrollPane(receiver = new JTextAreaPanel(COL, ROW)));
    senderScrollPane =
        setScrollPaneProperties(new JScrollPane(sender = new JTextAreaPanel(COL, ROW)));

    // Disable editability of receiver JTextAreaPanel.
    receiver.setEditable(false);

    // Initialize JButtonPanel.
    button = new JButtonPanel();

    // Define and initialize JButton(s).
    buildButtons();

    // Set panel size.
    setSize(width, height);

    // Set JPanel Layout.
    getContentPane().setLayout(new BorderLayout());

    // Add a JTextAreaPanel(s).
    getContentPane().add(receiverScrollPane, BorderLayout.NORTH);
    getContentPane().add(button, BorderLayout.CENTER);
    getContentPane().add(senderScrollPane, BorderLayout.SOUTH);

    // Build JMenuBar.
    buildMenu();

    // Build JButton action listeners.
    buildButtonActionListeners();

    // Set the screen and display dialog window in relation to screen size.
    dim = tk.getScreenSize();
    setLocation((dim.width / 100), (dim.height / 100));

    // --------------------- Begin Window ActionListener ---------------------/

    // Add window listener to the frame.
    addWindowListener(
        new WindowAdapter() {
          public void windowClosing(WindowEvent closingEvent) {
            // Exit on window frame close.
            System.exit(0);
          } // End of windowClosing() method.
        }); // End of addWindowListener() method for JFrame.

    // Set resizeable window off.
    setResizable(false);

    // Display the JFrame to the platform window manager.
    show();
  } // End of buildFrame() method.
예제 #4
0
  // Define a setMessage() method.
  private void setMessage(JTextAreaPanel out, JTextAreaPanel in) {
    // Try to get incoming message.
    try {
      // Send message text.
      in.setMessage(out.getText());

      // Consume sent message text.
      out.replaceRange(null, 0, out.getText().length());

    } // End of try to get message.

    // Catch BufferEmptyException.
    catch (BufferEmptyException e) {
      // Capture the stack trace into a String.
      setExceptionString(e.fillInStackTrace());

      // Pass message to the JOptionPane manager.
      setExceptionPane(
          "There is nothing in the buffer to send.\n\n" + "Do you want to see the stack trace?");
    } // End of catch on BufferEmptyException.
  } // End of setMessage() method.
예제 #5
0
  // Define a getMessage() method.
  private void getMessage(JTextAreaPanel in) {
    // Try to get incoming message.
    try {
      // Receive message text.
      in.getMessage();

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

      // Pass message to the JOptionPane manager.
      setExceptionPane(
          "There is nothing in the buffer to receive.\n\n" + "Do you want to see the stack trace?");
    } // End of catch on BufferEmptyException.
  } // End of getMessage() method.
예제 #6
0
  // Method to saveFile().
  private void saveFile() {
    // If a file name is returned.
    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 saveFile() method.