/*
 * Clase creada por Mart�n, modificada para su integracion con la herramienta.
 *
 *
 */
package com.uvigo.tMaestro.SCOCreator.tool.properties.cellEditors.contentEditors;

import com.uvigo.tMaestro.SCOCreator.tool.MainFrame;
import com.uvigo.tMaestro.SCOCreator.tool.MyFileFilter;
import com.uvigo.tMaestro.SCOCreator.tool.ImageView;
import com.uvigo.tMaestro.SCOCreator.tool.MyOptionPane;


import java.awt.*;
import java.beans.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import javax.xml.bind.*;
import com.uvigo.mhp.xml.activeImage.*;

public class ActiveImageContentEditorPanel extends JPanel implements ContentEditorPanel {

    private DefaultTableModel tableModel;
    private JAXBContext jc;
    private ObjectFactory objFactory;
    private ActiveImageConfiguration conf;
    private java.util.List zones;
    private MediaTracker trk;
    //Ruta hasta el directorio de la aplicacion
    private String sBase;
    //Ruta desde sBase hasta el directorio en donde esta el fichero xml
    private String sRuta;
    //Nombre del fichero .xml
    private String sFile;
    //Ruta al directorio del componente (dentro del directorio del proyecto). Desde /Projects
    private String sDir;
    //Listado de ficheros anadidos desde este panel en algun momento.
    //Guarda tanto los actuales como los descartados
    private java.util.ArrayList<java.io.File> oAddedFiles = new java.util.ArrayList<java.io.File>();
    /**
     * Referencia al dialogo que va a contener este panel
     */
    private ContentEditorDialog oDialog;
   
    
    /**
     * Constructor
     * @param sPathFile Ruta relativa al directorio de la aplicaci�n, del fichero .xml
     * @param oDialog Referenci al dialogo contenedor de este panel.
     */
    public ActiveImageContentEditorPanel(String sPathFile, ContentEditorDialog oDialog) {
          if ((sPathFile==null)||(sPathFile.isEmpty())) sPathFile="RESOURCES/ActiveImageCE.xml";//M: inicializaci�n
        initComponents();
        this.oDialog = oDialog;
        jPanel20.setBorder(new javax.swing.border.TitledBorder(MainFrame.oMainFrame.getMessage("Border5")));
        jPanel4.setBorder(new javax.swing.border.TitledBorder(MainFrame.oMainFrame.getMessage("Border6")));
        jPanel5.setBorder(new javax.swing.border.TitledBorder(MainFrame.oMainFrame.getMessage("Title21")));
        jButton10.setText(MainFrame.oMainFrame.getMessage("Button7"));
        jButton12.setText(MainFrame.oMainFrame.getMessage("Button6"));
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{},
                new String[]{
            MainFrame.oMainFrame.getMessage("Title21"),
            MainFrame.oMainFrame.getMessage("Title19"),
            MainFrame.oMainFrame.getMessage("Title20")
        }) {

            Class[] types = new Class[]{
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean[]{
                false, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });


        try {
            jc = JAXBContext.newInstance("com.uvigo.mhp.xml.activeImage");
            objFactory = new ObjectFactory();
            conf = objFactory.createActiveImageConfiguration();
            zones = conf.getActiveZone();
            tableModel = (DefaultTableModel) jTable1.getModel();
            trk = new MediaTracker(this);
            jScrollPane2.getViewport().setBackground(Color.white);
            jScrollPane2.getViewport().setOpaque(true);
            //M: ahora..
            sBase= new String (MainFrame.oMainFrame.sDir+ "/");
            sDir= new String("TEMP/COMPONENTS/");
             
            if (sPathFile != null) {
                //Parte de sPathFile que corresponde a ruta (que no es fichero)
                String[] split = sPathFile.split("/");
                sRuta = new String();
                int z = 0;
                for (z = 0; z < split.length - 1; z++) {
                    sRuta = sRuta.concat(split[z] + "/");
                }

                //Parte de sPathFile que corresponde al fichero
                sFile = split[split.length - 1];

                load(new java.io.File(sBase + sPathFile));
            }
        } catch (JAXBException ex) {
            System.out.println(ex.getMessage());
        }


    }

    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel13 = new javax.swing.JPanel();
        jButton10 = new javax.swing.JButton();
        jButton12 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jPanel20 = new javax.swing.JPanel();
        jPanel21 = new javax.swing.JPanel();
        jButton17 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jPanel22 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jLabel1 = new javax.swing.JLabel();
        jButton15 = new javax.swing.JButton();
        jTextField3 = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jLabel2 = new javax.swing.JLabel();

        setLayout(new java.awt.BorderLayout());

        jPanel13.setLayout(new java.awt.GridLayout(1, 0));

        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });

        jPanel13.add(jButton10);

        jButton12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton12ActionPerformed(evt);
            }
        });

        jPanel13.add(jButton12);

        add(jPanel13, java.awt.BorderLayout.SOUTH);

        jPanel3.setLayout(new java.awt.BorderLayout());

        jPanel20.setLayout(new java.awt.BorderLayout());

        jPanel21.setLayout(new java.awt.GridLayout(1, 0));

        jButton17.setText("+");
        jButton17.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton17ActionPerformed(evt);
            }
        });

        jPanel21.add(jButton17);

        jButton1.setText("-");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel21.add(jButton1);

        jPanel20.add(jPanel21, java.awt.BorderLayout.SOUTH);

        jPanel1.setLayout(new java.awt.BorderLayout());

        jScrollPane2.setBackground(new java.awt.Color(255, 255, 255));
        jScrollPane2.setAutoscrolls(true);
        jTable1.setCellSelectionEnabled(true);
        jTable1.setEditingColumn(1);
        jTable1.setEditingRow(1);
        jTable1.setOpaque(false);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });

        jScrollPane2.setViewportView(jTable1);

        jPanel1.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPanel2.setLayout(new java.awt.GridLayout(0, 1));

        jButton2.setText("<");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jPanel2.add(jButton2);

        jButton3.setText(">");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jPanel2.add(jButton3);

        jPanel1.add(jPanel2, java.awt.BorderLayout.WEST);

        jPanel20.add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel3.add(jPanel20, java.awt.BorderLayout.CENTER);

        jPanel22.setLayout(new java.awt.BorderLayout());

        jPanel4.setLayout(new java.awt.BorderLayout());

        jPanel4.setMinimumSize(new java.awt.Dimension(295, 225));
        jPanel4.setPreferredSize(new java.awt.Dimension(295, 225));
        jScrollPane1.setMinimumSize(new java.awt.Dimension(295, 175));
        jScrollPane1.setPreferredSize(new java.awt.Dimension(295, 175));
        jScrollPane1.setViewportView(jLabel1);

        jPanel4.add(jScrollPane1, java.awt.BorderLayout.NORTH);

        jButton15.setText("...");
        jButton15.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton15ActionPerformed(evt);
            }
        });

        jPanel4.add(jButton15, java.awt.BorderLayout.EAST);

        jTextField3.setColumns(20);
        jTextField3.setEditable(false);
        jPanel4.add(jTextField3, java.awt.BorderLayout.CENTER);

        jPanel22.add(jPanel4, java.awt.BorderLayout.WEST);

        jPanel5.setLayout(new java.awt.BorderLayout());

        jPanel5.setMinimumSize(new java.awt.Dimension(295, 225));
        jPanel5.setPreferredSize(new java.awt.Dimension(295, 225));
        jScrollPane3.setMinimumSize(new java.awt.Dimension(295, 225));
        jScrollPane3.setPreferredSize(new java.awt.Dimension(295, 225));
        jScrollPane3.setViewportView(jLabel2);

        jPanel5.add(jScrollPane3, java.awt.BorderLayout.CENTER);

        jPanel22.add(jPanel5, java.awt.BorderLayout.EAST);

        jPanel3.add(jPanel22, java.awt.BorderLayout.SOUTH);

        add(jPanel3, java.awt.BorderLayout.CENTER);

    }
    // </editor-fold>//GEN-END:initComponents
    private void jButton15ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton15ActionPerformed
    {//GEN-HEADEREND:event_jButton15ActionPerformed
        // Configurar la imagen principal

        javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
        MyFileFilter oFilter = new MyFileFilter();
        oFilter.vAddExtension("jpeg");
        oFilter.vAddExtension("jpg");
        oFilter.vSetDescripcion(".jpeg");
        chooser.addChoosableFileFilter(oFilter);
        oFilter = new MyFileFilter();
        oFilter.vAddExtension("gif");
        oFilter.vSetDescripcion(".gif");
        chooser.addChoosableFileFilter(oFilter);
        oFilter = new MyFileFilter();
        oFilter.vAddExtension("png");
        oFilter.vSetDescripcion(".png");
        chooser.addChoosableFileFilter(oFilter);
        oFilter = new MyFileFilter();
        oFilter.vAddExtension("tif");
        oFilter.vAddExtension("tiff");
        oFilter.vSetDescripcion(".tiff");
        chooser.addChoosableFileFilter(oFilter);
        oFilter = new MyFileFilter();
        oFilter.vAddExtension("tif");
        oFilter.vAddExtension("tiff");
        oFilter.vAddExtension("jpeg");
        oFilter.vAddExtension("jpg");
        oFilter.vAddExtension("png");
        oFilter.vAddExtension("gif");
        oFilter.vSetDescripcion(".jpeg .tiff .gif .png");
        chooser.addChoosableFileFilter(oFilter);
        chooser.setAccessory(new ImageView(chooser));
        chooser.setCurrentDirectory(MainFrame.oMainFrame.getLastDir());
        boolean bExit = false;
        while (!bExit) {
            int option = chooser.showOpenDialog(this);
            if (option == javax.swing.JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                //Si existe un fichero con el mismo nombre entre los elementos que el usuario ha configurado, lo avisamos
                if (new java.io.File(sBase + sDir, file.getName()).exists()) {
                    int iOption = MyOptionPane.showConfirmDialog(MainFrame.oMainFrame, MainFrame.oMainFrame.getMessage("Question4"), MainFrame.oMainFrame.getMessage("Title2"), javax.swing.JOptionPane.YES_NO_OPTION, javax.swing.JOptionPane.WARNING_MESSAGE);
                    if (iOption == javax.swing.JOptionPane.NO_OPTION) continue;
                }
                bExit = true;
                try {
                    setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR));
                    MainFrame.oMainFrame.setLastDir(file.getParentFile());
                    java.io.File oDirComponent = new java.io.File(sBase + sDir);
                    oDirComponent.mkdir();
                    java.io.File oMainImage = new java.io.File(oDirComponent, file.getName());
                    MainFrame.copyFile(file, oMainImage);
                    oAddedFiles.add(oMainImage);

                    URL url = file.toURL();
                    Image temp = Toolkit.getDefaultToolkit().getImage(url);
                    trk.addImage(temp, 0);
                    trk.waitForAll();

                    conf.setBackgroundImage(file.getName());
                    jTextField3.setText(file.getName());
                    jTextField3.setCaretPosition(0);
                    jLabel1.setIcon(new ImageIcon(temp));
                } catch (Exception ex) {
                    System.out.println(ex);
                }
                setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.DEFAULT_CURSOR));
            } else bExit = true;
        }
    }//GEN-LAST:event_jButton15ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton2ActionPerformed
    {//GEN-HEADEREND:event_jButton2ActionPerformed
        // Hay que desplazar una fila hacia arriba
        int i = jTable1.getSelectedRow();
        if (i <= 0) return;

        tableModel.moveRow(i, i, i - 1);
        Object ob1 = zones.get(i);
        Object ob2 = zones.get(i - 1);
        zones.set(i, ob2);
        zones.set(i - 1, ob1);

        jTable1.setColumnSelectionInterval(0, 0);
        jTable1.setRowSelectionInterval(i - 1, i - 1);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton3ActionPerformed
    {//GEN-HEADEREND:event_jButton3ActionPerformed
        // Hay que desplazar una fila hacia abajo

        int i = jTable1.getSelectedRow();
        if (i >= tableModel.getRowCount() - 1) return;

        tableModel.moveRow(i, i, i + 1);
        Object ob1 = zones.get(i);
        Object ob2 = zones.get(i + 1);
        zones.set(i, ob2);
        zones.set(i + 1, ob1);

        jTable1.setColumnSelectionInterval(0, 0);
        jTable1.setRowSelectionInterval(i + 1, i + 1);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
    {//GEN-HEADEREND:event_jButton1ActionPerformed
        // Eliminar la fila actual
        int i = jTable1.getSelectedRow();
        if (i == -1) return;

        tableModel.removeRow(i);
        zones.remove(i);

        if (jTable1.getRowCount() > 0) {
            jTable1.setColumnSelectionInterval(0, 0);
            if (i == jTable1.getRowCount())
                jTable1.setRowSelectionInterval(i - 1, i - 1);
            else
                jTable1.setRowSelectionInterval(i, i);
        }
        select(jTable1.getSelectedRow());
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton17ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton17ActionPerformed
    {//GEN-HEADEREND:event_jButton17ActionPerformed
        // Insertar una fila
        javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
        MyFileFilter oFilter = new MyFileFilter();
        oFilter.vAddExtension("jpeg");
        oFilter.vAddExtension("jpg");
        oFilter.vSetDescripcion(".jpeg");
        chooser.addChoosableFileFilter(oFilter);
        oFilter = new MyFileFilter();
        oFilter.vAddExtension("gif");
        oFilter.vSetDescripcion(".gif");
        chooser.addChoosableFileFilter(oFilter);
        oFilter = new MyFileFilter();
        oFilter.vAddExtension("png");
        oFilter.vSetDescripcion(".png");
        chooser.addChoosableFileFilter(oFilter);
        oFilter = new MyFileFilter();
        oFilter.vAddExtension("tif");
        oFilter.vAddExtension("tiff");
        oFilter.vSetDescripcion(".tiff");
        chooser.addChoosableFileFilter(oFilter);
        oFilter = new MyFileFilter();
        oFilter.vAddExtension("tif");
        oFilter.vAddExtension("tiff");
        oFilter.vAddExtension("jpeg");
        oFilter.vAddExtension("jpg");
        oFilter.vAddExtension("png");
        oFilter.vAddExtension("gif");
        oFilter.vSetDescripcion(".jpeg .tiff .gif .png");
        chooser.addChoosableFileFilter(oFilter);
        chooser.setAccessory(new ImageView(chooser));
        chooser.setCurrentDirectory(MainFrame.oMainFrame.getLastDir());

        boolean bExit = false;
        while (!bExit) {
            int option = chooser.showOpenDialog(this);
            if (option == javax.swing.JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
               //Si existe un fichero con el mismo nombre entre los elementos que el usuario ha configurado, lo avisamos
                if (new java.io.File(sBase + sDir, file.getName()).exists()) {
                    int iOption = MyOptionPane.showConfirmDialog(MainFrame.oMainFrame, MainFrame.oMainFrame.getMessage("Question4"), MainFrame.oMainFrame.getMessage("Title2"), javax.swing.JOptionPane.YES_NO_OPTION, javax.swing.JOptionPane.WARNING_MESSAGE);
                    if (iOption == javax.swing.JOptionPane.NO_OPTION) continue;
                }

                bExit = true;
                try {
                    setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR));
                    MainFrame.oMainFrame.setLastDir(file.getParentFile());
                    java.io.File oDirComponent = new java.io.File(sBase + sDir);
                    oDirComponent.mkdir();
                    java.io.File oImage = new java.io.File(oDirComponent, file.getName());
                    MainFrame.copyFile(file, oImage);
                    oAddedFiles.add(oImage);

                    Object temp[] = new Object[3];
                    temp[0] = new String(file.getName());
                    temp[1] = new String("");
                    temp[2] = new String("");
                    tableModel.addRow(temp);

                    ActiveZoneType azt = objFactory.createActiveZoneType();
                    azt.setLocator(file.getName());
                    azt.setContextName("");
                    azt.setComment("");

                    zones.add(azt);

                    jTable1.setColumnSelectionInterval(0, 0);
                    jTable1.setRowSelectionInterval(jTable1.getRowCount() - 1, jTable1.getRowCount() - 1);
                    select(jTable1.getRowCount() - 1);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.DEFAULT_CURSOR));
            } else bExit = true;
        }
    }//GEN-LAST:event_jButton17ActionPerformed

    /**
     * El usuario pulsa sobre cancelar
     */
    private void jButton12ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton12ActionPerformed
    {//GEN-HEADEREND:event_jButton12ActionPerformed
        oDialog.setAction(ContentEditorDialog.CANCEL, null);
        oDialog.dispose();
    }//GEN-LAST:event_jButton12ActionPerformed

    /**
     * Carga un fichero .xml
     */
    private void load(java.io.File file) {
        // Hay que cargar el contenido de un fichero XML.
        try {
            if (file == null) return;

            int i = tableModel.getRowCount();
            for (int j = 0; j < i; j++)
                tableModel.removeRow(0);

            Unmarshaller u = jc.createUnmarshaller();
            u.setValidating(true);
            conf = (ActiveImageConfiguration) u.unmarshal(new FileInputStream(file));

            URL url = new URL("file:/" + sBase + sRuta + conf.getBackgroundImage());
            Image oImage = Toolkit.getDefaultToolkit().getImage(url);
            
            trk.addImage(oImage, 0);
            trk.waitForAll();

            jTextField3.setText(conf.getBackgroundImage());
            jLabel1.setIcon(new ImageIcon(oImage));
            zones = conf.getActiveZone();

            for (int k = 0; k < zones.size(); k++) {
                Object temp[] = new Object[3];
                temp[0] = ((ActiveZoneType) zones.get(k)).getLocator();
                temp[1] = ((ActiveZoneType) zones.get(k)).getContextName();
                temp[2] = ((ActiveZoneType) zones.get(k)).getComment();

                tableModel.addRow(temp);
            }

            if (tableModel.getRowCount() == 0) return;

            jTable1.setColumnSelectionInterval(0, 0);
            jTable1.setRowSelectionInterval(0, 0);
            select(0);
        } catch (Exception ex) {
            System.out.println(ex);
        }

    }

    /**
     * El usuario ha pulsado sobre OK
     */
    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton10ActionPerformed
    {//GEN-HEADEREND:event_jButton10ActionPerformed
        // Hay que grabar un fichero XML con el contenido creado, en el
        // directorio del proyecto (dentro del directorio de componente).
        // Ademas hay que copiar las imagenes al mismo directorio

        if (jTextField3.getText().equals("")) return;


        try {
            setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR));
            java.io.File oDirComponent = new java.io.File(sBase + sDir);
            oDirComponent.mkdir();
            if (!sFile.contains("(")) sFile = new String("(" + MainFrame.oMainFrame.getPSheet().getSelectedOption() + ")" +
                        MainFrame.oMainFrame.oGetCapsulaActiva().oGetBean().getName() + ".xml");//M: le damos el nombre del componente
            java.io.File oFile = new File(oDirComponent, sFile);
            java.util.ArrayList<java.io.File> oFiles = new java.util.ArrayList<java.io.File>();

            for (int i = 0; i < tableModel.getRowCount(); i++) {
                ActiveZoneType azt = (ActiveZoneType) zones.get(i);
                String sImage = (String) tableModel.getValueAt(i, 0);
                azt.setLocator(sImage);
                azt.setContextName((String) tableModel.getValueAt(i, 1));
                azt.setComment((String) tableModel.getValueAt(i, 2));
                //Copiamos las imagenes secundarias
                java.io.File oComponentFile = new java.io.File(oDirComponent, sImage);
                oFiles.add(oComponentFile);
                if (!oComponentFile.exists()) {
                    MainFrame.copyFile(new java.io.File(sBase + sRuta + sImage), oComponentFile);
                }
            }

            conf.setBackgroundImage(jTextField3.getText());
            //Copiamos la imagen principal
            java.io.File oComponentFile = new java.io.File(oDirComponent, jTextField3.getText());
            oFiles.add(oComponentFile);
            if (!oComponentFile.exists()) {
                MainFrame.copyFile(new java.io.File(sBase + sRuta + jTextField3.getText()), oComponentFile);
            }

            Marshaller m = jc.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            oFiles.add(oFile);
            m.marshal(conf, new FileOutputStream(oFile));
            MainFrame.oMainFrame.getEditionProperty().putOptionFiles(oFiles, MainFrame.oMainFrame.getPSheet().getSelectedOption());
            oDialog.setAction(ContentEditorDialog.OK, sDir + sFile);
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println(ex);
            oDialog.setAction(ContentEditorDialog.CANCEL, null); // Si se produce alg�n error es como cancelar el proceso
        }
        setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.DEFAULT_CURSOR));
        oDialog.dispose();
    }//GEN-LAST:event_jButton10ActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_jTable1MouseClicked
    {//GEN-HEADEREND:event_jTable1MouseClicked
        // Cuando se selecciona una fila de la tabla hay que mostrar las imagenes asociadas
        Point p = new Point(evt.getX(), evt.getY());
        int fil = jTable1.rowAtPoint(p);
        if (fil == -1) return;
        select(fil);
    }//GEN-LAST:event_jTable1MouseClicked

    /**
     * Muestra la imagen de indice i en el panel dedicado a visualizar los localizadores
     */
    private void select(int i) {
        if (i == -1) return;
        try {
            String sImage = (String) tableModel.getValueAt(i, 0);

            //O la imagen se encuentra en el directorio del componente o se encuentra
            //en el mismo directorio que el fichero .xml
            java.net.URL oURL = new java.net.URL("file:/" + sBase + sDir + sImage);
            Image oImage = null;
            //Hacemos esto en lugar de verificar si el fichero existe (oFile.exists())
            //porque por algun motivo que desconozco la funcion exists en algunas ocasiones
            //existiendo el fichero devuelve un false. Asa nos curamos en salud.
            oImage = Toolkit.getDefaultToolkit().createImage(oURL); //Ruta relativa
            trk.addImage(oImage, 0);
            trk.waitForAll();
            if (oImage.getWidth(null) == -1) {
                oImage = Toolkit.getDefaultToolkit().createImage(new URL("file:/" + sBase + sRuta + sImage));
               trk.addImage(oImage, 0);
                trk.waitForAll();
            }


            jLabel2.setIcon(new ImageIcon(oImage));
        } catch (Exception oE) {
            oE.printStackTrace();
        }
    }

    /**
     * Devuelve los ficheros anadidos
     */
    public java.util.ArrayList<java.io.File> getFiles() {
        return oAddedFiles;
    }

    public Dimension getPreferredSize() {
        return new Dimension(600, 400);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton15;
    private javax.swing.JButton jButton17;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel20;
    private javax.swing.JPanel jPanel21;
    private javax.swing.JPanel jPanel22;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables
}
