package com.uvigo.mhp.xml;

import java.awt.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.xml.bind.*;
import com.uvigo.mhp.xml.ui.*;

public class WidgetContentEditorPanel extends javax.swing.JPanel
{
    private String[] strings;
    private Image[] images;
    private String[] urls;
    private int current;
    private MediaTracker trk;
    private JAXBContext jc;
    
    public WidgetContentEditorPanel()
    {
        initComponents();
        try
        {
            jc=JAXBContext.newInstance("com.uvigo.mhp.xml.ui");
            current=0;
            trk = new MediaTracker(this);
            strings=new String[8];
            images=new Image[8];
            urls=new String[8];
        }
        catch (Exception ex)
        {
            System.out.println(ex);
        }
    }
    
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jButton12 = new javax.swing.JButton();
        jButton11 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jSplitPane2 = new javax.swing.JSplitPane();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jPanel6 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        jToggleButton1 = new javax.swing.JToggleButton();
        jToggleButton2 = new javax.swing.JToggleButton();
        jToggleButton3 = new javax.swing.JToggleButton();
        jToggleButton4 = new javax.swing.JToggleButton();
        jToggleButton5 = new javax.swing.JToggleButton();
        jToggleButton6 = new javax.swing.JToggleButton();
        jToggleButton7 = new javax.swing.JToggleButton();
        jToggleButton8 = new javax.swing.JToggleButton();

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

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

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

        jPanel1.add(jButton12);

        jButton11.setText("Cargar");
        jButton11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton11ActionPerformed(evt);
            }
        });

        jPanel1.add(jButton11);

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

        jPanel1.add(jButton10);

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

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

        jSplitPane2.setDividerLocation(125);
        jSplitPane2.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jPanel4.setLayout(new java.awt.BorderLayout());

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jScrollPane1.setViewportView(jLabel1);

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

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

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

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

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

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

        jPanel6.add(jButton1);

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

        jPanel6.add(jButton2);

        jPanel3.add(jPanel6, java.awt.BorderLayout.EAST);

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

        jSplitPane2.setRightComponent(jPanel4);

        jScrollPane2.setBorder(new javax.swing.border.TitledBorder(null, "Texto "));
        jTextArea1.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jScrollPane2.setViewportView(jTextArea1);

        jSplitPane2.setLeftComponent(jScrollPane2);

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

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

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

        jPanel2.setBorder(new javax.swing.border.TitledBorder(" Estado "));
        buttonGroup1.add(jToggleButton1);
        jToggleButton1.setSelected(true);
        jToggleButton1.setText("NORMAL");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        jPanel2.add(jToggleButton1);

        buttonGroup1.add(jToggleButton2);
        jToggleButton2.setText("ACTIONED");
        jToggleButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton2ActionPerformed(evt);
            }
        });

        jPanel2.add(jToggleButton2);

        buttonGroup1.add(jToggleButton3);
        jToggleButton3.setText("FOCUSED");
        jToggleButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton3ActionPerformed(evt);
            }
        });

        jPanel2.add(jToggleButton3);

        buttonGroup1.add(jToggleButton4);
        jToggleButton4.setText("ACTIONED_FOCUSED");
        jToggleButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton4ActionPerformed(evt);
            }
        });

        jPanel2.add(jToggleButton4);

        buttonGroup1.add(jToggleButton5);
        jToggleButton5.setText("DISABLED");
        jToggleButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton5ActionPerformed(evt);
            }
        });

        jPanel2.add(jToggleButton5);

        buttonGroup1.add(jToggleButton6);
        jToggleButton6.setText("DISABLED_ACTIONED");
        jToggleButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton6ActionPerformed(evt);
            }
        });

        jPanel2.add(jToggleButton6);

        buttonGroup1.add(jToggleButton7);
        jToggleButton7.setText("DISABLED_FOCUSED");
        jToggleButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton7ActionPerformed(evt);
            }
        });

        jPanel2.add(jToggleButton7);

        buttonGroup1.add(jToggleButton8);
        jToggleButton8.setText("DISABLED_ACTIONED_FOCUSED");
        jToggleButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton8ActionPerformed(evt);
            }
        });

        jPanel2.add(jToggleButton8);

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

    }
    // </editor-fold>//GEN-END:initComponents
    
    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.
        
        javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
        int option = chooser.showSaveDialog(this);
        if (option == javax.swing.JFileChooser.APPROVE_OPTION)
        {
            try
            {
                File file = chooser.getSelectedFile();
                if (file == null) return;
                
                ObjectFactory objFactory=new ObjectFactory();
                ContentList clist=objFactory.createContentList();
                
                ContentType c=objFactory.createContentType();
                c.setTextContent(strings[0]);
                c.setGraphicContent(urls[0]);
                clist.setNormalStateContent(c);
                
                c=objFactory.createContentType();
                c.setTextContent(strings[1]);
                c.setGraphicContent(urls[1]);
                clist.setActionedStateContent(c);
                
                c=objFactory.createContentType();
                c.setTextContent(strings[2]);
                c.setGraphicContent(urls[2]);
                clist.setFocusedStateContent(c);
                
                c=objFactory.createContentType();
                c.setTextContent(strings[3]);
                c.setGraphicContent(urls[3]);
                clist.setActionedFocusedStateContent(c);
                
                c=objFactory.createContentType();
                c.setTextContent(strings[4]);
                c.setGraphicContent(urls[4]);
                clist.setDisabledStateContent(c);
                
                c=objFactory.createContentType();
                c.setTextContent(strings[5]);
                c.setGraphicContent(urls[5]);
                clist.setDisabledActionedStateContent(c);
                
                c=objFactory.createContentType();
                c.setTextContent(strings[6]);
                c.setGraphicContent(urls[6]);
                clist.setDisabledFocusedStateContent(c);
                
                c=objFactory.createContentType();
                c.setTextContent(strings[7]);
                c.setGraphicContent(urls[7]);
                clist.setDisabledActionedFocusedStateContent(c);
                
                Marshaller m = jc.createMarshaller();
                m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
                m.marshal(clist, new FileOutputStream(file));
            }
            catch (Exception ex)
            {
                System.out.println(ex);
            }
        }
    }//GEN-LAST:event_jButton10ActionPerformed
    
    private void jButton11ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton11ActionPerformed
    {//GEN-HEADEREND:event_jButton11ActionPerformed
        // Hay que cargar el contenido de un fichero XML.
        javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
        int option = chooser.showOpenDialog(this);
        if (option == javax.swing.JFileChooser.APPROVE_OPTION)
        {
            try
            {
                File file = chooser.getSelectedFile();
                if (file == null) return;
                
                Unmarshaller u=jc.createUnmarshaller();
                u.setValidating(true);
                ContentList clist=(ContentList) u.unmarshal(new FileInputStream(file));
                
                ContentType ctemp[]=new ContentType[8];
                ctemp[0]=clist.getNormalStateContent();
                ctemp[1]=clist.getActionedStateContent();
                ctemp[2]=clist.getFocusedStateContent();
                ctemp[3]=clist.getActionedFocusedStateContent();
                ctemp[4]=clist.getDisabledStateContent();
                ctemp[5]=clist.getDisabledActionedStateContent();
                ctemp[6]=clist.getDisabledFocusedStateContent();
                ctemp[7]=clist.getDisabledActionedFocusedStateContent();
                
                Image itemp[]=new Image[8];
                for (int i=0;i<8;i++)
                {
                    if (ctemp[i].getGraphicContent()==null) continue;
                    if (ctemp[i].getGraphicContent().equals("")) continue;
                    
                    URL temp=new URL(ctemp[i].getGraphicContent());
                    itemp[i]=Toolkit.getDefaultToolkit().getImage(temp);
                    trk.addImage(itemp[i],i);
                }
                
                trk.waitForAll();
                
                // Si todo se ha cargado correctamente, se inicializan los arrays
                for (int i=0;i<8;i++)
                {
                    strings[i]=ctemp[i].getTextContent();
                    images[i]=itemp[i];
                    urls[i]=ctemp[i].getGraphicContent();
                }
                
                jTextArea1.setText(strings[current]);
                if ((images[current]!=null) && (images[current].getWidth(null)!=-1) && (images[current].getHeight(null)!=-1))
                    jLabel1.setIcon(new ImageIcon(images[current]));
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
    }//GEN-LAST:event_jButton11ActionPerformed
    
    private void jButton12ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton12ActionPerformed
    {//GEN-HEADEREND:event_jButton12ActionPerformed
        jTextArea1.setText("");
        jTextField1.setText("");
        strings=new String[8];
        images=new Image[8];
        urls=new String[8];
    }//GEN-LAST:event_jButton12ActionPerformed
    
    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jTextField1ActionPerformed
    {//GEN-HEADEREND:event_jTextField1ActionPerformed
        try
        {
            URL url=new URL(jTextField1.getText());
            Image temp=Toolkit.getDefaultToolkit().getImage(url);
            trk.addImage(temp,0);
            trk.waitForAll();
            
            images[current]=temp;
            urls[current]=url.toString();
            jTextField1.setText(url.toString());
            jTextField1.setCaretPosition(0);
        }
        catch (Exception ex)
        {
            System.out.println(ex);
        }
    }//GEN-LAST:event_jTextField1ActionPerformed
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton2ActionPerformed
    {//GEN-HEADEREND:event_jButton2ActionPerformed
        images[current]=null;
    }//GEN-LAST:event_jButton2ActionPerformed
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
    {//GEN-HEADEREND:event_jButton1ActionPerformed
        javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
        int option = chooser.showOpenDialog(this);
        if (option == javax.swing.JFileChooser.APPROVE_OPTION)
        {
            try
            {
                File file = chooser.getSelectedFile();
                if (file == null) return;
                
                URL url=file.toURL();
                Image temp=Toolkit.getDefaultToolkit().getImage(url);
                trk.waitForAll();
                
                images[current]=temp;
                urls[current]=url.toString();
                jTextField1.setText(url.toString());
                jTextField1.setCaretPosition(0);
            }
            catch (Exception ex)
            {
                System.out.println(ex);
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed
    
    private void jToggleButton8ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jToggleButton8ActionPerformed
    {//GEN-HEADEREND:event_jToggleButton8ActionPerformed
        show(7);
    }//GEN-LAST:event_jToggleButton8ActionPerformed
    
    private void jToggleButton7ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jToggleButton7ActionPerformed
    {//GEN-HEADEREND:event_jToggleButton7ActionPerformed
        show(6);
    }//GEN-LAST:event_jToggleButton7ActionPerformed
    
    private void jToggleButton6ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jToggleButton6ActionPerformed
    {//GEN-HEADEREND:event_jToggleButton6ActionPerformed
        show(5);
    }//GEN-LAST:event_jToggleButton6ActionPerformed
    
    private void jToggleButton5ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jToggleButton5ActionPerformed
    {//GEN-HEADEREND:event_jToggleButton5ActionPerformed
        show(4);
    }//GEN-LAST:event_jToggleButton5ActionPerformed
    
    private void jToggleButton4ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jToggleButton4ActionPerformed
    {//GEN-HEADEREND:event_jToggleButton4ActionPerformed
        show(3);
    }//GEN-LAST:event_jToggleButton4ActionPerformed
    
    private void jToggleButton3ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jToggleButton3ActionPerformed
    {//GEN-HEADEREND:event_jToggleButton3ActionPerformed
        show(2);
    }//GEN-LAST:event_jToggleButton3ActionPerformed
    
    private void jToggleButton2ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jToggleButton2ActionPerformed
    {//GEN-HEADEREND:event_jToggleButton2ActionPerformed
        show(1);
    }//GEN-LAST:event_jToggleButton2ActionPerformed
    
    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jToggleButton1ActionPerformed
    {//GEN-HEADEREND:event_jToggleButton1ActionPerformed
        show(0);
    }//GEN-LAST:event_jToggleButton1ActionPerformed
    
    private void show(int i)
    {
        strings[current]=jTextArea1.getText();
        urls[current]=jTextField1.getText();
        
        current=i;
        if ((images[current]==null) || (images[current].getWidth(null)==-1) || (images[current].getHeight(null)==-1))
        {
            try
            {
                URL url=new URL(urls[current]);
                images[current]=Toolkit.getDefaultToolkit().getImage(url);
                trk.addImage(images[current],0);
                trk.waitForAll();
            }
            catch (Exception ex)
            {}
        }
        
        if ((images[current]!=null) && (images[current].getWidth(null)!=-1) && (images[current].getHeight(null)!=-1))
            jLabel1.setIcon(new ImageIcon(images[current]));
        else
            jLabel1.setIcon(null);
        
        jTextArea1.setText(strings[current]);
        jTextField1.setText(urls[current]);
        jTextField1.setCaretPosition(0);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JToggleButton jToggleButton2;
    private javax.swing.JToggleButton jToggleButton3;
    private javax.swing.JToggleButton jToggleButton4;
    private javax.swing.JToggleButton jToggleButton5;
    private javax.swing.JToggleButton jToggleButton6;
    private javax.swing.JToggleButton jToggleButton7;
    private javax.swing.JToggleButton jToggleButton8;
    // End of variables declaration//GEN-END:variables
    
}
