Mercurial > hg > Applications > tvnjviewer
annotate src/viewer_swing/java/com/glavsoft/viewer/Viewer.java @ 25:bf9480332e72
reconnect New connection window can select expression
author | sugi |
---|---|
date | Sun, 09 Nov 2014 11:13:05 +0900 |
parents | 94b36ee816ef |
children | e650eaf8deeb |
rev | line source |
---|---|
0 | 1 // Copyright (C) 2010, 2011, 2012, 2013 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 | |
24 | 27 import alice.datasegment.DataSegment; |
28 import alice.datasegment.ReceiveData; | |
29 | |
0 | 30 import com.glavsoft.rfb.protocol.ProtocolSettings; |
31 import com.glavsoft.viewer.cli.Parser; | |
32 import com.glavsoft.viewer.mvp.View; | |
33 import com.glavsoft.viewer.swing.ConnectionParams; | |
34 import com.glavsoft.viewer.swing.ParametersHandler; | |
35 import com.glavsoft.viewer.swing.SwingConnectionWorkerFactory; | |
36 import com.glavsoft.viewer.swing.SwingViewerWindowFactory; | |
37 import com.glavsoft.viewer.swing.gui.ConnectionView; | |
38 | |
39 import javax.swing.*; | |
24 | 40 |
0 | 41 import java.awt.*; |
42 import java.awt.event.WindowEvent; | |
43 import java.awt.event.WindowListener; | |
44 import java.io.IOException; | |
45 import java.io.InputStream; | |
46 import java.util.jar.Attributes; | |
47 import java.util.jar.Manifest; | |
48 import java.util.logging.*; | |
49 | |
50 @SuppressWarnings("serial") | |
51 public class Viewer extends JApplet implements Runnable, WindowListener { | |
52 | |
53 private Logger logger; | |
54 private int paramsMask; | |
55 private boolean allowAppletInteractiveConnections; | |
56 | |
57 private final ConnectionParams connectionParams; | |
58 private String passwordFromParams; | |
59 boolean isSeparateFrame = true; | |
60 boolean isApplet = true; | |
61 private final ProtocolSettings settings; | |
62 private final UiSettings uiSettings; | |
63 private volatile boolean isAppletStopped = false; | |
64 private ConnectionPresenter connectionPresenter; | |
25 | 65 private boolean needReconnect = true; |
0 | 66 |
67 public static void main(String[] args) { | |
68 Parser parser = new Parser(); | |
69 ParametersHandler.completeParserOptions(parser); | |
70 | |
71 parser.parse(args); | |
72 if (parser.isSet(ParametersHandler.ARG_HELP)) { | |
73 printUsage(parser.optionsUsage()); | |
74 System.exit(0); | |
75 } | |
76 Viewer viewer = new Viewer(parser); | |
77 SwingUtilities.invokeLater(viewer); | |
78 } | |
79 | |
80 public static void printUsage(String additional) { | |
81 System.out.println("Usage: java -jar (progfilename) [hostname [port_number]] [Options]\n" + | |
82 " or\n"+ | |
83 " java -jar (progfilename) [Options]\n" + | |
84 " or\n java -jar (progfilename) -help\n to view this help\n\n" + | |
85 "Where Options are:\n" + additional + | |
86 "\nOptions format: -optionName=optionValue. Ex. -host=localhost -port=5900 -viewonly=yes\n" + | |
87 "Both option name and option value are case insensitive."); | |
88 } | |
89 | |
90 public Viewer() { | |
91 logger = Logger.getLogger(getClass().getName()); | |
92 connectionParams = new ConnectionParams(); | |
93 settings = ProtocolSettings.getDefaultSettings(); | |
94 uiSettings = new UiSettings(); | |
95 } | |
96 | |
2
960c7717ad0d
add getUisetting method and change constructor type public
YU
parents:
0
diff
changeset
|
97 public Viewer(Parser parser) { |
0 | 98 this(); |
99 setLoggingLevel(parser.isSet(ParametersHandler.ARG_VERBOSE) ? Level.FINE : | |
100 parser.isSet(ParametersHandler.ARG_VERBOSE_MORE) ? Level.FINER : | |
101 Level.INFO); | |
24 | 102 |
0 | 103 paramsMask = ParametersHandler.completeSettingsFromCLI(parser, connectionParams, settings, uiSettings); |
104 passwordFromParams = parser.getValueFor(ParametersHandler.ARG_PASSWORD); | |
105 logger.info("TightVNC Viewer version " + ver()); | |
106 isApplet = false; | |
24 | 107 |
21
966878ff1227
not appear NEW TightVNC Connection window if first argument equal "localhost"
sugi
parents:
2
diff
changeset
|
108 if (parser.isSetPlainOptions()) { |
966878ff1227
not appear NEW TightVNC Connection window if first argument equal "localhost"
sugi
parents:
2
diff
changeset
|
109 if ("localhost".equals(parser.getPlainOptionAt(0))) { |
966878ff1227
not appear NEW TightVNC Connection window if first argument equal "localhost"
sugi
parents:
2
diff
changeset
|
110 connectionParams.hostName = "localhost"; |
966878ff1227
not appear NEW TightVNC Connection window if first argument equal "localhost"
sugi
parents:
2
diff
changeset
|
111 isApplet = true; |
24 | 112 ReceiveData rData = new ReceiveData(this, false, false); |
113 DataSegment.getLocal().put("viewer", rData, null); | |
25 | 114 this.needReconnect = false; |
21
966878ff1227
not appear NEW TightVNC Connection window if first argument equal "localhost"
sugi
parents:
2
diff
changeset
|
115 }; |
966878ff1227
not appear NEW TightVNC Connection window if first argument equal "localhost"
sugi
parents:
2
diff
changeset
|
116 } |
0 | 117 } |
118 | |
119 private void setLoggingLevel(Level levelToSet) { | |
120 final Logger appLogger = Logger.getLogger("com.glavsoft"); | |
121 appLogger.setLevel(levelToSet); | |
122 ConsoleHandler ch = null; | |
123 for (Handler h : appLogger.getHandlers()) { | |
124 if (h instanceof ConsoleHandler) { | |
125 ch = (ConsoleHandler) h; | |
126 break; | |
127 } | |
128 } | |
129 if (null == ch) { | |
130 ch = new ConsoleHandler(); | |
131 appLogger.addHandler(ch); | |
132 } | |
133 // ch.setFormatter(new SimpleFormatter()); | |
134 ch.setLevel(levelToSet); | |
135 } | |
136 | |
137 | |
138 @Override | |
139 public void windowClosing(WindowEvent e) { | |
140 if (e != null && e.getComponent() != null) { | |
141 final Window w = e.getWindow(); | |
142 if (w != null) { | |
143 w.setVisible(false); | |
144 w.dispose(); | |
145 } | |
146 } | |
147 closeApp(); | |
148 } | |
149 | |
150 /** | |
151 * Closes App(lication) or stops App(let). | |
152 */ | |
153 public void closeApp() { | |
154 if (connectionPresenter != null) { | |
155 connectionPresenter.cancelConnection(); | |
156 logger.info("Connections cancelled."); | |
157 } | |
158 if (isApplet) { | |
159 if ( ! isAppletStopped) { | |
160 logger.severe("Applet is stopped."); | |
161 isAppletStopped = true; | |
162 repaint(); | |
163 stop(); | |
164 } | |
165 } else { | |
166 System.exit(0); | |
167 } | |
168 } | |
169 | |
170 @Override | |
171 public void paint(Graphics g) { | |
172 if ( ! isAppletStopped) { | |
173 super.paint(g); | |
174 } else { | |
175 getContentPane().removeAll(); | |
176 g.clearRect(0, 0, getWidth(), getHeight()); | |
177 g.drawString("Disconnected", 10, 20); | |
178 } | |
179 } | |
180 | |
181 @Override | |
182 public void destroy() { | |
183 closeApp(); | |
184 super.destroy(); | |
185 } | |
186 | |
187 @Override | |
188 public void init() { | |
189 paramsMask = ParametersHandler.completeSettingsFromApplet(this, connectionParams, settings, uiSettings); | |
190 isSeparateFrame = ParametersHandler.isSeparateFrame; | |
191 passwordFromParams = getParameter(ParametersHandler.ARG_PASSWORD); | |
192 isApplet = true; | |
193 allowAppletInteractiveConnections = ParametersHandler.allowAppletInteractiveConnections; | |
194 repaint(); | |
195 | |
196 try { | |
197 SwingUtilities.invokeAndWait(this); | |
198 } catch (Exception e) { | |
199 logger.severe(e.getMessage()); | |
200 } | |
201 } | |
202 | |
203 @Override | |
204 public void start() { | |
205 super.start(); | |
206 } | |
207 | |
208 private boolean checkJsch() { | |
209 try { | |
210 Class.forName("com.jcraft.jsch.JSch"); | |
211 return true; | |
212 } catch (ClassNotFoundException e) { | |
213 return false; | |
214 } | |
215 } | |
216 | |
217 @Override | |
218 public void run() { | |
219 | |
220 final boolean hasJsch = checkJsch(); | |
221 final boolean allowInteractive = allowAppletInteractiveConnections || ! isApplet; | |
222 connectionPresenter = new ConnectionPresenter(hasJsch, allowInteractive); | |
223 connectionPresenter.addModel("ConnectionParamsModel", connectionParams); | |
224 final ConnectionView connectionView = new ConnectionView( | |
225 Viewer.this, // appWindowListener | |
226 connectionPresenter, hasJsch); | |
227 connectionPresenter.addView(ConnectionPresenter.CONNECTION_VIEW, connectionView); | |
228 if (isApplet) { | |
229 connectionPresenter.addView("AppletStatusStringView", new View() { | |
230 @Override | |
231 public void showView() { /*nop*/ } | |
232 @Override | |
233 public void closeView() { /*nop*/ } | |
234 @SuppressWarnings("UnusedDeclaration") | |
235 public void setMessage(String message) { | |
236 Viewer.this.getAppletContext().showStatus(message); | |
237 } | |
238 }); | |
239 } | |
25 | 240 if (!needReconnect) { |
241 connectionPresenter.setNeedReconnection(false); | |
242 } | |
0 | 243 |
244 SwingViewerWindowFactory viewerWindowFactory = new SwingViewerWindowFactory(isSeparateFrame, isApplet, this); | |
245 connectionPresenter.setConnectionWorkerFactory( | |
246 new SwingConnectionWorkerFactory(connectionView.getFrame(), passwordFromParams, connectionPresenter, viewerWindowFactory)); | |
247 connectionPresenter.startConnection(settings, uiSettings, paramsMask); | |
248 } | |
249 | |
250 @Override | |
251 public void windowOpened(WindowEvent e) { /* nop */ } | |
252 @Override | |
253 public void windowClosed(WindowEvent e) { /* nop */ } | |
254 @Override | |
255 public void windowIconified(WindowEvent e) { /* nop */ } | |
256 @Override | |
257 public void windowDeiconified(WindowEvent e) { /* nop */ } | |
258 @Override | |
259 public void windowActivated(WindowEvent e) { /* nop */ } | |
260 @Override | |
261 public void windowDeactivated(WindowEvent e) { /* nop */ } | |
262 | |
263 public static String ver() { | |
264 final InputStream mfStream = Viewer.class.getClassLoader().getResourceAsStream( | |
265 "META-INF/MANIFEST.MF"); | |
266 if (null == mfStream) { | |
267 System.out.println("No Manifest file found."); | |
268 return "-1"; | |
269 } | |
270 try { | |
271 Manifest mf = new Manifest(); | |
272 mf.read(mfStream); | |
273 Attributes atts = mf.getMainAttributes(); | |
274 return atts.getValue(Attributes.Name.IMPLEMENTATION_VERSION); | |
275 } catch (IOException e) { | |
276 return "-2"; | |
277 } | |
278 } | |
279 | |
2
960c7717ad0d
add getUisetting method and change constructor type public
YU
parents:
0
diff
changeset
|
280 // add |
960c7717ad0d
add getUisetting method and change constructor type public
YU
parents:
0
diff
changeset
|
281 public UiSettings getUiSettings() { |
960c7717ad0d
add getUisetting method and change constructor type public
YU
parents:
0
diff
changeset
|
282 return uiSettings; |
960c7717ad0d
add getUisetting method and change constructor type public
YU
parents:
0
diff
changeset
|
283 } |
960c7717ad0d
add getUisetting method and change constructor type public
YU
parents:
0
diff
changeset
|
284 |
0 | 285 } |