/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controlador.editores;

import desenho.formas.Legenda;
import java.awt.Color;
import javax.swing.JList;

/**
 *
 * @author ccandido
 */
public class LegendaEditor extends javax.swing.JFrame {

    /**
     * Creates new form LegendaEditor
     */
    public LegendaEditor() {
        initComponents();
    }

    /**
     * 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() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        btnNovo = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnAtualizar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        btnFechar = new javax.swing.JButton();
        Scroller = new javax.swing.JScrollPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

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

        btnNovo.setText("Novo");
        btnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovoActionPerformed(evt);
            }
        });
        jPanel1.add(btnNovo);

        btnEditar.setText("Editar");
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });
        jPanel1.add(btnEditar);

        btnAtualizar.setText("Atualizar");
        btnAtualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAtualizarActionPerformed(evt);
            }
        });
        jPanel1.add(btnAtualizar);

        btnExcluir.setText("Excluir");
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });
        jPanel1.add(btnExcluir);

        btnFechar.setText("Fechar");
        btnFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFecharActionPerformed(evt);
            }
        });
        jPanel1.add(btnFechar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(Scroller, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(Scroller)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovoActionPerformed
        if (atual == null) {
            return;
        }
        String cor = util.Dialogos.ShowDlgCor(this.getRootPane(), "");
        if (!"".equals(cor)) {
            Color c = util.Utilidades.StringToColor(cor);//new Color(Integer.parseInt(cor));
            String res = util.Dialogos.ShowDlgInputText(this.getRootPane(), "");
            if (!"".equals(res)) {
                atual.addLegenda(res, c);
                Init(atual);
                Lista.repaint();
                RefreshInpector();
            }
        }
    }//GEN-LAST:event_btnNovoActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        if (Lista != null) {
            if (Lista.getSelectedIndex() > -1) {
                String txt = atual.getItens().get(Lista.getSelectedIndex()).getTexto();
                String res = util.Dialogos.ShowDlgInputText(this.getRootPane(), txt);
                if (!"".equals(res)) {
                    atual.setTextLegenda(Lista.getSelectedIndex(), res);
                    Init(atual);
                    Lista.repaint();
                    RefreshInpector();
                }
            }
        }
    }//GEN-LAST:event_btnEditarActionPerformed

    private void btnAtualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAtualizarActionPerformed
        if (atual == null) {
            return;
        }
        atual.LoadColorList();
        RefreshInpector();
        invalidate();
    }//GEN-LAST:event_btnAtualizarActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
        if (Lista != null) {
            if (Lista.getSelectedIndex() > -1) {
                atual.RemoveLegenda(Lista.getSelectedIndex());
                //atual.getItens().remove(Lista.getSelectedIndex());
                Init(atual);
                RefreshInpector();
            }
        }
    }//GEN-LAST:event_btnExcluirActionPerformed

    private void btnFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFecharActionPerformed
        setVisible(false);
    }//GEN-LAST:event_btnFecharActionPerformed

    private Legenda.ItemDeLegenda[] itens;
    private JList Lista;
    private Legenda atual = null;

    public void Init(Legenda legenda) {
        atual = legenda;
        if (atual == null) {
            if (Lista != null) {
                remove(Lista);
                Scroller.setViewportView(null);
                return;
            }
        }

        if (!legenda.getItens().isEmpty()) {
            itens = legenda.getItens().toArray(new Legenda.ItemDeLegenda[]{});
        } else {
            itens = new Legenda.ItemDeLegenda[]{};
        }
        if (Lista != null) {
            remove(Lista);
        }
        Lista = new JList(itens);
        Lista.setModel(new javax.swing.AbstractListModel() {

            @Override
            public int getSize() {
                return itens.length;
            }

            @Override
            public Object getElementAt(int i) {
                return itens[i];
            }
        });
        add(Lista);
        Scroller.setViewportView(Lista);
        Lista.setCellRenderer(new JListItemParaItemLegenda(legenda.getTipo() == Legenda.TipoLegenda.tpLinhas));
        btnAtualizar.setEnabled(legenda.getTipo() == Legenda.TipoLegenda.tpCores);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane Scroller;
    private javax.swing.JButton btnAtualizar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnFechar;
    private javax.swing.JButton btnNovo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

    private void RefreshInpector() {
        if (atual != null) {
            atual.RefreshInpector();
        }
    }
}
