package com.evanram.code.java.dimchat_client; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import javax.crypto.BadPaddingException; import javax.swing.*; import javax.swing.text.DefaultCaret; public class GUI extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private static JTextField ipTextField; private static JTextField portTextField; private static JButton sendButton; private static JTextArea outputTextArea; public static boolean isConnected; private JLabel dimchatLabel = new JLabel("dimchat - client"); private static JTextField messageTextField; public static String connectionStateStr = "Connect"; public static JButton connectButton = new JButton("Connect"); private JButton debugButton; public GUI() { super("dimchat - client"); //ip:port stuff JPanel topPanel = new JPanel(new GridLayout(0, 1)); JPanel connectionPanel = new JPanel(new GridLayout(1, 5, 1, 3)); ipTextField = new JTextField(""); ipTextField.setToolTipText("Server IP (default: localhost)"); ipTextField.setHorizontalAlignment(SwingConstants.LEFT); portTextField = new JTextField(""); portTextField.setToolTipText("Server port (default: 38936)"); portTextField.setHorizontalAlignment(SwingConstants.LEFT); dimchatLabel.setEnabled(false); connectButton.addActionListener(this); connectButton.setActionCommand("connect"); connectButton.setMnemonic('C'); connectionPanel.add(dimchatLabel); connectionPanel.add(ipTextField); connectionPanel.add(portTextField); connectionPanel.add(connectButton); topPanel.add(connectionPanel); add(topPanel, BorderLayout.NORTH); //output stuff outputTextArea = new JTextArea("[dimchat] Connect to a server!", 20, 20); JPanel centerPanel = new JPanel(new GridLayout(1, 5, 1, 3)); JScrollPane scrollPane = new JScrollPane(outputTextArea); scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); DefaultCaret caret = (DefaultCaret)outputTextArea.getCaret(); caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); outputTextArea.setLineWrap(true); centerPanel.add(scrollPane); outputTextArea.setEditable(false); add(centerPanel, BorderLayout.CENTER); //input stuff messageTextField = new JTextField(20); messageTextField.setHorizontalAlignment(SwingConstants.LEFT); sendButton = new JButton("Send"); sendButton.setActionCommand("send"); sendButton.addActionListener(this); debugButton = new JButton(); debugButton.setSize(0, 0); debugButton.addActionListener(this); debugButton.setVisible(false); debugButton.setActionCommand("debug"); debugButton.setMnemonic('D'); JPanel lowerPanel = new JPanel();//new GridLayout()); lowerPanel.add(messageTextField); lowerPanel.add(sendButton); lowerPanel.add(debugButton); add(lowerPanel, BorderLayout.SOUTH); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(525, 375); //put in center of screen Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2); setVisible(true); messageTextField.requestFocus(); //allow us to press enter to do send button (since it's invisible) getRootPane().setDefaultButton(sendButton); } @Override public void actionPerformed(final ActionEvent event) { new Thread(new Runnable(){ //damn thing freezes if we don't have this in a separate thread @Override public void run() { //debug menu if("debug".equals(event.getActionCommand())) { setText("[dimchat] Connect to a server!"); debugButton.setVisible(false); dimchatLabel.setText(" dimchat - client"); } if("connect".equals(event.getActionCommand())) { if(ipTextField.getText().equals("_debug_")) { debugMode(); return; } connectButton.setText(connectionStateStr); toggleConnectionFields(false); toggleSendingFields(true); connectButton.setEnabled(true); if(isConnected) { connectionStateStr = "Connect"; connectButton.setText(connectionStateStr); try { Connections.socket.close(); } catch (IOException e) { Message.logError("Error when closing socket. Please restart application."); toggleConnectionFields(false); toggleSendingFields(false); } Client.newConnection(); setText(""); isConnected = false; return; } if(!isConnected) { connectionStateStr = "Leave"; connectButton.setText(connectionStateStr); } isConnected = true; //connect to server: //with empty ip if(ipTextField.getText().replace(" ", "").equals("")) { //empty ip and non-empty port if(Connections.isValidAndNonEmptyPort(portTextField.getText())) { new Connections(Integer.parseInt(portTextField.getText())); } //empty ip and empty port else { new Connections(); } } //with non-empty ip else { //non-empty ip and non-empty port if(Connections.isValidAndNonEmptyPort(portTextField.getText())) { new Connections(ipTextField.getText(), Integer.parseInt(portTextField.getText())); } //non-empty ip and empty port else { new Connections(ipTextField.getText()); } } } else if("send".equals(event.getActionCommand())) { Message.setLastInput(messageTextField.getText()); messageTextField.setText(""); } } }).start(); } public static void toggleConnectionFields(boolean toggle) { ipTextField.setEnabled(toggle); portTextField.setEnabled(toggle); } public static void toggleSendingFields(boolean toggle) { sendButton.setEnabled(toggle); messageTextField.setEnabled(toggle); } public static void disableAllButtons() { connectButton.setEnabled(false); sendButton.setEnabled(false); } public static void appendText(String message) { try { //prevent spamming enter button if(message.replace(" ", "").equals("")) return; outputTextArea.setText(outputTextArea.getText() + "\n" + message); } catch(NullPointerException e) { e.printStackTrace(); } } public static void setText(String message) { outputTextArea.setText(message); } private void debugMode() { setText("[+] - - - [+]\nDebug mode.\nDimchat copyright (c) 2014 Evan Ram.\nPress \'X!\' button to return to normal chat." + "\nUse port field to send debug commands. Try \'help\' command.\n[+] - - - [+]\n"); debugButton.setVisible(true); debugButton.setText("X!"); dimchatLabel.setText("dimchat - DEBUG"); if(portTextField.getText().equals("get")) { appendText("--------- showing dimensions ---------"); for(double i = 1; i<=10; i+=.5) { appendText("[" + getSize().width*i + ", " + getSize().height*i +"]"); } } else if(portTextField.getText().startsWith("DIM:")) { String[] a = portTextField.getText().substring(4).replace(" ", "").split(","); int a0 = Integer.parseInt(a[0]); int a1 = Integer.parseInt(a[1]); setSize(a0, a1); } } public static void initGUI(String title) { new GUI(); } }