You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ServerDialog.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright (C) 2011-2012 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 javax.swing.*;
  23. import javax.swing.border.*;
  24. import javax.swing.WindowConstants.*;
  25. import java.util.*;
  26. import com.tigervnc.rfb.*;
  27. class ServerDialog extends Dialog implements
  28. ActionListener,
  29. ItemListener
  30. {
  31. public ServerDialog(OptionsDialog options_,
  32. String defaultServerName, CConn cc_) {
  33. super(true);
  34. cc = cc_;
  35. setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
  36. setResizable(false);
  37. setSize(new Dimension(340, 135));
  38. setTitle("VNC Viewer : Connection Details");
  39. addWindowListener(new WindowAdapter() {
  40. public void windowClosing(WindowEvent e) {
  41. if (cc.viewer.firstApplet && cc.viewport == null) {
  42. System.exit(1);
  43. } else {
  44. ok = false;
  45. endDialog();
  46. }
  47. }
  48. });
  49. options = options_;
  50. getContentPane().setLayout(new GridBagLayout());
  51. JLabel serverLabel = new JLabel("Server:", JLabel.RIGHT);
  52. if (options.defaults.getString("server") != null) {
  53. server = new JComboBox(options.defaults.getString("server").split(","));
  54. } else {
  55. server = new JComboBox();
  56. }
  57. // Hack to set the left inset on editable JComboBox
  58. if (UIManager.getLookAndFeel().getID() == "Windows") {
  59. server.setBorder(BorderFactory.createCompoundBorder(server.getBorder(),
  60. BorderFactory.createEmptyBorder(0,2,0,0)));
  61. } else if (UIManager.getLookAndFeel().getID() == "Metal") {
  62. ComboBoxEditor editor = server.getEditor();
  63. JTextField jtf = (JTextField)editor.getEditorComponent();
  64. jtf.setBorder(new CompoundBorder(jtf.getBorder(), new EmptyBorder(0,2,0,0)));
  65. }
  66. server.setEditable(true);
  67. editor = server.getEditor();
  68. JPanel topPanel = new JPanel(new GridBagLayout());
  69. addGBComponent(new JLabel(cc.logo),topPanel, 0, 0, 1, 1, 0, 0, 0, 1, GridBagConstraints.HORIZONTAL, GridBagConstraints.LINE_START, new Insets(5,5,5,15));
  70. addGBComponent(serverLabel,topPanel, 1, 0, 1, 1, 0, 0, 0, 1, GridBagConstraints.HORIZONTAL, GridBagConstraints.LINE_END, new Insets(10,0,5,5));
  71. addGBComponent(server,topPanel, 2, 0, 1, 1, 0, 0, 1, 1, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, new Insets(10,0,5,40));
  72. optionsButton = new JButton("Options...");
  73. aboutButton = new JButton("About...");
  74. okButton = new JButton("OK");
  75. cancelButton = new JButton("Cancel");
  76. JPanel buttonPanel = new JPanel(new GridBagLayout());
  77. buttonPanel.setPreferredSize(new Dimension(340, 40));
  78. addGBComponent(aboutButton,buttonPanel, 0, 3, 1, 1, 0, 0, 0.2, 1, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, new Insets(0,5,0,5));
  79. addGBComponent(optionsButton,buttonPanel, 1, 3, 1, 1, 0, 0, 0, 1, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, new Insets(0,5,0,5));
  80. addGBComponent(okButton,buttonPanel, 2, 3, 1, 1, 0, 0, 0.8, 1, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, new Insets(0,5,0,5));
  81. addGBComponent(cancelButton,buttonPanel, 3, 3, 1, 1, 0, 0, 0.5, 1, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, new Insets(0,5,0,5));
  82. GridBagConstraints gbc = new GridBagConstraints();
  83. gbc.anchor = GridBagConstraints.LINE_START;
  84. gbc.fill = GridBagConstraints.BOTH;
  85. gbc.gridwidth = GridBagConstraints.REMAINDER;
  86. gbc.gridheight = 1;
  87. gbc.insets = new Insets(0,0,0,0);
  88. gbc.ipadx = 0;
  89. gbc.ipady = 0;
  90. gbc.weightx = 1;
  91. gbc.weighty = 1;
  92. getContentPane().add(topPanel,gbc);
  93. getContentPane().add(buttonPanel);
  94. server.addActionListener(this);
  95. optionsButton.addActionListener(this);
  96. aboutButton.addActionListener(this);
  97. okButton.addActionListener(this);
  98. cancelButton.addActionListener(this);
  99. pack();
  100. }
  101. public void itemStateChanged(ItemEvent e) {
  102. //Object s = e.getSource();
  103. }
  104. public void actionPerformed(ActionEvent e) {
  105. Object s = e.getSource();
  106. if (s instanceof JButton && (JButton)s == okButton) {
  107. ok = true;
  108. endDialog();
  109. } else if (s instanceof JButton && (JButton)s == cancelButton) {
  110. ok = false;
  111. endDialog();
  112. } else if (s instanceof JButton && (JButton)s == optionsButton) {
  113. options.showDialog(this);
  114. } else if (s instanceof JButton && (JButton)s == aboutButton) {
  115. cc.showAbout();
  116. } else if (s instanceof JComboBox && (JComboBox)s == server) {
  117. if (e.getActionCommand().equals("comboBoxEdited")) {
  118. server.insertItemAt(editor.getItem(), 0);
  119. server.setSelectedIndex(0);
  120. ok = true;
  121. endDialog();
  122. }
  123. }
  124. }
  125. public void endDialog() {
  126. if (ok) {
  127. try {
  128. if (!server.getSelectedItem().toString().equals("")) {
  129. String t = (options.defaults.getString("server")==null) ? "" : options.defaults.getString("server");
  130. StringTokenizer st = new StringTokenizer(t, ",");
  131. StringBuffer sb = new StringBuffer().append((String)server.getSelectedItem());
  132. while (st.hasMoreTokens()) {
  133. String s = st.nextToken();
  134. if (!s.equals((String)server.getSelectedItem()) && !s.equals("")) {
  135. sb.append(',');
  136. sb.append(s);
  137. }
  138. }
  139. options.defaults.setPref("server", sb.toString());
  140. }
  141. options.defaults.Save();
  142. } catch (java.io.IOException e) {
  143. System.out.println(e.toString());
  144. } catch(java.security.AccessControlException e) {
  145. System.out.println(e.toString());
  146. }
  147. }
  148. done = true;
  149. if (modal) {
  150. synchronized (this) {
  151. notify();
  152. }
  153. }
  154. this.dispose();
  155. }
  156. CConn cc;
  157. JComboBox server;
  158. ComboBoxEditor editor;
  159. JButton aboutButton, optionsButton, okButton, cancelButton;
  160. OptionsDialog options;
  161. static LogWriter vlog = new LogWriter("ServerDialog");
  162. }