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 <a href="http://mina.apache.org">Apache MINA Project</a>
    52   */
    53  public class GraphicalCharGenClient extends JFrame implements ImageListener {
    54
    55      private static final long serialVersionUID = 1L;
    56
    57      public static final int PORT = 33789;
    58      public static final String HOST = "localhost";
    59
    60      public GraphicalCharGenClient() {
    61          initComponents();
    62          jSpinnerHeight.setModel(spinnerHeightModel);
    63          jSpinnerWidth.setModel(spinnerWidthModel);
    64          jSpinnerChars.setModel(spinnerCharsModel);
    65          jTextFieldHost.setText(HOST);
    66          jTextFieldPort.setText(String.valueOf(PORT));
    67          setTitle("");
    68      }
    69
    70      private void jButtonConnectActionPerformed() {
    71          try {
    72              setTitle("connecting...");
    73              String host = jTextFieldHost.getText();
    74              int port = Integer.valueOf(jTextFieldPort.getText());
    75              if (imageClient != null) {
    76                  imageClient.disconnect();
    77              }
    78              imageClient = new ImageClient(host, port, this);
    79              imageClient.connect();
    80              jButtonConnect.setEnabled(!imageClient.isConnected());
    81          } catch (NumberFormatException e) {
    82              onException(e);
    83          } catch (IllegalArgumentException e) {
    84              onException(e);
    85          }
    86      }
    87
    88      private void jButtonDisconnectActionPerformed() {
    89          setTitle("disconnecting");
    90          imageClient.disconnect();
    91      }
    92
    93      private void jButtonSendRequestActionPerformed() {
    94          sendRequest();
    95      }
    96
    97      private void sendRequest() {
    98          int chars = spinnerCharsModel.getNumber().intValue();
    99          int height = spinnerHeightModel.getNumber().intValue();
    100         int width = spinnerWidthModel.getNumber().intValue();
    101         imageClient.sendRequest(new ImageRequest(width, height, chars));
    102     }
    103
    104     public void onImages(BufferedImage image1, BufferedImage image2) {
    105         if (checkBoxContinuous.isSelected()) {
    106             // already request next image
    107             sendRequest();
    108         }
    109         imagePanel1.setImages(image1, image2);
    110     }
    111
    112     public void onException(Throwable throwable) {
    113         Throwable cause = throwable;
    114         while (cause.getCause() != null) {
    115             cause = cause.getCause();
    116         }
    117         JOptionPane.showMessageDialog(
    118                 this,
    119                 cause.getMessage(),
    120                 throwable.getMessage(),
    121                 JOptionPane.ERROR_MESSAGE);
    122         setTitle("");
    123         jButtonConnect.setEnabled(!imageClient.isConnected());
    124         jButtonDisconnect.setEnabled(imageClient.isConnected());
    125     }
    126
    127     public void sessionOpened() {
    128         jButtonDisconnect.setEnabled(true);
    129         jButtonSendRequest.setEnabled(true);
    130         jButtonConnect.setEnabled(false);
    131         setTitle("connected");
    132     }
    133
    134     public void sessionClosed() {
    135         jButtonDisconnect.setEnabled(false);
    136         jButtonSendRequest.setEnabled(false);
    137         jButtonConnect.setEnabled(true);
    138         setTitle("not connected");
    139     }
    140
    141     @Override
    142     public void setTitle(String title) {
    143         super.setTitle("MINA - Chargen client - " + title);
    144     }
    145
    146
    147     private void initComponents() {
    148         JLabel jLabel1 = new JLabel();
    149         jTextFieldHost = new JTextField();
    150         jButtonConnect = new JButton();
    151         JLabel jLabel3 = new JLabel();
    152         jSpinnerWidth = new JSpinner();
    153         JLabel label5 = new JLabel();
    154         jSpinnerChars = new JSpinner();
    155         checkBoxContinuous = new JCheckBox();
    156         JLabel jLabel2 = new JLabel();
    157         jTextFieldPort = new JTextField();
    158         jButtonDisconnect = new JButton();
    159         JLabel jLabel4 = new JLabel();
    160         jSpinnerHeight = new JSpinner();
    161         jButtonSendRequest = new JButton();
    162         imagePanel1 = new ImagePanel();
    163
    164         //======== this ========
    165         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    166         setMinimumSize(new Dimension(700, 300));
    167         setPreferredSize(new Dimension(740, 600));
    168         Container contentPane = getContentPane();
    169         contentPane.setLayout(new GridBagLayout());
    170         ((GridBagLayout) contentPane.getLayout()).columnWidths = new int[]{36, 167, 99, 41, 66, 75, 57, 96, 0, 0};
    171         ((GridBagLayout) contentPane.getLayout()).rowHeights = new int[]{10, 31, 31, 256, 0};
    172         ((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};
    173         ((GridBagLayout) contentPane.getLayout()).rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, 1.0E-4};
    174
    175         //---- jLabel1 ----
    176         jLabel1.setText("Host");
    177         contentPane.add(jLabel1, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
    178                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    179                 new Insets(0, 5, 5, 5), 0, 0));
    180         contentPane.add(jTextFieldHost, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
    181                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    182                 new Insets(0, 5, 5, 10), 0, 0));
    183
    184         //---- jButtonConnect ----
    185         jButtonConnect.setText("Connect");
    186         jButtonConnect.addActionListener(new ActionListener() {
    187             public void actionPerformed(ActionEvent e) {
    188                 jButtonConnectActionPerformed();
    189             }
    190         });
    191         contentPane.add(jButtonConnect, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0,
    192                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    193                 new Insets(0, 5, 5, 10), 0, 0));
    194
    195         //---- jLabel3 ----
    196         jLabel3.setText("Width");
    197         contentPane.add(jLabel3, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0,
    198                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    199                 new Insets(0, 0, 5, 5), 0, 0));
    200         contentPane.add(jSpinnerWidth, new GridBagConstraints(4, 1, 1, 1, 0.0, 0.0,
    201                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    202                 new Insets(0, 5, 5, 10), 0, 0));
    203
    204         //---- label5 ----
    205         label5.setText("characters");
    206         contentPane.add(label5, new GridBagConstraints(5, 1, 1, 1, 0.0, 0.0,
    207                 GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
    208                 new Insets(0, 0, 5, 5), 0, 0));
    209         contentPane.add(jSpinnerChars, new GridBagConstraints(6, 1, 1, 1, 0.0, 0.0,
    210                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    211                 new Insets(0, 0, 5, 10), 0, 0));
    212
    213         //---- checkBoxContinuous ----
    214         checkBoxContinuous.setText("continuous");
    215         contentPane.add(checkBoxContinuous, new GridBagConstraints(7, 1, 1, 1, 0.0, 0.0,
    216                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    217                 new Insets(0, 5, 5, 10), 0, 0));
    218
    219         //---- jLabel2 ----
    220         jLabel2.setText("Port");
    221         contentPane.add(jLabel2, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
    222                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    223                 new Insets(0, 5, 5, 5), 0, 0));
    224         contentPane.add(jTextFieldPort, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
    225                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    226                 new Insets(0, 5, 5, 10), 0, 0));
    227
    228         //---- jButtonDisconnect ----
    229         jButtonDisconnect.setText("Disconnect");
    230         jButtonDisconnect.setEnabled(false);
    231         jButtonDisconnect.addActionListener(new ActionListener() {
    232             public void actionPerformed(ActionEvent e) {
    233                 jButtonDisconnectActionPerformed();
    234             }
    235         });
    236         contentPane.add(jButtonDisconnect, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0,
    237                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    238                 new Insets(0, 5, 5, 10), 0, 0));
    239
    240         //---- jLabel4 ----
    241         jLabel4.setText("Height");
    242         contentPane.add(jLabel4, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0,
    243                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    244                 new Insets(0, 0, 5, 5), 0, 0));
    245         contentPane.add(jSpinnerHeight, new GridBagConstraints(4, 2, 1, 1, 0.0, 0.0,
    246                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    247                 new Insets(0, 5, 5, 10), 0, 0));
    248
    249         //---- jButtonSendRequest ----
    250         jButtonSendRequest.setText("Send Request");
    251         jButtonSendRequest.setEnabled(false);
    252         jButtonSendRequest.addActionListener(new ActionListener() {
    253             public void actionPerformed(ActionEvent e) {
    254                 jButtonSendRequestActionPerformed();
    255             }
    256         });
    257         contentPane.add(jButtonSendRequest, new GridBagConstraints(5, 2, 2, 1, 0.0, 0.0,
    258                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    259                 new Insets(0, 5, 5, 10), 0, 0));
    260
    261         //======== imagePanel1 ========
    262         {
    263             imagePanel1.setBackground(new Color(51, 153, 255));
    264             imagePanel1.setPreferredSize(new Dimension(500, 500));
    265
    266             { // compute preferred size
    267                 Dimension preferredSize = new Dimension();
    268                 for (int i = 0; i < imagePanel1.getComponentCount(); i++) {
    269                     Rectangle bounds = imagePanel1.getComponent(i).getBounds();
    270                     preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
    271                     preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
    272                 }
    273                 Insets insets = imagePanel1.getInsets();
    274                 preferredSize.width += insets.right;
    275                 preferredSize.height += insets.bottom;
    276                 imagePanel1.setMinimumSize(preferredSize);
    277                 imagePanel1.setPreferredSize(preferredSize);
    278             }
    279         }
    280         contentPane.add(imagePanel1, new GridBagConstraints(0, 3, 9, 1, 0.0, 0.0,
    281                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    282                 new Insets(8, 5, 8, 5), 0, 0));
    283         pack();
    284         setLocationRelativeTo(getOwner());
    285     }
    286
    287     /**
    288      * @param args the command line arguments
    289      */
    290     public static void main(String args[]) {
    291         try {
    292             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    293         } catch (Exception e) {
    294             // ignore
    295         }
    296         java.awt.EventQueue.invokeLater(new Runnable() {
    297             public void run() {
    298                 new GraphicalCharGenClient().setVisible(true);
    299             }
    300         });
    301     }
    302
    303     private JTextField jTextFieldHost;
    304     private JButton jButtonConnect;
    305     private JSpinner jSpinnerWidth;
    306     private JSpinner jSpinnerChars;
    307     private JCheckBox checkBoxContinuous;
    308     private JTextField jTextFieldPort;
    309     private JButton jButtonDisconnect;
    310     private JSpinner jSpinnerHeight;
    311     private JButton jButtonSendRequest;
    312     private ImagePanel imagePanel1;
    313
    314     private SpinnerNumberModel spinnerHeightModel = new SpinnerNumberModel(100, 50, 600, 25);
    315     private SpinnerNumberModel spinnerWidthModel = new SpinnerNumberModel(200, 50, 1000, 25);
    316     private SpinnerNumberModel spinnerCharsModel = new SpinnerNumberModel(10, 1, 60, 1);
    317
    318     private ImageClient imageClient = new ImageClient(HOST, PORT, this);
    319 }