All pastes #748686 Raw Edit

Mine

public text v1 · immutable
#748686 ·published 2007-10-25 00:58 UTC
rendered paste body
package edu.elon.eim;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import org.jvnet.substance.SubstanceLookAndFeel;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using
 * Jigloo. Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY
 * FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class EimClient extends javax.swing.JFrame implements Runnable {
    private class LLListener implements ActionListener {

        /*
         * (non-Javadoc)
         * 
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
         */
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand().equalsIgnoreCase("Login")) {
                try {
                    s = new Socket(jHostTextField.getText(), Integer
                            .parseInt(jPortTextField.getText()));
                    makeStreamsThreads(s);
                    print.println("LOGIN " + jUsrTextField.getText());
                } catch (Exception e1) {
                    JOptionPane.showMessageDialog(getParent(),
                            "Invalid Login - check host and port",
                            "Invalid Login", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                jChatInput.setEditable(true);
                jSendText.setEnabled(true);
                jLLButton.setText("Logout");
                jHostTextField.setEditable(false);
                jPortTextField.setEditable(false);
                jUsrTextField.setEditable(false);
            } else if (e.getActionCommand().equalsIgnoreCase("Logout")) {
                jChatInput.setEditable(false);
                jSendText.setEnabled(false);
                jHostTextField.setEditable(true);
                jPortTextField.setEditable(true);
                jUsrTextField.setEditable(true);
                jLLButton.setText("Login");
                try {
                    print.println("LOGOUT " + jUsrTextField.getText());
                    Thread.sleep(750);
                    cleanup();
                } catch (Exception e1) {
                }
            }
        }
    }

    private class SendListener implements ActionListener {

        /*
         * (non-Javadoc)
         * 
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
         */
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                if (jChatInput.getText().equalsIgnoreCase("/clear")) {
                    jChatArea.setText("");
                    jChatInput.setText("");
                    return;
                }
                print.println("CHAT " + jChatInput.getText());
            } catch (Exception ex) {
            }
            jChatInput.setText("");
        }

    }

    private static Socket s;
    private static final long serialVersionUID = 1L;
    private InputStream i;
    private JTextArea jChatArea;
    private JTextArea jChatInput;
    private JTextField jHostTextField;
    private JButton jLLButton;
    private JPanel jLoginPanel;
    private JTextField jPortTextField;
    private JScrollPane jScrollPane1;
    private JPanel jSendArea;
    private JButton jSendText;
    private JTextField jUsrTextField;
    private Thread listener;
    private OutputStream o;
    private PrintWriter print;
    private Scanner scan;

    /**
     * 
     */
    public EimClient() {
        super();
        initGUI();
    }

    /**
     * Auto-generated main method to display this JFrame
     * 
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JDialog.setDefaultLookAndFeelDecorated(true);
        UIManager.setLookAndFeel(new SubstanceLookAndFeel());
        System.setProperty("sun.awt.noerasebackground", "true");
        // LiquidLookAndFeel.setLiquidDecorations(true, "mac");
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                EimClient inst = new EimClient();
                inst.setLocationRelativeTo(null);
                inst.setVisible(true);
                inst.setResizable(false);
            }
        });
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        try {
            while (true) {
                String line = scan.nextLine();
                JScrollBar sb = jScrollPane1.getVerticalScrollBar();
                sb.setValue(sb.getMaximum());
                jChatArea.append(line + "\n");
            }
        } catch (Exception ex) {
            if (jSendText.isEnabled()) {
                JScrollBar sb = jScrollPane1.getVerticalScrollBar();
                sb.setValue(sb.getMaximum());
                jChatArea
                        .append("Connection Lost... Login to Reconnect" + "\n");
                jChatInput.setEditable(false);
                jSendText.setEnabled(false);
                jHostTextField.setEditable(true);
                jPortTextField.setEditable(true);
                jUsrTextField.setEditable(true);
                jLLButton.setText("Login");
                listener.interrupt();
            }
        } finally {
            try {
                listener = null;
                print.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    private void cleanup() throws Exception {
    }

    private void initGUI() {
        try {
            setTitle("EIM");
            setLayout(new BorderLayout());
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            makeLogonPanel();
            makeChatAreaPanel();
            makeSendTextPanel();
            pack();
            this.setSize(350, 600);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void makeChatAreaPanel() {
        JPanel jChatPanel = new JPanel();
        FlowLayout jChatPanelLayout = new FlowLayout();
        jChatPanel.setLayout(jChatPanelLayout);
        add(jChatPanel, BorderLayout.CENTER);
        jChatArea = new JTextArea();
        jChatArea.setEditable(false);
        jChatArea.setLineWrap(true);
        jChatArea.setWrapStyleWord(true);
        jScrollPane1 = new JScrollPane(jChatArea);
        jScrollPane1.setPreferredSize(new java.awt.Dimension(332, 340));
        jScrollPane1
                .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane1
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        jChatPanel.add(jScrollPane1);
    }

    private void makejLoginPanelcomponents() {
        GridBagLayout panelLayout = new GridBagLayout();
        panelLayout.columnWeights = new double[] { 1.0, 1.0, 1.0 };
        panelLayout.columnWidths = new int[] { 150, 125, 125 };
        panelLayout.rowWeights = new double[] { 1.0, 1.0, 1.0 };
        panelLayout.rowHeights = new int[] {};
        jLoginPanel.setLayout(panelLayout);
        JLabel jHostLabel = new JLabel();
        jHostLabel.setText("Host: ");
        jHostLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        JLabel jPortLabel = new JLabel();
        jPortLabel.setText("Port: ");
        jPortLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        JLabel jUsrLabel = new JLabel();
        jUsrLabel.setText("Username: ");
        jUsrLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        jHostTextField = new JTextField();
        jHostTextField.setName("Host");
        jPortTextField = new JTextField();
        jPortTextField.setName("Port");
        jUsrTextField = new JTextField();
        jUsrTextField.setName("Username");
        jLLButton = new JButton();
        jLLButton.setText("Login");
        jLLButton.addActionListener(new LLListener());
        jLoginPanel.add(jHostLabel, new GridBagConstraints(0, 0, 1, 1, 1.0,
                1.0, GridBagConstraints.EAST, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 1, 1));
        jLoginPanel.add(jHostTextField, new GridBagConstraints(1, 0, 2, 1, 1.0,
                1.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                new Insets(0, 0, 0, 0), 0, 0));
        jLoginPanel.add(jPortLabel, new GridBagConstraints(0, 1, 1, 1, 1.0,
                1.0, GridBagConstraints.EAST, GridBagConstraints.NONE,
                new Insets(0, 0, 0, 0), 0, 0));
        jLoginPanel.add(jPortTextField, new GridBagConstraints(1, 1, 2, 1, 1.0,
                1.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                new Insets(0, 0, 0, 0), 0, 0));
        jLoginPanel.add(jUsrLabel, new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0,
                GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0,
                        0, 0, 0), 0, 0));
        jLoginPanel.add(jUsrTextField, new GridBagConstraints(1, 2, 2, 1, 1.0,
                1.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                new Insets(0, 0, 0, 0), 0, 0));
        jLoginPanel.add(jLLButton, new GridBagConstraints(2, 3, 1, 2, 1.0, 1.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
                        0, 0, 0, 0), 0, 0));
        GridBagConstraints constrnts = null;
        constrnts = new GridBagConstraints();
        constrnts.gridx = 0;
        constrnts.gridy = 0;
        constrnts.fill = GridBagConstraints.BOTH;
        constrnts.anchor = GridBagConstraints.EAST;
        constrnts.gridx++;
        constrnts.anchor = GridBagConstraints.WEST;
        constrnts.fill = GridBagConstraints.BOTH;
        constrnts.gridx = 0;
        constrnts.gridy++;
        constrnts.anchor = GridBagConstraints.EAST;
        constrnts.fill = GridBagConstraints.BOTH;
        constrnts.gridx++;
        constrnts.anchor = GridBagConstraints.WEST;
        constrnts.fill = GridBagConstraints.BOTH;
        constrnts.gridx = 0;
        constrnts.gridy++;
        constrnts.anchor = GridBagConstraints.EAST;
        constrnts.fill = GridBagConstraints.BOTH;
        constrnts.gridx++;
        constrnts.anchor = GridBagConstraints.WEST;
        constrnts.fill = GridBagConstraints.BOTH;
    }

    private void makeLogonPanel() {
        jLoginPanel = new JPanel();
        add(jLoginPanel, BorderLayout.NORTH);
        jLoginPanel.setPreferredSize(new java.awt.Dimension(334, 118));
        makejLoginPanelcomponents();
        TitledBorder title = BorderFactory.createTitledBorder(BorderFactory
                .createLineBorder(Color.black, 2), "Login", 1, 2);
        jLoginPanel.setBorder(title);

    }

    private void makeSendTextPanel() {
        jSendArea = new JPanel();
        add(jSendArea, BorderLayout.SOUTH);
        FlowLayout jPanel1Layout = new FlowLayout();
        jPanel1Layout.setAlignment(FlowLayout.RIGHT);
        jSendArea.setLayout(jPanel1Layout);
        jSendArea.setPreferredSize(new java.awt.Dimension(334, 96));
        jChatInput = new JTextArea();
        jChatInput.setEditable(false);
        jChatInput.setWrapStyleWord(true);
        jChatInput.setLineWrap(true);
        jChatInput.setPreferredSize(new java.awt.Dimension(324, 51));
        jSendArea.add(jChatInput);
        jSendText = new JButton();
        jSendText.setText("Send");
        jSendText.setEnabled(false);
        jSendText.addActionListener(new SendListener());
        jSendText.setPreferredSize(new java.awt.Dimension(84, 30));
        jSendArea.add(jSendText);
        jSendArea.setBorder(new LineBorder(Color.black, 2, false));

    }

    private void makeStreamsThreads(Socket s) {
        try {
            i = s.getInputStream();
            scan = new Scanner(i);
            o = s.getOutputStream();
            print = new PrintWriter(o, true);
            listener = new Thread(this);
            listener.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}