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:
021: package org.apache.mina.example.imagine.step1.client;
022:
023: import java.awt.Color;
024: import java.awt.Container;
025: import java.awt.Dimension;
026: import java.awt.GridBagConstraints;
027: import java.awt.GridBagLayout;
028: import java.awt.Insets;
029: import java.awt.Rectangle;
030: import java.awt.event.ActionEvent;
031: import java.awt.event.ActionListener;
032: import java.awt.image.BufferedImage;
033:
034: import javax.swing.JButton;
035: import javax.swing.JCheckBox;
036: import javax.swing.JFrame;
037: import javax.swing.JLabel;
038: import javax.swing.JOptionPane;
039: import javax.swing.JSpinner;
040: import javax.swing.JTextField;
041: import javax.swing.SpinnerNumberModel;
042: import javax.swing.UIManager;
043: import javax.swing.WindowConstants;
044:
045: import org.apache.mina.example.imagine.step1.ImageRequest;
046: import 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 Apache MINA Project (dev@mina.apache.org)
052: * @version $Rev: 627048 $, $Date: 2008-02-12 12:27:34 -0700 (Tue, 12 Feb 2008) $
053: */
054: public class GraphicalCharGenClient extends JFrame implements
055: ImageListener {
056:
057: private static final long serialVersionUID = 1L;
058:
059: public static final int PORT = 33789;
060: public static final String HOST = "localhost";
061:
062: public GraphicalCharGenClient() {
063: initComponents();
064: jSpinnerHeight.setModel(spinnerHeightModel);
065: jSpinnerWidth.setModel(spinnerWidthModel);
066: jSpinnerChars.setModel(spinnerCharsModel);
067: jTextFieldHost.setText(HOST);
068: jTextFieldPort.setText(String.valueOf(PORT));
069: setTitle("");
070: }
071:
072: private void jButtonConnectActionPerformed() {
073: try {
074: setTitle("connecting...");
075: String host = jTextFieldHost.getText();
076: int port = Integer.valueOf(jTextFieldPort.getText());
077: if (imageClient != null) {
078: imageClient.disconnect();
079: }
080: imageClient = new ImageClient(host, port, this );
081: imageClient.connect();
082: jButtonConnect.setEnabled(!imageClient.isConnected());
083: } catch (NumberFormatException e) {
084: onException(e);
085: } catch (IllegalArgumentException e) {
086: onException(e);
087: }
088: }
089:
090: private void jButtonDisconnectActionPerformed() {
091: setTitle("disconnecting");
092: imageClient.disconnect();
093: }
094:
095: private void jButtonSendRequestActionPerformed() {
096: sendRequest();
097: }
098:
099: private void sendRequest() {
100: int chars = spinnerCharsModel.getNumber().intValue();
101: int height = spinnerHeightModel.getNumber().intValue();
102: int width = spinnerWidthModel.getNumber().intValue();
103: imageClient.sendRequest(new ImageRequest(width, height, chars));
104: }
105:
106: public void onImages(BufferedImage image1, BufferedImage image2) {
107: if (checkBoxContinuous.isSelected()) {
108: // already request next image
109: sendRequest();
110: }
111: imagePanel1.setImages(image1, image2);
112: }
113:
114: public void onException(Throwable throwable) {
115: Throwable cause = throwable;
116: while (cause.getCause() != null) {
117: cause = cause.getCause();
118: }
119: JOptionPane.showMessageDialog(this , cause.getMessage(),
120: throwable.getMessage(), JOptionPane.ERROR_MESSAGE);
121: setTitle("");
122: jButtonConnect.setEnabled(!imageClient.isConnected());
123: jButtonDisconnect.setEnabled(imageClient.isConnected());
124: }
125:
126: public void sessionOpened() {
127: jButtonDisconnect.setEnabled(true);
128: jButtonSendRequest.setEnabled(true);
129: jButtonConnect.setEnabled(false);
130: setTitle("connected");
131: }
132:
133: public void sessionClosed() {
134: jButtonDisconnect.setEnabled(false);
135: jButtonSendRequest.setEnabled(false);
136: jButtonConnect.setEnabled(true);
137: setTitle("not connected");
138: }
139:
140: @Override
141: public void setTitle(String title) {
142: super .setTitle("MINA - Chargen client - " + title);
143: }
144:
145: private void initComponents() {
146: JLabel jLabel1 = new JLabel();
147: jTextFieldHost = new JTextField();
148: jButtonConnect = new JButton();
149: JLabel jLabel3 = new JLabel();
150: jSpinnerWidth = new JSpinner();
151: JLabel label5 = new JLabel();
152: jSpinnerChars = new JSpinner();
153: checkBoxContinuous = new JCheckBox();
154: JLabel jLabel2 = new JLabel();
155: jTextFieldPort = new JTextField();
156: jButtonDisconnect = new JButton();
157: JLabel jLabel4 = new JLabel();
158: jSpinnerHeight = new JSpinner();
159: jButtonSendRequest = new JButton();
160: imagePanel1 = new ImagePanel();
161:
162: //======== this ========
163: setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
164: setMinimumSize(new Dimension(700, 300));
165: setPreferredSize(new Dimension(740, 600));
166: Container contentPane = getContentPane();
167: contentPane.setLayout(new GridBagLayout());
168: ((GridBagLayout) contentPane.getLayout()).columnWidths = new int[] {
169: 36, 167, 99, 41, 66, 75, 57, 96, 0, 0 };
170: ((GridBagLayout) contentPane.getLayout()).rowHeights = new int[] {
171: 10, 31, 31, 256, 0 };
172: ((GridBagLayout) contentPane.getLayout()).columnWeights = new double[] {
173: 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[] {
175: 0.0, 0.0, 0.0, 1.0, 1.0E-4 };
176:
177: //---- jLabel1 ----
178: jLabel1.setText("Host");
179: contentPane.add(jLabel1, new GridBagConstraints(0, 1, 1, 1,
180: 0.0, 0.0, GridBagConstraints.CENTER,
181: GridBagConstraints.BOTH, new Insets(0, 5, 5, 5), 0, 0));
182: contentPane
183: .add(jTextFieldHost, new GridBagConstraints(1, 1, 1, 1,
184: 0.0, 0.0, GridBagConstraints.CENTER,
185: GridBagConstraints.BOTH,
186: new Insets(0, 5, 5, 10), 0, 0));
187:
188: //---- jButtonConnect ----
189: jButtonConnect.setText("Connect");
190: jButtonConnect.addActionListener(new ActionListener() {
191: public void actionPerformed(ActionEvent e) {
192: jButtonConnectActionPerformed();
193: }
194: });
195: contentPane
196: .add(jButtonConnect, new GridBagConstraints(2, 1, 1, 1,
197: 0.0, 0.0, GridBagConstraints.CENTER,
198: GridBagConstraints.BOTH,
199: new Insets(0, 5, 5, 10), 0, 0));
200:
201: //---- jLabel3 ----
202: jLabel3.setText("Width");
203: contentPane.add(jLabel3, new GridBagConstraints(3, 1, 1, 1,
204: 0.0, 0.0, GridBagConstraints.CENTER,
205: GridBagConstraints.BOTH, new Insets(0, 0, 5, 5), 0, 0));
206: contentPane
207: .add(jSpinnerWidth, new GridBagConstraints(4, 1, 1, 1,
208: 0.0, 0.0, GridBagConstraints.CENTER,
209: GridBagConstraints.BOTH,
210: new Insets(0, 5, 5, 10), 0, 0));
211:
212: //---- label5 ----
213: label5.setText("characters");
214: contentPane.add(label5, new GridBagConstraints(5, 1, 1, 1, 0.0,
215: 0.0, GridBagConstraints.EAST,
216: GridBagConstraints.VERTICAL, new Insets(0, 0, 5, 5), 0,
217: 0));
218: contentPane
219: .add(jSpinnerChars, new GridBagConstraints(6, 1, 1, 1,
220: 0.0, 0.0, GridBagConstraints.CENTER,
221: GridBagConstraints.BOTH,
222: new Insets(0, 0, 5, 10), 0, 0));
223:
224: //---- checkBoxContinuous ----
225: checkBoxContinuous.setText("continuous");
226: contentPane
227: .add(checkBoxContinuous, new GridBagConstraints(7, 1,
228: 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
229: GridBagConstraints.BOTH,
230: new Insets(0, 5, 5, 10), 0, 0));
231:
232: //---- jLabel2 ----
233: jLabel2.setText("Port");
234: contentPane.add(jLabel2, new GridBagConstraints(0, 2, 1, 1,
235: 0.0, 0.0, GridBagConstraints.CENTER,
236: GridBagConstraints.BOTH, new Insets(0, 5, 5, 5), 0, 0));
237: contentPane
238: .add(jTextFieldPort, new GridBagConstraints(1, 2, 1, 1,
239: 0.0, 0.0, GridBagConstraints.CENTER,
240: GridBagConstraints.BOTH,
241: new Insets(0, 5, 5, 10), 0, 0));
242:
243: //---- jButtonDisconnect ----
244: jButtonDisconnect.setText("Disconnect");
245: jButtonDisconnect.setEnabled(false);
246: jButtonDisconnect.addActionListener(new ActionListener() {
247: public void actionPerformed(ActionEvent e) {
248: jButtonDisconnectActionPerformed();
249: }
250: });
251: contentPane
252: .add(jButtonDisconnect, new GridBagConstraints(2, 2, 1,
253: 1, 0.0, 0.0, GridBagConstraints.CENTER,
254: GridBagConstraints.BOTH,
255: new Insets(0, 5, 5, 10), 0, 0));
256:
257: //---- jLabel4 ----
258: jLabel4.setText("Height");
259: contentPane.add(jLabel4, new GridBagConstraints(3, 2, 1, 1,
260: 0.0, 0.0, GridBagConstraints.CENTER,
261: GridBagConstraints.BOTH, new Insets(0, 0, 5, 5), 0, 0));
262: contentPane
263: .add(jSpinnerHeight, new GridBagConstraints(4, 2, 1, 1,
264: 0.0, 0.0, GridBagConstraints.CENTER,
265: GridBagConstraints.BOTH,
266: new Insets(0, 5, 5, 10), 0, 0));
267:
268: //---- jButtonSendRequest ----
269: jButtonSendRequest.setText("Send Request");
270: jButtonSendRequest.setEnabled(false);
271: jButtonSendRequest.addActionListener(new ActionListener() {
272: public void actionPerformed(ActionEvent e) {
273: jButtonSendRequestActionPerformed();
274: }
275: });
276: contentPane
277: .add(jButtonSendRequest, new GridBagConstraints(5, 2,
278: 2, 1, 0.0, 0.0, GridBagConstraints.CENTER,
279: GridBagConstraints.BOTH,
280: new Insets(0, 5, 5, 10), 0, 0));
281:
282: //======== imagePanel1 ========
283: {
284: imagePanel1.setBackground(new Color(51, 153, 255));
285: imagePanel1.setPreferredSize(new Dimension(500, 500));
286:
287: { // compute preferred size
288: Dimension preferredSize = new Dimension();
289: for (int i = 0; i < imagePanel1.getComponentCount(); i++) {
290: Rectangle bounds = imagePanel1.getComponent(i)
291: .getBounds();
292: preferredSize.width = Math.max(bounds.x
293: + bounds.width, preferredSize.width);
294: preferredSize.height = Math.max(bounds.y
295: + bounds.height, preferredSize.height);
296: }
297: Insets insets = imagePanel1.getInsets();
298: preferredSize.width += insets.right;
299: preferredSize.height += insets.bottom;
300: imagePanel1.setMinimumSize(preferredSize);
301: imagePanel1.setPreferredSize(preferredSize);
302: }
303: }
304: contentPane.add(imagePanel1, new GridBagConstraints(0, 3, 9, 1,
305: 0.0, 0.0, GridBagConstraints.CENTER,
306: GridBagConstraints.BOTH, new Insets(8, 5, 8, 5), 0, 0));
307: pack();
308: setLocationRelativeTo(getOwner());
309: }
310:
311: /**
312: * @param args the command line arguments
313: */
314: public static void main(String args[]) {
315: try {
316: UIManager.setLookAndFeel(UIManager
317: .getSystemLookAndFeelClassName());
318: } catch (Exception e) {
319: // ignore
320: }
321: java.awt.EventQueue.invokeLater(new Runnable() {
322: public void run() {
323: new GraphicalCharGenClient().setVisible(true);
324: }
325: });
326: }
327:
328: private JTextField jTextFieldHost;
329: private JButton jButtonConnect;
330: private JSpinner jSpinnerWidth;
331: private JSpinner jSpinnerChars;
332: private JCheckBox checkBoxContinuous;
333: private JTextField jTextFieldPort;
334: private JButton jButtonDisconnect;
335: private JSpinner jSpinnerHeight;
336: private JButton jButtonSendRequest;
337: private ImagePanel imagePanel1;
338:
339: private SpinnerNumberModel spinnerHeightModel = new SpinnerNumberModel(
340: 100, 50, 600, 25);
341: private SpinnerNumberModel spinnerWidthModel = new SpinnerNumberModel(
342: 200, 50, 1000, 25);
343: private SpinnerNumberModel spinnerCharsModel = new SpinnerNumberModel(
344: 10, 1, 60, 1);
345:
346: private ImageClient imageClient = new ImageClient(HOST, PORT, this);
347: }
|