package com.uvigo.mhp.xml;

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.media.*;
import javax.xml.bind.*;
import com.uvigo.mhp.mediaPlayer.*;
import com.uvigo.mhp.xml.mediaPlayer.*;

public class MediaPlayerContentEditorPanel_2 extends JPanel implements TableModelListener, ListSelectionListener
{
    private DefaultListModel listModel;
    private DefaultListSelectionModel selectionModel;
    private DefaultTableModel tableModel;
    private boolean moving;
    private Player player;
    
    private java.util.List resourceList;
    private java.util.List currentTimestampList;
    private JAXBContext jc;
    private ObjectFactory objFactory;
    private MediaList ml;
    
    public MediaPlayerContentEditorPanel_2()
    {
        initComponents();
        try
        {
            jc=JAXBContext.newInstance("com.uvigo.mhp.xml.mediaPlayer");
            objFactory = new ObjectFactory();
            ml=objFactory.createMediaList();
            resourceList=ml.getResource();
            
            listModel=new DefaultListModel();
            jList1.setModel(listModel);
            tableModel=(DefaultTableModel) jTable1.getModel();
            selectionModel=(DefaultListSelectionModel) jTable1.getSelectionModel();
            TableColumn tc=jTable1.getColumn("Tiempo");
            tc.setMaxWidth(100);
            tableModel.addTableModelListener(this);
            jList1.addListSelectionListener(this);
            moving=false;
        }
        catch (JAXBException ex)
        {
            System.out.println(ex.getMessage());
        }
    }
    
    private void initComponents()//GEN-BEGIN:initComponents
    {
        jPanel13 = new javax.swing.JPanel();
        jButton12 = new javax.swing.JButton();
        jButton11 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jPanel5 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton13 = new javax.swing.JButton();
        jSplitPane2 = new javax.swing.JSplitPane();
        jPanel6 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jButton4 = new javax.swing.JButton();
        jPanel8 = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jPanel12 = new javax.swing.JPanel();
        jPanel10 = new javax.swing.JPanel();
        jPanel11 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

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

        jPanel13.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);
            }
        });

        jPanel13.add(jButton12);

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

        jPanel13.add(jButton11);

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

        jPanel13.add(jButton10);

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

        jSplitPane1.setDividerLocation(150);
        jSplitPane1.setDividerSize(5);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel1.setBorder(new javax.swing.border.TitledBorder("Localizadores "));
        jPanel2.setLayout(new java.awt.BorderLayout());

        jScrollPane1.setViewportView(jList1);

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

        jPanel5.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);
            }
        });

        jPanel5.add(jButton2);

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

        jPanel5.add(jButton3);

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

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

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

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

        jPanel3.add(jButton1);

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

        jPanel3.add(jButton13);

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

        jSplitPane1.setLeftComponent(jPanel1);

        jSplitPane2.setDividerLocation(250);
        jSplitPane2.setDividerSize(5);
        jPanel6.setLayout(new java.awt.BorderLayout());

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

        jButton4.setText("A\u00f1adir sello temporal actual");
        jButton4.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton4ActionPerformed(evt);
            }
        });

        jPanel7.add(jButton4);

        jPanel6.add(jPanel7, java.awt.BorderLayout.SOUTH);

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

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

        jPanel9.setBackground(new java.awt.Color(0, 0, 0));
        jButton7.setText("PLAY");
        jButton7.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton7ActionPerformed(evt);
            }
        });

        jPanel9.add(jButton7);

        jButton8.setText("PAUSE");
        jButton8.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton8ActionPerformed(evt);
            }
        });

        jPanel9.add(jButton8);

        jButton9.setText("STOP");
        jButton9.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton9ActionPerformed(evt);
            }
        });

        jPanel9.add(jButton9);

        jPanel8.add(jPanel9, java.awt.BorderLayout.WEST);

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

        jPanel8.add(jPanel12, java.awt.BorderLayout.CENTER);

        jPanel6.add(jPanel8, java.awt.BorderLayout.CENTER);

        jSplitPane2.setRightComponent(jPanel6);

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

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

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

        jTextField1.addFocusListener(new java.awt.event.FocusAdapter()
        {
            public void focusGained(java.awt.event.FocusEvent evt)
            {
                jTextField1FocusGained(evt);
            }
        });

        jPanel11.add(jTextField1);

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

        jPanel11.add(jButton5);

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

        jPanel11.add(jButton6);

        jPanel10.add(jPanel11, java.awt.BorderLayout.SOUTH);

        jScrollPane2.setAutoscrolls(true);
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][]
            {

            },
            new String []
            {
                "Tiempo", "Contexto", "Texto"
            }
        )
        {
            Class[] types = new Class []
            {
                java.lang.Double.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex)
            {
                return types [columnIndex];
            }
        });
        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);

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

        jSplitPane2.setLeftComponent(jPanel10);

        jSplitPane1.setRightComponent(jSplitPane2);

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

    }//GEN-END:initComponents
    
    private void jButton13ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton13ActionPerformed
    {//GEN-HEADEREND:event_jButton13ActionPerformed
        // hay que eliminar una entrada de la lista de localizadores
        int i=jList1.getSelectedIndex();
        if (i==-1) return;
        listModel.remove(i);
        resourceList.remove(i);
        
        if (listModel.size()==0)
        {
            int j=tableModel.getRowCount();
            for (int k=0;k<j;k++)
                tableModel.removeRow(0);
            jPanel12.removeAll();
            return;
        }
        
        if (i==listModel.size())
            jList1.setSelectedIndex(i-1);
        else
            jList1.setSelectedIndex(i);
    }//GEN-LAST:event_jButton13ActionPerformed
    
    private void jButton12ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton12ActionPerformed
    {//GEN-HEADEREND:event_jButton12ActionPerformed
        try
        {
            ml=objFactory.createMediaList();
            resourceList=ml.getResource();
            listModel.clear();
            if (player!=null)
            {
                player.deallocate();
                player=null;
            }
            
            jPanel12.removeAll();
            jPanel12.repaint();
            int j=tableModel.getRowCount();
            for (int i=0;i<j;i++)
                tableModel.removeRow(0);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_jButton12ActionPerformed
    
    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);
                ml=(MediaList) u.unmarshal(new FileInputStream(file));
                resourceList=ml.getResource();
                
                for (int i=0;i<resourceList.size();i++)
                {
                    ResourceType rt=(ResourceType) resourceList.get(i);
                    listModel.addElement(rt.getLocator());
                }
            }
            catch (Exception ex)
            {
                System.out.println(ex);
            }
        }
    }//GEN-LAST:event_jButton11ActionPerformed
    
    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;
                
                Marshaller m = jc.createMarshaller();
                m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
                m.marshal(ml, new FileOutputStream(file));
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
                System.out.println(ex);
            }
        }
    }//GEN-LAST:event_jButton10ActionPerformed
        
    private void jTable1MouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_jTable1MouseClicked
    {//GEN-HEADEREND:event_jTable1MouseClicked
        // Cuando se selecciona un sello temporal en la tabla se coloca en ese
        // punto la reproduccion.
        Point p=new Point(evt.getX(),evt.getY());
        int col=jTable1.columnAtPoint(p);
        int fil=jTable1.rowAtPoint(p);
        if ((col==-1) || (fil==-1)) return;
        double d=((Double) tableModel.getValueAt(fil,0)).doubleValue();
        player.stop();
        player.setMediaTime(new Time(d));
    }//GEN-LAST:event_jTable1MouseClicked
    
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton4ActionPerformed
    {//GEN-HEADEREND:event_jButton4ActionPerformed
        // Hay que anadir a la lista de sellos temporales uno para el instante actual
        // en la reproduccion.
        if (player==null) return;
        try
        {
            Object temp[]=new Object[3];
            temp[0]=new Double(player.getMediaTime().getSeconds());
            temp[1]=new String("");
            temp[2]=new String("");
            newTimestamp(temp, false);
        }
        catch (Exception ex)
        {}
    }//GEN-LAST:event_jButton4ActionPerformed
    
    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton9ActionPerformed
    {//GEN-HEADEREND:event_jButton9ActionPerformed
        if (player==null) return;
        player.stop();
        player.setMediaTime(new Time(0));
    }//GEN-LAST:event_jButton9ActionPerformed
    
    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton8ActionPerformed
    {//GEN-HEADEREND:event_jButton8ActionPerformed
        if (player==null) return;
        player.stop();
        player.setMediaTime(player.getMediaTime());
    }//GEN-LAST:event_jButton8ActionPerformed
    
    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton7ActionPerformed
    {//GEN-HEADEREND:event_jButton7ActionPerformed
        if (player==null) return;
        player.setMediaTime(player.getMediaTime());
        player.start();
    }//GEN-LAST:event_jButton7ActionPerformed
    
    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton5ActionPerformed
    {//GEN-HEADEREND:event_jButton5ActionPerformed
        // hay que a�adir un nuevo sello temporal con el valor indicado
        // en el campo de texto.
        if (player==null) return;
        try
        {
            Object temp[]=new Object[3];
            temp[0]=new Double(jTextField1.getText());
            temp[1]=new String("");
            temp[2]=new String("");
            newTimestamp(temp, false);
            jTextField1.setText("");
        }
        catch (Exception ex)
        {}
    }//GEN-LAST:event_jButton5ActionPerformed
    
    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton6ActionPerformed
    {//GEN-HEADEREND:event_jButton6ActionPerformed
        // hay que borrar el sello temporal seleccionado en la tabla
        int i[]=jTable1.getSelectedRows();
        if (i.length==0) return;
        
        for (int j=0;j<i.length;j++)
        {
            tableModel.removeRow(i[j]-j);
            currentTimestampList.remove(i[j]-j);
        }
        
        if (jTable1.getRowCount()==0) return;
        if (i[0]==jTable1.getRowCount())
        {
            jTable1.setColumnSelectionInterval(0, 0);
            jTable1.setRowSelectionInterval(i[0]-1, i[0]-1);
        }
        else
        {
            jTable1.setColumnSelectionInterval(0, 0);
            jTable1.setRowSelectionInterval(i[0], i[0]);
        }
    }//GEN-LAST:event_jButton6ActionPerformed
    
    private void jTextField1FocusGained(java.awt.event.FocusEvent evt)//GEN-FIRST:event_jTextField1FocusGained
    {//GEN-HEADEREND:event_jTextField1FocusGained
        jTextField1.setText(""); // para no seguir escribiendo a continuacion del
        // valor introducido anteriormente
    }//GEN-LAST:event_jTextField1FocusGained
    
    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jTextField1ActionPerformed
    {//GEN-HEADEREND:event_jTextField1ActionPerformed
        // Cuando se pulsa ENTER en el campo de texto se hace lo mismo que
        // cuando se pulsa el boton anadir
        jButton5ActionPerformed(null);
    }//GEN-LAST:event_jTextField1ActionPerformed
    
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // desplazar hacia abajo el localizador para que se reproduzca mas
        // adelante en la secuencia
        int i=jList1.getSelectedIndex();
        if (i==-1) return;
        if (i==listModel.size()-1) return;
        
        moving=true;
        Object aux=listModel.remove(i);
        Object aux2=resourceList.remove(i);
        listModel.add(i+1,aux);
        resourceList.add(i+1,aux2);
        jList1.setSelectedIndex(i+1);
    }//GEN-LAST:event_jButton3ActionPerformed
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // desplazar hacia arriba el localizador para que se reproduzca antes
        // en la secuencia
        int i=jList1.getSelectedIndex();
        if (i<=0) return;
        
        moving=true;
        Object aux=listModel.remove(i);
        Object aux2=resourceList.remove(i);
        listModel.add(i-1,aux);
        resourceList.add(i-1,aux2);
        jList1.setSelectedIndex(i-1);
    }//GEN-LAST:event_jButton2ActionPerformed
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST: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;
                
                listModel.addElement(file.toURL());
                
                ResourceType rt=objFactory.createResourceType();
                rt.setLocator(file.toURL().toString());
                resourceList.add(rt);
            }
            catch (Exception ex)
            {
                System.out.println(ex);
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed
    
    public void tableChanged(TableModelEvent e)
    {
        // hay que reordenar los sellos temporales en la tabla.
        if (e.getType()==TableModelEvent.UPDATE)
        {
            int j=e.getFirstRow();
            if (e.getColumn()==0)
            {
                // si se ha modificado el valor del sello temporal, hay que
                // reordenar la tabla
                double d=((Double) tableModel.getValueAt(j,0)).doubleValue();
                String s1=(String) tableModel.getValueAt(j,1);
                String s2=(String) tableModel.getValueAt(j,2);
                if (jTable1.getRowCount()==1) return;
                
                Object temp[]=new Object[3];
                temp[0]=new Double(d);
                temp[1]=s1;
                temp[2]=s2;
                
                tableModel.removeRow(j);
                currentTimestampList.remove(j);
                for (int i=0;i<jTable1.getRowCount();i++)
                {
                    double d1=((Double) temp[0]).doubleValue();
                    double d2=((Double) tableModel.getValueAt(i,0)).doubleValue();
                    if (d1==d2)
                    {
                        // Se impone una nueva cadena para un sello temporal ya existente
                        tableModel.setValueAt(s1, i, 1);
                        tableModel.setValueAt(s2, i, 2);
                        TimestampType ts=(TimestampType) currentTimestampList.get(i);
                        ts.setContextName(s1);
                        ts.setComment(s2);
                        return;
                    }
                    if (d1<d2)
                    {
                        tableModel.insertRow(i,temp);
                        try
                        {
                            TimestampType ts=objFactory.createTimestampType();
                            ts.setContextName(s1);
                            ts.setTimestamp(d);
                            ts.setComment(s2);
                            currentTimestampList.add(i, ts);
                        }
                        catch (Exception ex)
                        {
                            ex.printStackTrace();
                        }
                        
                        jTable1.setColumnSelectionInterval(0, 0);
                        jTable1.setRowSelectionInterval(i, i);
                        return;
                    }
                }
                tableModel.addRow(temp);
                
                try
                {
                    TimestampType ts=objFactory.createTimestampType();
                    ts.setContextName(s1);
                    ts.setTimestamp(d);
                    ts.setComment(s2);
                    currentTimestampList.add(ts);
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
                }
                
                jTable1.setColumnSelectionInterval(0, 0);
                jTable1.setRowSelectionInterval(jTable1.getRowCount()-1, jTable1.getRowCount()-1);
            }
            else
            {
                // si se ha modificado la cadena, hay que actualizarla en el
                // vector timestamps
                String s1=(String) tableModel.getValueAt(j,1);
                String s2=(String) tableModel.getValueAt(j,2);
                TimestampType ts=(TimestampType) currentTimestampList.get(j);
                ts.setContextName(s1);
                ts.setComment(s2);
            }
        }
    }
    
    public void valueChanged(ListSelectionEvent evt)
    {
        // al cambiar el localizador seleccionado, hay que cambiar el medio que
        // se reproduce y actualizar la tabla de sellos temporales.
        if(evt.getValueIsAdjusting())
            return;
        
        if (jList1.getSelectedIndex()==-1)
            return;
        
        if (moving)
        {
            moving=false;
            return;
        }
        
        jPanel12.removeAll();
        ResourceType rt=(ResourceType) resourceList.get(jList1.getSelectedIndex());
        currentTimestampList=rt.getTimestamp();
        reloadTable();
        try
        {
            player=Manager.createRealizedPlayer(new MediaLocator(rt.getLocator()));
            jPanel12.add(player.getVisualComponent(),"Center");
            jPanel12.add(new MediaScrollBar(player),"South");
            jPanel12.validate();
            doLayout();
        }
        catch (Exception ex)
        {
            System.out.println(ex);
        }
        return;
    }
    
    private void reloadTable()
    {
        // Al cambiar el medio, hay que actualizar la tabla de sellos temporales.
        int j=tableModel.getRowCount();
        for (int i=0;i<j;i++)
            tableModel.removeRow(0);
        
        Object temp[]=new Object[3];
        for (int i=0;i<currentTimestampList.size();i++)
        {
            temp[0]=new Double(((TimestampType) currentTimestampList.get(i)).getTimestamp());
            temp[1]=((TimestampType) currentTimestampList.get(i)).getContextName();
            temp[2]=((TimestampType) currentTimestampList.get(i)).getComment();
            newTimestamp(temp, true);
        }
        
        selectionModel.clearSelection();
    }
    
    private void newTimestamp(Object temp[], boolean reloading)
    {
        // reloading es true cuando se est� recargando la tabla de sellos, y
        // false cuando se a�ade uno nuevo.
        
        try
        {
            if (jTable1.getRowCount()==0)
            {
                tableModel.insertRow(0,temp);
                jTable1.setColumnSelectionInterval(0, 0);
                jTable1.setRowSelectionInterval(0, 0);
                
                if (!reloading)
                {
                    TimestampType ts=objFactory.createTimestampType();
                    ts.setTimestamp(((Double) temp[0]).doubleValue());
                    ts.setContextName((String) temp[1]);
                    ts.setComment((String) temp[2]);
                    currentTimestampList.add(ts);
                }
                return;
            }
            for (int i=0;i<jTable1.getRowCount();i++)
            {
                double d1=((Double) temp[0]).doubleValue();
                double d2=((Double) tableModel.getValueAt(i,0)).doubleValue();
                if (d1==d2) return;
                if (d1<d2)
                {
                    tableModel.insertRow(i,temp);
                    jTable1.setColumnSelectionInterval(0, 0);
                    jTable1.setRowSelectionInterval(i, i);
                    
                    if (!reloading)
                    {
                        TimestampType ts=objFactory.createTimestampType();
                        ts.setTimestamp(d1);
                        ts.setContextName((String) temp[1]);
                        ts.setComment((String) temp[2]);
                        currentTimestampList.add(i,ts);
                    }
                    return;
                }
            }
            tableModel.addRow(temp);
            jTable1.setColumnSelectionInterval(0, 0);
            jTable1.setRowSelectionInterval(jTable1.getRowCount()-1, jTable1.getRowCount()-1);
            
            if (!reloading)
            {
                TimestampType ts=objFactory.createTimestampType();
                ts.setTimestamp(((Double) temp[0]).doubleValue());
                ts.setContextName((String) temp[1]);
                ts.setComment((String) temp[2]);
                currentTimestampList.add(ts);
            }
        }
        catch (JAXBException ex)
        {
            ex.printStackTrace();
        }
    }
    
    public Dimension getPreferredSize()
    {
        return new Dimension(600,400);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jButton7;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JButton jButton8;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton11;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JButton jButton3;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jButton9;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton6;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JList jList1;
    private javax.swing.JButton jButton5;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JButton jButton13;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jButton4;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}