001/*
002 *   Licensed to the Apache Software Foundation (ASF) under one
003 *   or more contributor license agreements.  See the NOTICE file
004 *   distributed with this work for additional information
005 *   regarding copyright ownership.  The ASF licenses this file
006 *   to you under the Apache License, Version 2.0 (the
007 *   "License"); you may not use this file except in compliance
008 *   with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *   Unless required by applicable law or agreed to in writing,
013 *   software distributed under the License is distributed on an
014 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *   KIND, either express or implied.  See the License for the
016 *   specific language governing permissions and limitations
017 *   under the License.
018 *
019 */
020
021package org.apache.mina.example.imagine.step1.client;
022
023import java.awt.Color;
024import java.awt.Container;
025import java.awt.Dimension;
026import java.awt.GridBagConstraints;
027import java.awt.GridBagLayout;
028import java.awt.Insets;
029import java.awt.Rectangle;
030import java.awt.event.ActionEvent;
031import java.awt.event.ActionListener;
032import java.awt.image.BufferedImage;
033
034import javax.swing.JButton;
035import javax.swing.JCheckBox;
036import javax.swing.JFrame;
037import javax.swing.JLabel;
038import javax.swing.JOptionPane;
039import javax.swing.JSpinner;
040import javax.swing.JTextField;
041import javax.swing.SpinnerNumberModel;
042import javax.swing.UIManager;
043import javax.swing.WindowConstants;
044
045import org.apache.mina.example.imagine.step1.ImageRequest;
046import org.apache.mina.example.imagine.step1.server.ImageServer;
047
048/**
049 * Swing application that acts as a client of the {@link ImageServer}
050 *
051 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
052 */
053public class GraphicalCharGenClient extends JFrame implements ImageListener {
054
055    private static final long serialVersionUID = 1L;
056
057    public static final int PORT = 33789;
058    public static final String HOST = "localhost";
059
060    public GraphicalCharGenClient() {
061        initComponents();
062        jSpinnerHeight.setModel(spinnerHeightModel);
063        jSpinnerWidth.setModel(spinnerWidthModel);
064        jSpinnerChars.setModel(spinnerCharsModel);
065        jTextFieldHost.setText(HOST);
066        jTextFieldPort.setText(String.valueOf(PORT));
067        setTitle("");
068    }
069
070    private void jButtonConnectActionPerformed() {
071        try {
072            setTitle("connecting...");
073            String host = jTextFieldHost.getText();
074            int port = Integer.valueOf(jTextFieldPort.getText());
075            if (imageClient != null) {
076                imageClient.disconnect();
077            }
078            imageClient = new ImageClient(host, port, this);
079            imageClient.connect();
080            jButtonConnect.setEnabled(!imageClient.isConnected());
081        } catch (NumberFormatException e) {
082            onException(e);
083        } catch (IllegalArgumentException e) {
084            onException(e);
085        }
086    }
087
088    private void jButtonDisconnectActionPerformed() {
089        setTitle("disconnecting");
090        imageClient.disconnect();
091    }
092
093    private void jButtonSendRequestActionPerformed() {
094        sendRequest();
095    }
096
097    private void sendRequest() {
098        int chars = spinnerCharsModel.getNumber().intValue();
099        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}