MainPanel.java

package window;

import main.Chat;
import java.util.Calendar;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import javax.swing.*;

class MainPanel extends JPanel implements ActionListener {

  private JTextField textField;
  private TextPane textPaneIn;
  private JTextPane textPaneOut;

  MainPanel() {
    // set a layout to the BorderLayout
    setLayout(new BorderLayout());

    // create main components
    textField = new JTextField();
    textPaneIn = new TextPane();
    textPaneOut = new JTextPane();
    JButton button = new JButton("Send");

    // set a font and properties for text components
    Font font = new Font("Arial", Font.PLAIN, 12);
    textField.setFont(font);
    textPaneIn.setFont(font);
    textPaneOut.setFont(font);
    textPaneIn.setEditable(false);

    // set tool tip texts
    textField.setToolTipText("Type a host name (domain / IP) of the target here!");
    textPaneIn.setToolTipText("You can see incoming messages here!");
    textPaneOut.setToolTipText("Type your message here!");
    button.setToolTipText("Press this button to send the message!");

    // set an action for the button
    button.addActionListener(this);
    button.setMnemonic(KeyEvent.VK_S);

    // put borders between main components
    JPanel panel = new JPanel();
    JPanel panelIn = new JPanel();
    JPanel panelOut = new JPanel();
    panelIn.add(add(new JScrollPane(textPaneIn)));
    panelIn.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0)); // top, left, bottom, right
    panelIn.setLayout(new GridLayout(0, 1));
    panelOut.add(add(new JScrollPane(textPaneOut)));
    panelOut.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0)); // top, left, bottom, right
    panelOut.setLayout(new GridLayout(0, 1));
    panel.add(panelIn);
    panel.add(panelOut);
    panel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0)); // top, left, bottom, right
    panel.setLayout(new GridLayout(0, 1));

    // add main components
    add(textField, BorderLayout.NORTH);
    add(panel, BorderLayout.CENTER);
    add(button, BorderLayout.SOUTH);
  }

  String getTextField() {
    return textField.getText();
  }

  void setTextField(String str) {
    textField.setText(str);
  }

  void messageIn(String str) {
    String head = str.substring(0, str.indexOf('\n'));
    String message = str.substring(str.indexOf('\n'), str.length());
    textPaneIn.setEditable(true);
    textPaneIn.append(Boolean.TRUE, new Color(60, 100, 180), head);
    textPaneIn.append(Boolean.FALSE, new Color(60, 100, 180), message);
    textPaneIn.setEditable(false);
    textPaneIn.setCaretPosition(textPaneIn.getDocument().getLength() - 1);
    System.out.println(str);
  }

  void messageOut() {
    if (textPaneOut.getText().length() > 0) {
      Chat.stopReceive = true;

      String host = Chat.name;
      if (MenuFrame.getMenuHost() != "host" && host.indexOf("/") >= 0) {
        if (MenuFrame.getMenuHost() == "domain") {
          host = host.substring(0, host.indexOf("/"));
        } else if (MenuFrame.getMenuHost() == "ip") {
          host = host.substring(host.indexOf("/") + 1, host.length());
        }
      }

      String time = "";
      if (MenuFrame.getMenuTime()) {
        Calendar cal = Calendar.getInstance();
        time = " [" + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND) + "]";
      }

      String head = host + time + '\n';
      String message = textPaneOut.getText() + '\n';
      if (Chat.sendMessage(head + message)) {
        textPaneIn.setEditable(true);
        textPaneIn.append(Boolean.TRUE, textPaneIn.getCaretColor(), head);
        textPaneIn.append(Boolean.FALSE, textPaneIn.getCaretColor(), message);
        textPaneIn.setEditable(false);
        textPaneIn.setCaretPosition(textPaneIn.getDocument().getLength() - 1);
        textPaneOut.setText("");
        System.out.println(head + message);
      }

      Chat.stopReceive = false;
    } else {
      JOptionPane.showMessageDialog(null, "You have to type the text to send!", "Warning", JOptionPane.WARNING_MESSAGE);
    }

    textPaneOut.grabFocus();
  }

  public void actionPerformed(ActionEvent e) {
    System.out.println(e.getActionCommand());

    if ("Send".equals(e.getActionCommand())) {
      messageOut();
    }
  }
}