KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > swingclient > MainWindow


1 /* Copyright 2004 Sun Microsystems, Inc. All rights reserved. You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at:
2
3  http://developer.sun.com/berkeley_license.html
4
5  $Id: MainWindow.java,v 1.10 2005/08/16 23:29:24 inder Exp $ */

6
7
8
9 package com.sun.j2ee.blueprints.swingclient;
10
11 import javax.xml.parsers.*;
12
13 import javax.xml.transform.*;
14
15 import javax.xml.transform.dom.*;
16
17 import javax.xml.transform.stream.*;
18
19 import org.w3c.dom.*;
20
21 import java.io.*;
22
23 import java.util.*;
24
25
26
27
28
29 /**
30
31  * The main GUI window for the swing client
32
33  * @author Inderjeet Singh
34
35  */

36
37 public class MainWindow extends javax.swing.JFrame JavaDoc {
38
39     
40
41     /** Creates new form MainWindow */
42
43     public MainWindow() {
44
45         initComponents();
46
47         initServiceURL();
48
49         loadFileInTextPane(fileTextField.getText());
50
51     }
52
53     
54
55     private void initServiceURL() {
56
57         // Check if the service URL has been supplied through a System property
58

59         // Otherwise we will use a default URL running on localhost
60

61         String JavaDoc serviceurl = System.getProperty("stringwebservice.url");
62
63         if (serviceurl != null) {
64
65             serviceUrlTextField.setText(serviceurl);
66
67         }
68
69     }
70
71     /** This method is called from within the constructor to
72
73      * initialize the form.
74
75      * WARNING: Do NOT modify this code. The content of this method is
76
77      * always regenerated by the Form Editor.
78
79      */

80
81     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
82

83     private void initComponents() {
84
85         paramTypeButtonGroup = new javax.swing.ButtonGroup JavaDoc();
86
87         topPanel = new javax.swing.JPanel JavaDoc();
88
89         title1Label = new javax.swing.JLabel JavaDoc();
90
91         title2Label = new javax.swing.JLabel JavaDoc();
92
93         centerPanel = new javax.swing.JPanel JavaDoc();
94
95         jPanel3 = new javax.swing.JPanel JavaDoc();
96
97         fileLabel = new javax.swing.JLabel JavaDoc();
98
99         jPanel2 = new javax.swing.JPanel JavaDoc();
100
101         fileTextField = new javax.swing.JTextField JavaDoc();
102
103         loadFileButton = new javax.swing.JButton JavaDoc();
104
105         clearButton = new javax.swing.JButton JavaDoc();
106
107         descriptionLabel = new javax.swing.JLabel JavaDoc();
108
109         scrollPane = new javax.swing.JScrollPane JavaDoc();
110
111         textPane = new javax.swing.JTextPane JavaDoc();
112
113         bottomPanel = new javax.swing.JPanel JavaDoc();
114
115         paramTypePanel = new javax.swing.JPanel JavaDoc();
116
117         paramTypeLabel = new javax.swing.JLabel JavaDoc();
118
119         radioButtonPanel = new javax.swing.JPanel JavaDoc();
120
121         stubsRadioButton = new javax.swing.JRadioButton JavaDoc();
122
123         dynamicProxiesRadioButton = new javax.swing.JRadioButton JavaDoc();
124
125         diiRadioButton = new javax.swing.JRadioButton JavaDoc();
126
127         servicePanel = new javax.swing.JPanel JavaDoc();
128
129         sendPOButton = new javax.swing.JButton JavaDoc();
130
131         serviceUrlTextField = new javax.swing.JTextField JavaDoc();
132
133         serviceUrlLabel = new javax.swing.JLabel JavaDoc();
134
135
136
137         getContentPane().setLayout(new java.awt.BorderLayout JavaDoc(10, 10));
138
139
140
141         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
142
143         setTitle("Swing Client for a J2EE Web Service");
144
145         setName("Swing Client for J2EE Web Service");
146
147         topPanel.setLayout(new java.awt.BorderLayout JavaDoc(10, 10));
148
149
150
151         topPanel.setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(5, 5, 5, 5)));
152
153         topPanel.setPreferredSize(new java.awt.Dimension JavaDoc(33, 50));
154
155         title1Label.setFont(new java.awt.Font JavaDoc("MS Sans Serif", 1, 16));
156
157         title1Label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
158
159         title1Label.setText("Swing Client for the Purchase Order Web");
160
161         title1Label.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
162
163         topPanel.add(title1Label, java.awt.BorderLayout.NORTH);
164
165
166
167         title2Label.setFont(new java.awt.Font JavaDoc("MS Sans Serif", 1, 16));
168
169         title2Label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
170
171         title2Label.setText("Service in Java BluePrints Solutions Catalog");
172
173         title2Label.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
174
175         title2Label.setMinimumSize(new java.awt.Dimension JavaDoc(33, 21));
176
177         topPanel.add(title2Label, java.awt.BorderLayout.SOUTH);
178
179
180
181         getContentPane().add(topPanel, java.awt.BorderLayout.NORTH);
182
183
184
185         centerPanel.setLayout(new java.awt.BorderLayout JavaDoc(10, 10));
186
187
188
189         centerPanel.setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(5, 5, 5, 5)));
190
191         jPanel3.setLayout(new java.awt.BorderLayout JavaDoc(10, 0));
192
193
194
195         fileLabel.setLabelFor(fileTextField);
196
197         fileLabel.setText("Load Purchase Order XML File From: ");
198
199         jPanel3.add(fileLabel, java.awt.BorderLayout.CENTER);
200
201
202
203         jPanel2.setLayout(new java.awt.FlowLayout JavaDoc(java.awt.FlowLayout.CENTER, 10, 5));
204
205
206
207         fileTextField.setText("/resources/purchaseorder.xml");
208
209         fileTextField.setMinimumSize(new java.awt.Dimension JavaDoc(50, 21));
210
211         fileTextField.setPreferredSize(new java.awt.Dimension JavaDoc(200, 21));
212
213         jPanel2.add(fileTextField);
214
215
216
217         loadFileButton.setText("Load");
218
219         loadFileButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
220
221             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
222
223                 loadFileButtonActionPerformed(evt);
224
225             }
226
227         });
228
229
230
231         jPanel2.add(loadFileButton);
232
233
234
235         clearButton.setText("Clear");
236
237         clearButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
238
239             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
240
241                 clearButtonActionPerformed(evt);
242
243             }
244
245         });
246
247
248
249         jPanel2.add(clearButton);
250
251
252
253         jPanel3.add(jPanel2, java.awt.BorderLayout.SOUTH);
254
255
256
257         descriptionLabel.setText("<html>This Swing application illustrates three communication modes to access a Web service <br>from a stand-alone client: Stubs, Dynamic Proxy and Dynaic Invocation Interface (DII). ");
258
259         descriptionLabel.setAutoscrolls(true);
260
261         descriptionLabel.setMaximumSize(new java.awt.Dimension JavaDoc(200, 42));
262
263         descriptionLabel.setMinimumSize(new java.awt.Dimension JavaDoc(200, 42));
264
265         descriptionLabel.setPreferredSize(new java.awt.Dimension JavaDoc(200, 42));
266
267         jPanel3.add(descriptionLabel, java.awt.BorderLayout.NORTH);
268
269         descriptionLabel.getAccessibleContext().setAccessibleName("<html>This Swing application illustrates three communication modes to access a Web service <br>from a stand-alone client: Stubs, Dynamic Proxy and Dynaic Invocation Interface (DII).");
270
271
272
273         centerPanel.add(jPanel3, java.awt.BorderLayout.NORTH);
274
275
276
277         scrollPane.setAutoscrolls(true);
278
279         textPane.setMinimumSize(new java.awt.Dimension JavaDoc(200, 175));
280
281         textPane.setPreferredSize(new java.awt.Dimension JavaDoc(200, 175));
282
283         scrollPane.setViewportView(textPane);
284
285
286
287         centerPanel.add(scrollPane, java.awt.BorderLayout.CENTER);
288
289
290
291         getContentPane().add(centerPanel, java.awt.BorderLayout.CENTER);
292
293
294
295         bottomPanel.setLayout(new java.awt.BorderLayout JavaDoc(10, 5));
296
297
298
299         bottomPanel.setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(5, 5, 5, 5)));
300
301         paramTypePanel.setLayout(new java.awt.BorderLayout JavaDoc());
302
303
304
305         paramTypeLabel.setText("Invoke StringPurchaseOrder Web Service Using the Communication Mode:");
306
307         paramTypePanel.add(paramTypeLabel, java.awt.BorderLayout.NORTH);
308
309
310
311         radioButtonPanel.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
312
313         paramTypeButtonGroup.add(stubsRadioButton);
314
315         stubsRadioButton.setSelected(true);
316
317         stubsRadioButton.setText("Stubs");
318
319         radioButtonPanel.add(stubsRadioButton);
320
321
322
323         paramTypeButtonGroup.add(dynamicProxiesRadioButton);
324
325         dynamicProxiesRadioButton.setText("Dynamic Proxy");
326
327         radioButtonPanel.add(dynamicProxiesRadioButton);
328
329
330
331         paramTypeButtonGroup.add(diiRadioButton);
332
333         diiRadioButton.setText("Dynamic Invocation Interface (DII)");
334
335         radioButtonPanel.add(diiRadioButton);
336
337
338
339         paramTypePanel.add(radioButtonPanel, java.awt.BorderLayout.SOUTH);
340
341
342
343         bottomPanel.add(paramTypePanel, java.awt.BorderLayout.NORTH);
344
345
346
347         servicePanel.setLayout(new java.awt.BorderLayout JavaDoc(10, 5));
348
349
350
351         sendPOButton.setText("Send PO");
352
353         sendPOButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
354
355             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
356
357                 sendPOButtonActionPerformed(evt);
358
359             }
360
361         });
362
363
364
365         servicePanel.add(sendPOButton, java.awt.BorderLayout.EAST);
366
367
368
369         serviceUrlTextField.setText("http://localhost:8080/webservice/StringPurchaseOrderService");
370
371         servicePanel.add(serviceUrlTextField, java.awt.BorderLayout.CENTER);
372
373
374
375         serviceUrlLabel.setText("Web Service URL:");
376
377         servicePanel.add(serviceUrlLabel, java.awt.BorderLayout.WEST);
378
379
380
381         bottomPanel.add(servicePanel, java.awt.BorderLayout.SOUTH);
382
383
384
385         getContentPane().add(bottomPanel, java.awt.BorderLayout.SOUTH);
386
387
388
389         pack();
390
391     }
392
393     // </editor-fold>//GEN-END:initComponents
394

395     
396
397     private void loadFileButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_loadFileButtonActionPerformed
398

399         loadFileInTextPane(fileTextField.getText());
400
401     }//GEN-LAST:event_loadFileButtonActionPerformed
402

403     
404
405     private void loadFileInTextPane(String JavaDoc fileName) {
406
407         try {
408
409             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
410
411             DocumentBuilder builder = factory.newDocumentBuilder();
412
413             InputStream is = getClass().getResourceAsStream(fileName);
414
415             Document doc = builder.parse(is);
416
417             Source source = new DOMSource(doc);
418
419             Transformer transformer = TransformerFactory.newInstance().newTransformer();
420
421             StringWriter sw = new StringWriter();
422
423             Result result = new StreamResult(sw);
424
425             transformer.transform(source, result);
426
427             textPane.setText(sw.toString());
428
429         } catch (Exception JavaDoc e) {
430
431             e.printStackTrace();
432
433         }
434
435     }
436
437     
438
439     private void clearButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_clearButtonActionPerformed
440

441         fileTextField.setText("");
442
443         textPane.setText("");
444
445     }//GEN-LAST:event_clearButtonActionPerformed
446

447     
448
449     private void sendPOButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_sendPOButtonActionPerformed
450

451         String JavaDoc result = "";
452
453         POServiceBD bd = new POServiceBD(serviceUrlTextField.getText());
454
455         String JavaDoc xmlDocStr = textPane.getText();
456
457         String JavaDoc responseMsg = "";
458
459         try {
460
461             if (stubsRadioButton.isSelected()) {
462
463                 result = bd.submitPOUsingStubs(xmlDocStr);
464
465             } else if (dynamicProxiesRadioButton.isSelected()) {
466
467                 result = bd.submitPOUsingDynamicProxy(xmlDocStr);
468
469             } else if (diiRadioButton.isSelected()) {
470
471                 result = bd.submitPOUsingDII(xmlDocStr);
472
473             }
474
475             responseMsg = "The Web service returned: " + result;
476
477         } catch (InvalidPOException e) {
478
479             responseMsg = "The submitted purchase order is invalid.\n\n The error reported by the server is\n" + convert(e);
480
481         } catch (RuntimeException JavaDoc e) {
482
483             responseMsg = "The StringPurchaseOrder Web service is not available.\n\n The error reported by the server is\n" + convert(e);
484
485         }
486
487         textPane.setText(responseMsg);
488
489     }//GEN-LAST:event_sendPOButtonActionPerformed
490

491     
492
493     private String JavaDoc convert(Exception JavaDoc e) {
494
495         ByteArrayOutputStream stackTrace = new ByteArrayOutputStream();
496
497         PrintStream ps = new PrintStream(stackTrace);
498
499         ps.println("Received Exception: " + e.getMessage());
500
501         e.printStackTrace(new PrintStream(stackTrace));
502
503         return stackTrace.toString();
504
505     }
506
507     /**
508
509      * @param args the command line arguments
510
511      */

512
513     public static void main(String JavaDoc args[]) {
514
515         java.awt.EventQueue.invokeLater(new Runnable JavaDoc() {
516
517             public void run() {
518
519                 MainWindow window = new MainWindow();
520
521                 window.setVisible(true);
522
523             }
524
525         });
526
527     }
528
529     
530
531     // Variables declaration - do not modify//GEN-BEGIN:variables
532

533     private javax.swing.JPanel JavaDoc bottomPanel;
534
535     private javax.swing.JPanel JavaDoc centerPanel;
536
537     private javax.swing.JButton JavaDoc clearButton;
538
539     private javax.swing.JLabel JavaDoc descriptionLabel;
540
541     private javax.swing.JRadioButton JavaDoc diiRadioButton;
542
543     private javax.swing.JRadioButton JavaDoc dynamicProxiesRadioButton;
544
545     private javax.swing.JLabel JavaDoc fileLabel;
546
547     private javax.swing.JTextField JavaDoc fileTextField;
548
549     private javax.swing.JPanel JavaDoc jPanel2;
550
551     private javax.swing.JPanel JavaDoc jPanel3;
552
553     private javax.swing.JButton JavaDoc loadFileButton;
554
555     private javax.swing.ButtonGroup JavaDoc paramTypeButtonGroup;
556
557     private javax.swing.JLabel JavaDoc paramTypeLabel;
558
559     private javax.swing.JPanel JavaDoc paramTypePanel;
560
561     private javax.swing.JPanel JavaDoc radioButtonPanel;
562
563     private javax.swing.JScrollPane JavaDoc scrollPane;
564
565     private javax.swing.JButton JavaDoc sendPOButton;
566
567     private javax.swing.JPanel JavaDoc servicePanel;
568
569     private javax.swing.JLabel JavaDoc serviceUrlLabel;
570
571     private javax.swing.JTextField JavaDoc serviceUrlTextField;
572
573     private javax.swing.JRadioButton JavaDoc stubsRadioButton;
574
575     private javax.swing.JTextPane JavaDoc textPane;
576
577     private javax.swing.JLabel JavaDoc title1Label;
578
579     private javax.swing.JLabel JavaDoc title2Label;
580
581     private javax.swing.JPanel JavaDoc topPanel;
582
583     // End of variables declaration//GEN-END:variables
584

585     
586
587 }
588
589
Popular Tags