KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > chat > ChatGUI


1
2 package org.objectweb.proactive.examples.chat;
3
4 import java.awt.BorderLayout JavaDoc;
5 import java.awt.Dimension JavaDoc;
6 import java.awt.Toolkit JavaDoc;
7 import java.awt.event.ActionEvent JavaDoc;
8 import java.awt.event.KeyEvent JavaDoc;
9 import java.awt.event.WindowAdapter JavaDoc;
10 import java.awt.event.WindowEvent JavaDoc;
11
12 import javax.swing.AbstractAction JavaDoc;
13 import javax.swing.Action JavaDoc;
14 import javax.swing.BorderFactory JavaDoc;
15 import javax.swing.Box JavaDoc;
16 import javax.swing.JButton JavaDoc;
17 import javax.swing.JFrame JavaDoc;
18 import javax.swing.JLabel JavaDoc;
19 import javax.swing.JOptionPane JavaDoc;
20 import javax.swing.JPanel JavaDoc;
21 import javax.swing.JScrollPane JavaDoc;
22 import javax.swing.JTextArea JavaDoc;
23 import javax.swing.JTextField JavaDoc;
24 import javax.swing.WindowConstants JavaDoc;
25
26
27
28 /**
29  * @author Laurent Baduel
30  */

31
32 public class ChatGUI extends JFrame JavaDoc {
33
34     public JTextField JavaDoc message = new JTextField JavaDoc(); //55
35
public JTextField JavaDoc location = new JTextField JavaDoc(); //20
36
public JTextArea JavaDoc text = new JTextArea JavaDoc(25,55); //25,55
37
public JTextArea JavaDoc list = new JTextArea JavaDoc(0,4);
38     private JButton JavaDoc quit = new JButton JavaDoc(new QuitAction());
39     private JButton JavaDoc send = new JButton JavaDoc(new SendAction());
40     private JButton JavaDoc migrate = new JButton JavaDoc(new MigrateAction());
41
42     private Chat oa;
43
44     /*
45      * Builds the Graphic User Interface.
46      */

47     public ChatGUI(final Chat oa, String JavaDoc writerName) {
48         super("Chat with ProActive");
49         
50         this.oa = oa;
51
52         setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
53
54         // Position of the window (centered)
55
Toolkit JavaDoc tk = Toolkit.getDefaultToolkit();
56         Dimension JavaDoc 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 JavaDoc panel = new JPanel JavaDoc();
63         panel.setLayout(new BorderLayout JavaDoc());
64
65         Box JavaDoc horizontalBoxMigration = Box.createHorizontalBox();
66         horizontalBoxMigration.add(new JLabel JavaDoc(" migrate to "));
67         horizontalBoxMigration.add(location);
68         horizontalBoxMigration.add(migrate);
69         
70         Box JavaDoc verticalBoxText = Box.createVerticalBox();
71         verticalBoxText.add(new JLabel JavaDoc(" --- History of messages --- "));
72         // Add a scroll bar to the text area
73
JScrollPane JavaDoc textScrollPanel = new JScrollPane JavaDoc(text);
74         text.setEditable(false);
75         verticalBoxText.add(textScrollPanel);
76         verticalBoxText.setBorder(BorderFactory.createTitledBorder(""));
77
78         Box JavaDoc verticalBoxMessage = Box.createVerticalBox();
79         verticalBoxMessage.add(new JLabel JavaDoc("Message to send :"));
80         verticalBoxMessage.add(message);
81         verticalBoxMessage.setBorder(BorderFactory.createTitledBorder(writerName));
82
83         Box JavaDoc horizontalBoxMessage = Box.createHorizontalBox();
84         horizontalBoxMessage.add(verticalBoxMessage);
85         horizontalBoxMessage.add(send);
86         horizontalBoxMessage.add(quit);
87
88         Box JavaDoc verticalBoxConnectedUsers = Box.createVerticalBox();
89         verticalBoxConnectedUsers.add(new JLabel JavaDoc("Connected users"));
90         JScrollPane JavaDoc listScrollPanel = new JScrollPane JavaDoc(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 JavaDoc() {
103             // Focus on the edit message field
104
public void windowOpened( WindowEvent JavaDoc e ) {
105                 message.requestFocus();
106             }
107             // Pop a windows to confirm the close of the application
108
public void windowClosing(WindowEvent JavaDoc 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     /**
127      * Action to leave the application (with confirmation)
128      */

129     private class QuitAction extends AbstractAction JavaDoc {
130
131         public QuitAction() {
132             putValue(Action.NAME, "Quit");
133             putValue(Action.MNEMONIC_KEY, new Integer JavaDoc(KeyEvent.VK_Q));
134             putValue(ACTION_COMMAND_KEY, "quit");
135             putValue(SHORT_DESCRIPTION, "Quit");
136             putValue(LONG_DESCRIPTION, "Quit the application");
137 // putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.CTRL_MASK));
138
}
139
140         public void actionPerformed(ActionEvent JavaDoc e) {
141             // Pop a windows to confirm the close of the application
142
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 // oa.disposeFrame();
149
ChatGUI.this.dispose();
150             }
151         }
152     }
153
154     /**
155      * Action to send a message
156      */

157     private class SendAction extends AbstractAction JavaDoc {
158
159         public SendAction() {
160             putValue(Action.NAME, "Send");
161             putValue(Action.MNEMONIC_KEY, new Integer JavaDoc(KeyEvent.VK_S));
162             putValue(ACTION_COMMAND_KEY, "send");
163             putValue(SHORT_DESCRIPTION, "Send");
164             putValue(LONG_DESCRIPTION, "Send a message");
165 // putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.CTRL_MASK));
166
}
167
168         public void actionPerformed(ActionEvent JavaDoc 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     /**
178      * Action to migrate to another node
179      */

180     private class MigrateAction extends AbstractAction JavaDoc {
181
182         public MigrateAction() {
183             putValue(Action.NAME, "Migrate !");
184             putValue(Action.MNEMONIC_KEY, new Integer JavaDoc(KeyEvent.VK_M));
185             putValue(ACTION_COMMAND_KEY, "migrate");
186             putValue(SHORT_DESCRIPTION, "Migrate");
187             putValue(LONG_DESCRIPTION, "Migrate to a specified node");
188 // putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_3, ActionEvent.CTRL_MASK));
189
}
190
191         public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc[] args) {
200         ChatGUI fenetre = new ChatGUI(null,"anonymous");
201     }
202     
203 }
204
Popular Tags