Txerrinko

examen SWING XML

Jun 15th, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.55 KB | None | 0 0
  1. package ExamenSwingXML;
  2.  
  3. import org.jdom2.*;
  4. import org.jdom2.input.*;
  5. import org.jdom2.output.*;
  6. import java.util.*;
  7.  
  8.  
  9. import javax.swing.*;
  10. import javax.swing.event.ListSelectionEvent;
  11. import javax.swing.event.ListSelectionListener;
  12.  
  13. import java.awt.BorderLayout;
  14. import java.awt.FlowLayout;
  15. import java.awt.GridLayout;
  16. import java.awt.event.*;
  17. import java.io.FileWriter;
  18. import java.io.IOException;
  19. public class GestorMusical extends JFrame implements ActionListener {
  20.  
  21.     /**
  22.      * @param args
  23.      */
  24.    
  25.     PadresitoModal pm;
  26.    
  27.     static Document doc;
  28.     JLabel selMus,selAut,pistEl;
  29.     JRadioButton[] autores;
  30.     JComboBox listaPistas;
  31.     JButton verPistas;
  32.     JLabel imgAutor;
  33.     JList pistasAlbum;
  34.     JTextArea pistasElegidas;
  35.     JButton guardar,reset;
  36.     String nomFich="albumes.xml";
  37.    
  38.    
  39.     Pista [] p;
  40.    
  41.     public void parsear() throws JDOMException, IOException{
  42.         SAXBuilder builder=new SAXBuilder();
  43.         doc=builder.build(nomFich);
  44.        
  45.     }
  46.     public void ver() throws IOException{
  47.         XMLOutputter salida=new XMLOutputter(Format.getPrettyFormat());
  48.         //FileWriter fw=new FileWriter("albumes.xml");
  49.         salida.output(doc,  System.out);
  50.     }
  51.    
  52.     public void borraPista(Pista pis){
  53.        
  54.         Pista []aux=new Pista[p.length-1];
  55.         int contPis=0;
  56.         for (int i=0; i<p.length;i++){
  57.            
  58.           if (!pis.comparaPista(p[i])){
  59.              
  60.               aux[contPis]=p[i];
  61.               contPis++;  
  62.           }
  63.                
  64.         }
  65.        
  66.     p=aux; 
  67.        
  68.     }
  69.    
  70.    
  71.    
  72.     /*
  73.     public boolean  borraPista(Pista pis){
  74.        
  75.         Pista []aux=new Pista[p.length-1];
  76.         int contLetras=0;
  77.        
  78.         for (int i=0; i<p.length;i++){
  79.            
  80.           if (p[i].getTitulo().equals(pis.getTitulo()) && p[i].getMinutos()
  81.                   ==pis.getMinutos() && p[i].getSegundos()==pis.getMinutos()){
  82.              
  83.               aux[contLetras]=p[i];
  84.               contLetras++;  
  85.          return true;
  86.           }
  87.                
  88.         }
  89.        
  90.       p=aux;
  91.    
  92.     return false;
  93.     }
  94.     */
  95.     public ArrayList <String> todosAutores() throws JDOMException, IOException{
  96.        
  97.        ArrayList <String> todosAutores=new ArrayList<String>();
  98.    
  99.         Element raiz=doc.getRootElement();
  100.        
  101.         List<Element> autores=raiz.getChildren();
  102.        
  103.         Iterator it=autores.iterator();
  104.        
  105.         Element e1;
  106.        
  107.        
  108.         while(it.hasNext()){
  109.            
  110.             e1=(Element) it.next();
  111.            
  112.         String  auts=e1.getAttributeValue("autor");
  113.  
  114.         if (!todosAutores.contains(auts)){
  115.             todosAutores.add(auts);
  116.         }
  117.        
  118.         }
  119.         return todosAutores;       
  120.     }
  121.    
  122.     public static ArrayList<CD> cdsDeAutor(String nomAutor) throws JDOMException, IOException{
  123.        
  124.           ArrayList <CD> cdsAutor=new ArrayList<CD>();
  125.             Element raiz=doc.getRootElement();
  126.             List<Element> autores=raiz.getChildren();
  127.            
  128.             Iterator it=autores.iterator();
  129.            
  130.             Element e1;
  131.             while(it.hasNext()){
  132.                
  133.                 e1=(Element) it.next();
  134.                
  135.                 String auts=e1.getAttributeValue("autor");
  136.                
  137.                
  138.            if (auts.equals(nomAutor)){
  139.                  
  140.                    String titcds=e1.getChildText("titulo");
  141.                   String img= e1.getChildText("imagen");
  142.                  
  143.                    Element pistasel=e1.getChild("pistas");
  144.                    //cogemos solo el elemento pistas del autor que hayamos pedido
  145.                    
  146.                  List <Element> pistas=pistasel.getChildren("pista");
  147.                    //cogemos todos  sus hijos (cada pista)
  148.                  
  149.                  Pista[]  pistArray=new Pista[pistas.size()];
  150.                    //le paso el tamaño de las pistas al array
  151.                    
  152.                    Iterator it2=pistas.iterator();
  153.                    int contPistas=0;
  154.                    
  155.                    Element e2;
  156.                    while (it2.hasNext()){
  157.                        
  158.                        e2=(Element) it2.next();
  159.                        
  160.                        String tit=e2.getText();
  161.                        String dur=e2.getAttributeValue("duracion");
  162.                        String min=dur.substring(0,dur.indexOf(':'));
  163.                        String seg=dur.substring(dur.indexOf(':')+1);
  164.                        //por cada posicion vamos metiendo una pista en el array
  165.                        
  166.                        contPistas++;
  167.                        
  168.                        pistArray[contPistas-1]=new Pista(tit,
  169.                                  Integer.parseInt(min),Integer.parseInt(seg));
  170.                    
  171.                    }  
  172.                        cdsAutor.add(new CD (auts,titcds,
  173.                             img,pistArray));         
  174.            }
  175.         }
  176.             return cdsAutor;
  177.     }
  178.    
  179.  
  180.    
  181.     public  void  crearBotones () throws JDOMException, IOException{
  182.         ArrayList <String> botones=todosAutores();
  183.         String [] nombres=new String[botones.size()];
  184.        
  185.         for (int i=0;i<nombres.length;i++){
  186.               nombres [i]=botones.get(i);
  187.         }
  188.         //le pasamos la cantidad de nombres array de JButton
  189.         autores=new JRadioButton[nombres.length];
  190.        
  191.         for (int i=0;i<autores.length;i++){
  192.            
  193.             autores[i]=new JRadioButton(nombres[i]);
  194.             autores[i].addActionListener(new ActionListener(){
  195.  
  196.                 @Override
  197.                 public void actionPerformed(ActionEvent e) {
  198.                     // TODO Auto-generated method stub
  199.                    
  200.                     try {
  201.                         //arraylist de cs cogemos lo que nos han pasado
  202.                         Object seleccionado=e.getSource();
  203.                 //obtenemos el autor seleccionado del radioButton
  204.                         ArrayList<CD> cd=cdsDeAutor(((JRadioButton) seleccionado).getText());
  205.                        
  206.                         //sacamos el JList
  207.                    
  208.                        
  209.                        
  210.                         listaPistas.setEnabled(true);
  211.                         listaPistas.removeAllItems();
  212.                         verPistas.setEnabled(true);
  213.                    
  214.                         //pistasAlbum.setEnabled(true);
  215.                         pistasAlbum.setVisible(true);
  216.                        
  217.                         for(int i=0;i<cd.size();i++){
  218.                             //añadimos al JComboBox
  219.                             listaPistas.addItem(cd.get(i));
  220.                         }
  221.                      }
  222.                      catch (JDOMException e1) {
  223.                         // TODO Auto-generated catch block
  224.                         e1.printStackTrace();
  225.                     } catch (IOException e1) {
  226.                         // TODO Auto-generated catch block
  227.                         e1.printStackTrace();
  228.                     }
  229.                 }
  230.             }
  231.             );
  232.        
  233.         }
  234.        
  235.        
  236.     }  
  237.        
  238.     public GestorMusical(String nFich) throws JDOMException, IOException{
  239.        
  240.         super("Organizador de musica");
  241.         setSize(700,400);
  242.          nomFich=nFich;
  243.         pm=new PadresitoModal(this);
  244.         JPanel p1=new JPanel();
  245.         selMus=new JLabel("SELECTOR MUSICAL");
  246.         p1.add(selMus);
  247.        
  248.         JPanel p2=new JPanel();
  249.         p2.setLayout(new BoxLayout(p2,BoxLayout.Y_AXIS));
  250.         selAut=new JLabel("Selecciona un autor");
  251.         p2.add(selAut);
  252.         parsear();
  253.         crearBotones();
  254.    
  255.         ButtonGroup grupo=new ButtonGroup();
  256.        
  257.         for (int i=0;i<autores.length;i++){
  258.             grupo.add(autores[i]);
  259.         }
  260.    
  261.        p2.add(autores[0]);
  262.        p2.add(autores[1]);
  263.        p2.add(autores[2]);
  264.        
  265.    
  266.         JPanel p3=new JPanel();
  267.         p3.setLayout(null);
  268.         //de primeras  todo estara oculto
  269.         listaPistas=new JComboBox();
  270.         listaPistas.setBounds(70,0,160,40);
  271.         listaPistas.setEnabled(false);
  272.        
  273.         verPistas=new JButton("Ver Pistas");
  274.         verPistas.setBounds(250, 0, 100,40);
  275.         verPistas.setEnabled(false);
  276.         //escuchador aparte
  277.         verPistas.addActionListener(new EscuchadorAutor());
  278.    
  279.         imgAutor=new JLabel("Imagen");
  280.         imgAutor.setBounds(200,40,200,120);
  281.        
  282.         pistasAlbum=new JList();
  283.         pistasAlbum.setBounds(150, 150, 200,200);
  284.         pistasAlbum.setVisible(false);
  285.        
  286.        
  287.        
  288.         pistasAlbum.addMouseListener(new MouseListener(){
  289.  
  290.             @Override
  291.             public void mouseClicked(MouseEvent e) {
  292.                //nos devuelve la pista
  293.                 //pistasAlbum.setVisible(true);
  294.                  Pista pp=(Pista) pistasAlbum.getSelectedValue();
  295.                  //obtenemos lo que hay en las pistas que hemos seleccionado
  296.                  //y despues lo mostramos en el mismo añadiendo las que hayamos escogido
  297.                  
  298.                    borraPista(pp);                       
  299.                       //establecemos la lista de albumes a las que tenemos
  300.                       //de la ultima vez que hemos añadido
  301.                     pistasAlbum.setListData(p);
  302.                          
  303.                          
  304.              //nos creamos un objeto pista auxiliar que tenga
  305.                  //la longitud de la pista -1
  306.              
  307.                  //a pistaselegidas le vamos añadiendo  las que seleccionamos
  308.                 // del JList mas la que tenemos obtenidad en el JTextArea
  309.               pistasElegidas.setText(pp.toString()+pistasElegidas.getText());
  310.              
  311.             }
  312.  
  313.             @Override
  314.             public void mouseEntered(MouseEvent arg0) {
  315.                 // TODO Auto-generated method stub
  316.                
  317.             }
  318.  
  319.             @Override
  320.             public void mouseExited(MouseEvent arg0) {
  321.                 // TODO Auto-generated method stub
  322.                
  323.             }
  324.  
  325.             @Override
  326.             public void mousePressed(MouseEvent arg0) {
  327.                 // TODO Auto-generated method stub
  328.                
  329.             }
  330.  
  331.             @Override
  332.             public void mouseReleased(MouseEvent arg0) {
  333.                 // TODO Auto-generated method stub
  334.                
  335.             }
  336.  
  337.             });
  338.         p3.add(listaPistas);
  339.         p3.add(verPistas);
  340.         p3.add(imgAutor);
  341.         p3.add(pistasAlbum);
  342.        
  343.         JPanel p4=new JPanel();
  344.         p4.setLayout(new BoxLayout(p4,BoxLayout.Y_AXIS));
  345.         pistEl=new JLabel("Pistas Elegidas");
  346.         pistasElegidas=new JTextArea();
  347.         pistasElegidas.setEnabled(true);
  348.         pistasElegidas.setEditable(false);
  349.         p4.add(pistEl);
  350.         p4.add(pistasElegidas);
  351.        
  352.        
  353.         JPanel p5=new JPanel();
  354.         p5.setLayout(new GridLayout(1,2));
  355.    
  356.        
  357.         guardar=new JButton("GUARDAR");
  358.         guardar.addActionListener(this);
  359.    
  360.    
  361.         reset=new JButton("RESET");
  362.         reset.addActionListener(this);
  363.         p5.add(guardar);
  364.         p5.add(reset);
  365.        
  366.        
  367.        
  368.        
  369.         add(p1,BorderLayout.NORTH);
  370.         add(p2,BorderLayout.WEST);
  371.         add(p3,BorderLayout.CENTER);
  372.         add(p4,BorderLayout.EAST);
  373.         add(p5,BorderLayout.SOUTH);
  374.        
  375.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  376.         setVisible(true);
  377.        
  378.     }
  379.    
  380.    
  381.    
  382.    
  383.     public static void main(String[] args) throws JDOMException, IOException {
  384.         // TODO Auto-generated method stub
  385.   GestorMusical gm=new GestorMusical("albumes.xml");
  386.   gm.parsear();
  387.  
  388.    
  389.     ArrayList <CD> cds=cdsDeAutor("Madonna");      
  390.     for (int i=0;i<cds.size();i++){
  391.         CD c=cds.get(i);
  392.         System.out.println(c.toString());
  393.     }
  394.    
  395.  
  396. }
  397.     public class EscuchadorAutor implements ActionListener {
  398.  
  399.         @Override
  400.         public void actionPerformed(ActionEvent e) {
  401.             // TODO Auto-generated method stub
  402.  
  403.             Object seleccionado=e.getSource();
  404.            
  405.             if (seleccionado==verPistas){
  406.          
  407.                 //cargar la imagen
  408.                 //JComboBox
  409.                
  410.                 //creamos objeto cd pasandole
  411.                 //la lista de pistas seleccionadas
  412.                 CD sel=(CD) listaPistas.getSelectedItem();
  413.                
  414.                 //obtenemos la imagen
  415.                 imgAutor.setIcon(new ImageIcon(sel.getImagen()));
  416.                
  417.              //al objeto Pista le pasamos las pistas seleccionadas
  418.             p=sel.getPist();
  419.                 //p nos da el array de y set listdata toda la cantidad de pistas
  420.             pistasAlbum.setListData(p);
  421.                
  422.                
  423.    
  424.             }
  425.         }
  426.  
  427.     }
  428.  
  429.    
  430.    
  431.    
  432.    
  433.     public void actionPerformed(ActionEvent e) {
  434.         // TODO Auto-generated method stub
  435.        
  436.         Object seleccionado=e.getSource();
  437.        
  438.         if (seleccionado==guardar){
  439.             pm.setVisible(true);
  440.            
  441.         }else if (seleccionado==reset){
  442.            
  443.             //vaciar JList
  444.             DefaultListModel model = new DefaultListModel();
  445.             pistasAlbum.setModel(model);
  446.             pistasAlbum.setVisible(false);
  447.             verPistas.setEnabled(false);
  448.             imgAutor.setIcon(null);
  449.             pistasElegidas.setText("");
  450.             listaPistas.setEnabled(false);
  451.             listaPistas.removeAllItems();
  452.         }
  453.        
  454.        
  455.     }
  456. }
Advertisement
Add Comment
Please, Sign In to add comment