/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jme3.gde.materialdefinition.editor;

import com.jme3.gde.materials.MaterialPreviewRenderer;
import com.jme3.material.Material;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

/**
 *
 * @author Nehon
 */
public class MatPanel extends javax.swing.JPanel implements MouseListener, ComponentListener {
    
    private final MaterialPreviewRenderer renderer;
    private Material mat;
    /**
     * Creates new form PreviewPanel
     */
    public MatPanel() {
        initComponents();
        setBounds(0, 0, 120, 120);
        toolBar.setVisible(false);
        addMouseListener(this);
        renderer = new MaterialPreviewRenderer(previewLabel);
    }
    
    public void cleanup() {
        renderer.cleanUp();        
    }
    
    public void showMaterial(Material mat) {
        this.mat = mat;
        renderer.showMaterial(mat);       
    }
    
    public void showMaterial(Material mat, String technique) {
        this.mat = mat;
        renderer.showMaterial(mat, technique);       
    }

    public void setExpandActionListener(ActionListener action){        
        expandButton.addActionListener(action);
    }
    
    /**
     * 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        toolBar = new javax.swing.JPanel();
        sphereButton = new javax.swing.JButton();
        boxButton = new javax.swing.JButton();
        reloadButton = new javax.swing.JButton();
        quadButton = new javax.swing.JButton();
        expandButton = new javax.swing.JButton();
        previewLabel = new javax.swing.JLabel();

        setOpaque(false);
        setLayout(null);

        toolBar.setOpaque(false);

        sphereButton.setBackground(new java.awt.Color(153, 153, 153));
        sphereButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/jme3/gde/materialdefinition/icons/Sphere.png"))); // NOI18N
        sphereButton.setToolTipText("Sphere");
        sphereButton.setBorder(null);
        sphereButton.setBorderPainted(false);
        sphereButton.setContentAreaFilled(false);
        sphereButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        sphereButton.setFocusable(false);
        sphereButton.setIconTextGap(0);
        sphereButton.setMaximumSize(new java.awt.Dimension(24, 24));
        sphereButton.setMinimumSize(new java.awt.Dimension(24, 24));
        sphereButton.setPreferredSize(new java.awt.Dimension(24, 24));
        sphereButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                sphereButtonMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                sphereButtonMouseExited(evt);
            }
        });
        sphereButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sphereButtonActionPerformed(evt);
            }
        });

        boxButton.setBackground(new java.awt.Color(153, 153, 153));
        boxButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/jme3/gde/materialdefinition/icons/cube.png"))); // NOI18N
        boxButton.setToolTipText("Cube");
        boxButton.setBorder(null);
        boxButton.setBorderPainted(false);
        boxButton.setContentAreaFilled(false);
        boxButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        boxButton.setFocusable(false);
        boxButton.setIconTextGap(0);
        boxButton.setMaximumSize(new java.awt.Dimension(24, 24));
        boxButton.setMinimumSize(new java.awt.Dimension(24, 24));
        boxButton.setPreferredSize(new java.awt.Dimension(24, 24));
        boxButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                boxButtonMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                boxButtonMouseExited(evt);
            }
        });
        boxButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxButtonActionPerformed(evt);
            }
        });

        reloadButton.setBackground(new java.awt.Color(153, 153, 153));
        reloadButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/jme3/gde/materialdefinition/icons/reload.png"))); // NOI18N
        reloadButton.setToolTipText("Refresh");
        reloadButton.setBorder(null);
        reloadButton.setBorderPainted(false);
        reloadButton.setContentAreaFilled(false);
        reloadButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        reloadButton.setFocusable(false);
        reloadButton.setIconTextGap(0);
        reloadButton.setMaximumSize(new java.awt.Dimension(24, 24));
        reloadButton.setMinimumSize(new java.awt.Dimension(24, 24));
        reloadButton.setPreferredSize(new java.awt.Dimension(24, 24));
        reloadButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                reloadButtonMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                reloadButtonMouseExited(evt);
            }
        });
        reloadButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reloadButtonActionPerformed(evt);
            }
        });

        quadButton.setBackground(new java.awt.Color(153, 153, 153));
        quadButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/jme3/gde/materialdefinition/icons/Quad.png"))); // NOI18N
        quadButton.setToolTipText("Quad");
        quadButton.setBorder(null);
        quadButton.setBorderPainted(false);
        quadButton.setContentAreaFilled(false);
        quadButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        quadButton.setFocusable(false);
        quadButton.setIconTextGap(0);
        quadButton.setMaximumSize(new java.awt.Dimension(24, 24));
        quadButton.setMinimumSize(new java.awt.Dimension(24, 24));
        quadButton.setPreferredSize(new java.awt.Dimension(24, 24));
        quadButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                quadButtonMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                quadButtonMouseExited(evt);
            }
        });
        quadButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quadButtonActionPerformed(evt);
            }
        });

        expandButton.setBackground(new java.awt.Color(153, 153, 153));
        expandButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/jme3/gde/materialdefinition/icons/expend.png"))); // NOI18N
        expandButton.setToolTipText("Display Backdrop");
        expandButton.setBorder(null);
        expandButton.setBorderPainted(false);
        expandButton.setContentAreaFilled(false);
        expandButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        expandButton.setFocusable(false);
        expandButton.setIconTextGap(0);
        expandButton.setMaximumSize(new java.awt.Dimension(24, 24));
        expandButton.setMinimumSize(new java.awt.Dimension(24, 24));
        expandButton.setPreferredSize(new java.awt.Dimension(24, 24));
        expandButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                expandButtonMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                expandButtonMouseExited(evt);
            }
        });

        javax.swing.GroupLayout toolBarLayout = new javax.swing.GroupLayout(toolBar);
        toolBar.setLayout(toolBarLayout);
        toolBarLayout.setHorizontalGroup(
            toolBarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(toolBarLayout.createSequentialGroup()
                .addGroup(toolBarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sphereButton, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(boxButton, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(quadButton, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(104, Short.MAX_VALUE))
            .addGroup(toolBarLayout.createSequentialGroup()
                .addComponent(expandButton, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(reloadButton, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        toolBarLayout.setVerticalGroup(
            toolBarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(toolBarLayout.createSequentialGroup()
                .addGroup(toolBarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(expandButton, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(reloadButton, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                .addComponent(sphereButton, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(boxButton, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(quadButton, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5))
        );

        add(toolBar);
        toolBar.setBounds(0, 0, 120, 120);

        previewLabel.setBackground(new java.awt.Color(100, 100, 100));
        previewLabel.setForeground(new java.awt.Color(100, 100, 100));
        previewLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        previewLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        previewLabel.setIconTextGap(0);
        previewLabel.setMaximumSize(new java.awt.Dimension(100, 100));
        previewLabel.setMinimumSize(new java.awt.Dimension(100, 100));
        previewLabel.setOpaque(true);
        previewLabel.setPreferredSize(new java.awt.Dimension(100, 100));
        add(previewLabel);
        previewLabel.setBounds(20, 20, 100, 100);
    }// </editor-fold>//GEN-END:initComponents

    private void expandButtonMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_expandButtonMouseEntered
        mouseEntered(SwingUtilities.convertMouseEvent(expandButton, evt, this));
    }//GEN-LAST:event_expandButtonMouseEntered

    private void expandButtonMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_expandButtonMouseExited
        mouseExited(SwingUtilities.convertMouseEvent(expandButton, evt, this));
    }//GEN-LAST:event_expandButtonMouseExited

    private void sphereButtonMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sphereButtonMouseEntered
        mouseEntered(SwingUtilities.convertMouseEvent(sphereButton, evt, this));
    }//GEN-LAST:event_sphereButtonMouseEntered

    private void sphereButtonMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sphereButtonMouseExited
        mouseExited(SwingUtilities.convertMouseEvent(sphereButton, evt, this));
    }//GEN-LAST:event_sphereButtonMouseExited

    private void sphereButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sphereButtonActionPerformed
        renderer.switchDisplay(MaterialPreviewRenderer.DisplayType.Sphere);
        refresh();
    }//GEN-LAST:event_sphereButtonActionPerformed

    private void boxButtonMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_boxButtonMouseEntered
        mouseEntered(SwingUtilities.convertMouseEvent(boxButton, evt, this));
    }//GEN-LAST:event_boxButtonMouseEntered

    private void boxButtonMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_boxButtonMouseExited
        mouseExited(SwingUtilities.convertMouseEvent(boxButton, evt, this));
    }//GEN-LAST:event_boxButtonMouseExited

    private void boxButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxButtonActionPerformed
        renderer.switchDisplay(MaterialPreviewRenderer.DisplayType.Box);
        refresh();
    }//GEN-LAST:event_boxButtonActionPerformed

    private void reloadButtonMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_reloadButtonMouseEntered
        mouseEntered(SwingUtilities.convertMouseEvent(reloadButton, evt, this));
    }//GEN-LAST:event_reloadButtonMouseEntered

    private void reloadButtonMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_reloadButtonMouseExited
        mouseExited(SwingUtilities.convertMouseEvent(reloadButton, evt, this));
    }//GEN-LAST:event_reloadButtonMouseExited

    private void reloadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reloadButtonActionPerformed
        refresh();
    }//GEN-LAST:event_reloadButtonActionPerformed

    private void quadButtonMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_quadButtonMouseEntered
        mouseEntered(SwingUtilities.convertMouseEvent(quadButton, evt, this));
    }//GEN-LAST:event_quadButtonMouseEntered

    private void quadButtonMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_quadButtonMouseExited
        mouseExited(SwingUtilities.convertMouseEvent(quadButton, evt, this));
    }//GEN-LAST:event_quadButtonMouseExited

    private void quadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quadButtonActionPerformed
        renderer.switchDisplay(MaterialPreviewRenderer.DisplayType.Quad);
        refresh();
    }//GEN-LAST:event_quadButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton boxButton;
    private javax.swing.JButton expandButton;
    private javax.swing.JLabel previewLabel;
    private javax.swing.JButton quadButton;
    private javax.swing.JButton reloadButton;
    private javax.swing.JButton sphereButton;
    private javax.swing.JPanel toolBar;
    // End of variables declaration//GEN-END:variables

    @Override
    public void mouseClicked(MouseEvent e) {
    }
    
    @Override
    public void mousePressed(MouseEvent e) {
    }
    
    @Override
    public void mouseReleased(MouseEvent e) {
    }
    
    @Override
    public void mouseEntered(MouseEvent e) {
        toolBar.setVisible(true);
        t.stop();
    }
    Timer t = new Timer(500, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            toolBar.setVisible(false);
        }
    });
    
    @Override
    public void mouseExited(MouseEvent e) {
        t.restart();
    }
    
    @Override
    public void componentResized(ComponentEvent e) {
        update(e.getComponent());
    }
    
    @Override
    public void componentMoved(ComponentEvent e) {
        update(e.getComponent());
    }
    
    @Override
    public void componentShown(ComponentEvent e) {
        update(e.getComponent());
    }
    
    @Override
    public void componentHidden(ComponentEvent e) {
    }
    
    protected void update(Component c) {
        setLocation(c.getLocation().x + c.getWidth() - 150, c.getLocation().y + 10 - 120);
    }
    
    private void refresh() {
        if (mat != null) {
            renderer.showMaterial(mat);
        }
    }
}
