1 package zirc.gui ; 2 3 import java.io.* ; 4 import java.util.* ; 5 6 import java.awt.* ; 7 import java.awt.event.* ; 8 import javax.swing.* ; 9 import javax.swing.border.* ; 10 11 24 31 32 public class ServerEditDialog extends JDialog 33 { 34 private Locale language ; 35 private final File serverList = new File("fichiers/servers.ini") ; 36 private ArrayList al ; private String [][] list ; 38 private DefaultComboBoxModel cm = new DefaultComboBoxModel() ; private String name, description, server, port ; 40 private JLabel jLabel1 = new JLabel() ; 41 private JLabel jLabel2 = new JLabel() ; 42 private JTextField jtDescription = new JTextField() ; 43 private JLabel jLabel3 = new JLabel() ; 44 private JTextField jtServer = new JTextField() ; 45 private JLabel jLabel4 = new JLabel() ; 46 private JTextField jtPort = new JTextField() ; 47 private JButton jbNewServer = new JButton() ; 48 private JButton jbSaveServer = new JButton() ; 49 private JButton jbExitServer = new JButton() ; 50 private JButton jbDeleteServer = new JButton() ; 51 private JComboBox jcName = new JComboBox() ; 52 private Border border1 = BorderFactory.createLineBorder(Color.gray) ; 53 54 59 public ServerEditDialog(OptionDialog owner, Locale _language) 60 { 61 super(owner) ; 62 language = _language ; 63 try 64 { 65 jbInit() ; 66 setLanguage(language) ; 67 } 68 catch (Exception e) 69 { 70 e.printStackTrace() ; 71 } 72 } 73 74 private void jbInit() throws Exception 75 { 76 jcName.setBorder(border1) ; 77 jcName.setEditable(true) ; 78 this.setSize(new Dimension(336, 219)) ; 79 this.addWindowFocusListener(new java.awt.event.WindowFocusListener () 80 { 81 public void windowGainedFocus(WindowEvent e) 82 { 83 } 84 85 public void windowLostFocus(WindowEvent e) 86 { 87 this_windowLostFocus(e) ; 88 } 89 }) ; 90 this.getContentPane().setLayout(null) ; 91 jLabel1.setText("Description: ") ; 92 jLabel1.setBounds(new Rectangle(12, 15, 80, 15)) ; 93 jLabel2.setBounds(new Rectangle(12, 51, 80, 15)) ; 94 jLabel2.setText("Nom:") ; 95 jtDescription.setBorder(border1) ; 96 jtDescription.setText("") ; 97 jtDescription.setBounds(new Rectangle(95, 48, 227, 21)) ; 98 jLabel3.setBounds(new Rectangle(12, 88, 80, 15)) ; 99 jLabel3.setText("Serveur: ") ; 100 jtServer.setBounds(new Rectangle(95, 84, 227, 21)) ; 101 jtServer.setBorder(border1) ; 102 jtServer.setToolTipText("") ; 103 jtServer.setText("") ; 104 jLabel4.setBounds(new Rectangle(12, 124, 80, 15)) ; 105 jLabel4.setText("Port(s): ") ; 106 jtPort.setBorder(border1) ; 107 jtPort.setText("") ; 108 jtPort.setBounds(new Rectangle(95, 120, 227, 21)) ; 109 jtPort.addKeyListener(new java.awt.event.KeyAdapter () 110 { 111 public void keyTyped(KeyEvent e) 112 { 113 jtPort_keyTyped(e) ; 114 } 115 }) ; 116 jbNewServer.setBounds(new Rectangle(8, 166, 83, 25)) ; 117 jbNewServer.setBorder(border1) ; 118 jbNewServer.setPreferredSize(new Dimension(95, 25)) ; 119 jbNewServer.setText("Nouveau") ; 120 jbNewServer.addActionListener(new java.awt.event.ActionListener () 121 { 122 public void actionPerformed(ActionEvent e) 123 { 124 jbNewServer_actionPerformed(e) ; 125 } 126 }) ; 127 this.setResizable(false) ; 128 this.setTitle("Server edition") ; 129 jbSaveServer.setText("Sauver") ; 130 jbSaveServer.addActionListener(new java.awt.event.ActionListener () 131 { 132 public void actionPerformed(ActionEvent e) 133 { 134 jbSaveServer_actionPerformed(e) ; 135 } 136 }) ; 137 jbSaveServer.setBounds(new Rectangle(96, 166, 69, 25)) ; 138 jbSaveServer.setBorder(border1) ; 139 jbExitServer.setText("Quitter") ; 140 jbExitServer.addActionListener(new java.awt.event.ActionListener () 141 { 142 public void actionPerformed(ActionEvent e) 143 { 144 jbExitServer_actionPerformed(e) ; 145 } 146 }) ; 147 jbExitServer.setBounds(new Rectangle(170, 166, 61, 25)) ; 148 jbExitServer.setBorder(border1) ; 149 jbDeleteServer.setBounds(new Rectangle(236, 166, 90, 25)) ; 150 jbDeleteServer.setBorder(border1) ; 151 jbDeleteServer.setText("Supprimer") ; 152 jbDeleteServer.addActionListener(new java.awt.event.ActionListener () 153 { 154 public void actionPerformed(ActionEvent e) 155 { 156 jbDeleteServer_actionPerformed(e) ; 157 } 158 }) ; 159 jcName.setBounds(new Rectangle(96, 14, 227, 21)) ; 160 jcName.addActionListener(new java.awt.event.ActionListener () 161 { 162 public void actionPerformed(ActionEvent e) 163 { 164 jcName_actionPerformed(e) ; 165 } 166 }) ; 167 this.getContentPane().add(jLabel1, null) ; 168 this.getContentPane().add(jtDescription, null) ; 169 this.getContentPane().add(jLabel2, null) ; 170 this.getContentPane().add(jtServer, null) ; 171 this.getContentPane().add(jLabel3, null) ; 172 this.getContentPane().add(jtPort, null) ; 173 this.getContentPane().add(jLabel4, null) ; 174 this.getContentPane().add(jcName, null) ; 175 this.getContentPane().add(jbNewServer, null) ; 176 this.getContentPane().add(jbSaveServer, null) ; 177 this.getContentPane().add(jbExitServer, null) ; 178 this.getContentPane().add(jbDeleteServer, null) ; 179 } 180 181 190 public void loadList() 191 { 192 cm.removeAllElements() ; 193 al = new ArrayList() ; 194 BufferedReader in = null ; 195 String line ; 196 197 try 198 { 199 in = new BufferedReader(new FileReader(serverList)) ; 200 while ((line = in.readLine()) != null) 202 { 203 al.add(line) ; 204 } 205 206 list = new String [al.size()][4] ; 208 for (int i = 0 ; i < list.length ; i++) 209 { 210 StringTokenizer str = new StringTokenizer(al.get(i).toString(), ":") ; 211 for (int j = 0 ; j < list[0].length ; j++) 212 { 213 list[i][j] = str.nextElement().toString() ; 214 } 215 } 216 } 217 catch (FileNotFoundException ex) 218 { 219 ex.printStackTrace() ; 220 } 221 catch (IOException ex1) 222 { 223 ex1.printStackTrace() ; 224 } 225 226 for (int i = 0 ; i < list.length ; i++) 228 { 229 cm.addElement(list[i][0]) ; 230 } 231 jcName.setModel(cm) ; 232 } 233 234 246 public void setLAF(String laf) throws UnsupportedLookAndFeelException, IllegalAccessException , InstantiationException , 247 ClassNotFoundException , NullPointerException 248 { 249 UIManager.setLookAndFeel(laf) ; 250 SwingUtilities.updateComponentTreeUI(this) ; 251 } 252 253 259 void jbNewServer_actionPerformed(ActionEvent e) 260 { 261 jcName.setSelectedIndex( -1) ; 262 jtDescription.setText("") ; 263 jtServer.setText("") ; 264 jtPort.setText("") ; 265 } 266 267 274 void jbSaveServer_actionPerformed(ActionEvent e) 275 { 276 PrintWriter out = null ; 277 278 try 279 { out = new PrintWriter(new BufferedWriter(new FileWriter(serverList, true))) ; 281 out.println(jcName.getSelectedItem() + ": " + jtDescription.getText() + 282 ":" + 283 jtServer.getText() + ":" + jtPort.getText()) ; 284 out.flush() ; 285 } 286 catch (IOException ex) 287 { 288 ex.printStackTrace() ; 289 System.err.println("io error") ; 290 } 291 finally 292 { 293 out.close() ; 294 loadList() ; 295 } 296 } 297 298 304 void jbExitServer_actionPerformed(ActionEvent e) 305 { 306 this.dispose() ; 307 } 308 309 316 void jbDeleteServer_actionPerformed(ActionEvent e) 317 { 318 jtDescription.setText("") ; 319 jtServer.setText("") ; 320 jtPort.setText("") ; 321 al.remove(cm.getIndexOf(cm.getSelectedItem())) ; 323 saveFile() ; loadList() ; } 326 327 331 private void saveFile() 332 { 333 PrintWriter out = null ; 334 try 335 { 336 out = new PrintWriter(new BufferedWriter(new FileWriter(serverList, false))) ; 337 for (int i = 0 ; i < al.size() ; i++) 338 { 339 out.println(al.get(i).toString()) ; 340 out.flush() ; 341 } 342 } 343 catch (IOException ex) 344 { 345 ex.printStackTrace() ; 346 } 347 finally 348 { 349 out.close() ; 350 loadList() ; 351 } 352 } 353 354 361 void jcName_actionPerformed(ActionEvent e) 362 { 363 try 364 { 365 int i = cm.getIndexOf(cm.getSelectedItem()) ; 366 jtDescription.setText(list[i][1]) ; 367 jtServer.setText(list[i][2]) ; 368 jtPort.setText(list[i][3]) ; 369 } 370 catch (ArrayIndexOutOfBoundsException ex) 371 { 372 } 374 } 375 376 384 void jtPort_keyTyped(KeyEvent e) 385 { 386 allowInteger(e) ; 387 } 388 389 397 public void allowInteger(KeyEvent e) 398 { 399 if (e.getKeyChar() != 8 && (e.getKeyChar() < 48 || e.getKeyChar() > 57)) 400 { 401 if (e.getKeyChar() != 44 && e.getKeyChar() != 45) 402 { 403 e.setKeyChar((char)(0)) ; 404 } 405 } 406 } 407 408 public void setLanguage(Locale lang) 409 { 410 ResourceBundle messages = ResourceBundle.getBundle("ServerDialog", lang) ; 411 jLabel2.setText(messages.getString("nom")) ; 412 jLabel3.setText(messages.getString("serveur")) ; 413 jbNewServer.setText(messages.getString("jbnouveau")) ; 414 jbSaveServer.setText(messages.getString("jbsauver")) ; 415 jbExitServer.setText(messages.getString("jbquitter")) ; 416 jbDeleteServer.setText(messages.getString("jbsupprimer")) ; 417 418 } 419 420 void this_windowLostFocus(WindowEvent e) 421 { 422 } 424 } 425 | Popular Tags |