comparison src/viewer_swing/java/com/glavsoft/viewer/ConnectionManager.java @ 0:4689cc86d6cb

create TreeViewer2 Repository
author Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
date Tue, 03 Jul 2012 13:20:49 +0900
parents
children dca3bd61b830
comparison
equal deleted inserted replaced
-1:000000000000 0:4689cc86d6cb
1 // Copyright (C) 2010, 2011 GlavSoft LLC.
2 // All rights reserved.
3 //
4 //-------------------------------------------------------------------------
5 // This file is part of the TightVNC software. Please visit our Web site:
6 //
7 // http://www.tightvnc.com/
8 //
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License along
20 // with this program; if not, write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 //-------------------------------------------------------------------------
23 //
24
25 package com.glavsoft.viewer;
26
27 import com.glavsoft.rfb.protocol.ProtocolSettings;
28 import com.glavsoft.viewer.swing.ParametersHandler;
29 import com.glavsoft.viewer.swing.Utils;
30 import com.glavsoft.viewer.swing.gui.ConnectionDialog;
31
32 import javax.swing.*;
33 import java.awt.*;
34 import java.awt.event.WindowListener;
35 import java.io.IOException;
36 import java.io.Serializable;
37 import java.net.Socket;
38 import java.net.UnknownHostException;
39
40
41 public class ConnectionManager implements Serializable {
42 /**
43 *
44 */
45 private static final long serialVersionUID = 1L;
46 private final WindowListener appWindowListener;
47 volatile boolean forceConnectionDialog;
48 private JFrame containerFrame;
49 private final boolean isApplet;
50
51 public ConnectionManager(WindowListener appWindowListener, boolean isApplet) {
52 this.appWindowListener = appWindowListener;
53 this.isApplet = isApplet;
54 }
55
56 public void showReconnectDialog(String title, String message) {
57 JOptionPane reconnectPane = new JOptionPane(message + "\nTry another connection?",
58 JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
59 final JDialog reconnectDialog = reconnectPane.createDialog(containerFrame, title);
60 reconnectDialog.setModalityType(isApplet ?
61 Dialog.ModalityType.APPLICATION_MODAL : Dialog.ModalityType.TOOLKIT_MODAL);
62 try {
63 reconnectDialog.setAlwaysOnTop(true);
64 } catch (SecurityException e) { /*nop*/ }
65 java.util.List<Image> icons = Utils.getIcons();
66 if (icons.size() != 0) {
67 reconnectDialog.setIconImages(icons);
68 }
69 reconnectDialog.setVisible(true);
70 if (reconnectPane.getValue() == null ||
71 (Integer)reconnectPane.getValue() == JOptionPane.NO_OPTION) {
72 appWindowListener.windowClosing(null);
73 } else {
74 forceConnectionDialog = !isApplet;
75 }
76 }
77
78 public Socket connectToHost(final ParametersHandler.ConnectionParams connectionParams, ProtocolSettings settings) {
79 Socket socket = null;
80 ConnectionDialog connectionDialog = null;
81 boolean wasError = false;
82 do {
83 if (forceConnectionDialog || wasError ||
84 connectionParams.isHostNameEmpty() ||
85 -1 == connectionParams.portNumber) {
86 forceConnectionDialog = false;
87 if (null == connectionDialog) {
88 connectionDialog = new ConnectionDialog(containerFrame,
89 appWindowListener, connectionParams.hostName, connectionParams.portNumber,
90 settings, isApplet);
91 }
92 connectionDialog.setVisible(true);
93 connectionParams.hostName = connectionDialog.getServerNameString();
94 connectionParams.portNumber = connectionDialog.getPort();
95 }
96 Viewer.logger.info("Connecting to host " + connectionParams.hostName + ":" + connectionParams.portNumber);
97 try {
98 socket = new Socket(connectionParams.hostName, connectionParams.portNumber);
99 wasError = false;
100 } catch (UnknownHostException e) {
101 Viewer.logger.severe("Unknown host: " + connectionParams.hostName);
102 showConnectionErrorDialog("Unknown host: '" + connectionParams.hostName + "'");
103 wasError = true;
104 } catch (IOException e) {
105 Viewer.logger.severe("Couldn't connect to: " +
106 connectionParams.hostName + ":" + connectionParams.portNumber +
107 ": " + e.getMessage());
108 showConnectionErrorDialog("Couldn't connect to: '" + connectionParams.hostName +
109 "'\n" + e.getMessage());
110 wasError = true;
111 }
112 } while (!isApplet && (connectionParams.isHostNameEmpty() || wasError));
113 if (connectionDialog != null) {
114 connectionDialog.dispose();
115 }
116 return socket;
117 }
118
119 public void showConnectionErrorDialog(final String message) {
120 JOptionPane errorPane = new JOptionPane(message, JOptionPane.ERROR_MESSAGE);
121 final JDialog errorDialog = errorPane.createDialog(containerFrame, "Connection error");
122 errorDialog.setModalityType(isApplet ? Dialog.ModalityType.APPLICATION_MODAL : Dialog.ModalityType.TOOLKIT_MODAL);
123 try {
124 errorDialog.setAlwaysOnTop(true);
125 } catch (SecurityException e) { /*nop*/ }
126 errorDialog.setVisible(true);
127 }
128
129 public void setContainerFrame(JFrame containerFrame) {
130 this.containerFrame = containerFrame;
131 }
132 }