MazeGUIClient.java Sample

This topic inludes the source code for the MazeGUIClient.java Sample.

Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/SamplesApp/ProxyClient/mazegen/swing_client/

Sample Source Code


001 package mazegen.swing_client;
002 
003 import javax.swing.*;          
004 import java.awt.*;
005 import java.awt.event.*;
006 
007 import java.io.IOException;
008 import java.rmi.RemoteException;
009 
010 // proxy classes are located in the weblogic.jws.proxies package.
011 import weblogic.jws.proxies.*;
012 
013 /**
014  <p>This is an example of a web service client that uses the web wervice
015  * proxy generated by WebLogic Server.  This example is a simple Swing
016  * client that exercises the MazeGenerator web service and displays mazes
017  * returned from the web service.</p>
018  *
019  <p>See "MazeGenerator Sample" in the WebLogic Workshop documentation.</p>
020  *
021  <p>This client accesses the web service via proxy classes found in
022  * SamplesApp/APP-INF/lib/MazeGenerator.jar.  That JAR file is obtained by selecting
023  * the "Java Proxy" link on a web service's Test View Overview Page.  If
024  * the web service is served from the local machine, the URL to the Overview
025  * Page is:
026  * http://localhost:7001/samples/proxy/mazegen/MazeGanerator.jws</p>
027  */
028 public class MazeGUIClient {
029     private int rows = 11;
030     private int cols = 26;
031 
032     JTextArea textArea; 
033 
034     /*
035      * <p>Declare the proxy objects.  There is a main proxy object with the
036      * same name as the web service but with "_Impl" appended, then
037      * protocol-specific proxie classes that contain the actual method
038      * stubs for the web service's operations.</p>
039      *
040      * <p>This web service supports SOAP over HTTP, so it
041      * has a proxy with the name <web service>SOAP.</p>
042      */
043     MazeGenerator_Impl proxy;
044     MazeGeneratorSoap soapProxy;
045 
046     /**
047      * Utility function to create the Swing UI.
048      *
049      * Note that the anonymous ActionListener for generateButton
050      * invokes the proxy with the number of desired rows and columns
051      * and displays the response in textArea.
052      */
053     public Component createComponents() {
054         GridBagLayout gridbag = new GridBagLayout();
055         GridBagConstraints constraints = new GridBagConstraints();
056 
057         /**********************************************************
058          * create input panel
059          */
060         final JLabel rowLabel = new JLabel("Rows: ");
061         final JTextField rowTextField = new JTextField(10);
062         rowTextField.setColumns(4);
063         rowTextField.setText("11");
064         rowTextField.addActionListener(new ActionListener() {
065             public void actionPerformed(ActionEvent e) {
066                 String text = rowTextField.getText()
067                 try
068                 {
069                     rows = Integer.parseInt(text);
070                 }
071                 catch (NumberFormatException ex)
072                 {
073                     // if the value isn't valid, default to 11
074                     rows = 11;
075                     rowTextField.setText("11");
076                 
077             }
078         });
079         rowTextField.addFocusListener(new FocusListener() {
080             public void focusGained(FocusEvent e) {
081             }
082             public void focusLost(FocusEvent e) {
083                 String text = rowTextField.getText()
084                 try
085                 {
086                     rows = Integer.parseInt(text);
087                 }
088                 catch (NumberFormatException ex)
089                 {
090                     // if the value isn't valid, default to 11
091                     rows = 11;
092                     rowTextField.setText("11");
093                 
094             }
095         });
096         final JLabel colLabel = new JLabel("Columns: ");
097         final JTextField colTextField = new JTextField(10);
098         colTextField.setColumns(4);
099         colTextField.setText("26");
100         colTextField.addActionListener(new ActionListener() {
101             public void actionPerformed(ActionEvent e) {
102                 String text = colTextField.getText()
103                 try
104                 {
105                     cols = Integer.parseInt(text);
106                 }
107                 catch (NumberFormatException ex)
108                 {
109                     // if the value isn't valid, default to 26
110                     cols = 26;
111                     colTextField.setText("26");
112                 
113             }
114         });
115         colTextField.addFocusListener(new FocusListener() {
116             public void focusGained(FocusEvent e) {
117             }
118             public void focusLost(FocusEvent e) {
119                 String text = colTextField.getText()
120                 try
121                 {
122                     cols = Integer.parseInt(text);
123                 }
124                 catch (NumberFormatException ex)
125                 {
126                     // if the value isn't valid, default to 26
127                     cols = 26;
128                     colTextField.setText("26");
129                 
130             }
131         });
132 
133         JButton generateButton = new JButton("Generate Maze");
134         generateButton.setMnemonic(KeyEvent.VK_G);
135         generateButton.addActionListener(new ActionListener() {
136             public void actionPerformed(ActionEvent e) {
137                 String mazeText;
138                 try
139                 {
140                     /*
141                      * Invoke the web service via the proxy.  Call
142                      * the web service's printRandomMaze method with
143                      * the desired number of rows and columns.  Display
144                      * the String response in textArea.
145                      */
146                     mazeText = soapProxy.printRandomMaze(rows, cols);
147                     textArea.setText(mazeText);
148                 }
149                 catch (RemoteException ex)
150                 {
151                     System.out.println("Error invoking service via proxy");
152                     ex.printStackTrace();
153                 }
154             }
155         });
156 
157         constraints.fill = GridBagConstraints.NONE;
158         constraints.gridx = GridBagConstraints.RELATIVE;
159         constraints.gridy = 0;
160         gridbag.setConstraints(generateButton, constraints);
161 
162         JPanel inputPanel = new JPanel();
163         inputPanel.setBorder(BorderFactory.createEmptyBorder(
164                                         5//top
165                                         5//left
166                                         5//bottom
167                                         5//right
168                                         );
169         inputPanel.setLayout(new GridLayout(1,0,5,0));
170         inputPanel.add(rowLabel);
171         inputPanel.add(rowTextField);
172         inputPanel.add(colLabel);
173         inputPanel.add(colTextField);
174         inputPanel.add(generateButton);
175 
176         constraints.fill = GridBagConstraints.HORIZONTAL;
177         constraints.gridx = 0;
178         constraints.gridy = 0;
179         gridbag.setConstraints(inputPanel, constraints);
180 
181         /**********************************************************
182          * create output panel
183          */
184         JPanel outputPanel = new JPanel();
185 
186         textArea = new JTextArea(2580);
187         textArea.setFont(new Font("Courier", Font.PLAIN, 12));
188 
189         outputPanel.add(textArea);
190 
191         constraints.fill = GridBagConstraints.HORIZONTAL;
192         constraints.gridx = 0;
193         constraints.gridy = GridBagConstraints.RELATIVE;
194         gridbag.setConstraints(outputPanel, constraints);
195 
196         /**********************************************************
197          * create main panel
198          */
199         JPanel mainPanel = new JPanel();
200         mainPanel.setBorder(BorderFactory.createEmptyBorder(
201                                         10//top
202                                         10//left
203                                         10//bottom
204                                         10//right
205                                         );
206         mainPanel.setLayout(gridbag);
207         mainPanel.add(inputPanel);
208         mainPanel.add(outputPanel);
209 
210         return mainPanel;
211     }
212 
213     /**
214      * Utility function to instantiate the proxy, then get the
215      * SOAP-specific proxy from the main proxy.
216      */
217     private void CreateProxy()
218     {
219         try
220         {
221             /*
222              * Construct the proxy.
223              *
224              * Note that there is an alternative constructor in which you can pass the URL of
225              * the target service's WSDL (which is typically the URL of the service with ?WSDL
226              * appended).  That technique is used for "dynamic" proxies. The static WSDL is
227              * already built into the generated proxy and will be used if the no-argument
228              * proxy constructor is used (as below). Using the dynamic form causes an extra
229              * hit on the server for every web service request submitted.
230              */
231             proxy = new MazeGenerator_Impl();
232             soapProxy = proxy.getMazeGeneratorSoap();
233         }
234         catch (IOException ex)
235         {
236             System.out.println("Error creating proxy");
237         }
238     }
239 
240     public static void main(String[] args) {
241         try {
242             UIManager.setLookAndFeel(
243                 UIManager.getCrossPlatformLookAndFeelClassName());
244         catch (Exception e) {}
245 
246         // Create the top-level container
247         JFrame frame = new JFrame("MazeGUIClient");
248         MazeGUIClient app = new MazeGUIClient();
249 
250         // Create the proxy
251         app.CreateProxy();
252 
253         // Create the UI
254         Component contents = app.createComponents();
255         frame.getContentPane().add(contents, BorderLayout.CENTER);
256 
257         /*
258          * Make the UI visible, then implicitly loop forever on
259          * user events until the program is terminated.
260          */
261         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
262         frame.pack();
263         frame.setVisible(true);
264     }
265 }