KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > chat > monolithic > 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.monolithic;
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 local CCM_Client interface
35  * generated by the OpenCCM's IDL3 to IDL2 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
43 public class ClientImpl
44      extends org.omg.CORBA.LocalObject JavaDoc
45   implements CCM_Client,
46              org.omg.Components.SessionComponent,
47              java.awt.event.ActionListener JavaDoc
48 {
49     // ==================================================================
50
//
51
// Internal state.
52
//
53
// ==================================================================
54

55     /** The name of the component. */
56     private String JavaDoc name_;
57     
58     /** To refer to the GUI frame. */
59     private java.awt.Frame JavaDoc frame_;
60     /** To refer to the GUI user input field. */
61     private java.awt.TextField JavaDoc text_;
62     /** To refer to the GUI user output area. */
63     private java.awt.TextArea JavaDoc textArea_;
64
65     /** To refer to the component context. */
66     private CCM_Client_Context the_context_;
67
68
69     public static final java.awt.Color JavaDoc white_ = java.awt.Color.white;
70     
71     
72     // ==================================================================
73
//
74
// Constructor.
75
//
76
// ==================================================================
77

78     /** The default constructor. */
79     public
80     ClientImpl()
81     {
82     }
83
84     // ==================================================================
85
//
86
// Internal methods.
87
//
88
// ==================================================================
89

90     // ==================================================================
91
//
92
// Public methods.
93
//
94
// ==================================================================
95

96     // ==================================================================
97
//
98
// Methods for OMG IDL Components::EnterpriseComponent
99
//
100
// ==================================================================
101

102     /**
103      * Complete the component configuration.
104      *
105      * @exception org.omg.Components.InvalidConfiguration
106      * Thrown if the configuration is invalid.
107      */

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

158     /**
159      * Set the session component context.
160      *
161      * @param context The session component context.
162      *
163      * @throw org.omg.Components.CCMException For any problems.
164      */

165     public void
166     set_session_context(org.omg.Components.SessionContext context)
167     throws org.omg.Components.CCMException
168     {
169         the_context_ = (CCM_Client_Context)context;
170     }
171
172     /**
173      * Container callback to signal that the component is activated.
174      *
175      * @throw org.omg.Components.CCMException For any problems.
176      */

177     public void
178     ccm_activate()
179     throws org.omg.Components.CCMException
180     {
181         // Nothing to do currently.
182
}
183
184     /**
185      * Container callback to signal that the component is activated.
186      *
187      * @throw org.omg.Components.CCMException For any problems.
188      */

189     public void
190     ccm_passivate()
191     throws org.omg.Components.CCMException
192     {
193         // Nothing to do currently.
194
}
195
196     /**
197      * Container callback to signal that the component is removed.
198      *
199      * @throw org.omg.Components.CCMException For any problems.
200      */

201     public void
202     ccm_remove()
203     throws org.omg.Components.CCMException
204     {
205         // Release the associated frame.
206
if(frame_!=null)
207             frame_.dispose();
208         frame_ = null;
209     }
210
211     // ==================================================================
212
//
213
// Methods for OMG IDL chat::CCM_NamedComponent_Executor
214
//
215
// ==================================================================
216

217     /**
218      * The mutator method for the attribute name.
219      *
220      * @param n The name.
221      */

222     public void
223     name(String JavaDoc n)
224     {
225         name_ = n;
226
227         if (frame_ != null)
228             frame_.setTitle(name_ + "'s Client GUI");
229     }
230
231     /**
232      * The accessor method for the attribute name.
233      *
234      * @return The name.
235      */

236     public String JavaDoc
237     name()
238     {
239         return name_;
240     }
241
242     // ==================================================================
243
//
244
// Methods for interface java.awt.event.ActionListener
245
//
246
// ==================================================================
247

248     /**
249      * When the button is selectionned.
250      *
251      * @param e The associate ActionEvent.
252      */

253     public void
254     actionPerformed(java.awt.event.ActionEvent JavaDoc e)
255     {
256         // Obtain the object reference associated to the
257
// 'the_service' receptacle.
258
Service service = the_context_.get_connection_the_service();
259
260         // Check if the connection is available.
261
if(service == null)
262         {
263             System.err.println("The chat::Client::the_service receptacle is not set!");
264             return;
265         }
266
267         // Calls the display service.
268
service.display(name_ + ":" + text_.getText());
269         
270         // Reset entered text
271
text_.setText("");
272         
273     }
274     
275     
276     
277     // ==================================================================
278
//
279
// Methods for OMG IDL chat::CCM_Client
280
//
281
// ==================================================================
282

283     
284     public void
285         push_from_servers(TextEvent event)
286         {
287             push(event);
288         }
289     
290     
291     
292     // ==================================================================
293
//
294
// Methods for OMG IDL chat::CCM_TextEventConsumer
295
//
296
// ==================================================================
297

298     /**
299      * To receive chat::Event events published by chat::Server components.
300      *
301      * @param event The received event.
302      */

303     public void
304     push(TextEvent event)
305     {
306         // Put the text into the text area.
307
textArea_.append(event.text + "\n");
308     }
309     
310
311 }
312
Popular Tags