1 2 package org.objectweb.proactive.examples.chat; 3 4 import java.awt.BorderLayout ; 5 import java.awt.Dimension ; 6 import java.awt.Toolkit ; 7 import java.awt.event.ActionEvent ; 8 import java.awt.event.KeyEvent ; 9 import java.awt.event.WindowAdapter ; 10 import java.awt.event.WindowEvent ; 11 12 import javax.swing.AbstractAction ; 13 import javax.swing.Action ; 14 import javax.swing.BorderFactory ; 15 import javax.swing.Box ; 16 import javax.swing.JButton ; 17 import javax.swing.JFrame ; 18 import javax.swing.JLabel ; 19 import javax.swing.JOptionPane ; 20 import javax.swing.JPanel ; 21 import javax.swing.JScrollPane ; 22 import javax.swing.JTextArea ; 23 import javax.swing.JTextField ; 24 import javax.swing.WindowConstants ; 25 26 27 28 31 32 public class ChatGUI extends JFrame { 33 34 public JTextField message = new JTextField (); public JTextField location = new JTextField (); public JTextArea text = new JTextArea (25,55); public JTextArea list = new JTextArea (0,4); 38 private JButton quit = new JButton (new QuitAction()); 39 private JButton send = new JButton (new SendAction()); 40 private JButton migrate = new JButton (new MigrateAction()); 41 42 private Chat oa; 43 44 47 public ChatGUI(final Chat oa, String writerName) { 48 super("Chat with ProActive"); 49 50 this.oa = oa; 51 52 setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); 53 54 Toolkit tk = Toolkit.getDefaultToolkit(); 56 Dimension d = tk.getScreenSize(); 57 int screenHeight = d.height; 58 int screenWidth = d.width; 59 setSize(screenWidth/2, screenHeight/2); 60 setLocation(screenWidth/4, screenHeight/4); 61 62 JPanel panel = new JPanel (); 63 panel.setLayout(new BorderLayout ()); 64 65 Box horizontalBoxMigration = Box.createHorizontalBox(); 66 horizontalBoxMigration.add(new JLabel (" migrate to ")); 67 horizontalBoxMigration.add(location); 68 horizontalBoxMigration.add(migrate); 69 70 Box verticalBoxText = Box.createVerticalBox(); 71 verticalBoxText.add(new JLabel (" --- History of messages --- ")); 72 JScrollPane textScrollPanel = new JScrollPane (text); 74 text.setEditable(false); 75 verticalBoxText.add(textScrollPanel); 76 verticalBoxText.setBorder(BorderFactory.createTitledBorder("")); 77 78 Box verticalBoxMessage = Box.createVerticalBox(); 79 verticalBoxMessage.add(new JLabel ("Message to send :")); 80 verticalBoxMessage.add(message); 81 verticalBoxMessage.setBorder(BorderFactory.createTitledBorder(writerName)); 82 83 Box horizontalBoxMessage = Box.createHorizontalBox(); 84 horizontalBoxMessage.add(verticalBoxMessage); 85 horizontalBoxMessage.add(send); 86 horizontalBoxMessage.add(quit); 87 88 Box verticalBoxConnectedUsers = Box.createVerticalBox(); 89 verticalBoxConnectedUsers.add(new JLabel ("Connected users")); 90 JScrollPane listScrollPanel = new JScrollPane (list); 91 list.setEditable(false); 92 verticalBoxConnectedUsers.add(listScrollPanel); 93 verticalBoxConnectedUsers.setBorder(BorderFactory.createTitledBorder("")); 94 95 panel.add(horizontalBoxMigration,BorderLayout.NORTH); 96 panel.add(verticalBoxText,BorderLayout.CENTER); 97 panel.add(horizontalBoxMessage,BorderLayout.SOUTH); 98 panel.add(verticalBoxConnectedUsers,BorderLayout.EAST); 99 100 getContentPane().add(panel); 101 102 addWindowListener(new WindowAdapter () { 103 public void windowOpened( WindowEvent e ) { 105 message.requestFocus(); 106 } 107 public void windowClosing(WindowEvent e) { 109 int reponse = JOptionPane.showConfirmDialog( ChatGUI.this, 110 "Are you sure you want to quit ?", 111 "Quit the chat", 112 JOptionPane.YES_NO_OPTION); 113 if (reponse == JOptionPane.YES_OPTION) { 114 oa.disconnect(); 115 ChatGUI.this.dispose(); 116 } 117 } 118 }); 119 120 pack(); 121 show(); 122 123 } 124 125 126 129 private class QuitAction extends AbstractAction { 130 131 public QuitAction() { 132 putValue(Action.NAME, "Quit"); 133 putValue(Action.MNEMONIC_KEY, new Integer (KeyEvent.VK_Q)); 134 putValue(ACTION_COMMAND_KEY, "quit"); 135 putValue(SHORT_DESCRIPTION, "Quit"); 136 putValue(LONG_DESCRIPTION, "Quit the application"); 137 } 139 140 public void actionPerformed(ActionEvent e) { 141 int reponse = JOptionPane.showConfirmDialog( ChatGUI.this, 143 "Are you sure you want to quit ?", 144 "Quit the chat", 145 JOptionPane.YES_NO_OPTION); 146 if (reponse == JOptionPane.YES_OPTION) { 147 oa.disconnect(); 148 ChatGUI.this.dispose(); 150 } 151 } 152 } 153 154 157 private class SendAction extends AbstractAction { 158 159 public SendAction() { 160 putValue(Action.NAME, "Send"); 161 putValue(Action.MNEMONIC_KEY, new Integer (KeyEvent.VK_S)); 162 putValue(ACTION_COMMAND_KEY, "send"); 163 putValue(SHORT_DESCRIPTION, "Send"); 164 putValue(LONG_DESCRIPTION, "Send a message"); 165 } 167 168 public void actionPerformed(ActionEvent e) { 169 if (message.getText().length() != 0) { 170 oa.writeMessage(new Message(oa.getName(),message.getText())); 171 message.setText(""); 172 } 173 message.requestFocus(); 174 } 175 } 176 177 180 private class MigrateAction extends AbstractAction { 181 182 public MigrateAction() { 183 putValue(Action.NAME, "Migrate !"); 184 putValue(Action.MNEMONIC_KEY, new Integer (KeyEvent.VK_M)); 185 putValue(ACTION_COMMAND_KEY, "migrate"); 186 putValue(SHORT_DESCRIPTION, "Migrate"); 187 putValue(LONG_DESCRIPTION, "Migrate to a specified node"); 188 } 190 191 public void actionPerformed(ActionEvent e) { 192 if (location.getText().length() != 0) 193 oa.migrateTo(location.getText()); 194 else 195 location.requestFocus(); 196 } 197 } 198 199 public static void main(String [] args) { 200 ChatGUI fenetre = new ChatGUI(null,"anonymous"); 201 } 202 203 } 204 | Popular Tags |