1 package zirc.gui ; 2 3 import java.io.* ; 4 import java.util.* ; 5 import javax.xml.parsers.* ; 6 7 import java.awt.* ; 8 import java.awt.event.* ; 9 import javax.swing.* ; 10 import javax.swing.table.* ; 11 12 import zirc.base.* ; 13 import zirc.xml.* ; 14 15 22 23 24 public class GestionFavori extends JDialog 25 { 26 private JPanel panel1 = new JPanel() ; 27 private BorderLayout borderLayout1 = new BorderLayout() ; 28 private JTabbedPane jTabbedPane1 = new JTabbedPane() ; 29 30 private DefaultTableModel tabChanel = new DefaultTableModel() ; 31 private XmlPars parsChanel = new XmlPars(new String [] 32 {"id", "nom", "des", "serveur", "port"}) ; 33 private ArrayList aChanel = new ArrayList() ; 34 35 private DefaultTableModel tabServ = new DefaultTableModel() ; 36 private XmlPars parsServ = new XmlPars(new String [] 37 {"reseau", "nom", "adress", "port"}) ; 38 private ArrayList aServ = new ArrayList() ; 39 40 private String chanFile = "favoris/chanels.xml" ; 41 private String servFile = "favoris/servers.xml" ; 42 private String serversFile = "fichiers/servers.xml" ; 43 44 JPanel jPanelGestServ = new JPanel() ; 45 JPanel jPanel3 = new JPanel() ; 46 DefaultListModel modelListServ = new DefaultListModel() ; 47 DefaultListModel modelListFav = new DefaultListModel() ; 48 49 BorderLayout borderLayout4 = new BorderLayout() ; 50 JLabel jLabel1 = new JLabel() ; 51 JPanel jPanel4 = new JPanel() ; 52 BorderLayout borderLayout5 = new BorderLayout() ; 53 JButton jButtonSupprimer = new JButton() ; 54 FlowLayout flowLayout1 = new FlowLayout() ; 55 JPanel jPanel2 = new JPanel() ; 56 JButton jButtonAjout = new JButton() ; 57 JLabel jLabel2 = new JLabel() ; 58 JScrollPane jScrollPane3 = new JScrollPane() ; 59 JList jListListServ = new JList() ; 60 JScrollPane jScrollPane4 = new JScrollPane() ; 61 JList jListServFavori = new JList() ; 62 JButton jButton2 = new JButton() ; 63 JButton jButton4 = new JButton() ; 64 JPanel jPanelChanelMenu = new JPanel() ; 65 JScrollPane jScrollPane1 = new JScrollPane() ; 66 BorderLayout borderLayout2 = new BorderLayout() ; 67 JPanel jPanelChanel = new JPanel() ; 68 JTable jTableChanels = new JTable() ; 69 GridLayout gridLayout1 = new GridLayout() ; 70 71 public GestionFavori(Frame frame, String title, boolean modal) 72 { 73 super(frame, title, modal) ; 74 try 75 { 76 jbInit() ; 77 pack() ; 78 } 79 catch (Exception ex) 80 { 81 ex.printStackTrace() ; 82 } 83 } 84 85 public GestionFavori() 86 { 87 this(null, "", false) ; 88 } 89 90 private void jbInit() throws Exception 91 { 92 panel1.setLayout(borderLayout1) ; 93 jPanelGestServ.setLayout(gridLayout1) ; 94 jPanel3.setLayout(borderLayout4) ; 95 jLabel1.setText("Liste des Serveurs") ; 96 jPanel4.setLayout(borderLayout5) ; 97 jButtonSupprimer.setText(" << ") ; 98 jButtonSupprimer.addActionListener(new java.awt.event.ActionListener () 99 { 100 public void actionPerformed(ActionEvent e) 101 { 102 jButtonSupprimer_actionPerformed(e) ; 103 } 104 }) ; 105 flowLayout1.setAlignment(FlowLayout.CENTER) ; 106 flowLayout1.setHgap(1) ; 107 flowLayout1.setVgap(50) ; 108 jPanel2.setLayout(flowLayout1) ; 109 jButtonAjout.setText(" >> ") ; 110 jButtonAjout.addActionListener(new java.awt.event.ActionListener () 111 { 112 public void actionPerformed(ActionEvent e) 113 { 114 jButtonAjout_actionPerformed(e) ; 115 } 116 }) ; 117 jLabel2.setText("Les Serveurs Favoris") ; 118 jListListServ.setModel(modelListServ) ; 119 jButton2.setText("Supprimer") ; 120 jButton2.addActionListener(new java.awt.event.ActionListener () 121 { 122 public void actionPerformed(ActionEvent e) 123 { 124 jButton2_actionPerformed(e) ; 125 } 126 }) ; 127 jButton4.setText("Joindre") ; 128 jPanelChanel.setDebugGraphicsOptions(0) ; 129 jPanelChanel.setLayout(borderLayout2) ; 130 jTableChanels.setModel(tabChanel) ; 131 chargerTabChanel() ; 132 this.setModal(true) ; 133 this.setTitle("") ; 134 jPanel2.setMaximumSize(new Dimension(50, 50)) ; 135 jPanel2.setMinimumSize(new Dimension(50, 50)) ; 136 jPanel2.setPreferredSize(new Dimension(50, 50)) ; 137 getContentPane().add(panel1, BorderLayout.CENTER) ; 138 panel1.add(jTabbedPane1, BorderLayout.CENTER) ; 139 jTabbedPane1.add(jPanelGestServ, "Gestion Serveur") ; 140 141 jPanelGestServ.add(jPanel3, null) ; 142 jPanel3.add(jLabel1, BorderLayout.NORTH) ; 143 jPanel3.add(jScrollPane3, BorderLayout.CENTER) ; 144 jScrollPane3.getViewport().add(jListListServ, null) ; 145 jPanelGestServ.add(jPanel2, null) ; 146 jPanel2.add(jButtonSupprimer, null) ; 147 jPanel2.add(jButtonAjout, null) ; 148 jPanelGestServ.add(jPanel4, null) ; 149 jPanel4.add(jLabel2, BorderLayout.NORTH) ; 150 jPanel4.add(jScrollPane4, BorderLayout.CENTER) ; 151 jTabbedPane1.add(jPanelChanel, " Les Chanels ") ; 152 jPanelChanel.add(jScrollPane1, BorderLayout.CENTER) ; 153 jScrollPane1.getViewport().add(jTableChanels, null) ; 154 jPanelChanel.add(jPanelChanelMenu, BorderLayout.NORTH) ; 155 jPanelChanelMenu.add(jButton4, null) ; 156 jPanelChanelMenu.add(jButton2, null) ; 157 jScrollPane4.getViewport().add(jListServFavori, null) ; 158 jPanel2.add(jButtonSupprimer, null) ; 159 chargerListServ() ; 160 chargerListFavServ() ; 161 jListServFavori.setModel(modelListFav) ; 162 163 } 164 165 public void chargerTabChanel() 166 { 167 aChanel = chargeXmlChan() ; 169 tabChanel.addColumn("id") ; 171 tabChanel.addColumn("nom") ; 172 tabChanel.addColumn("description") ; 173 tabChanel.addColumn("serveur") ; 174 tabChanel.addColumn("port") ; 175 for (int i = 0 ; i < aChanel.size() ; i++) 176 { 177 tabChanel.addRow((String [])aChanel.get(i)) ; 178 } 179 180 } 181 182 public ArrayList chargeXmlChan() 183 { 184 ArrayList chan = new ArrayList() ; 185 parsChanel.aListClear() ; 186 try 188 { 189 SAXParserFactory usine = SAXParserFactory.newInstance() ; 190 SAXParser analyseur = usine.newSAXParser() ; 191 analyseur.parse(new File(chanFile), parsChanel) ; 192 } 193 catch (Exception e) 194 { 195 e.printStackTrace() ; 196 } 197 chan = parsChanel.getAList() ; 199 200 return chan ; 201 } 202 203 public ArrayList chargeXmlServ(String chemin) 204 { 205 ArrayList serv = new ArrayList() ; 206 parsServ.aListClear() ; 207 try 209 { 210 SAXParserFactory usine = SAXParserFactory.newInstance() ; 211 SAXParser analyseur = usine.newSAXParser() ; 212 analyseur.parse(new File(chemin), parsServ) ; 213 } 214 catch (Exception e) 215 { 216 e.printStackTrace() ; 217 } 218 serv = parsServ.getAList() ; 220 221 return serv ; 222 } 223 224 public ArrayList creeLesServers(ArrayList al) 225 { 226 ArrayList servers = new ArrayList() ; 227 228 for (int i = 0 ; i < al.size() ; i++) 229 { 230 String [] st = (String [])al.get(i) ; 231 232 servers.add(new Server(st[0], st[1], st[2], st[3])) ; 233 } 234 235 return servers ; 236 } 237 238 public ArrayList tranServToString(ArrayList _server) 239 { 240 ArrayList enString = new ArrayList() ; 241 242 for (int i = 0 ; i < _server.size() ; i++) 243 { 244 Server serv = (Server)_server.get(i) ; 245 String [] st = new String [] 246 {serv.getReseau(), serv.getNom(), serv.getAdress(), serv.getPort()} ; 247 enString.add(st) ; 248 } 249 250 return enString ; 251 } 252 253 public void chargerListFavServ() 254 { 255 ArrayList ali = creeLesServers(chargeXmlServ(servFile)) ; 256 for (int i = 0 ; i < ali.size() ; i++) 257 { 258 modelListFav.addElement((Server)ali.get(i)) ; 259 } 260 } 261 262 public void chargerListServ() 263 { 264 ArrayList ali = creeLesServers(chargeXmlServ(serversFile)) ; 265 for (int i = 0 ; i < ali.size() ; i++) 266 { 267 modelListServ.addElement((Server)ali.get(i)) ; 268 } 269 } 270 271 public void saveXmlChan(ArrayList _aChanel) 273 { 274 try 275 { 276 BufferedWriter out = new BufferedWriter((new FileWriter(chanFile))) ; 277 out.write("<favori>") ; 278 out.newLine() ; 279 for (int i = 0 ; i < _aChanel.size() ; i++) 280 { 281 String [] s = new String [5] ; 282 s = (String [])_aChanel.get(i) ; 283 out.write(" <chanel>") ; 284 out.newLine() ; 285 out.write(" <id>" + i + "</id>") ; out.newLine() ; 287 out.write(" <nom>" + s[1] + "</nom>") ; 288 out.newLine() ; 289 out.write(" <des>" + s[2] + "</des>") ; 290 out.newLine() ; 291 out.write(" <serveur>" + s[3] + "</serveur>") ; 292 out.newLine() ; 293 out.write(" <port>" + s[4] + "</port>") ; 294 out.newLine() ; 295 out.write(" </chanel>") ; 296 out.newLine() ; 297 } 298 out.write("</favori>") ; 299 out.close() ; 300 } 301 catch (IOException ex) 302 { 303 System.out.println("impossible d'enregistrer sur le fichier") ; 304 } 305 catch (Exception ex) 306 { 307 System.out.println(ex) ; 308 } 309 } 310 311 public void saveXmlServ(ArrayList _aServ) 313 { 314 try 315 { 316 BufferedWriter out = new BufferedWriter((new FileWriter(servFile))) ; 317 out.write("<servers>") ; 318 out.newLine() ; 319 320 for (int i = 0 ; i < _aServ.size() ; i++) 321 { 322 String [] s = new String [4] ; 323 s = (String [])_aServ.get(i) ; 324 out.write(" <infos>") ; 325 out.newLine() ; 326 out.write(" <reseau>" + s[0] + "</reseau>") ; out.newLine() ; 328 out.write(" <nom>" + s[1] + "</nom>") ; 329 out.newLine() ; 330 out.write(" <adress>" + s[2] + "</adress>") ; 331 out.newLine() ; 332 out.write(" <port>" + s[3] + "</port>") ; 333 out.newLine() ; 334 out.write(" </infos>") ; 335 out.newLine() ; 336 } 337 out.write("</servers>") ; 338 out.close() ; 339 } 340 catch (IOException ex) 341 { 342 System.out.println("impossible d'enregistrer sur le fichier") ; 343 } 344 catch (Exception ex) 345 { 346 System.out.println(ex) ; 347 } 348 349 } 350 351 void jButton2_actionPerformed(ActionEvent e) 352 { 353 aChanel.remove(jTableChanels.getSelectedRow()) ; 354 tabChanel.removeRow(jTableChanels.getSelectedRow()) ; 355 356 saveXmlChan(aChanel) ; 357 358 } 359 360 public ArrayList getAServ() 361 { 362 return aServ ; 363 } 364 365 public ArrayList getAChan() 366 { 367 return aChanel ; 368 } 369 370 public void setLAF(String laf) throws UnsupportedLookAndFeelException, IllegalAccessException , InstantiationException , 372 ClassNotFoundException , NullPointerException 373 { 374 UIManager.setLookAndFeel(laf) ; 375 SwingUtilities.updateComponentTreeUI(this) ; 376 } 377 378 void jButtonAjout_actionPerformed(ActionEvent e) 379 { 380 int[] lesSelect = jListListServ.getSelectedIndices() ; 381 for (int i = 0 ; i < lesSelect.length ; i++) 382 { 383 modelListFav.addElement(modelListServ.get(lesSelect[i])) ; 384 } 385 386 ArrayList ali = new ArrayList() ; 387 for (int i = 0 ; i < modelListFav.getSize() ; i++) 388 { 389 ali.add(modelListFav.get(i)) ; 390 } 391 saveXmlServ(tranServToString(ali)) ; 392 393 } 394 395 void jButtonSave_actionPerformed(ActionEvent e) 396 { 397 ArrayList ali = new ArrayList() ; 398 for (int i = 0 ; i < modelListFav.getSize() ; i++) 399 { 400 ali.add(modelListFav.get(i)) ; 401 } 402 saveXmlServ(tranServToString(ali)) ; 403 } 404 405 void jButtonSupprimer_actionPerformed(ActionEvent e) 406 { 407 int[] lesSelect = jListServFavori.getSelectedIndices() ; 408 for (int i = 0 ; i < lesSelect.length ; i++) 409 { 410 modelListFav.remove((lesSelect[i])) ; 411 } 412 413 ArrayList ali = new ArrayList() ; 414 for (int i = 0 ; i < modelListFav.getSize() ; i++) 415 { 416 ali.add(modelListFav.get(i)) ; 417 } 418 saveXmlServ(tranServToString(ali)) ; 419 420 } 421 422 void jTableChanel_caretPositionChanged(InputMethodEvent e) 423 { 424 425 } 426 427 } 428 | Popular Tags |