/*
 * IIOPreferencesPane.java
 *
 * Created on September 23, 2004, 9:13 PM
 */

package kiyut.ekspos.prefs;

import java.util.*;

import javax.imageio.*;
import javax.imageio.spi.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

/**
 *
 * @author  Kiyut
 */
public class IIOPreferencesPane extends PreferencesPane {
    
    /** Creates new form IIOPreferencesPane */
    public IIOPreferencesPane() {
        initComponents();
        initCustomComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jScrollPane1 = new javax.swing.JScrollPane();
        readerTable = new javax.swing.JTable();

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

        readerTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Format", "Provider"
            }
        ));
        jScrollPane1.setViewportView(readerTable);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(jScrollPane1, gridBagConstraints);

    }//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable readerTable;
    // End of variables declaration//GEN-END:variables
    
    private void initCustomComponents() {
        IIOTableModel model = new IIOTableModel();
        readerTable.setModel(model);
        TableColumnModel columnModel = readerTable.getColumnModel();
        columnModel.getColumn(0).setPreferredWidth(50);
        
        JComboBox editorCombo = new JComboBox();
        editorCombo.setEditable(false);
        columnModel.getColumn(1).setCellEditor(new IIOTableCellEditor(editorCombo));
        
    }
    
    /** {@inheritDoc} */
    public void loadPrefs() {
        // do nothing
        //IIOPreferences prefs = IIOPreferences.getInstance();
    }
    
    /** {@inheritDoc} */
    public void savePrefs() {
        IIOPreferences prefs = IIOPreferences.getInstance();
        
        Map<String,ImageReaderSpi> readerMap = prefs.getImageReaderWriterPreferences().getPreferredImageReaderSpi();
        for (int i=0; i<readerTable.getRowCount(); i++) {
            String format = (String)readerTable.getValueAt(i,0);
            String provider = (String)readerTable.getValueAt(i,1);
            ImageReader reader = getImageReader(format,provider);
            if (reader != null) {
                readerMap.put(format,reader.getOriginatingProvider());
            }
        }
        
        prefs.save();
    }
    
    /** Return provider name. It is formated as vendorName + space + version
     * @return provider name
     */
    public static String getProviderName(ImageReaderWriterSpi spi) {
        return spi.getVendorName() + " " + spi.getVersion();
    }
    
    /** Return ImageReader or null
     * @param format the Format
     * @param provider the provider name
     * @return ImageReader or null
     * @see #getCodecName(ImageReaderWriterSpi)
     */
    public static ImageReader getImageReader(String format, String provider) {
        ImageReader reader = null;
        Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(format);
        while (it.hasNext()) {
            ImageReader readerTemp = it.next();
            ImageReaderSpi spi = readerTemp.getOriginatingProvider();
            if (spi == null) { continue; }
            String str = getProviderName(spi);
            if (str.equals(provider)) {
                //System.err.println(format + "-" + codec + "-" + str + "-equals");
                reader = readerTemp;
                break;
            }
        }
        return reader;
    }
    
    private static class IIOTableModel extends DefaultTableModel {
        public IIOTableModel() {
            // setting column
            addColumn("Format");
            addColumn("Provider");
            
            // setting data
            IIOPreferences prefs = IIOPreferences.getInstance();
            Map readerMap = prefs.getImageReaderWriterPreferences().getPreferredImageReaderSpi();
            Iterator it = readerMap.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry entry = (Map.Entry)it.next();
                ImageReaderSpi spi = (ImageReaderSpi)entry.getValue();
                String[] dataRow = new String[2];
                dataRow[0] = (String)entry.getKey();
                dataRow[1] = getProviderName(spi);
                addRow(dataRow);
            }
        }
        
        @Override
        public boolean isCellEditable(int row, int col) {
            boolean b = false;
            if (col == 1) {
                b = true;
            }
            
            return b;
        }
    }
    
    private static class IIOTableCellEditor extends DefaultCellEditor {
        public IIOTableCellEditor(JComboBox comboBox) {
            super(comboBox);
        }
        
        @Override
        public java.awt.Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
            JComboBox comboBox = (JComboBox)getComponent();
            comboBox.removeAllItems();
            Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName((String)table.getValueAt(row,0));
            while (it.hasNext()) {
                ImageReaderSpi spi = it.next().getOriginatingProvider();
                comboBox.addItem(getProviderName(spi));
            }
            return super.getTableCellEditorComponent(table, value, isSelected,row, column);
        }
    }
}
