KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > sessiondemo > ClientImpl


1 // ====================================================================
2
//
3
// ECM: The Extensible Container Model
4
// Copyright (C) 2004 THALES
5
// Contact: openccm-ecm@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): Mathieu Vadet.
23
// Initial Funding: IST COACH European project (IST-2001-34445)
24
// http://www.ist-coach.org
25
//
26
// ====================================================================
27

28
29
30 package org.objectweb.ccm.sessiondemo;
31
32 /**
33  ** Implementation of the OMG IDL3 <tt>Client</tt> component type.
34  **/

35 public class ClientImpl
36 extends Client_MainSegBase
37 implements java.awt.event.ActionListener JavaDoc, java.io.Serializable JavaDoc
38 {
39     // client
40
private javax.swing.JFrame JavaDoc _frame;
41     private javax.swing.JList JavaDoc _product_list;
42     private javax.swing.JList JavaDoc _caddie_list;
43     private Server _server;
44     private org.omg.Components.Cookie _i_server_ck;
45
46     // default constructor
47
public
48     ClientImpl()
49     {
50         // client
51
_frame = null;
52         _product_list = null;
53         _caddie_list = null;
54         _server = null;
55         _i_server_ck = null;
56     }
57
58     //
59
// overriden operations
60
//
61

62     final public void
63     configuration_complete()
64     throws org.omg.Components.InvalidConfiguration
65     {
66         // create a GUI
67
// NORTH: panel with buttons "enter" and "leave"
68
javax.swing.JPanel JavaDoc npanel = new javax.swing.JPanel JavaDoc(new java.awt.BorderLayout JavaDoc());
69         javax.swing.JButton JavaDoc enterb = new javax.swing.JButton JavaDoc("Enter");
70         enterb.setActionCommand("enter");
71         enterb.addActionListener(this);
72         javax.swing.JButton JavaDoc leaveb = new javax.swing.JButton JavaDoc("Leave");
73         leaveb.setActionCommand("leave");
74         leaveb.addActionListener(this);
75         npanel.add(enterb, java.awt.BorderLayout.WEST);
76         npanel.add(leaveb, java.awt.BorderLayout.EAST);
77
78         // SOUTH: box with products and caddie panels
79
// EAST: panel with product list and button "Purchase"
80
javax.swing.JPanel JavaDoc bnpanel = new javax.swing.JPanel JavaDoc(new java.awt.BorderLayout JavaDoc());
81         _product_list = new javax.swing.JList JavaDoc();
82         javax.swing.JButton JavaDoc purchaseb = new javax.swing.JButton JavaDoc("Purchase");
83         purchaseb.setActionCommand("purchase");
84         purchaseb.addActionListener(this);
85         bnpanel.add(_product_list, java.awt.BorderLayout.NORTH);
86         bnpanel.add(purchaseb, java.awt.BorderLayout.SOUTH);
87
88         // WEST: panel with caddie list and button "Remove"
89
javax.swing.JPanel JavaDoc bspanel = new javax.swing.JPanel JavaDoc(new java.awt.BorderLayout JavaDoc());
90         _caddie_list = new javax.swing.JList JavaDoc();
91         javax.swing.JButton JavaDoc removeb = new javax.swing.JButton JavaDoc("Remove");
92         removeb.setActionCommand("remove");
93         removeb.addActionListener(this);
94         bspanel.add(_caddie_list, java.awt.BorderLayout.NORTH);
95         bspanel.add(removeb, java.awt.BorderLayout.SOUTH);
96
97         javax.swing.JPanel JavaDoc spanel = new javax.swing.JPanel JavaDoc(new java.awt.BorderLayout JavaDoc());
98         spanel.add(bnpanel, java.awt.BorderLayout.EAST);
99         spanel.add(bspanel, java.awt.BorderLayout.WEST);
100
101         // MAIN panel
102
javax.swing.JPanel JavaDoc panel = new javax.swing.JPanel JavaDoc(new java.awt.BorderLayout JavaDoc());
103         panel.add(npanel, java.awt.BorderLayout.NORTH);
104         panel.add(spanel, java.awt.BorderLayout.SOUTH);
105         _frame = new javax.swing.JFrame JavaDoc("Client GUI");
106         _frame.getContentPane().add(panel);
107         _frame.pack();
108         _frame.show();
109     }
110
111     //
112
// IDL:objectweb.org/ccm/sessiondemo/Client:1.0
113
//
114

115     //
116
// java.awt.event.ActionListener
117
//
118

119     final public void
120     actionPerformed(java.awt.event.ActionEvent JavaDoc e)
121     {
122         String JavaDoc cmd = e.getActionCommand();
123
124         if (cmd.equals("enter")) {
125             // create a new Server component and connect it
126
try {
127                 org.omg.Components.LocalReceptacles lrecs = getCCM2Context().get_receptacles();
128                 org.omg.CORBA.Object JavaDoc obj = lrecs.get_connections("server_home")[0].ref();
129                 ServerHome shome = ServerHomeHelper.narrow(obj);
130                 // NOTE: typed operation
131
// ServerHome shome = get_connection_server_home();
132

133                 _server = shome.create();
134                 _server.configuration_complete();
135
136                 _i_server_ck = lrecs.connect("i_server", _server.provide_i_server());
137                 // NOTE: typed operation
138
// connect_i_server(_server.provide_i_server());
139

140                 // obtain product list and display
141
obj = lrecs.get_connections("i_server")[0].ref();
142                 IServer iserver = IServerHelper.narrow(obj);
143                 Product[] products = iserver.list_products();
144                 // NOTE: typed operation
145
// Product[] products = get_connection_i_server().list_products();
146

147                 String JavaDoc[] pids = new String JavaDoc[products.length];
148                 for (int i=0;i<products.length;i++) {
149                     pids[i] = products[i].id;
150                 }
151
152                 _product_list.setListData(pids);
153                 return ;
154             } catch (Exception JavaDoc ex) {
155                 ex.printStackTrace();
156                 return;
157             }
158         }
159
160         if (cmd.equals("leave")) {
161             // remove everything from caddie
162
try {
163                 org.omg.Components.LocalReceptacles lrecs = getCCM2Context().get_receptacles();
164                 javax.swing.ListModel JavaDoc lmodel = _caddie_list.getModel();
165                 String JavaDoc[] ids = new String JavaDoc[lmodel.getSize()];
166                 for (int i=0;i<ids.length;i++) {
167                     ids[i] = (String JavaDoc)lmodel.getElementAt(i);
168                 }
169
170                 org.omg.CORBA.Object JavaDoc obj = lrecs.get_connections("i_server")[0].ref();
171                 IServer iserver = IServerHelper.narrow(obj);
172                 // NOTE: typed operation
173
// IServer iserver = get_connection_i_server();
174
iserver.remove_products(ids);
175
176                 // disconnect
177
lrecs.disconnect(_i_server_ck);
178                 // NOTE: typed operation
179
// disconnect_i_server();
180

181                 // clear list
182
_product_list.setListData(new Object JavaDoc[0]);
183                 _caddie_list.setListData(new Object JavaDoc[0]);
184
185                 // destroy component
186
obj = lrecs.get_connections("server_home")[0].ref();
187                 ServerHome shome = ServerHomeHelper.narrow(obj);
188                 shome.remove_component(_server);
189                 return ;
190             } catch (Exception JavaDoc ex) {
191                 ex.printStackTrace();
192                 return ;
193             }
194         }
195
196         if (cmd.equals("purchase")) {
197             // obtain the purchased ids
198
try {
199                 org.omg.Components.LocalReceptacles lrecs = getCCM2Context().get_receptacles();
200                 Object JavaDoc[] objs = _product_list.getSelectedValues();
201                 String JavaDoc[] ids = new String JavaDoc[objs.length];
202                 for (int i=0;i<objs.length;i++) {
203                     ids[i] = (String JavaDoc)objs[i];
204                 }
205
206                 // purchase
207
org.omg.CORBA.Object JavaDoc obj = lrecs.get_connections("i_server")[0].ref();
208                 IServer iserver = IServerHelper.narrow(obj);
209                 // NOTE: typed operation
210
// IServer iserver = get_connection_i_server();
211
iserver.purchase_products(ids);
212
213                 // update caddie and display
214
Product[] products = iserver.view_caddie();
215                 String JavaDoc[] pids = new String JavaDoc[products.length];
216                 for (int i=0;i<products.length;i++) {
217                     pids[i] = products[i].id;
218                 }
219
220                 _product_list.clearSelection();
221                 _caddie_list.setListData(pids);
222                 return ;
223             } catch (Exception JavaDoc ex) {
224                 ex.printStackTrace();
225                 return ;
226             }
227         }
228
229         if (cmd.equals("remove")) {
230             // obtain the removed ids
231
try {
232                 org.omg.Components.LocalReceptacles lrecs = getCCM2Context().get_receptacles();
233                 Object JavaDoc[] objs = _caddie_list.getSelectedValues();
234                 String JavaDoc[] ids = new String JavaDoc[objs.length];
235                 for (int i=0;i<objs.length;i++) {
236                     ids[i] = (String JavaDoc)objs[i];
237                 }
238
239                 // remove
240
org.omg.CORBA.Object JavaDoc obj = lrecs.get_connections("i_server")[0].ref();
241                 IServer iserver = IServerHelper.narrow(obj);
242                 // NOTE: typed operation
243
// IServer iserver = get_connection_i_server();
244
iserver.remove_products(ids);
245
246                 // update caddie and display
247
Product[] products = iserver.view_caddie();
248                 String JavaDoc[] pids = new String JavaDoc[products.length];
249                 for (int i=0;i<products.length;i++) {
250                     pids[i] = products[i].id;
251                 }
252
253                 _caddie_list.clearSelection();
254                 _caddie_list.setListData(pids);
255                 return ;
256             } catch (Exception JavaDoc ex) {
257                 ex.printStackTrace();
258                 return ;
259             }
260         }
261     }
262 }
263
Popular Tags