// Path: liblab/src/com/sqrt/liblab/entry/model/Material.java
// public class Material extends LabEntry {
//     /**
//      * The textures
//      */
//     public final List<Texture> textures = new LinkedList<Texture>();
// 
//     public Material(LabFile container, String name) {
//         super(container, name);
//     }
// }
// 
// Path: liblab/src/com/sqrt/liblab/entry/model/Texture.java
// public class Texture {
//     /**
//      * Whether index 0 should be transparent
//      */
//     public boolean hasAlpha;
//     /**
//      * The width of this texture in pixels
//      */
//     public final int width;
//     /**
//      * The height of this texture in pixels
//      */
//     public final int height;
//     /**
//      * Indices into a colormap
//      */
//     public final byte[] indices;
// 
//     /**
//      * Constructs a new texture of the specified dimension with the specified indices
//      * @param width the width of the texture
//      * @param height the height of the texture
//      * @param indices the indices into the colormap
//      */
//     public Texture(int width, int height, byte[] indices) {
//         this.width = width;
//         this.height = height;
//         this.indices = indices;
//     }
// 
//     /**
//      * Returns a bufferedimage that represents this texture when indexed against the specified colormap
//      * @param colorMap the colormap that contains the colors
//      * @return an image
//      */
//     public BufferedImage render(ColorMap colorMap) {
//         if (colorMap == null)
//             return null;
//         BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED, colorMap.toColorModel());
//         bi.getRaster().setDataElements(0, 0, width, height, indices);
//         return bi;
//     }
// }
// 
// Path: grimedi/src/com/sqrt4/grimedi/ui/component/ColorMapSelector.java
// public class ColorMapSelector extends JPanel {
//     private LabCollection _last;
// 
//     public ColorMapSelector() {
//         initComponents();
//     }
// 
//     public void setLabFile(LabFile container) {
//         if (container.container == _last)
//             return;
//         _last = container.container;
//         Vector<DataSource> colorMaps = new Vector<DataSource>();
//         colorMaps.addAll(container.container.findByType(ColorMap.class));
//         colorMapSelector.setModel(new DefaultComboBoxModel(colorMaps));
//     }
// 
//     public void addItemListener(ItemListener listener) {
//         colorMapSelector.addItemListener(listener);
//     }
// 
//     public void removeItemListener(ItemListener listener) {
//         colorMapSelector.removeItemListener(listener);
//     }
// 
//     public ColorMap getSelected() {
//         DataSource edp = (DataSource) colorMapSelector.getSelectedItem();
//         try {
//             edp.position(0);
//             return (ColorMap) CodecMapper.codecForProvider(edp).read(edp);
//         } catch (IOException e) {
//             MainWindow.getInstance().handleException(e);
//             return null;
//         }
//     }
// 
//     public void setEnabled(boolean enabled) {
//         super.setEnabled(enabled);
//         colorMapSelector.setEnabled(enabled);
//     }
// 
//     private void createUIComponents() {
//         colorMapSelector = new JComboBox();
//     }
// 
//     private void initComponents() {
//         // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
//         createUIComponents();
// 
// 
//         //======== this ========
//         setLayout(new BorderLayout());
//         add(colorMapSelector, BorderLayout.CENTER);
//         // JFormDesigner - End of component initialization  //GEN-END:initComponents
//     }
// 
//     // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
//     private JComboBox colorMapSelector;
//     // JFormDesigner - End of variables declaration  //GEN-END:variables
// }


import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import com.sqrt.liblab.entry.model.Material;
import com.sqrt.liblab.entry.model.Texture;
import com.sqrt4.grimedi.ui.component.ColorMapSelector;
import javax.swing.*;


/*
 * Copyright (C) 2014  James Lawrence.
 *
 *     This file is part of GrimEdi.
 *
 *     GrimEdi is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Created by JFormDesigner on Mon Mar 18 20:01:59 GMT 2013
 */

package com.sqrt4.grimedi.ui.editor;



/**
 * @author James Lawrence
 */
public class MaterialView extends EditorPanel<Material> {
    public MaterialView() {
        initComponents();
    }

    ImageIcon icon = new ImageIcon(getClass().getResource("/material.png"));

    public ImageIcon getIcon() {
        return icon;
    }

    private void updatePreview() {
        preview.setIcon(null);
        if (imageList.getSelectedValue() == null || colorMapSelector.getSelected() == null)
            return;
// Path: liblab/src/com/sqrt/liblab/entry/model/Material.java
// public class Material extends LabEntry {
//     /**
//      * The textures
//      */
//     public final List<Texture> textures = new LinkedList<Texture>();
// 
//     public Material(LabFile container, String name) {
//         super(container, name);
//     }
// }
// 
// Path: liblab/src/com/sqrt/liblab/entry/model/Texture.java
// public class Texture {
//     /**
//      * Whether index 0 should be transparent
//      */
//     public boolean hasAlpha;
//     /**
//      * The width of this texture in pixels
//      */
//     public final int width;
//     /**
//      * The height of this texture in pixels
//      */
//     public final int height;
//     /**
//      * Indices into a colormap
//      */
//     public final byte[] indices;
// 
//     /**
//      * Constructs a new texture of the specified dimension with the specified indices
//      * @param width the width of the texture
//      * @param height the height of the texture
//      * @param indices the indices into the colormap
//      */
//     public Texture(int width, int height, byte[] indices) {
//         this.width = width;
//         this.height = height;
//         this.indices = indices;
//     }
// 
//     /**
//      * Returns a bufferedimage that represents this texture when indexed against the specified colormap
//      * @param colorMap the colormap that contains the colors
//      * @return an image
//      */
//     public BufferedImage render(ColorMap colorMap) {
//         if (colorMap == null)
//             return null;
//         BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED, colorMap.toColorModel());
//         bi.getRaster().setDataElements(0, 0, width, height, indices);
//         return bi;
//     }
// }
// 
// Path: grimedi/src/com/sqrt4/grimedi/ui/component/ColorMapSelector.java
// public class ColorMapSelector extends JPanel {
//     private LabCollection _last;
// 
//     public ColorMapSelector() {
//         initComponents();
//     }
// 
//     public void setLabFile(LabFile container) {
//         if (container.container == _last)
//             return;
//         _last = container.container;
//         Vector<DataSource> colorMaps = new Vector<DataSource>();
//         colorMaps.addAll(container.container.findByType(ColorMap.class));
//         colorMapSelector.setModel(new DefaultComboBoxModel(colorMaps));
//     }
// 
//     public void addItemListener(ItemListener listener) {
//         colorMapSelector.addItemListener(listener);
//     }
// 
//     public void removeItemListener(ItemListener listener) {
//         colorMapSelector.removeItemListener(listener);
//     }
// 
//     public ColorMap getSelected() {
//         DataSource edp = (DataSource) colorMapSelector.getSelectedItem();
//         try {
//             edp.position(0);
//             return (ColorMap) CodecMapper.codecForProvider(edp).read(edp);
//         } catch (IOException e) {
//             MainWindow.getInstance().handleException(e);
//             return null;
//         }
//     }
// 
//     public void setEnabled(boolean enabled) {
//         super.setEnabled(enabled);
//         colorMapSelector.setEnabled(enabled);
//     }
// 
//     private void createUIComponents() {
//         colorMapSelector = new JComboBox();
//     }
// 
//     private void initComponents() {
//         // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
//         createUIComponents();
// 
// 
//         //======== this ========
//         setLayout(new BorderLayout());
//         add(colorMapSelector, BorderLayout.CENTER);
//         // JFormDesigner - End of component initialization  //GEN-END:initComponents
//     }
// 
//     // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
//     private JComboBox colorMapSelector;
//     // JFormDesigner - End of variables declaration  //GEN-END:variables
// }


// Path: grimedi/src/com/sqrt4/grimedi/ui/editor/MaterialView.java
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import com.sqrt.liblab.entry.model.Material;
import com.sqrt.liblab.entry.model.Texture;
import com.sqrt4.grimedi.ui.component.ColorMapSelector;
import javax.swing.*;



/*
 * Copyright (C) 2014  James Lawrence.
 *
 *     This file is part of GrimEdi.
 *
 *     GrimEdi is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Created by JFormDesigner on Mon Mar 18 20:01:59 GMT 2013
 */

package com.sqrt4.grimedi.ui.editor;



/**
 * @author James Lawrence
 */
public class MaterialView extends EditorPanel<Material> {
    public MaterialView() {
        initComponents();
    }

    ImageIcon icon = new ImageIcon(getClass().getResource("/material.png"));

    public ImageIcon getIcon() {
        return icon;
    }

    private void updatePreview() {
        preview.setIcon(null);
        if (imageList.getSelectedValue() == null || colorMapSelector.getSelected() == null)
            return;
        preview.setIcon(new ImageIcon(((Texture) imageList.getSelectedValue()).render(colorMapSelector.getSelected())));