KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > chat > cif > ClientImpl


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2002 USTL - LIFL - GOAL
5 Contact: openccm-team@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Areski Flissi.
23 Contributor(s):
24
25 ====================================================================*/

26
27 package org.objectweb.ccm.chat.cif;
28
29 import org.objectweb.ccm.chat.*;
30
31 /**
32  * This is the implementation of the OMG IDL3 chat::Client component type.
33  *
34  * This class inherits from the skeleton
35  * generated by the OpenCCM's CIF to Java mapping generator.
36  *
37  * This class also implements the java.awt.event.ActionListener to manage
38  * users' interactions on the Swing GUI associated to this component.
39  *
40  * @author <a HREF="mailto:Areski.Flissi@lifl.fr">Areski Flissi</A>
41  */

42 public class ClientImpl
43      extends org.objectweb.ccm.chat.ClientSessionComposition.ComponentImpl
44   implements java.awt.event.ActionListener JavaDoc
45 {
46     // ==================================================================
47
//
48
// Internal state.
49
//
50
// ==================================================================
51

52     /** The name of the component. */
53     private String JavaDoc name_;
54     
55     /** To refer to the GUI frame. */
56     private java.awt.Frame JavaDoc frame_;
57     /** To refer to the GUI user input field. */
58     private java.awt.TextField JavaDoc text_;
59     /** To refer to the GUI user output area. */
60     private java.awt.TextArea JavaDoc textArea_;
61     
62     public static final java.awt.Color JavaDoc white_ = java.awt.Color.white;
63     
64     // ==================================================================
65
//
66
// Constructor.
67
//
68
// ==================================================================
69

70     /** The default constructor. */
71     public
72     ClientImpl()
73     {
74     }
75
76     // ==================================================================
77
//
78
// Internal methods.
79
//
80
// ==================================================================
81

82     // ==================================================================
83
//
84
// Public methods.
85
//
86
// ==================================================================
87

88     // ==================================================================
89
//
90
// Methods for OMG IDL Components::EnterpriseComponent
91
//
92
// ==================================================================
93

94     /**
95      * Complete the component configuration.
96      *
97      * @exception org.omg.Components.InvalidConfiguration
98      * Thrown if the configuration is invalid.
99      */

100     public void
101     configuration_complete()
102     throws org.omg.Components.InvalidConfiguration
103     {
104     // Checks if the configuration is completed.
105
if(name_ == null)
106         throw new org.omg.Components.InvalidConfiguration();
107
108     // Check if the connection to the server is set.
109
if(get_context().get_connection_the_service() == null)
110         throw new org.omg.Components.InvalidConfiguration();
111
112     //Instantiating the GUI.
113
// Creates a AWT Frame.
114
frame_ = new java.awt.Frame JavaDoc(name_ + "'s Chat GUI");
115         
116     // Creates a text area for displaying inputs.
117
textArea_ = new java.awt.TextArea JavaDoc();
118     textArea_.setEditable(false);
119     textArea_.setBackground(white_);
120          
121          
122     //Creates a text field for user's inputs.
123
text_ = new java.awt.TextField JavaDoc("", 1);
124     // Creates a button to invoke the connected server component.
125
java.awt.Button JavaDoc button = new java.awt.Button JavaDoc("Print Text");
126     button.addActionListener(this);
127     // Constructs and shows the GUI.
128
java.awt.Panel JavaDoc panel = new java.awt.Panel JavaDoc(
129                                 new java.awt.BorderLayout JavaDoc()
130                                     );
131     System.out.println("___________________________");
132     frame_.setLayout ( new java.awt.BorderLayout JavaDoc() ) ;
133     frame_.add(panel);
134                  
135     panel.add(textArea_,java.awt.BorderLayout.NORTH);
136     panel.add(text_,java.awt.BorderLayout.CENTER);
137     panel.add(button,java.awt.BorderLayout.SOUTH);
138     
139     frame_.setSize(400,300);
140     frame_.pack();
141     frame_.show();
142     
143     }
144
145     // ==================================================================
146
//
147
// Methods for the OMG IDL org.omg.Components.SessionComponent
148
//
149
// ==================================================================
150

151     /**
152      * Container callback to signal that the component is removed.
153      *
154      * @throw org.omg.Components.CCMException For any problems.
155      */

156     public void
157     ccm_remove()
158     throws org.omg.Components.CCMException
159     {
160          // Release the associated frame.
161
if(frame_!=null)
162             frame_.dispose();
163         frame_ = null;
164     }
165
166     // ==================================================================
167
//
168
// Methods for OMG IDL chat::CCM_NamedComponent_Executor
169
//
170
// ==================================================================
171

172     /**
173      * The mutator method for the attribute name.
174      *
175      * @param n The name.
176      */

177     public void
178     name(String JavaDoc n)
179     {
180         name_ = n;
181
182         if (frame_ != null)
183            frame_.setTitle(name_ + "'s Client GUI");
184     }
185
186     /**
187      * The accessor method for the attribute name.
188      *
189      * @return The name.
190      */

191     public String JavaDoc
192     name()
193     {
194         return name_;
195     }
196
197
198
199     // ==================================================================
200
//
201
// Methods for interface java.awt.event.ActionListener
202
//
203
// ==================================================================
204

205     /**
206      * When the button is selectionned.
207      *
208      * @param e The associate ActionEvent.
209      */

210     public void
211     actionPerformed(java.awt.event.ActionEvent JavaDoc e)
212     {
213         // Obtain the object reference associated to the
214
// 'the_service' receptacle.
215
Service service = get_context().get_connection_the_service();
216
217         // Check if the connection is available.
218
if(service == null)
219         {
220             System.err.println("The chat::Client::the_service receptacle is not set!");
221             return;
222         }
223  
224         // Calls the display service.
225
service.display(name_ + ":" + text_.getText());
226         
227         // Reset entered text
228
text_.setText("");
229         
230     }
231     
232     
233     
234     // ==================================================================
235
//
236
// Methods for OMG IDL chat::CCM_TextEventConsumer
237
//
238
// ==================================================================
239

240     /**
241      * To receive chat::Event events published by chat::Server components.
242      *
243      * @param event The received event.
244      */

245     public void
246     push(TextEvent event)
247     {
248         // Put the text into the text area.
249
textArea_.append(event.text + "\n");
250     }
251     
252 }
253
Popular Tags