/*
 * ContentEditor.java
 *
 */
package com.uvigo.tMaestro.SCOCreator.tool.properties.cellEditors;

import com.uvigo.mhp.xml.mediaPlayer.MediaList;
import com.uvigo.mhp.xml.mediaPlayer.ObjectFactory;
import com.uvigo.mhp.xml.mediaPlayer.ResourceType;
import com.uvigo.tMaestro.SCOCreator.tool.Code;
import com.uvigo.tMaestro.SCOCreator.tool.properties.cellEditors.contentEditors.*;
import com.uvigo.tMaestro.SCOCreator.tool.properties.Propiedad;
import com.uvigo.tMaestro.SCOCreator.tool.properties.CellEditor;
import com.uvigo.tMaestro.SCOCreator.tool.properties.PropertiesCellEditor;
import com.uvigo.tMaestro.SCOCreator.tool.MainFrame;
import com.uvigo.tMaestro.SCOCreator.tool.MyFileFilter;
import com.uvigo.tMaestro.SCOCreator.tool.ProgressBar;
import java.io.File;
import javax.xml.bind.JAXBContext;

/**
 * Esta clase es el TableCellEditor proporcionado para editar determinadas propiedades
 * relacionadas con los contenidos de los elementos de com.uvigo.mhp
 * @author raul
 * @version 0.1
 */
public class ContentEditor extends javax.swing.AbstractCellEditor implements CellEditor, java.awt.event.ActionListener {

    /**
     * El editor que delega en este la responsabilidad
     * de realizar la edici�n.
     */
    private PropertiesCellEditor oEditor;
    /**
     * Variable que almacena el valor editado.
     */
    private Object oValue;

    /**
     * Crea una nuevas instancia de la clase
     */
    public ContentEditor() {
    }

    
    

       ; /**
             * M�todo que devuelve el editor para su presentaci�n en la tabla.
             * Es un panel con un TextField y un bot�n para lanzar un JDialog
             */
                     
            
            
          
           public java.awt.Component getTableCellEditorComponent(javax.swing.JTable oTable,Object oValue,boolean isSelected,int iRow,int iColumn) {
        javax.swing.JPanel oPanel = new javax.swing.JPanel();
        javax.swing.JLabel oJLabel= new javax.swing.JLabel();
        this.oValue=oValue;
        if (oValue!=null) {
            String[] sWords = oValue.toString().split("/");
            String sFile = sWords[sWords.length - 1].replaceAll("[^a-zA-Z_0-9][0-9][^a-zA-Z_0-9]", "").replaceAll(".xml", "") + " - " + MainFrame.oMainFrame.getOption(MainFrame.oMainFrame.iGetOpcion());
            oJLabel.setText(sFile);
        }
        javax.swing.JButton oButton = new javax.swing.JButton("E");
        oButton.addActionListener(this);
        oPanel.setLayout(new java.awt.GridBagLayout());
        java.awt.Rectangle oRect = oTable.getCellRect(iRow, iColumn, true);
        oButton.setSize(20, oRect.height);
        oButton.setMinimumSize(oButton.getSize());
        oJLabel.setSize(oRect.width - 20, oRect.height);
        oJLabel.setMinimumSize(oJLabel.getSize());
        oPanel.add(oJLabel);
        oPanel.add(oButton);
        return oPanel;

    }

    /**
     * Devuelve la clase correspondiente
     * a este editor.
     * @return El objeto Class correspondiente a com.uvigo.tMaestro.SCOCreator.tool.properties.cellEditors.WidgetContentEditor;
     */
    public Class getTypeEditor() {
        return com.uvigo.tMaestro.SCOCreator.tool.properties.cellEditors.ContentEditor.class;
    }

    /**
     * La funci�n devuelve el valor seleccionado a trav�s de este editor.
     * @return El valo editado.
     */
    public Object getCellEditorValue() {
        return oValue;
    }

    /**
     * Esta funci�n establece cual es el editor que ha delegado
     * en este la edici�n.
     * Se utiliza la referencia a este editor para avisarlo de que una propiedad
     * ha concluido o cancelado su edici�n
     * @param oEditor El PropertiesCellEditor que ha delegado la edici�n.
     */
    public void setDelegation(PropertiesCellEditor oEditor) {
        this.oEditor = oEditor;
    }

    /**
     * M�todo que es llamado cuando el usuario pulsa sobre el bot�n de edici�n
     * de la propiedad.
     * @param oEvento El evento que provoca la llamada a esta funci�n.
     */
    public void actionPerformed(java.awt.event.ActionEvent oEvento) {
        //System.out.println("oValue: "+oValue);
        if (oEvento.getActionCommand().equals("E")) { //Si el usuario ha pulsado el bot�n ...
            Propiedad oProperty = MainFrame.oMainFrame.getEditionProperty();
            Object oBean = oProperty.getBean();
            ContentEditorDialog oDialog = null;
            if (oBean instanceof com.uvigo.mhp.ui.HButton || oBean instanceof com.uvigo.mhp.ui.HSwitch || oBean instanceof com.uvigo.mhp.ui.HScrollBar) {
                oDialog = new ContentEditorDialog(MainFrame.oMainFrame, (String) oValue, ContentEditorDialog.Type.WIDGET);
                oDialog.setTitle("WidgetContentEditor");
                oDialog.setSize(500, 300);
            } else if (oBean instanceof com.uvigo.mhp.mediaPlayer.MediaPlayer) {
                //M: Damos opci�n de a�adir recursos MHP o PC
                Object[] options = {"MHP", "PC"};
                int iSelection = javax.swing.JOptionPane.showOptionDialog(MainFrame.oMainFrame, MainFrame.oMainFrame.getMessage("Question14"), "MediaPlayerContent",
                        javax.swing.JOptionPane.YES_NO_OPTION, javax.swing.JOptionPane.QUESTION_MESSAGE,
                        null, options, options[0]);
                switch (iSelection) {
                    case javax.swing.JOptionPane.CLOSED_OPTION:
                        return;case javax.swing.JOptionPane.NO_OPTION:
                       // System.out.println("entra NO OPTION");
                        oDialog = new ContentEditorDialog(MainFrame.oMainFrame, (String) oValue, ContentEditorDialog.Type.MEDIAPLAYER);
                        oDialog.setTitle("MediaPlayerContentEditor");
                        oDialog.setSize(500, 375);
                        break;
                        //SSSS
                    case javax.swing.JOptionPane.YES_OPTION:
                      //  System.out.println("entra YES_OPTION");
                        oDialog = new ContentEditorDialog(MainFrame.oMainFrame, (String) oValue, ContentEditorDialog.Type.MHPMEDIAPLAYER);
                        oDialog.setTitle("MHPMediaPlayerContentEditor");
                        oDialog.setSize(500, 375);
                        /*
                    case javax.swing.JOptionPane.YES_OPTION:
                        javax.swing.JFileChooser oChooser = new javax.swing.JFileChooser();
                        MyFileFilter oFilter = new MyFileFilter();
                        oFilter.vSetDescripcion("pseudo-videos MHP");
                        oChooser.setFileFilter(oFilter);
                        oChooser.setDialogTitle("MediaPlayerContentEditorMHP");
                        oChooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
                        int iVal = oChooser.showOpenDialog(MainFrame.oMainFrame);
                        if (iVal == javax.swing.JFileChooser.APPROVE_OPTION) { //si MHP
                            try {
                                //copiamos fichero (pseudo-v�deo)
                                final File fVout = oChooser.getSelectedFile();
                                final File fVin = new File(MainFrame.oMainFrame.fTemp, "/COMPONENTS/" + fVout.getName());
                                fVin.mkdir();
                                final Code oCode = new Code() {

                                    public void run(ProgressBar oBar) {
                                        try {
                                            java.io.File[] fVoutfiles = fVout.listFiles();
                                            for (int i = 0; i < fVoutfiles.length; i++) {
                                                File fVinfile = new File(fVin, fVoutfiles[i].getName());
                                                MainFrame.copyFile(fVoutfiles[i], fVinfile);
                                            }
                                        } catch (Exception ex) {
                                            System.out.println(ex);
                                        }
                                        oBar.dispose();
                                    }
                                };
                                new Thread(new Runnable() {

                                    public void run() {
                                        new ProgressBar(MainFrame.oMainFrame, 0, 0, MainFrame.oMainFrame.getMessage("Title49"), true, oCode, 100);
                                    }
                                }).start();


                                //generamos xml del mp
                                JAXBContext jc = JAXBContext.newInstance("com.uvigo.mhp.xml.mediaPlayer");
                                javax.xml.bind.Marshaller oM = jc.createMarshaller();

                                String sXml = new String("(" + MainFrame.oMainFrame.getPSheet().getSelectedOption() + ")" +
                                        MainFrame.oMainFrame.oGetCapsulaActiva().oGetBean().getName() + ".xml");
                                java.io.File fXml = new java.io.File(MainFrame.oMainFrame.fTemp, "/COMPONENTS/" + sXml);
                                java.io.FileOutputStream oFOS = new java.io.FileOutputStream(fXml);

                                ObjectFactory oFactory = new ObjectFactory();
                                MediaList ml = oFactory.createMediaList();
                                ResourceType rt = oFactory.createResourceType();
                                rt.setLocator(fVout.getName());
                                ml.getResource().add(rt);

                                oM.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
                                oM.marshal(ml, oFOS);
                                oFOS.flush();
                                oFOS.close();
                                //actualizamos propiedades
                                java.util.ArrayList<java.io.File> oFiles = new java.util.ArrayList<java.io.File>();
                                oFiles.add(fXml);
                                oFiles.add(fVin);
                                MainFrame.oMainFrame.getEditionProperty().putOptionFiles(oFiles, MainFrame.oMainFrame.getPSheet().getSelectedOption());
                                oValue = "TEMP/COMPONENTS/" + sXml;
                                oEditor.vEditado();
                                return;

                            } catch (Exception oE) {
                                oE.printStackTrace();
                                return;
                            }
                        }

                        return;
                         * */
                   
                }
//               oDialog = new ContentEditorDialog(MainFrame.oMainFrame, (String) oValue, ContentEditorDialog.Type.MEDIAPLAYER);
//                oDialog.setTitle("MediaPlayerContentEditor");
//                oDialog.setSize(500, 375);
            } else if (oBean instanceof com.uvigo.mhp.activeImage.ActiveImage) {
                oDialog = new ContentEditorDialog(MainFrame.oMainFrame, (String) oValue, ContentEditorDialog.Type.ACTIVEIMAGE);
                oDialog.setTitle("ActiveImageContentEditor");
                oDialog.setSize(600, 500);
            } else if (oBean instanceof com.uvigo.mhp.ui.HAnimated) {
                oDialog = new ContentEditorDialog(MainFrame.oMainFrame, (String) oValue, ContentEditorDialog.Type.ANIMATED);
                oDialog.setTitle("AnimatedContentEditor");
                oDialog.setSize(500, 400);
            } else if (oBean instanceof com.uvigo.mhp.textPanel.TextPanel) {
                oDialog = new ContentEditorDialog(MainFrame.oMainFrame, (String) oValue, ContentEditorDialog.Type.TEXTPANEL);
                oDialog.setTitle("TextPanelContentEditor");
                oDialog.setSize(650, 500);
            } else if (oBean instanceof com.uvigo.mhp.ui.HMenu) {
                oDialog = new ContentEditorDialog(MainFrame.oMainFrame, (String) oValue, ContentEditorDialog.Type.MENU);
                oDialog.setTitle("MenuContentEditor");
                oDialog.setSize(500, 400);
            } else if (oBean instanceof com.uvigo.mhp.ui.HMultiPanel) {
                oDialog = new ContentEditorDialog(MainFrame.oMainFrame, (String) oValue, ContentEditorDialog.Type.MULTIPANEL);
                oDialog.setTitle("MultiPanelContentEditor");
                oDialog.setSize(500, 400);
            } else return;


            oDialog.setLocation(MainFrame.oMainFrame.getX() + (MainFrame.oMainFrame.getWidth() - oDialog.getWidth()) / 2, MainFrame.oMainFrame.getY() + (MainFrame.oMainFrame.getHeight() - oDialog.getHeight()) / 2);
            oDialog.setVisible(true);
            if (oDialog.getAction() == ContentEditorDialog.OK) {
                oValue = oDialog.getNewFilePath();
                oEditor.vEditado();
            } else oEditor.vCancel();
        }
    }
}
