您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ServerDialog.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright (C) 2011-2016 Brian P. Hinz
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  17. * USA.
  18. */
  19. package com.tigervnc.vncviewer;
  20. import java.awt.*;
  21. import java.awt.event.*;
  22. import java.io.File;
  23. import java.nio.CharBuffer;
  24. import javax.swing.*;
  25. import javax.swing.border.*;
  26. import javax.swing.filechooser.*;
  27. import javax.swing.WindowConstants.*;
  28. import java.util.*;
  29. import com.tigervnc.rfb.*;
  30. import static java.awt.GridBagConstraints.HORIZONTAL;
  31. import static java.awt.GridBagConstraints.LINE_START;
  32. import static java.awt.GridBagConstraints.LINE_END;
  33. import static java.awt.GridBagConstraints.NONE;
  34. import static java.awt.GridBagConstraints.REMAINDER;
  35. import static com.tigervnc.vncviewer.Parameters.*;
  36. class ServerDialog extends Dialog implements Runnable {
  37. @SuppressWarnings({"unchecked","rawtypes"})
  38. public ServerDialog(String defaultServerName,
  39. CharBuffer vncServerName) {
  40. super(true);
  41. this.vncServerName = vncServerName;
  42. setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
  43. setTitle("VNC Viewer: Connection Details");
  44. setResizable(false);
  45. addWindowListener(new WindowAdapter() {
  46. public void windowClosing(WindowEvent e) {
  47. endDialog();
  48. System.exit(1);
  49. }
  50. });
  51. JLabel serverLabel = new JLabel("VNC server:", JLabel.RIGHT);
  52. String valueStr = new String(defaultServerName);
  53. ArrayList<String> servernames = new ArrayList<String>();
  54. if (!valueStr.isEmpty())
  55. servernames.add(valueStr);
  56. String history = UserPreferences.get("ServerDialog", "history");
  57. if (history != null) {
  58. for (String s : history.split(",")) {
  59. if (servernames.indexOf(s) < 0)
  60. servernames.add(s);
  61. }
  62. }
  63. serverName = new MyJComboBox(servernames.toArray());
  64. serverName.addActionListener(new ActionListener() {
  65. public void actionPerformed(ActionEvent e) {
  66. JComboBox s = (JComboBox)e.getSource();
  67. if (e.getActionCommand().equals("comboBoxEdited")) {
  68. s.insertItemAt(editor.getItem(), 0);
  69. s.setSelectedIndex(0);
  70. }
  71. }
  72. });
  73. if (servernames.size() == 0)
  74. serverName.setPrototypeDisplayValue("255.255.255.255:5900");
  75. serverName.setEditable(true);
  76. editor = serverName.getEditor();
  77. editor.getEditorComponent().addKeyListener(new KeyListener() {
  78. public void keyTyped(KeyEvent e) {}
  79. public void keyReleased(KeyEvent e) {}
  80. public void keyPressed(KeyEvent e) {
  81. if (e.getKeyCode() == KeyEvent.VK_ENTER) {
  82. serverName.insertItemAt(editor.getItem(), 0);
  83. serverName.setSelectedIndex(0);
  84. handleConnect();
  85. }
  86. }
  87. });
  88. Container contentPane = this.getContentPane();
  89. contentPane.setLayout(new GridBagLayout());
  90. JLabel icon = new JLabel(VncViewer.logoIcon);
  91. optionsButton = new JButton("Options...");
  92. optionsButton.addActionListener(new ActionListener() {
  93. public void actionPerformed(ActionEvent e) {
  94. handleOptions();
  95. }
  96. });
  97. JButton loadButton = new JButton("Load...");
  98. loadButton.addActionListener(new ActionListener() {
  99. public void actionPerformed(ActionEvent e) {
  100. handleLoad();
  101. }
  102. });
  103. JButton saveAsButton = new JButton("Save As...");
  104. saveAsButton.addActionListener(new ActionListener() {
  105. public void actionPerformed(ActionEvent e) {
  106. handleSaveAs();
  107. }
  108. });
  109. aboutButton = new JButton("About...");
  110. aboutButton.addActionListener(new ActionListener() {
  111. public void actionPerformed(ActionEvent e) {
  112. handleAbout();
  113. }
  114. });
  115. cancelButton = new JButton("Cancel");
  116. cancelButton.addActionListener(new ActionListener() {
  117. public void actionPerformed(ActionEvent e) {
  118. handleCancel();
  119. }
  120. });
  121. connectButton = new JButton("Connect \u21B5");
  122. connectButton.addActionListener(new ActionListener() {
  123. public void actionPerformed(ActionEvent e) {
  124. handleConnect();
  125. }
  126. });
  127. contentPane.add(icon,
  128. new GridBagConstraints(0, 0,
  129. 1, 1,
  130. LIGHT, LIGHT,
  131. LINE_START, NONE,
  132. new Insets(5, 5, 5, 5),
  133. NONE, NONE));
  134. contentPane.add(serverLabel,
  135. new GridBagConstraints(1, 0,
  136. 1, 1,
  137. LIGHT, LIGHT,
  138. LINE_START, NONE,
  139. new Insets(5, 10, 5, 5),
  140. NONE, NONE));
  141. contentPane.add(serverName,
  142. new GridBagConstraints(2, 0,
  143. REMAINDER, 1,
  144. HEAVY, LIGHT,
  145. LINE_START, HORIZONTAL,
  146. new Insets(5, 0, 5, 5),
  147. NONE, NONE));
  148. JPanel buttonPane1 = new JPanel();
  149. Box box = Box.createHorizontalBox();
  150. JSeparator separator1 = new JSeparator();
  151. JSeparator separator2 = new JSeparator();
  152. GroupLayout layout = new GroupLayout(buttonPane1);
  153. buttonPane1.setLayout(layout);
  154. layout.setAutoCreateGaps(false);
  155. layout.setAutoCreateContainerGaps(false);
  156. layout.setHorizontalGroup(
  157. layout.createSequentialGroup()
  158. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  159. .addGroup(layout.createSequentialGroup()
  160. .addGap(10)
  161. .addComponent(optionsButton))
  162. .addComponent(separator1)
  163. .addGroup(layout.createSequentialGroup()
  164. .addGap(10)
  165. .addComponent(aboutButton)))
  166. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
  167. .addGroup(layout.createSequentialGroup()
  168. .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
  169. .addComponent(loadButton)
  170. .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
  171. .addComponent(saveAsButton)
  172. .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
  173. .addComponent(box)
  174. .addGap(10))
  175. .addComponent(separator2)
  176. .addGroup(layout.createSequentialGroup()
  177. .addComponent(cancelButton)
  178. .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
  179. .addComponent(connectButton)
  180. .addGap(10)))
  181. );
  182. layout.setVerticalGroup(
  183. layout.createSequentialGroup()
  184. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  185. .addComponent(optionsButton)
  186. .addComponent(loadButton)
  187. .addComponent(saveAsButton)
  188. .addComponent(box))
  189. .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
  190. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  191. .addComponent(separator1)
  192. .addComponent(separator2))
  193. .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
  194. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  195. .addComponent(aboutButton)
  196. .addComponent(cancelButton)
  197. .addComponent(connectButton))
  198. .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
  199. );
  200. layout.linkSize(SwingConstants.HORIZONTAL,
  201. optionsButton, loadButton, saveAsButton,
  202. aboutButton, cancelButton, box);
  203. contentPane.add(buttonPane1,
  204. new GridBagConstraints(0, 1,
  205. REMAINDER, 1,
  206. LIGHT, LIGHT,
  207. LINE_START, HORIZONTAL,
  208. new Insets(5, 0, 10, 0),
  209. NONE, NONE));
  210. pack();
  211. }
  212. public void run() {
  213. this.showDialog();
  214. }
  215. private void handleOptions() {
  216. OptionsDialog.showDialog(this);
  217. }
  218. private void handleLoad() {
  219. String title = "Select a TigerVNC configuration file";
  220. File dflt = new File(FileUtils.getVncHomeDir().concat("default.tigervnc"));
  221. FileNameExtensionFilter filter =
  222. new FileNameExtensionFilter("TigerVNC configuration (*.tigervnc)", "tigervnc");
  223. File f = showChooser(title, dflt, filter);
  224. if (f != null && f.exists() && f.canRead())
  225. loadViewerParameters(f.getAbsolutePath());
  226. }
  227. private void handleSaveAs() {
  228. String title = "Save the TigerVNC configuration to file";
  229. File dflt = new File(FileUtils.getVncHomeDir().concat("default.tigervnc"));
  230. if (!dflt.exists() || !dflt.isFile())
  231. dflt = new File(FileUtils.getVncHomeDir());
  232. FileNameExtensionFilter filter =
  233. new FileNameExtensionFilter("TigerVNC configuration (*.tigervnc)", "tigervnc");
  234. File f = showChooser(title, dflt, filter);
  235. while (f != null && f.exists() && f.isFile()) {
  236. String msg = f.getAbsolutePath();
  237. msg = msg.concat(" already exists. Do you want to overwrite?");
  238. Object[] options = {"Overwrite", "No \u21B5"};
  239. JOptionPane op =
  240. new JOptionPane(msg, JOptionPane.QUESTION_MESSAGE,
  241. JOptionPane.OK_CANCEL_OPTION, null, options, options[1]);
  242. JDialog dlg = op.createDialog(this, "TigerVNC Viewer");
  243. dlg.setIconImage(VncViewer.frameIcon);
  244. dlg.setAlwaysOnTop(true);
  245. dlg.setVisible(true);
  246. if (op.getValue() == options[0])
  247. break;
  248. else
  249. f = showChooser(title, f, filter);
  250. }
  251. if (f != null && (!f.exists() || f.canWrite()))
  252. saveViewerParameters(f.getAbsolutePath(), (String)serverName.getSelectedItem());
  253. }
  254. private void handleAbout() {
  255. VncViewer.about_vncviewer(this);
  256. }
  257. private void handleCancel() {
  258. endDialog();
  259. }
  260. private void handleConnect() {
  261. String servername = (String)serverName.getSelectedItem();
  262. vncServerName.put(servername).flip();
  263. saveViewerParameters(null, servername);
  264. endDialog();
  265. }
  266. @SuppressWarnings("rawtypes")
  267. MyJComboBox serverName;
  268. ComboBoxEditor editor;
  269. JButton aboutButton, optionsButton, connectButton, cancelButton;
  270. OptionsDialog options;
  271. CharBuffer vncServerName;
  272. static LogWriter vlog = new LogWriter("ServerDialog");
  273. }