Advertisement
Guest User

Untitled

a guest
Oct 13th, 2012
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.39 KB | None | 0 0
  1. package com.jesseweiman.RPChat.gui;
  2.  
  3. import com.jesseweiman.RPChat.Message;
  4. import com.jesseweiman.RPChat.Properties;
  5. import com.jesseweiman.RPChat.Utils;
  6. import com.jesseweiman.RPChat.chat.ChatManager;
  7. import com.jesseweiman.RPChat.manager.ConfigManager;
  8.  
  9. import javax.swing.*;
  10. import javax.swing.text.BadLocationException;
  11. import java.awt.*;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.event.ActionListener;
  14.  
  15. /**
  16.  * Created with IntelliJ IDEA.
  17.  * User: Jesse
  18.  * Date: 10/2/12
  19.  * Time: 1:04 AM
  20.  * To change this template use File | Settings | File Templates.
  21.  */
  22. public class GUI extends JFrame implements ActionListener {
  23.  
  24.     ConfigManager configManager = new ConfigManager();
  25.     ChatManager chatManager = new ChatManager(this, configManager);
  26.  
  27.     GridBagLayout chatLayout = new GridBagLayout();
  28.     JPanel chatPanel = new JPanel(chatLayout);
  29.  
  30.     JTextPane statusPane = new JTextPane();
  31.     JScrollPane statusView = new JScrollPane(statusPane);
  32.     JTextPane recentChat = new JTextPane();
  33.     JScrollPane chatView = new JScrollPane(recentChat);
  34.     JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, chatPanel, statusView);
  35.  
  36.     JTextField inputField = new JTextField();
  37.     JButton sendButton = new JButton("Send");
  38.  
  39.     JMenuBar menuBar = new JMenuBar();
  40.     JMenu logMenu = new JMenu("Logs");
  41.     JMenu homeMenu = new JMenu("RPChat");
  42.     JMenuItem setNameItem = new JMenuItem("Set Name");
  43.     JMenuItem quitItem = new JMenuItem("Quit");
  44.     JMenu networkMenu = new JMenu("Network");
  45.     JMenuItem setPortItem = new JMenuItem("Set Listening Port");
  46.     JMenuItem setAddrItem = new JMenuItem("Set Buddy's Address");
  47.  
  48.  
  49.     public GUI(){
  50.         setupMenu();
  51.         setupChatPanel();
  52.         setupStatusPanel();
  53.         setupSplitPane();
  54.         setupListeners();
  55.  
  56.         setSize(600, 400);
  57.         setJMenuBar(menuBar);
  58.  
  59.         add(splitPane);
  60.  
  61.         setVisible(true);
  62.         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  63.     }
  64.  
  65.     private void sendMessage(){
  66.         String message = inputField.getText().trim();
  67.  
  68.         if(!message.isEmpty()){
  69.             chatManager.sendMessage(message);
  70.             updateChat();
  71.         }
  72.  
  73.         inputField.setText("");
  74.         inputField.requestFocusInWindow();
  75.     }
  76.  
  77.     public synchronized void updateChat(){
  78.         final JScrollBar scrollBar = chatView.getVerticalScrollBar();
  79.         boolean scrollBarVisible = scrollBar.isVisible();
  80.         boolean scrollBottom = false;
  81.         final int scrollValue = scrollBar.getValue();
  82.  
  83.         if(scrollBarVisible){
  84.             scrollBottom = scrollBar.getMaximum() == scrollBar.getValue() + scrollBar.getVisibleAmount();
  85.         }
  86.  
  87.         appendText();
  88.  
  89. //        if(!scrollBarVisible && scrollBar.isVisible() || scrollBottom) {
  90. //            scrollToBottom();
  91. //        }
  92.  
  93.         if( scrollBottom )
  94.         {
  95.             // force the slider to the bottom
  96.             EventQueue.invokeLater(new Runnable()
  97.             {
  98.                 @Override
  99.                 public void run()
  100.                 {
  101.                     scrollBar.setValue(scrollBar.getMaximum());
  102.                 }
  103.             });
  104.         }
  105.         else
  106.         {
  107.             // keep the slider where it is
  108.             EventQueue.invokeLater(new Runnable()
  109.             {
  110.                 @Override
  111.                 public void run()
  112.                 {
  113.                     scrollBar.setValue(scrollValue);
  114.                 }
  115.             });
  116.         }
  117.     }
  118.  
  119.     private void appendText(){
  120.         Message[] messages = chatManager.getMessages();
  121.         try{
  122.             recentChat.getDocument().insertString(recentChat.getDocument().getLength(), Utils.buildMessageString(messages[messages.length - 1]) + "\n", null);
  123.         }
  124.         catch(BadLocationException e){
  125.             //this will never happen
  126.             e.printStackTrace();
  127.         }
  128.     }
  129.  
  130.     private void scrollToBottom()
  131.     {
  132.         Rectangle visible = recentChat.getVisibleRect();
  133.         Rectangle bounds = recentChat.getBounds();
  134.         // You need to add some kind of offset to scroll it to
  135.         // the end; I put 50, but even some smaller values will do
  136.         visible.y = bounds.height - visible.height + 50;
  137.  
  138.         recentChat.scrollRectToVisible(visible);
  139.     }
  140.  
  141.     @Override
  142.     public void actionPerformed(ActionEvent e) {
  143.         JComponent component = (JComponent)e.getSource();
  144.  
  145.         if(component instanceof JMenuItem){
  146.             if(component == quitItem)
  147.                 System.exit(0);
  148.             else if(component == setNameItem){
  149.                 InputDialog nameDialogue = new InputDialog(this, configManager, "Name", "Please enter your name", configManager.getString(Properties.NAME_KEY, ChatManager.DEFAULT_NAME));
  150.                 nameDialogue.display();
  151.                 if(nameDialogue.clickedOK() && !nameDialogue.getInput().trim().isEmpty()){
  152.                     configManager.setProperty(Properties.NAME_KEY, nameDialogue.getInput());
  153.                 }
  154.             }
  155.             else if(component == setAddrItem){
  156.                 InputDialog nameDialogue = new InputDialog(this, configManager, "Name", "Please enter your name", configManager.getString(Properties.ADDRESS_KEY, "localhost"));
  157.                 nameDialogue.display();
  158.                 if(nameDialogue.clickedOK() && !nameDialogue.getInput().trim().isEmpty()){
  159.                     configManager.setProperty(Properties.ADDRESS_KEY, nameDialogue.getInput());
  160.                 }
  161.             }
  162.  
  163.         }
  164.         else if(component == sendButton || component == inputField)
  165.             sendMessage();
  166.     }
  167.  
  168.  
  169.     /*****************************************
  170.      *     BORING BOILER PLATE CODE
  171.      ******************************************/
  172.  
  173.     private void setupListeners() {
  174.         sendButton.addActionListener(this);
  175.         inputField.addActionListener(this);
  176.     }
  177.  
  178.     private void setupStatusPanel() {
  179.         statusPane.setEditable(false);
  180.     }
  181.  
  182.     private void setupSplitPane() {
  183.         splitPane.setResizeWeight(0.75);
  184.     }
  185.  
  186.     private void setupMenu(){
  187.         homeMenu.add(setNameItem);
  188.         homeMenu.add(quitItem);
  189.  
  190.         networkMenu.add(setPortItem);
  191.         networkMenu.add(setAddrItem);
  192.  
  193.         menuBar.add(homeMenu);
  194.         menuBar.add(logMenu);
  195.         menuBar.add(networkMenu);
  196.  
  197.         setNameItem.addActionListener(this);
  198.         quitItem.addActionListener(this);
  199.         setPortItem.addActionListener(this);
  200.         setAddrItem.addActionListener(this);
  201.     }
  202.  
  203.     private void setupChatPanel(){
  204.         chatPanel.setBackground(Color.BLUE);
  205.  
  206.         GridBagConstraints c = new GridBagConstraints();
  207.         c.fill = GridBagConstraints.BOTH;
  208.  
  209.         c.gridx = 0;
  210.         c.gridy = 0;
  211.         c.gridwidth = 2;
  212.         c.weightx = 1.0;
  213.         c.weighty = 1.0;
  214.         recentChat.setMinimumSize(new Dimension(75, 100));
  215.         recentChat.setEditable(false);
  216.         chatLayout.setConstraints(chatView, c);
  217.         chatPanel.add(chatView);
  218.  
  219.         c.gridwidth = 1;
  220.  
  221.         c.gridx = 0;
  222.         c.gridy = 1;
  223.         c.weightx = 1.0;
  224.         c.weighty = 0.0;
  225.         inputField.setMinimumSize(new Dimension(0, 25));
  226.         chatLayout.setConstraints(inputField, c);
  227.         chatPanel.add(inputField);
  228.  
  229.         c.gridx = 1;
  230.         c.weightx = 0;
  231.         chatLayout.setConstraints(sendButton, c);
  232.         chatPanel.add(sendButton);
  233.     }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement