1 28 29 30 package org.objectweb.ccm.sessiondemo; 31 32 35 public class ClientImpl 36 extends Client_MainSegBase 37 implements java.awt.event.ActionListener , java.io.Serializable 38 { 39 private javax.swing.JFrame _frame; 41 private javax.swing.JList _product_list; 42 private javax.swing.JList _caddie_list; 43 private Server _server; 44 private org.omg.Components.Cookie _i_server_ck; 45 46 public 48 ClientImpl() 49 { 50 _frame = null; 52 _product_list = null; 53 _caddie_list = null; 54 _server = null; 55 _i_server_ck = null; 56 } 57 58 62 final public void 63 configuration_complete() 64 throws org.omg.Components.InvalidConfiguration 65 { 66 javax.swing.JPanel npanel = new javax.swing.JPanel (new java.awt.BorderLayout ()); 69 javax.swing.JButton enterb = new javax.swing.JButton ("Enter"); 70 enterb.setActionCommand("enter"); 71 enterb.addActionListener(this); 72 javax.swing.JButton leaveb = new javax.swing.JButton ("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 javax.swing.JPanel bnpanel = new javax.swing.JPanel (new java.awt.BorderLayout ()); 81 _product_list = new javax.swing.JList (); 82 javax.swing.JButton purchaseb = new javax.swing.JButton ("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 javax.swing.JPanel bspanel = new javax.swing.JPanel (new java.awt.BorderLayout ()); 90 _caddie_list = new javax.swing.JList (); 91 javax.swing.JButton removeb = new javax.swing.JButton ("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 spanel = new javax.swing.JPanel (new java.awt.BorderLayout ()); 98 spanel.add(bnpanel, java.awt.BorderLayout.EAST); 99 spanel.add(bspanel, java.awt.BorderLayout.WEST); 100 101 javax.swing.JPanel panel = new javax.swing.JPanel (new java.awt.BorderLayout ()); 103 panel.add(npanel, java.awt.BorderLayout.NORTH); 104 panel.add(spanel, java.awt.BorderLayout.SOUTH); 105 _frame = new javax.swing.JFrame ("Client GUI"); 106 _frame.getContentPane().add(panel); 107 _frame.pack(); 108 _frame.show(); 109 } 110 111 115 119 final public void 120 actionPerformed(java.awt.event.ActionEvent e) 121 { 122 String cmd = e.getActionCommand(); 123 124 if (cmd.equals("enter")) { 125 try { 127 org.omg.Components.LocalReceptacles lrecs = getCCM2Context().get_receptacles(); 128 org.omg.CORBA.Object obj = lrecs.get_connections("server_home")[0].ref(); 129 ServerHome shome = ServerHomeHelper.narrow(obj); 130 133 _server = shome.create(); 134 _server.configuration_complete(); 135 136 _i_server_ck = lrecs.connect("i_server", _server.provide_i_server()); 137 140 obj = lrecs.get_connections("i_server")[0].ref(); 142 IServer iserver = IServerHelper.narrow(obj); 143 Product[] products = iserver.list_products(); 144 147 String [] pids = new String [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 ex) { 155 ex.printStackTrace(); 156 return; 157 } 158 } 159 160 if (cmd.equals("leave")) { 161 try { 163 org.omg.Components.LocalReceptacles lrecs = getCCM2Context().get_receptacles(); 164 javax.swing.ListModel lmodel = _caddie_list.getModel(); 165 String [] ids = new String [lmodel.getSize()]; 166 for (int i=0;i<ids.length;i++) { 167 ids[i] = (String )lmodel.getElementAt(i); 168 } 169 170 org.omg.CORBA.Object obj = lrecs.get_connections("i_server")[0].ref(); 171 IServer iserver = IServerHelper.narrow(obj); 172 iserver.remove_products(ids); 175 176 lrecs.disconnect(_i_server_ck); 178 181 _product_list.setListData(new Object [0]); 183 _caddie_list.setListData(new Object [0]); 184 185 obj = lrecs.get_connections("server_home")[0].ref(); 187 ServerHome shome = ServerHomeHelper.narrow(obj); 188 shome.remove_component(_server); 189 return ; 190 } catch (Exception ex) { 191 ex.printStackTrace(); 192 return ; 193 } 194 } 195 196 if (cmd.equals("purchase")) { 197 try { 199 org.omg.Components.LocalReceptacles lrecs = getCCM2Context().get_receptacles(); 200 Object [] objs = _product_list.getSelectedValues(); 201 String [] ids = new String [objs.length]; 202 for (int i=0;i<objs.length;i++) { 203 ids[i] = (String )objs[i]; 204 } 205 206 org.omg.CORBA.Object obj = lrecs.get_connections("i_server")[0].ref(); 208 IServer iserver = IServerHelper.narrow(obj); 209 iserver.purchase_products(ids); 212 213 Product[] products = iserver.view_caddie(); 215 String [] pids = new String [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 ex) { 224 ex.printStackTrace(); 225 return ; 226 } 227 } 228 229 if (cmd.equals("remove")) { 230 try { 232 org.omg.Components.LocalReceptacles lrecs = getCCM2Context().get_receptacles(); 233 Object [] objs = _caddie_list.getSelectedValues(); 234 String [] ids = new String [objs.length]; 235 for (int i=0;i<objs.length;i++) { 236 ids[i] = (String )objs[i]; 237 } 238 239 org.omg.CORBA.Object obj = lrecs.get_connections("i_server")[0].ref(); 241 IServer iserver = IServerHelper.narrow(obj); 242 iserver.remove_products(ids); 245 246 Product[] products = iserver.view_caddie(); 248 String [] pids = new String [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 ex) { 257 ex.printStackTrace(); 258 return ; 259 } 260 } 261 } 262 } 263 | Popular Tags |