KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > jms > topics > GuiChat > GuiContiner


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Coridan.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  *
46  * Created on Jan 10, 2005
47  *
48  * Coridan LTD
49  */

50 package sample.jms.topics.GuiChat;
51 import java.awt.*;
52 import java.awt.event.ActionEvent JavaDoc;
53 import java.awt.event.ActionListener JavaDoc;
54 import javax.swing.*;
55 import javax.swing.border.BevelBorder JavaDoc;
56
57
58 /*=================================================================================
59   For instructions on how to run this sample please refer to the file
60   sample\jms\topics\GuiChat\Readme.txt under the MantaRay installation directory.
61 =================================================================================*/

62
63
64 public class GuiContiner implements ActionListener JavaDoc {
65     //GUI variables
66
JFrame chantFrame;
67     Container chatPanel;
68     static JTextArea chatArea;
69     JTextField message;
70     JTextField name;
71     JLabel messageInstructions;
72     JLabel nameInstructions;
73     JLabel cahtTitle;
74     JButton submitName;
75     JButton submitMessage;
76     JScrollPane chatAreaPane;
77     JPanel upperPanel;
78     JPanel enterMessagePanel;
79
80     GuiChat chat=null;
81     String JavaDoc userName="anonymous";
82
83     /**
84      * creates the chat itself and the GUI frame and contents
85      */

86     public GuiContiner(){
87         //the GuiChat creates the chat itself
88
chat = new GuiChat(this);
89
90         // create and set up the container and all its components.
91
chantFrame = new JFrame("MantaRay Chat");
92         JFrame.setDefaultLookAndFeelDecorated(true);
93         chantFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
94
95         //create labels
96
messageInstructions = new JLabel("Enter Message:", SwingConstants.LEFT);
97         messageInstructions.setForeground(Color.blue);
98         nameInstructions = new JLabel(" Enter Name:", SwingConstants.CENTER);
99         nameInstructions.setForeground(Color.blue);
100         cahtTitle = new JLabel("Conversation");
101         cahtTitle.setFont(new Font("font",1,16));
102         cahtTitle.setForeground(Color.black);
103
104         //create bottons
105
submitMessage= new JButton("Send Message");
106         submitName = new JButton("Change Name");
107         submitName.addActionListener(this);
108         submitMessage.addActionListener(this);
109
110         //create the message text field
111
message = new JTextField(30);
112         message.setToolTipText("Enter your message here");
113         message.setHorizontalAlignment(SwingConstants.LEFT);
114         message.setMaximumSize(new Dimension(70,25));
115        // message.setSize(50,25);
116
message.addActionListener(this);
117
118         //create the user name text field
119
name = new JTextField(10);
120         name.setToolTipText("Enter user name here");
121         name.setHorizontalAlignment(SwingConstants.LEFT);
122         name.setMaximumSize(new Dimension(50,25));
123         name.addActionListener(this);
124
125         //create that chat area
126
chatArea = new JTextArea(8,30);
127         chatArea.setEditable(false);
128         chatArea.setFont(new Font("font",2,14));
129         chatAreaPane = new JScrollPane(chatArea);
130         chatAreaPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED ));
131
132         //create and populate upper panel
133
upperPanel = new JPanel(new GridLayout(1,4));
134         upperPanel.add(cahtTitle);
135         upperPanel.add(nameInstructions);
136         upperPanel.add(name);
137         upperPanel.add(submitName);
138
139         //Create and populate the inputed message panel
140
enterMessagePanel= new JPanel(new BorderLayout(1,1));
141         enterMessagePanel.add(messageInstructions, BorderLayout.WEST);
142         messageInstructions.setLabelFor(message);
143         enterMessagePanel.add(message, BorderLayout.CENTER);
144         enterMessagePanel.add(submitMessage, BorderLayout.EAST);
145
146         //add componentes and to the container
147
chatPanel=chantFrame.getContentPane();
148         chatPanel.add(upperPanel, BorderLayout.NORTH);
149         chatPanel.add(chatAreaPane, BorderLayout.CENTER );
150         chatPanel.add(enterMessagePanel, BorderLayout.SOUTH );
151
152         //Display the window.
153
chantFrame.pack();
154         chantFrame.setVisible(true);
155     }//constractor
156

157     /* (non-Javadoc)
158      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
159      */

160     public void actionPerformed(ActionEvent JavaDoc e) {
161         //a message had been sent
162
if (e.getSource() == message || e.getSource() == submitMessage){
163             chat.chatter(userName,message.getText());
164             message.setText("");
165         }
166
167         //the user's name was edited
168
if (e.getSource() == name||e.getSource()==submitName){
169             if (!name.getText().equals("")){
170                 userName=name.getText();
171             }
172         }
173
174     }//actionPerformed
175

176     /**
177      * when a message is received from the chat this methoda displays it
178      * @param msg the message that was received, including the sender's name in it
179      */

180     public void doChat(String JavaDoc msg) {
181         chatArea.append(msg + "\n");
182         chatArea.setCaretPosition(chatArea.getDocument().getLength());
183     }//doChat
184

185     /** Main program entry point. */
186     public static void main(String JavaDoc argv[]) {
187         System.out.println("MANTARAY GUI CHAT IS STARTING");
188         //creating and showing this application's GUI.
189
javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc() {
190             public void run() {
191                 GuiContiner GuiDisplay = new GuiContiner();
192             }
193         });
194     }//main
195

196 }//GuiContiner
197
Popular Tags