1 12 package org.jacorb.trading.client.offers; 13 14 import java.awt.*; 15 import java.awt.event.*; 16 import java.io.*; 17 import org.omg.CORBA.ORB ; 18 import org.omg.CORBA.TypeCode ; 19 import org.omg.CosTrading.*; 20 import org.omg.CosTrading.AdminPackage.*; 21 import org.omg.CosTrading.RegisterPackage.*; 22 import org.omg.CosTrading.ProxyPackage.*; 23 import org.omg.CosTradingDynamic.*; 24 import org.jacorb.trading.client.util.*; 25 26 public class Offers 27 extends Frame 28 implements ActionListener, ItemListener, Runnable 29 { 30 private Choice m_view; 31 private List m_offers; 32 private TextArea m_description; 33 private Button m_refresh; 34 private Button m_withdraw; 35 private Button m_withdrawConstraint; 36 private Label m_status; 37 private ConstraintDialog m_constraintDialog; 38 private Admin m_admin; 39 private Register m_register; 40 private Proxy m_proxy; 41 42 private static ORB s_orb; 43 44 45 public Offers(Admin admin, Register reg, Proxy proxy) 46 { 47 super("Service Offers"); 48 49 setFont(new Font("Helvetica", Font.PLAIN, 12)); 50 51 m_admin = admin; 52 m_register = reg; 53 m_proxy = proxy; 54 55 createContents(); 56 57 refreshOffers(); 58 } 59 60 61 protected void createContents() 62 { 63 Panel panel = new Panel(); 64 panel.setLayout(new GridBagLayout()); 65 66 Panel selectPanel = new Panel(); 67 selectPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); 68 selectPanel.add(new Label("View", Label.LEFT)); 69 m_view = new Choice(); 70 m_view.add("Offers"); 71 m_view.add("Proxy Offers"); 72 m_view.addItemListener(this); 73 selectPanel.add(m_view); 74 75 Constrain.constrain(panel, selectPanel, 0, 0, 2, 1, 76 GridBagConstraints.HORIZONTAL, GridBagConstraints.NORTHWEST, 1.0, 0.0, 77 5, 10, 0, 5); 78 79 Panel offersPanel = new Panel(); 80 offersPanel.setLayout(new GridBagLayout()); 81 Constrain.constrain(offersPanel, new Label("Offers", Label.LEFT), 82 0, 0, 1, 1, GridBagConstraints.NONE, GridBagConstraints.NORTHWEST, 83 0.0, 0.0, 0, 0, 0, 0); 84 m_offers = new List(10, false); 85 m_offers.addItemListener(this); 86 Constrain.constrain(offersPanel, m_offers, 0, 1, 1, 1, 87 GridBagConstraints.BOTH, GridBagConstraints.NORTHWEST, 1.0, 1.0, 88 0, 0, 0, 0); 89 90 Constrain.constrain(panel, offersPanel, 0, 1, 1, 1, GridBagConstraints.BOTH, 91 GridBagConstraints.NORTHWEST, 0.25, 1.0, 5, 10, 5, 5); 92 93 Panel infoPanel = new Panel(); 94 infoPanel.setLayout(new GridBagLayout()); 95 Constrain.constrain(infoPanel, new Label("Offer information", Label.LEFT), 96 0, 0, 2, 1, GridBagConstraints.NONE, GridBagConstraints.NORTHWEST, 97 0.0, 0.0, 0, 0, 0, 0); 98 m_description = new TextArea(10, 40); 99 m_description.setEditable(false); 100 Constrain.constrain(infoPanel, m_description, 0, 1, 2, 1, 101 GridBagConstraints.BOTH, GridBagConstraints.NORTHWEST, 1.0, 1.0, 102 0, 0, 0, 0); 103 104 Constrain.constrain(panel, infoPanel, 1, 1, 1, 1, GridBagConstraints.BOTH, 105 GridBagConstraints.NORTHEAST, 0.75, 1.0, 5, 5, 5, 10); 106 107 Panel buttonPanel = new Panel(); 108 buttonPanel.setLayout(new FlowLayout()); 109 110 m_refresh = new Button("Refresh"); 111 m_refresh.setActionCommand("refresh"); 112 m_refresh.addActionListener(this); 113 buttonPanel.add(m_refresh); 114 115 m_withdraw = new Button("Withdraw..."); 116 m_withdraw.setActionCommand("withdraw"); 117 m_withdraw.addActionListener(this); 118 buttonPanel.add(m_withdraw); 119 120 m_withdrawConstraint = new Button("Withdraw using constraint..."); 121 m_withdrawConstraint.setActionCommand("withdrawConstraint"); 122 m_withdrawConstraint.addActionListener(this); 123 buttonPanel.add(m_withdrawConstraint); 124 125 Constrain.constrain(panel, buttonPanel, 0, 2, 2, 1, GridBagConstraints.NONE, 126 GridBagConstraints.NORTHWEST, 0.0, 0.0, 5, 5, 0, 10); 127 128 m_status = new Label("", Label.LEFT); 129 Constrain.constrain(panel, m_status, 0, 3, 2, 1, 130 GridBagConstraints.HORIZONTAL, GridBagConstraints.NORTHWEST, 1.0, 0.0, 131 0, 10, 3, 10); 132 133 add(panel); 134 } 135 136 137 public void itemStateChanged(ItemEvent e) 138 { 139 if (e.getItemSelectable() == m_view) 140 refreshOffers(); 141 else if (e.getItemSelectable() == m_offers) { 142 updateButtons(); 143 clearStatus(); 144 if (e.getStateChange() == ItemEvent.SELECTED) 145 describe(); 146 } 147 } 148 149 150 public void actionPerformed(ActionEvent e) 151 { 152 String cmd = e.getActionCommand(); 153 154 if (cmd.equals("refresh")) 155 refreshOffers(); 156 else if (cmd.equals("withdrawConstraint")) { 157 if (m_constraintDialog == null) { 158 m_constraintDialog = new ConstraintDialog(this, m_register); 159 m_constraintDialog.setActionCommand("refresh"); 160 m_constraintDialog.addActionListener(this); 161 } 162 163 clearStatus(); 164 m_constraintDialog.setVisible(true); 165 } 166 else if (cmd.equals("withdraw")) { 167 String id = m_offers.getSelectedItem(); 168 ConfirmDialog confirm = 169 new ConfirmDialog(this, "Remove offer '" + id + "'?"); 170 confirm.setActionCommand("confirm"); 171 confirm.addActionListener(this); 172 clearStatus(); 173 confirm.setVisible(true); 174 } 175 else if (cmd.equals("confirm")) 176 removeOffer(); 177 } 178 179 180 protected void removeOffer() 181 { 182 try { 183 String id = m_offers.getSelectedItem(); 184 if (m_view.getSelectedItem().equals("Offers")) 185 m_register.withdraw(id); 186 else 187 m_proxy.withdraw_proxy(id); 188 m_offers.remove(id); 189 m_description.setText(""); 190 clearStatus(); 191 updateButtons(); 192 } 193 catch (IllegalOfferId e) { 194 showStatus("Illegal offer ID '" + e.id + "'"); 195 } 196 catch (UnknownOfferId e) { 197 showStatus("Unknown offer ID '" + e.id + "'"); 198 } 199 catch (ProxyOfferId e) { 200 showStatus("Offer '" + e.id + "' is a proxy"); 201 } 202 catch (NotProxyOfferId e) { 203 showStatus("Offer '" + e.id + "' is not a proxy"); 204 } 205 } 206 207 208 protected void refreshOffers() 209 { 210 new Thread (this).start(); 211 } 212 213 214 public void run() 215 { 216 showStatus("Refreshing offers..."); 217 218 m_offers.removeAll(); 219 m_description.setText(""); 220 updateButtons(); 221 222 try { 223 OfferIdSeqHolder ids = new OfferIdSeqHolder(); 224 OfferIdIteratorHolder iter = new OfferIdIteratorHolder(); 225 226 if (m_view.getSelectedItem().equals("Offers")) 227 m_admin.list_offers(100, ids, iter); 228 else 229 m_admin.list_proxies(100, ids, iter); 230 231 int count = ids.value.length; 232 233 showStatus("Received " + count + " offers..."); 234 235 for (int i = 0; i < ids.value.length; i++) 236 m_offers.add(ids.value[i]); 237 238 if (iter.value != null) { 239 OfferIdSeqHolder seq = new OfferIdSeqHolder(); 240 boolean more; 241 do { 242 more = iter.value.next_n(100, seq); 243 count += seq.value.length; 244 showStatus("Received " + count + " offers..."); 245 for (int i = 0; i < seq.value.length; i++) 246 m_offers.add(seq.value[i]); 247 } 248 while (more); 249 250 iter.value.destroy(); 251 } 252 } 253 catch (NotImplemented e) { 254 showStatus("Admin::list_offers not implemented"); 255 } 256 } 257 258 259 protected void updateButtons() 260 { 261 if (m_offers.getSelectedIndex() < 0) 262 m_withdraw.setEnabled(false); 263 else 264 m_withdraw.setEnabled(true); 265 266 if (m_view.getSelectedItem().equals("Offers")) 267 m_withdrawConstraint.setEnabled(true); 268 else 269 m_withdrawConstraint.setEnabled(false); 270 } 271 272 273 protected void describe() 274 { 275 String id = m_offers.getSelectedItem(); 276 if (m_view.getSelectedItem().equals("Offers")) 277 describeOffer(id); 278 else 279 describeProxy(id); 280 } 281 282 283 protected void describeOffer(String id) 284 { 285 try { 286 OfferInfo info = m_register.describe(id); 287 288 StringWriter sw = new StringWriter(); 289 PrintWriter pw = new PrintWriter(sw); 290 291 pw.println("Type: " + info.type); 292 pw.println(); 293 294 describeProperties(pw, info.properties); 295 296 pw.println(); 297 pw.println("Reference:"); 298 String ref = s_orb.object_to_string(info.reference); 299 pw.println(" " + ref); 300 301 pw.flush(); 302 303 m_description.setText(sw.toString()); 304 } 305 catch (IllegalOfferId e) { 306 showStatus("Illegal offer ID '" + e.id + "'"); 307 } 308 catch (UnknownOfferId e) { 309 showStatus("Unknown offer ID '" + e.id + "'"); 310 } 311 catch (ProxyOfferId e) { 312 showStatus("Offer '" + e.id + "' is a proxy"); 313 } 314 } 315 316 317 protected void describeProxy(String id) 318 { 319 try { 320 ProxyInfo info = m_proxy.describe_proxy(id); 321 322 StringWriter sw = new StringWriter(); 323 PrintWriter pw = new PrintWriter(sw); 324 325 pw.println("Type: " + info.type); 326 pw.println(); 327 328 describeProperties(pw, info.properties); 329 330 pw.println(); 331 pw.println("Target:"); 332 String ref = s_orb.object_to_string(info.target); 333 pw.println(" " + ref); 334 335 pw.println(); 336 pw.println("If match all: " + info.if_match_all); 337 338 pw.println(); 339 pw.println("Recipe:"); 340 pw.println(" " + info.recipe); 341 342 pw.println(); 343 pw.println("Policies to pass on:"); 344 for (int p = 0; p < info.policies_to_pass_on.length; p++) { 345 pw.print(" " + info.policies_to_pass_on[p].name + " = "); 346 AnyUtil.print(s_orb,pw, info.policies_to_pass_on[p].value); 347 pw.println(); 348 } 349 350 pw.flush(); 351 352 m_description.setText(sw.toString()); 353 } 354 catch (IllegalOfferId e) { 355 showStatus("Illegal offer ID '" + e.id + "'"); 356 } 357 catch (UnknownOfferId e) { 358 showStatus("Unknown offer ID '" + e.id + "'"); 359 } 360 catch (NotProxyOfferId e) { 361 showStatus("Offer '" + e.id + "' is not a proxy"); 362 } 363 } 364 365 366 protected void describeProperties(PrintWriter pw, Property[] props) 367 { 368 pw.println("Properties:"); 369 370 for (int p = 0; p < props.length; p++) { 371 pw.print(" " + props[p].name + " = "); 372 373 TypeCode tc = props[p].value.type(); 374 if (tc.equal(DynamicPropHelper.type())) { 375 pw.println("<Dynamic>"); 376 DynamicProp dp = DynamicPropHelper.extract(props[p].value); 377 pw.print(" eval_if = "); 378 String ref = s_orb.object_to_string(dp.eval_if); 379 pw.println(ref); 380 pw.print(" returned_type = "); 381 AnyUtil.print(pw, dp.returned_type); 382 pw.println(); 383 pw.print(" extra_info = "); 384 AnyUtil.print(s_orb,pw, dp.extra_info); 385 pw.println(); 386 } 387 else { 388 AnyUtil.print(s_orb,pw, props[p].value); 389 pw.println(); 390 } 391 } 392 } 393 394 395 protected void showStatus(String message) 396 { 397 m_status.setText(message); 398 } 399 400 401 protected void clearStatus() 402 { 403 m_status.setText(""); 404 } 405 406 407 protected static void usage() 408 { 409 System.out.println("Usage: Offers iorfile"); 410 System.exit(1); 411 } 412 413 414 public static void main(String [] args) 415 { 416 s_orb = org.omg.CORBA.ORB.init(args,null); 417 418 Admin admin = null; 419 Register reg = null; 420 Proxy proxy = null; 421 422 try 423 { 424 org.omg.CORBA.Object obj = s_orb.resolve_initial_references("TradingService"); 425 426 if (obj == null) { 427 System.out.println("Invalid object"); 428 System.exit(1); 429 } 430 431 Lookup lookup = LookupHelper.narrow(obj); 432 admin = lookup.admin_if(); 433 reg = lookup.register_if(); 434 proxy = lookup.proxy_if(); 435 } 436 catch (Exception e) { 437 e.printStackTrace(); 438 System.exit(1); 439 } 440 441 Offers app = new Offers(admin, reg, proxy); 442 443 app.addWindowListener( 444 new WindowAdapter() 445 { 446 public void windowClosing(WindowEvent e) 447 { 448 e.getWindow().dispose(); 449 System.exit(0); 450 } 451 } 452 ); 453 454 app.pack(); 455 app.setVisible(true); 456 } 457 } 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | Popular Tags |