View Javadoc

1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  
21  package org.apache.mina.example.imagine.step1.client;
22  
23  import java.awt.Color;
24  import java.awt.Container;
25  import java.awt.Dimension;
26  import java.awt.GridBagConstraints;
27  import java.awt.GridBagLayout;
28  import java.awt.Insets;
29  import java.awt.Rectangle;
30  import java.awt.event.ActionEvent;
31  import java.awt.event.ActionListener;
32  import java.awt.image.BufferedImage;
33  
34  import javax.swing.JButton;
35  import javax.swing.JCheckBox;
36  import javax.swing.JFrame;
37  import javax.swing.JLabel;
38  import javax.swing.JOptionPane;
39  import javax.swing.JSpinner;
40  import javax.swing.JTextField;
41  import javax.swing.SpinnerNumberModel;
42  import javax.swing.UIManager;
43  import javax.swing.WindowConstants;
44  
45  import org.apache.mina.example.imagine.step1.ImageRequest;
46  import org.apache.mina.example.imagine.step1.server.ImageServer;
47  
48  /**
49   * Swing application that acts as a client of the {@link ImageServer}
50   *
51   * @author The Apache MINA Project (dev@mina.apache.org)
52   * @version $Rev: 662903 $, $Date: 2008-06-04 00:09:53 +0200 (mer, 04 jun 2008) $
53   */
54  public class GraphicalCharGenClient extends JFrame implements ImageListener {
55  
56      private static final long serialVersionUID = 1L;
57  
58      public static final int PORT = 33789;
59      public static final String HOST = "localhost";
60  
61      public GraphicalCharGenClient() {
62          initComponents();
63          jSpinnerHeight.setModel(spinnerHeightModel);
64          jSpinnerWidth.setModel(spinnerWidthModel);
65          jSpinnerChars.setModel(spinnerCharsModel);
66          jTextFieldHost.setText(HOST);
67          jTextFieldPort.setText(String.valueOf(PORT));
68          setTitle("");
69      }
70  
71      private void jButtonConnectActionPerformed() {
72          try {
73              setTitle("connecting...");
74              String host = jTextFieldHost.getText();
75              int port = Integer.valueOf(jTextFieldPort.getText());
76              if (imageClient != null) {
77                  imageClient.disconnect();
78              }
79              imageClient = new ImageClient(host, port, this);
80              imageClient.connect();
81              jButtonConnect.setEnabled(!imageClient.isConnected());
82          } catch (NumberFormatException e) {
83              onException(e);
84          } catch (IllegalArgumentException e) {
85              onException(e);
86          }
87      }
88  
89      private void jButtonDisconnectActionPerformed() {
90          setTitle("disconnecting");
91          imageClient.disconnect();
92      }
93  
94      private void jButtonSendRequestActionPerformed() {
95          sendRequest();
96      }
97  
98      private void sendRequest() {
99          int chars = spinnerCharsModel.getNumber().intValue();
100         int height = spinnerHeightModel.getNumber().intValue();
101         int width = spinnerWidthModel.getNumber().intValue();
102         imageClient.sendRequest(new ImageRequest(width, height, chars));
103     }
104 
105     public void onImages(BufferedImage image1, BufferedImage image2) {
106         if (checkBoxContinuous.isSelected()) {
107             // already request next image
108             sendRequest();
109         }
110         imagePanel1.setImages(image1, image2);
111     }
112 
113     public void onException(Throwable throwable) {
114         Throwable cause = throwable;
115         while (cause.getCause() != null) {
116             cause = cause.getCause();
117         }
118         JOptionPane.showMessageDialog(
119                 this,
120                 cause.getMessage(),
121                 throwable.getMessage(),
122                 JOptionPane.ERROR_MESSAGE);
123         setTitle("");
124         jButtonConnect.setEnabled(!imageClient.isConnected());
125         jButtonDisconnect.setEnabled(imageClient.isConnected());
126     }
127 
128     public void sessionOpened() {
129         jButtonDisconnect.setEnabled(true);
130         jButtonSendRequest.setEnabled(true);
131         jButtonConnect.setEnabled(false);
132         setTitle("connected");
133     }
134 
135     public void sessionClosed() {
136         jButtonDisconnect.setEnabled(false);
137         jButtonSendRequest.setEnabled(false);
138         jButtonConnect.setEnabled(true);
139         setTitle("not connected");
140     }
141 
142     @Override
143     public void setTitle(String title) {
144         super.setTitle("MINA - Chargen client - " + title);
145     }
146 
147 
148     private void initComponents() {
149         JLabel jLabel1 = new JLabel();
150         jTextFieldHost = new JTextField();
151         jButtonConnect = new JButton();
152         JLabel jLabel3 = new JLabel();
153         jSpinnerWidth = new JSpinner();
154         JLabel label5 = new JLabel();
155         jSpinnerChars = new JSpinner();
156         checkBoxContinuous = new JCheckBox();
157         JLabel jLabel2 = new JLabel();
158         jTextFieldPort = new JTextField();
159         jButtonDisconnect = new JButton();
160         JLabel jLabel4 = new JLabel();
161         jSpinnerHeight = new JSpinner();
162         jButtonSendRequest = new JButton();
163         imagePanel1 = new ImagePanel();
164 
165         //======== this ========
166         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
167         setMinimumSize(new Dimension(700, 300));
168         setPreferredSize(new Dimension(740, 600));
169         Container contentPane = getContentPane();
170         contentPane.setLayout(new GridBagLayout());
171         ((GridBagLayout) contentPane.getLayout()).columnWidths = new int[]{36, 167, 99, 41, 66, 75, 57, 96, 0, 0};
172         ((GridBagLayout) contentPane.getLayout()).rowHeights = new int[]{10, 31, 31, 256, 0};
173         ((GridBagLayout) contentPane.getLayout()).columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0E-4};
174         ((GridBagLayout) contentPane.getLayout()).rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, 1.0E-4};
175 
176         //---- jLabel1 ----
177         jLabel1.setText("Host");
178         contentPane.add(jLabel1, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
179                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
180                 new Insets(0, 5, 5, 5), 0, 0));
181         contentPane.add(jTextFieldHost, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
182                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
183                 new Insets(0, 5, 5, 10), 0, 0));
184 
185         //---- jButtonConnect ----
186         jButtonConnect.setText("Connect");
187         jButtonConnect.addActionListener(new ActionListener() {
188             public void actionPerformed(ActionEvent e) {
189                 jButtonConnectActionPerformed();
190             }
191         });
192         contentPane.add(jButtonConnect, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0,
193                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
194                 new Insets(0, 5, 5, 10), 0, 0));
195 
196         //---- jLabel3 ----
197         jLabel3.setText("Width");
198         contentPane.add(jLabel3, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0,
199                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
200                 new Insets(0, 0, 5, 5), 0, 0));
201         contentPane.add(jSpinnerWidth, new GridBagConstraints(4, 1, 1, 1, 0.0, 0.0,
202                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
203                 new Insets(0, 5, 5, 10), 0, 0));
204 
205         //---- label5 ----
206         label5.setText("characters");
207         contentPane.add(label5, new GridBagConstraints(5, 1, 1, 1, 0.0, 0.0,
208                 GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
209                 new Insets(0, 0, 5, 5), 0, 0));
210         contentPane.add(jSpinnerChars, new GridBagConstraints(6, 1, 1, 1, 0.0, 0.0,
211                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
212                 new Insets(0, 0, 5, 10), 0, 0));
213 
214         //---- checkBoxContinuous ----
215         checkBoxContinuous.setText("continuous");
216         contentPane.add(checkBoxContinuous, new GridBagConstraints(7, 1, 1, 1, 0.0, 0.0,
217                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
218                 new Insets(0, 5, 5, 10), 0, 0));
219 
220         //---- jLabel2 ----
221         jLabel2.setText("Port");
222         contentPane.add(jLabel2, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
223                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
224                 new Insets(0, 5, 5, 5), 0, 0));
225         contentPane.add(jTextFieldPort, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
226                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
227                 new Insets(0, 5, 5, 10), 0, 0));
228 
229         //---- jButtonDisconnect ----
230         jButtonDisconnect.setText("Disconnect");
231         jButtonDisconnect.setEnabled(false);
232         jButtonDisconnect.addActionListener(new ActionListener() {
233             public void actionPerformed(ActionEvent e) {
234                 jButtonDisconnectActionPerformed();
235             }
236         });
237         contentPane.add(jButtonDisconnect, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0,
238                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
239                 new Insets(0, 5, 5, 10), 0, 0));
240 
241         //---- jLabel4 ----
242         jLabel4.setText("Height");
243         contentPane.add(jLabel4, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0,
244                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
245                 new Insets(0, 0, 5, 5), 0, 0));
246         contentPane.add(jSpinnerHeight, new GridBagConstraints(4, 2, 1, 1, 0.0, 0.0,
247                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
248                 new Insets(0, 5, 5, 10), 0, 0));
249 
250         //---- jButtonSendRequest ----
251         jButtonSendRequest.setText("Send Request");
252         jButtonSendRequest.setEnabled(false);
253         jButtonSendRequest.addActionListener(new ActionListener() {
254             public void actionPerformed(ActionEvent e) {
255                 jButtonSendRequestActionPerformed();
256             }
257         });
258         contentPane.add(jButtonSendRequest, new GridBagConstraints(5, 2, 2, 1, 0.0, 0.0,
259                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
260                 new Insets(0, 5, 5, 10), 0, 0));
261 
262         //======== imagePanel1 ========
263         {
264             imagePanel1.setBackground(new Color(51, 153, 255));
265             imagePanel1.setPreferredSize(new Dimension(500, 500));
266 
267             { // compute preferred size
268                 Dimension preferredSize = new Dimension();
269                 for (int i = 0; i < imagePanel1.getComponentCount(); i++) {
270                     Rectangle bounds = imagePanel1.getComponent(i).getBounds();
271                     preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
272                     preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
273                 }
274                 Insets insets = imagePanel1.getInsets();
275                 preferredSize.width += insets.right;
276                 preferredSize.height += insets.bottom;
277                 imagePanel1.setMinimumSize(preferredSize);
278                 imagePanel1.setPreferredSize(preferredSize);
279             }
280         }
281         contentPane.add(imagePanel1, new GridBagConstraints(0, 3, 9, 1, 0.0, 0.0,
282                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
283                 new Insets(8, 5, 8, 5), 0, 0));
284         pack();
285         setLocationRelativeTo(getOwner());
286     }
287 
288     /**
289      * @param args the command line arguments
290      */
291     public static void main(String args[]) {
292         try {
293             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
294         } catch (Exception e) {
295             // ignore
296         }
297         java.awt.EventQueue.invokeLater(new Runnable() {
298             public void run() {
299                 new GraphicalCharGenClient().setVisible(true);
300             }
301         });
302     }
303 
304     private JTextField jTextFieldHost;
305     private JButton jButtonConnect;
306     private JSpinner jSpinnerWidth;
307     private JSpinner jSpinnerChars;
308     private JCheckBox checkBoxContinuous;
309     private JTextField jTextFieldPort;
310     private JButton jButtonDisconnect;
311     private JSpinner jSpinnerHeight;
312     private JButton jButtonSendRequest;
313     private ImagePanel imagePanel1;
314 
315     private SpinnerNumberModel spinnerHeightModel = new SpinnerNumberModel(100, 50, 600, 25);
316     private SpinnerNumberModel spinnerWidthModel = new SpinnerNumberModel(200, 50, 1000, 25);
317     private SpinnerNumberModel spinnerCharsModel = new SpinnerNumberModel(10, 1, 60, 1);
318 
319     private ImageClient imageClient = new ImageClient(HOST, PORT, this);
320 }