Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ExamenSwingXML;
- import org.jdom2.*;
- import org.jdom2.input.*;
- import org.jdom2.output.*;
- import java.util.*;
- import javax.swing.*;
- import javax.swing.event.ListSelectionEvent;
- import javax.swing.event.ListSelectionListener;
- import java.awt.BorderLayout;
- import java.awt.FlowLayout;
- import java.awt.GridLayout;
- import java.awt.event.*;
- import java.io.FileWriter;
- import java.io.IOException;
- public class GestorMusical extends JFrame implements ActionListener {
- /**
- * @param args
- */
- PadresitoModal pm;
- static Document doc;
- JLabel selMus,selAut,pistEl;
- JRadioButton[] autores;
- JComboBox listaPistas;
- JButton verPistas;
- JLabel imgAutor;
- JList pistasAlbum;
- JTextArea pistasElegidas;
- JButton guardar,reset;
- String nomFich="albumes.xml";
- Pista [] p;
- public void parsear() throws JDOMException, IOException{
- SAXBuilder builder=new SAXBuilder();
- doc=builder.build(nomFich);
- }
- public void ver() throws IOException{
- XMLOutputter salida=new XMLOutputter(Format.getPrettyFormat());
- //FileWriter fw=new FileWriter("albumes.xml");
- salida.output(doc, System.out);
- }
- public void borraPista(Pista pis){
- Pista []aux=new Pista[p.length-1];
- int contPis=0;
- for (int i=0; i<p.length;i++){
- if (!pis.comparaPista(p[i])){
- aux[contPis]=p[i];
- contPis++;
- }
- }
- p=aux;
- }
- /*
- public boolean borraPista(Pista pis){
- Pista []aux=new Pista[p.length-1];
- int contLetras=0;
- for (int i=0; i<p.length;i++){
- if (p[i].getTitulo().equals(pis.getTitulo()) && p[i].getMinutos()
- ==pis.getMinutos() && p[i].getSegundos()==pis.getMinutos()){
- aux[contLetras]=p[i];
- contLetras++;
- return true;
- }
- }
- p=aux;
- return false;
- }
- */
- public ArrayList <String> todosAutores() throws JDOMException, IOException{
- ArrayList <String> todosAutores=new ArrayList<String>();
- Element raiz=doc.getRootElement();
- List<Element> autores=raiz.getChildren();
- Iterator it=autores.iterator();
- Element e1;
- while(it.hasNext()){
- e1=(Element) it.next();
- String auts=e1.getAttributeValue("autor");
- if (!todosAutores.contains(auts)){
- todosAutores.add(auts);
- }
- }
- return todosAutores;
- }
- public static ArrayList<CD> cdsDeAutor(String nomAutor) throws JDOMException, IOException{
- ArrayList <CD> cdsAutor=new ArrayList<CD>();
- Element raiz=doc.getRootElement();
- List<Element> autores=raiz.getChildren();
- Iterator it=autores.iterator();
- Element e1;
- while(it.hasNext()){
- e1=(Element) it.next();
- String auts=e1.getAttributeValue("autor");
- if (auts.equals(nomAutor)){
- String titcds=e1.getChildText("titulo");
- String img= e1.getChildText("imagen");
- Element pistasel=e1.getChild("pistas");
- //cogemos solo el elemento pistas del autor que hayamos pedido
- List <Element> pistas=pistasel.getChildren("pista");
- //cogemos todos sus hijos (cada pista)
- Pista[] pistArray=new Pista[pistas.size()];
- //le paso el tamaño de las pistas al array
- Iterator it2=pistas.iterator();
- int contPistas=0;
- Element e2;
- while (it2.hasNext()){
- e2=(Element) it2.next();
- String tit=e2.getText();
- String dur=e2.getAttributeValue("duracion");
- String min=dur.substring(0,dur.indexOf(':'));
- String seg=dur.substring(dur.indexOf(':')+1);
- //por cada posicion vamos metiendo una pista en el array
- contPistas++;
- pistArray[contPistas-1]=new Pista(tit,
- Integer.parseInt(min),Integer.parseInt(seg));
- }
- cdsAutor.add(new CD (auts,titcds,
- img,pistArray));
- }
- }
- return cdsAutor;
- }
- public void crearBotones () throws JDOMException, IOException{
- ArrayList <String> botones=todosAutores();
- String [] nombres=new String[botones.size()];
- for (int i=0;i<nombres.length;i++){
- nombres [i]=botones.get(i);
- }
- //le pasamos la cantidad de nombres array de JButton
- autores=new JRadioButton[nombres.length];
- for (int i=0;i<autores.length;i++){
- autores[i]=new JRadioButton(nombres[i]);
- autores[i].addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- try {
- //arraylist de cs cogemos lo que nos han pasado
- Object seleccionado=e.getSource();
- //obtenemos el autor seleccionado del radioButton
- ArrayList<CD> cd=cdsDeAutor(((JRadioButton) seleccionado).getText());
- //sacamos el JList
- listaPistas.setEnabled(true);
- listaPistas.removeAllItems();
- verPistas.setEnabled(true);
- //pistasAlbum.setEnabled(true);
- pistasAlbum.setVisible(true);
- for(int i=0;i<cd.size();i++){
- //añadimos al JComboBox
- listaPistas.addItem(cd.get(i));
- }
- }
- catch (JDOMException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- }
- );
- }
- }
- public GestorMusical(String nFich) throws JDOMException, IOException{
- super("Organizador de musica");
- setSize(700,400);
- nomFich=nFich;
- pm=new PadresitoModal(this);
- JPanel p1=new JPanel();
- selMus=new JLabel("SELECTOR MUSICAL");
- p1.add(selMus);
- JPanel p2=new JPanel();
- p2.setLayout(new BoxLayout(p2,BoxLayout.Y_AXIS));
- selAut=new JLabel("Selecciona un autor");
- p2.add(selAut);
- parsear();
- crearBotones();
- ButtonGroup grupo=new ButtonGroup();
- for (int i=0;i<autores.length;i++){
- grupo.add(autores[i]);
- }
- p2.add(autores[0]);
- p2.add(autores[1]);
- p2.add(autores[2]);
- JPanel p3=new JPanel();
- p3.setLayout(null);
- //de primeras todo estara oculto
- listaPistas=new JComboBox();
- listaPistas.setBounds(70,0,160,40);
- listaPistas.setEnabled(false);
- verPistas=new JButton("Ver Pistas");
- verPistas.setBounds(250, 0, 100,40);
- verPistas.setEnabled(false);
- //escuchador aparte
- verPistas.addActionListener(new EscuchadorAutor());
- imgAutor=new JLabel("Imagen");
- imgAutor.setBounds(200,40,200,120);
- pistasAlbum=new JList();
- pistasAlbum.setBounds(150, 150, 200,200);
- pistasAlbum.setVisible(false);
- pistasAlbum.addMouseListener(new MouseListener(){
- @Override
- public void mouseClicked(MouseEvent e) {
- //nos devuelve la pista
- //pistasAlbum.setVisible(true);
- Pista pp=(Pista) pistasAlbum.getSelectedValue();
- //obtenemos lo que hay en las pistas que hemos seleccionado
- //y despues lo mostramos en el mismo añadiendo las que hayamos escogido
- borraPista(pp);
- //establecemos la lista de albumes a las que tenemos
- //de la ultima vez que hemos añadido
- pistasAlbum.setListData(p);
- //nos creamos un objeto pista auxiliar que tenga
- //la longitud de la pista -1
- //a pistaselegidas le vamos añadiendo las que seleccionamos
- // del JList mas la que tenemos obtenidad en el JTextArea
- pistasElegidas.setText(pp.toString()+pistasElegidas.getText());
- }
- @Override
- public void mouseEntered(MouseEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseExited(MouseEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mousePressed(MouseEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseReleased(MouseEvent arg0) {
- // TODO Auto-generated method stub
- }
- });
- p3.add(listaPistas);
- p3.add(verPistas);
- p3.add(imgAutor);
- p3.add(pistasAlbum);
- JPanel p4=new JPanel();
- p4.setLayout(new BoxLayout(p4,BoxLayout.Y_AXIS));
- pistEl=new JLabel("Pistas Elegidas");
- pistasElegidas=new JTextArea();
- pistasElegidas.setEnabled(true);
- pistasElegidas.setEditable(false);
- p4.add(pistEl);
- p4.add(pistasElegidas);
- JPanel p5=new JPanel();
- p5.setLayout(new GridLayout(1,2));
- guardar=new JButton("GUARDAR");
- guardar.addActionListener(this);
- reset=new JButton("RESET");
- reset.addActionListener(this);
- p5.add(guardar);
- p5.add(reset);
- add(p1,BorderLayout.NORTH);
- add(p2,BorderLayout.WEST);
- add(p3,BorderLayout.CENTER);
- add(p4,BorderLayout.EAST);
- add(p5,BorderLayout.SOUTH);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
- }
- public static void main(String[] args) throws JDOMException, IOException {
- // TODO Auto-generated method stub
- GestorMusical gm=new GestorMusical("albumes.xml");
- gm.parsear();
- ArrayList <CD> cds=cdsDeAutor("Madonna");
- for (int i=0;i<cds.size();i++){
- CD c=cds.get(i);
- System.out.println(c.toString());
- }
- }
- public class EscuchadorAutor implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- Object seleccionado=e.getSource();
- if (seleccionado==verPistas){
- //cargar la imagen
- //JComboBox
- //creamos objeto cd pasandole
- //la lista de pistas seleccionadas
- CD sel=(CD) listaPistas.getSelectedItem();
- //obtenemos la imagen
- imgAutor.setIcon(new ImageIcon(sel.getImagen()));
- //al objeto Pista le pasamos las pistas seleccionadas
- p=sel.getPist();
- //p nos da el array de y set listdata toda la cantidad de pistas
- pistasAlbum.setListData(p);
- }
- }
- }
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- Object seleccionado=e.getSource();
- if (seleccionado==guardar){
- pm.setVisible(true);
- }else if (seleccionado==reset){
- //vaciar JList
- DefaultListModel model = new DefaultListModel();
- pistasAlbum.setModel(model);
- pistasAlbum.setVisible(false);
- verPistas.setEnabled(false);
- imgAutor.setIcon(null);
- pistasElegidas.setText("");
- listaPistas.setEnabled(false);
- listaPistas.removeAllItems();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment