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.

NewServerDialog.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package org.ownage.dmdirc.ui;
  23. import org.ownage.dmdirc.Server;
  24. import org.ownage.dmdirc.ServerManager;
  25. import java.awt.event.ActionEvent;
  26. import java.awt.event.ActionListener;
  27. import java.awt.event.WindowEvent;
  28. import java.awt.event.WindowListener;
  29. import javax.swing.InputVerifier;
  30. import javax.swing.JComponent;
  31. import javax.swing.JTextField;
  32. /**
  33. * Dialog that allows the user to enter details of a new server to connect to
  34. * @author chris
  35. */
  36. public class NewServerDialog extends javax.swing.JDialog {
  37. /**
  38. * A previously created instance of NewServerDialog
  39. */
  40. private static NewServerDialog me;
  41. /**
  42. * Creates a new instance of the dialog
  43. */
  44. private NewServerDialog() {
  45. super((java.awt.Frame)MainFrame.getMainFrame(), false);
  46. initComponents();
  47. if (ServerManager.getServerManager().numServers() == 0) {
  48. jCheckBox1.setSelected(true);
  49. jCheckBox1.setEnabled(false);
  50. }
  51. jTextField2.setInputVerifier(new PortVerifier());
  52. addCallbacks();
  53. setVisible(true);
  54. }
  55. /**
  56. * Creates the new server dialog if one doesn't exist, and displays it.
  57. */
  58. public static void showNewServerDialog() {
  59. if (me == null) {
  60. me = new NewServerDialog();
  61. } else {
  62. me.setVisible(true);
  63. me.requestFocus();
  64. }
  65. }
  66. /**
  67. * Adds listeners for various objects in the dialog
  68. */
  69. private void addCallbacks() {
  70. jButton2.addActionListener(new ActionListener() {
  71. public void actionPerformed(ActionEvent actionEvent) {
  72. NewServerDialog.this.setVisible(false);
  73. }
  74. });
  75. jButton1.addActionListener(new ActionListener() {
  76. public void actionPerformed(ActionEvent actionEvent) {
  77. String host = jTextField1.getText();
  78. String pass = jTextField3.getText();
  79. int port = Integer.parseInt(jTextField2.getText());
  80. // Open in a new window?
  81. if (jCheckBox1.isSelected()) {
  82. Server server = new Server(host, port, pass);
  83. } else {
  84. }
  85. NewServerDialog.this.setVisible(false);
  86. }
  87. });
  88. }
  89. /** This method is called from within the constructor to
  90. * initialize the form.
  91. * WARNING: Do NOT modify this code. The content of this method is
  92. * always regenerated by the Form Editor.
  93. */
  94. // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
  95. private void initComponents() {
  96. jLabel1 = new javax.swing.JLabel();
  97. jTextField1 = new javax.swing.JTextField();
  98. jLabel2 = new javax.swing.JLabel();
  99. jLabel3 = new javax.swing.JLabel();
  100. jTextField2 = new javax.swing.JTextField();
  101. jLabel4 = new javax.swing.JLabel();
  102. jTextField3 = new javax.swing.JTextField();
  103. jCheckBox1 = new javax.swing.JCheckBox();
  104. jCheckBox2 = new javax.swing.JCheckBox();
  105. jCheckBox3 = new javax.swing.JCheckBox();
  106. jButton1 = new javax.swing.JButton();
  107. jButton2 = new javax.swing.JButton();
  108. setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
  109. setTitle("Connect to a new server");
  110. jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
  111. jLabel1.setText("Server:");
  112. jTextField1.setText("irc.quakenet.org");
  113. jLabel2.setText("To connect to a new IRC server, enter the server name below");
  114. jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
  115. jLabel3.setText("Port:");
  116. jTextField2.setText("6667");
  117. jLabel4.setText("Password:");
  118. jCheckBox1.setText("Open in a new server window");
  119. jCheckBox1.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
  120. jCheckBox1.setMargin(new java.awt.Insets(0, 0, 0, 0));
  121. jCheckBox2.setText("Remember server password");
  122. jCheckBox2.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
  123. jCheckBox2.setMargin(new java.awt.Insets(0, 0, 0, 0));
  124. jCheckBox3.setText("Connect to this server automatically in the future");
  125. jCheckBox3.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
  126. jCheckBox3.setMargin(new java.awt.Insets(0, 0, 0, 0));
  127. jButton1.setText("OK");
  128. jButton2.setText("Cancel");
  129. org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
  130. getContentPane().setLayout(layout);
  131. layout.setHorizontalGroup(
  132. layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  133. .add(layout.createSequentialGroup()
  134. .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  135. .add(layout.createSequentialGroup()
  136. .addContainerGap()
  137. .add(jLabel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  138. .add(layout.createSequentialGroup()
  139. .add(16, 16, 16)
  140. .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
  141. .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  142. .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  143. .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
  144. .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  145. .add(layout.createSequentialGroup()
  146. .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 56, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
  147. .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
  148. .add(jLabel4)
  149. .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
  150. .add(jTextField3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE))
  151. .add(jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)))
  152. .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
  153. .add(232, 232, 232)
  154. .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 76, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
  155. .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
  156. .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 76, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
  157. .add(layout.createSequentialGroup()
  158. .add(28, 28, 28)
  159. .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  160. .add(jCheckBox2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE)
  161. .add(jCheckBox1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE)
  162. .add(jCheckBox3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE))))
  163. .addContainerGap())
  164. );
  165. layout.setVerticalGroup(
  166. layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  167. .add(layout.createSequentialGroup()
  168. .addContainerGap()
  169. .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 23, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
  170. .add(16, 16, 16)
  171. .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
  172. .add(jLabel1)
  173. .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
  174. .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
  175. .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
  176. .add(jLabel3)
  177. .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
  178. .add(jLabel4)
  179. .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
  180. .add(15, 15, 15)
  181. .add(jCheckBox1)
  182. .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
  183. .add(jCheckBox2)
  184. .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
  185. .add(jCheckBox3)
  186. .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 19, Short.MAX_VALUE)
  187. .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
  188. .add(jButton1)
  189. .add(jButton2))
  190. .addContainerGap())
  191. );
  192. pack();
  193. }// </editor-fold>//GEN-END:initComponents
  194. // Variables declaration - do not modify//GEN-BEGIN:variables
  195. private javax.swing.JButton jButton1;
  196. private javax.swing.JButton jButton2;
  197. private javax.swing.JCheckBox jCheckBox1;
  198. private javax.swing.JCheckBox jCheckBox2;
  199. private javax.swing.JCheckBox jCheckBox3;
  200. private javax.swing.JLabel jLabel1;
  201. private javax.swing.JLabel jLabel2;
  202. private javax.swing.JLabel jLabel3;
  203. private javax.swing.JLabel jLabel4;
  204. private javax.swing.JTextField jTextField1;
  205. private javax.swing.JTextField jTextField2;
  206. private javax.swing.JTextField jTextField3;
  207. // End of variables declaration//GEN-END:variables
  208. }
  209. /**
  210. * Verifies that the port number is a valid port
  211. */
  212. class PortVerifier extends InputVerifier {
  213. /**
  214. * Verifies that the number specified in the textfield is a valid port
  215. * @param jComponent The component to be tested
  216. * @return true iff the number is a valid port, false otherwise
  217. */
  218. public boolean verify(JComponent jComponent) {
  219. JTextField tf = (JTextField)jComponent;
  220. try {
  221. Integer port = Integer.parseInt(tf.getText());
  222. return (port > 0 && port <= 65535);
  223. } catch (Exception e) {
  224. return false;
  225. }
  226. }
  227. }