//M: Código de Eva adaptado (de JInternalFrame a JDialog)
package uk.ac.reload.editor.style;

import java.awt.Font;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import javax.media.CannotRealizeException;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.Time;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;

public class Preview extends JDialog {

    private String path;
    // Variables declaration -
    private javax.swing.JLabel jLabel0;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabelBACK;
    private javax.swing.JLabel jLabelBACKGROUNDIMAGE;
    private javax.swing.JLabel jLabelDOWN;
    private javax.swing.JLabel jLabelEXIT;
    private javax.swing.JLabel jLabelINFO;
    private javax.swing.JLabel jLabelUP;
    // End of variables declaration
    // Vector que contiene los elementos del estilo
    private Vector style = new Vector();
    // Unidad del curso
    private Unit unidadPrincipal;
    // M�sica de fondo del men�
    private Player player;
    // Indica cual es el elemento que tiene el focus
    int focused = 8;
    // Indica cual es el primer elemento de la lista
    int offset = 8;
    // Variables para la representaci�n de los componentes
    private URL dirURLup = null;
    private URL dirURLdown = null;
    String name = null, fontstyle = null, size = null, namef, fontstylef, sizef;
    private String r = null,  g = null,  b = null,  rf = null,  gf = null,  bf = null;
    java.awt.Color colorn;
    java.awt.Color colorf;
    // Namespace asociados al fichero imsmanifest.xml
    Namespace manifestNS;
    Namespace metadataNS;
    Namespace manifestStyle;

    public Preview(java.awt.Frame parent, File projectFolder) {
        super(parent, true);
        this.addWindowListener(new java.awt.event.WindowAdapter() {

            public void windowClosing(java.awt.event.WindowEvent oEvento) {
                JDialogClosing();
            }
        });

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Preview Style Preferences");
        setBounds(new java.awt.Rectangle(300, 200, 500, 500));
        setIconImage(new javax.swing.ImageIcon(getClass().getResource("/uk/ac/reload/editor/resources/images/sconlupa.gif")).getImage());

        path = "file:/" + projectFolder.getPath() + "/";
        Unit[] unidades = readManifest();
        unidadPrincipal = unidades[0];
        style = getStyleValues(path);
        Unit[] subunidades = unidadPrincipal.getSubUnits();
        initComponents(subunidades.length);
    }

    public void JDialogClosing() {
        player.stop();
        this.setVisible(false);
    }

    private Unit[] readManifest() {
        SAXBuilder builder = new SAXBuilder(false);
        org.jdom.Document doc = null;
        try {
            String namem = path + "imsmanifest.xml";
            doc = builder.build(new URL(namem));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //Construyo el arbol en memoria desde el fichero que se lo paso por parametro.
        org.jdom.Element raiz = doc.getRootElement();

        //Cogemos los namespaces
        manifestNS = raiz.getNamespace();
        metadataNS = raiz.getNamespace("imsmd");

        org.jdom.Element organizations = raiz.getChild("organizations", manifestNS);
        List organizationList = organizations.getChildren("organization", manifestNS);
        Unit[] units = addElementsToTree(organizationList, raiz.getChild("resources", manifestNS));

        return units;
    }

    private Unit[] addElementsToTree(List nodeList, org.jdom.Element resourcesNode) {
        List<Unit> units = new LinkedList<Unit>();
        for (Object obj : nodeList) {
            if (obj instanceof org.jdom.Element) {
                org.jdom.Element element = (org.jdom.Element) obj;
                String[] resources = null;
                String resourceID = element.getAttributeValue("identifierref");
                if (resourceID != null) {
                    resources = getResourcesUrls(resourceID, resourcesNode);
                }
                Unit u = new Unit(element.getChild("title", manifestNS).getTextTrim(), getMetadata(element), null, resources);
                Unit[] subUnits = addElementsToTree(element.getChildren("item", manifestNS), resourcesNode);
                u.setSubUnits(subUnits);
                units.add(u);
            }
        }
        return units.toArray(new Unit[0]);
    }

    private String[] getResourcesUrls(String resourceID, org.jdom.Element resourcesNode) {
        List l = resourcesNode.getChildren("resource", manifestNS);
        List<String> resourcesFileNames = new ArrayList<String>();
        for (Object o : l) {
            if (o instanceof org.jdom.Element) {
                org.jdom.Element resource = (org.jdom.Element) o;
                if (resourceID.equals(resource.getAttributeValue("identifier").trim())) {
                    for (Object res : resource.getChildren("file", manifestNS)) {
                        String value = ((org.jdom.Element) res).getAttributeValue("href").trim();
                        if (value != null) resourcesFileNames.add(value);
                    }
                    break;
                }
            }
        }
        if (resourcesFileNames.size() == 0) return null;
        else return resourcesFileNames.toArray(new String[0]);
    }

    private String getMetadata(org.jdom.Element element) {
        org.jdom.Element metadata;
        org.jdom.Element lom;
        org.jdom.Element general;
        if ((metadata = element.getChild("metadata", manifestNS)) != null)
            if ((lom = metadata.getChild("lom", metadataNS)) != null)
                if ((general = lom.getChild("general", metadataNS)) != null) {
                    String description = null;
                    if (general.getChild("description", metadataNS) != null)
                        description = general.getChild("description", metadataNS).getChild("langstring", metadataNS).getTextTrim();
                    return /*title+"\n"+*/ description;
                }
        return null;
    }

    private Vector getStyleValues(String namemanifest) {
        Vector style = new Vector();
        HashMap valueStyle = new HashMap();

        SAXBuilder builder = new SAXBuilder(false);
        org.jdom.Document doc = null;
        try {
            String namem = namemanifest + "imsmanifest.xml";
            doc = builder.build(new URL(namem));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        org.jdom.Element raiz = doc.getRootElement();

        manifestStyle = raiz.getNamespace("idtv");

        //org.jdom.Element e = (Element) raiz.getChildren().get(0);

        org.jdom.Element oo = raiz.getChild("organizations", manifestNS);
        org.jdom.Element o = oo.getChild("organization", manifestNS);
        org.jdom.Element p = o.getChild("style", manifestStyle);

        // Lectura de todos cada elemento de estilo:


        org.jdom.Element element = p.getChild("backgroundimage", manifestStyle);
        valueStyle.put("backgroundimage", element.getText());

        element = p.getChild("backgroundmusic", manifestStyle);
        valueStyle.put("backgroundmusic", element.getText());

        element = p.getChild("exiticon", manifestStyle);
        valueStyle.put("exiticon", element.getText());

        element = p.getChild("gobackicon", manifestStyle);
        valueStyle.put("gobackicon", element.getText());

        element = p.getChild("bulletpoints", manifestStyle);
        valueStyle.put("bulletpoints", element.getText());

        element = p.getChild("scrolluparrow", manifestStyle);
        valueStyle.put("scrolluparrow", element.getText());

        element = p.getChild("scrolldownarrow", manifestStyle);
        valueStyle.put("scrolldownarrow", element.getText());

        element = p.getChild("normalfont", manifestStyle);
        List lista2 = element.getChildren();
        HashMap valuesfont = new HashMap();
        for (Object obj : lista2) {
            if (obj instanceof org.jdom.Element) {
                org.jdom.Element element2 = (org.jdom.Element) obj;
                if (element2.getName().equals("name")) valuesfont.put("name", element2.getText());
                if (element2.getName().equals("fontstyle")) valuesfont.put("fontstyle", element2.getText());
                if (element2.getName().equals("size")) valuesfont.put("size", element2.getText());
            }
        }
        valueStyle.put("normalfont", valuesfont);

        element = p.getChild("normalcolor", manifestStyle);
        lista2 = element.getChildren();
        valuesfont = new HashMap();
        for (Object obj : lista2) {
            if (obj instanceof org.jdom.Element) {
                org.jdom.Element element2 = (org.jdom.Element) obj;
                if (element2.getName().equals("r")) valuesfont.put("r", element2.getText());
                if (element2.getName().equals("g")) valuesfont.put("g", element2.getText());
                if (element2.getName().equals("b")) valuesfont.put("b", element2.getText());
            }
        }
        valueStyle.put("normalcolor", valuesfont);

        element = p.getChild("focusedfont", manifestStyle);
        lista2 = element.getChildren();
        valuesfont = new HashMap();
        for (Object obj : lista2) {
            if (obj instanceof org.jdom.Element) {
                org.jdom.Element element2 = (org.jdom.Element) obj;
                if (element2.getName().equals("name")) valuesfont.put("name", element2.getText());
                if (element2.getName().equals("fontstyle")) valuesfont.put("fontstyle", element2.getText());
                if (element2.getName().equals("size")) valuesfont.put("size", element2.getText());
            }
        }
        valueStyle.put("focusedfont", valuesfont);

        element = p.getChild("focusedcolor", manifestStyle);
        lista2 = element.getChildren();
        valuesfont = new HashMap();
        for (Object obj : lista2) {
            if (obj instanceof org.jdom.Element) {
                org.jdom.Element element2 = (org.jdom.Element) obj;
                if (element2.getName().equals("r")) valuesfont.put("r", element2.getText());
                if (element2.getName().equals("g")) valuesfont.put("g", element2.getText());
                if (element2.getName().equals("b")) valuesfont.put("b", element2.getText());
            }
        }
        valueStyle.put("focusedcolor", valuesfont);

        style.add(valueStyle);
        return style;

    }

    private void myPersonalInitComponents(ImageIcon imagen, Player player) {
        jLabelBACKGROUNDIMAGE.setIcon(imagen);
        startPlayerUnidades(player);
        setFocused(0);
        setFocusable(true);
    }

    private void initComponents(int numItems) {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel0 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabelINFO = new javax.swing.JLabel();
        jLabelEXIT = new javax.swing.JLabel();
        jLabelUP = new javax.swing.JLabel();
        jLabelDOWN = new javax.swing.JLabel();
        jLabelBACK = new javax.swing.JLabel();
        jLabelBACKGROUNDIMAGE = new javax.swing.JLabel();

        //-----------------------------------
        // Lectura de las variables de estilo: 
        String backgroundimage = null;
        String backgroundmusic = null;
        String exiticon = null;
        String gobackicon = null;
        String bulletpoints = null;
        String scrolluparrow = null;
        String scrolldownarrow = null;

        for (int i = 0; i < style.size(); i++) {
            HashMap map = (HashMap) style.get(i);
            backgroundimage = (String) map.get("backgroundimage");
            backgroundmusic = (String) map.get("backgroundmusic");
            exiticon = (String) map.get("exiticon");
            gobackicon = (String) map.get("gobackicon");
            bulletpoints = (String) map.get("bulletpoints");
            scrolluparrow = (String) map.get("scrolluparrow");
            scrolldownarrow = (String) map.get("scrolldownarrow");
            HashMap normalfont = (HashMap) map.get("normalfont");
            HashMap normalcolor = (HashMap) map.get("normalcolor");
            HashMap focusedfont = (HashMap) map.get("focusedfont");
            HashMap focusedcolor = (HashMap) map.get("focusedcolor");

            name = (String) normalfont.get("name");
            fontstyle = (String) normalfont.get("fontstyle");
            size = (String) normalfont.get("size");

            r = (String) normalcolor.get("r");
            g = (String) normalcolor.get("g");
            b = (String) normalcolor.get("b");

            namef = (String) focusedfont.get("name");
            fontstylef = (String) focusedfont.get("fontstyle");
            sizef = (String) focusedfont.get("size");

            rf = (String) focusedcolor.get("r");
            gf = (String) focusedcolor.get("g");
            bf = (String) focusedcolor.get("b");
        }

        colorn = new java.awt.Color(Integer.parseInt(r), Integer.parseInt(g), Integer.parseInt(b));
        colorf = new java.awt.Color(Integer.parseInt(rf), Integer.parseInt(gf), Integer.parseInt(bf));

        // Imagen de fondo del menú       
        URL dirURL = null;
        try {
            dirURL = new URL(path + backgroundimage);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        }
        ImageIcon icono = new javax.swing.ImageIcon(dirURL);

        // Música de fondo del menú
        URL dirPlayer = null;
        try {
            dirPlayer = new URL(path + backgroundmusic);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        Player mediaPlayer = null;
        try {
            mediaPlayer = Manager.createRealizedPlayer(dirPlayer);
        } catch (NoPlayerException e) {
        } catch (CannotRealizeException e) {
        } catch (IOException e) {
        }
        this.player = mediaPlayer;

        // Iconos up&down
        try {
            dirURLup = new URL(path + scrolluparrow);
            dirURLdown = new URL(path + scrolldownarrow);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        }

        // Iconos BulletPoints
        URL dirURLbulletpoints = null;
        try {
            dirURLbulletpoints = new URL(path + bulletpoints);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        }
        ImageIcon iconbulletpoints = new javax.swing.ImageIcon(dirURLbulletpoints);

        //-----------------------------------
        myPersonalInitComponents(icono, mediaPlayer);

        getContentPane().setLayout(new java.awt.GridBagLayout());
        setBackground(new java.awt.Color(191, 191, 255));
        setMaximumSize(new java.awt.Dimension(720, 576));
        setMinimumSize(new java.awt.Dimension(720, 576));
        setPreferredSize(new java.awt.Dimension(720, 576));

        addKeyListener(new java.awt.event.KeyAdapter() {

            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        final java.awt.Color colorNormal = colorn;
        final java.awt.Color colorFocused = colorf;

        if (numItems > 0) {
            if (focused != 0) jLabel0.setFont(new java.awt.Font(name, Integer.parseInt(fontstyle), Integer.parseInt(size)));
            jLabel0.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
            jLabel0.setIcon(iconbulletpoints);
            jLabel0.setMaximumSize(new java.awt.Dimension(300, 50));
            jLabel0.setMinimumSize(new java.awt.Dimension(300, 50));
            jLabel0.setPreferredSize(new java.awt.Dimension(300, 50));
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 2;
            gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
            getContentPane().add(jLabel0, gridBagConstraints);
        }
        if (numItems > 1) {
            if (focused != 1) jLabel1.setFont(new java.awt.Font(name, Integer.parseInt(fontstyle), Integer.parseInt(size)));
            jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
            jLabel1.setIcon(iconbulletpoints);
            jLabel1.setMaximumSize(new java.awt.Dimension(300, 50));
            jLabel1.setMinimumSize(new java.awt.Dimension(300, 50));
            jLabel1.setPreferredSize(new java.awt.Dimension(300, 50));
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 3;
            gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
            //gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
            getContentPane().add(jLabel1, gridBagConstraints);
        }
        if (numItems > 2) {
            if (focused != 2) jLabel2.setFont(new java.awt.Font(name, Integer.parseInt(fontstyle), Integer.parseInt(size)));
            jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
            jLabel2.setIcon(iconbulletpoints);
            jLabel2.setMaximumSize(new java.awt.Dimension(300, 50));
            jLabel2.setMinimumSize(new java.awt.Dimension(300, 50));
            jLabel2.setPreferredSize(new java.awt.Dimension(300, 50));
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 4;
            gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
            //gridBagConstraints.insets = new java.awt.Insets(0, 30, 0, 0);
            getContentPane().add(jLabel2, gridBagConstraints);
        }
        if (numItems > 3) {
            if (focused != 3) jLabel3.setFont(new java.awt.Font(name, Integer.parseInt(fontstyle), Integer.parseInt(size)));
            jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
            jLabel3.setIcon(iconbulletpoints);
            jLabel3.setMaximumSize(new java.awt.Dimension(300, 50));
            jLabel3.setMinimumSize(new java.awt.Dimension(300, 50));
            jLabel3.setPreferredSize(new java.awt.Dimension(300, 50));
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 5;
            gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
            //gridBagConstraints.insets = new java.awt.Insets(0, 60, 0, 0);
            getContentPane().add(jLabel3, gridBagConstraints);
        }
        if (numItems > 4) {
            if (focused != 4) jLabel4.setFont(new java.awt.Font(name, Integer.parseInt(fontstyle), Integer.parseInt(size)));
            jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
            jLabel4.setIcon(iconbulletpoints);
            jLabel4.setMaximumSize(new java.awt.Dimension(300, 50));
            jLabel4.setMinimumSize(new java.awt.Dimension(300, 50));
            jLabel4.setPreferredSize(new java.awt.Dimension(300, 50));
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 6;
            gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
            //gridBagConstraints.insets = new java.awt.Insets(0, 100, 0, 0);
            getContentPane().add(jLabel4, gridBagConstraints);
        }
        if (numItems > 5) {
            if (focused != 5) jLabel5.setFont(new java.awt.Font(name, Integer.parseInt(fontstyle), Integer.parseInt(size)));
            jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
            jLabel5.setIcon(iconbulletpoints);
            jLabel5.setMaximumSize(new java.awt.Dimension(300, 50));
            jLabel5.setMinimumSize(new java.awt.Dimension(300, 50));
            jLabel5.setPreferredSize(new java.awt.Dimension(300, 50));
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 7;
            gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
            //gridBagConstraints.insets = new java.awt.Insets(0, 150, 0, 0);
            getContentPane().add(jLabel5, gridBagConstraints);
        }
        if (numItems > 6) {
            if (focused != 6) jLabel6.setFont(new java.awt.Font(name, Integer.parseInt(fontstyle), Integer.parseInt(size)));
            jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
            jLabel6.setIcon(iconbulletpoints);
            jLabel6.setMaximumSize(new java.awt.Dimension(300, 50));
            jLabel6.setMinimumSize(new java.awt.Dimension(300, 50));
            jLabel6.setPreferredSize(new java.awt.Dimension(300, 50));
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 8;
            gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
            //gridBagConstraints.insets = new java.awt.Insets(0, 210, 0, 0);
            getContentPane().add(jLabel6, gridBagConstraints);
        }
        if (numItems > 7) {
            if (focused != 7) jLabel7.setFont(new java.awt.Font(name, Integer.parseInt(fontstyle), Integer.parseInt(size)));
            jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
            jLabel7.setIcon(iconbulletpoints);
            jLabel7.setMaximumSize(new java.awt.Dimension(300, 50));
            jLabel7.setMinimumSize(new java.awt.Dimension(300, 50));
            jLabel7.setPreferredSize(new java.awt.Dimension(300, 50));
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 9;
            gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
            //gridBagConstraints.insets = new java.awt.Insets(0, 280, 0, 0);
            getContentPane().add(jLabel7, gridBagConstraints);
        }
        jLabelINFO.setBackground(new java.awt.Color(255, 255, 255));
        jLabelINFO.setBorder(javax.swing.BorderFactory.createMatteBorder(2, 2, 2, 2, new java.awt.Color(0, 0, 0)));
        jLabelINFO.setMaximumSize(new java.awt.Dimension(500, 60));
        jLabelINFO.setMinimumSize(new java.awt.Dimension(500, 60));
        jLabelINFO.setOpaque(true);
        jLabelINFO.setPreferredSize(new java.awt.Dimension(500, 60));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(4, 40, 0, 40);
        getContentPane().add(jLabelINFO, gridBagConstraints);

        URL dirURLicon = null;
        try {
            dirURLicon = new URL(path + exiticon);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        }

        jLabelEXIT.setIcon(new javax.swing.ImageIcon(dirURLicon));
        jLabelEXIT.setBorder(javax.swing.BorderFactory.createMatteBorder(4, 4, 4, 4, new java.awt.Color(255, 0, 0)));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 0, 20);
        getContentPane().add(jLabelEXIT, gridBagConstraints);

        jLabelUP.setMaximumSize(new java.awt.Dimension(26, 26));
        jLabelUP.setMinimumSize(new java.awt.Dimension(26, 26));
        jLabelUP.setPreferredSize(new java.awt.Dimension(26, 26));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(20, 100, 0, 0);
        getContentPane().add(jLabelUP, gridBagConstraints);

        jLabelDOWN.setMaximumSize(new java.awt.Dimension(26, 26));
        jLabelDOWN.setMinimumSize(new java.awt.Dimension(26, 26));
        jLabelDOWN.setPreferredSize(new java.awt.Dimension(26, 26));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 10;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 100, 0, 0);
        getContentPane().add(jLabelDOWN, gridBagConstraints);

        try {
            dirURLicon = new URL(path + gobackicon);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        }
        jLabelBACK.setIcon(new javax.swing.ImageIcon(dirURLicon));
        jLabelBACK.setBorder(javax.swing.BorderFactory.createMatteBorder(4, 4, 4, 4, new java.awt.Color(0, 204, 0)));
        jLabelBACK.setMaximumSize(new java.awt.Dimension(45, 62));
        jLabelBACK.setMinimumSize(new java.awt.Dimension(45, 62));
        jLabelBACK.setPreferredSize(new java.awt.Dimension(45, 62));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(2, 20, 0, 0);
        getContentPane().add(jLabelBACK, gridBagConstraints);

        jLabelBACKGROUNDIMAGE.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabelBACKGROUNDIMAGE.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLabelBACKGROUNDIMAGE.setMaximumSize(new java.awt.Dimension(720, 554));
        jLabelBACKGROUNDIMAGE.setMinimumSize(new java.awt.Dimension(720, 554));
        jLabelBACKGROUNDIMAGE.setPreferredSize(new java.awt.Dimension(720, 554));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.gridheight = 11;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        getContentPane().add(jLabelBACKGROUNDIMAGE, gridBagConstraints);
        pack();
    }

    // Acciones relacionadas con los eventos del teclado   
    private void formKeyPressed(java.awt.event.KeyEvent evt) {
        switch (evt.getKeyCode()) {
            case KeyEvent.VK_UP:
                goUP();
                break;
            case KeyEvent.VK_DOWN:
                goDOWN();
                break;
        }
    }

    private void goUP() {
        setFocused(focused - 1);
    }

    private void goDOWN() {
        setFocused(focused + 1);
    }

    // Establece el "foco" en un determinado objeto
    private void setFocused(int f) {

        Unit[] subunidades = unidadPrincipal.getSubUnits();
        if (f < 0 || f >= subunidades.length) return;
        setUnitsCells(f);

        getLabel(focused).setForeground(colorn);
        getLabel(focused).setFont(new Font(name, Integer.parseInt(fontstyle), Integer.parseInt(size)));
        getLabel(f).setForeground(colorf);
        getLabel(f).setFont(new Font(namef, Integer.parseInt(fontstylef), Integer.parseInt(sizef)));

        jLabelINFO.setText("<html><center>&nbsp;&nbsp;&nbsp;" + unidadPrincipal.getName().toUpperCase() + " <font size=+2><b>\u2192</b></font> " +
                subunidades[f].getName().toUpperCase() + ":   " +
                subunidades[f].getDescription() + "</center></html>");

        focused = f;
    }

    // Funciones del reproductor
    public void stopPlayerUnidades() {
        if (player != null) {
            player.stop();
            player.setMediaTime(new Time(0));
        }
    }

    private void startPlayerUnidades(Player p) {
        if (p != null) p.start();
    }

    private void pausePlayerUnidades() {
        if (player != null) player.stop();
    }

    // Obtiene la etiqueta correspondiente a un determinado indice
    private JLabel getLabel(int i) {
        switch ((i - offset) % 8) { // i-offset debe ser SIEMPRE >= 0
            case 0:
                return jLabel0;
            case 1:
                return jLabel1;
            case 2:
                return jLabel2;
            case 3:
                return jLabel3;
            case 4:
                return jLabel4;
            case 5:
                return jLabel5;
            case 6:
                return jLabel6;
            case 7:
                return jLabel7;
            default:
                return null;
        }
    }

    // Establece los elementos visibles en pantalla
    private void setUnitsCells(int elemento) {

        if (elemento >= offset && elemento < offset + 8) return;
        Unit[] subunidades = unidadPrincipal.getSubUnits();
        if (elemento < offset) offset = elemento;
        else if (elemento >= offset + 8) offset = elemento - 7;

        int ini = offset;
        int finNOR = offset + 8;
        int finR = subunidades.length;
        int fin = Math.min(finR, finNOR);

        for (int i = ini; i < fin; i++) {
            getLabel(i).setText(subunidades[i].getName());
            getLabel(i).setForeground(colorn);
        }

        for (int i = finR; i < finNOR; i++) {
            getLabel(i).setText(null);
            getLabel(i).setIcon(null);
        }

        if (offset > 0) jLabelUP.setIcon(new javax.swing.ImageIcon(dirURLup));
        else jLabelUP.setIcon(null);
        if (offset + 7 < subunidades.length - 1) jLabelDOWN.setIcon(new javax.swing.ImageIcon(dirURLdown));
        else jLabelDOWN.setIcon(null);
    }
}

