/*
 * Copyright (C) 2014 SAA
 *
 * This program 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/>.
 */
package controlador.conversor;

import controlador.Diagrama;
import diagramas.conceitual.DiagramaConceitual;
import diagramas.logico.DiagramaLogico;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import util.BrLogger;

/**
 *
 * @author SAA
 */
public class conversorDialogo extends javax.swing.JDialog {

    /**
     * Creates new form conversorDialogo
     * @param parent
     * @param modal
     */
    public conversorDialogo(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        getRootPane().setDefaultButton(btnPronto);
        getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "CANCEL");
        getRootPane().getActionMap().put("CANCEL", new AbstractAction("CANCEL") {
            @Override
            public void actionPerformed(ActionEvent ae) {
                btnCancelarActionPerformed(ae);
            }
        });
    }

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

        buttonGroup1 = new javax.swing.ButtonGroup();
        jToolBar1 = new javax.swing.JToolBar();
        btnMostrarOrigem = new javax.swing.JToggleButton();
        btnMostrarDestino = new javax.swing.JToggleButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel3 = new javax.swing.JPanel();
        conversorDrawer1 = new controlador.conversor.conversorDrawer();
        jPanel1 = new javax.swing.JPanel();
        btnPronto = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnYesToAll = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jToolBar1.setBackground(new java.awt.Color(204, 204, 255));
        jToolBar1.setFloatable(false);
        jToolBar1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jToolBar1.setRollover(true);

        buttonGroup1.add(btnMostrarOrigem);
        btnMostrarOrigem.setText("Origem");
        btnMostrarOrigem.setFocusable(false);
        btnMostrarOrigem.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnMostrarOrigem.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnMostrarOrigem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMostrarOrigemActionPerformed(evt);
            }
        });
        jToolBar1.add(btnMostrarOrigem);

        buttonGroup1.add(btnMostrarDestino);
        btnMostrarDestino.setSelected(true);
        btnMostrarDestino.setText("Destino");
        btnMostrarDestino.setFocusable(false);
        btnMostrarDestino.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnMostrarDestino.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnMostrarDestino.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMostrarDestinoActionPerformed(evt);
            }
        });
        jToolBar1.add(btnMostrarDestino);

        jScrollPane1.setBackground(new java.awt.Color(153, 153, 153));

        conversorDrawer1.setMinimumSize(new java.awt.Dimension(600, 300));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(conversorDrawer1, javax.swing.GroupLayout.PREFERRED_SIZE, 627, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(conversorDrawer1, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(jPanel3);

        btnPronto.setText("Continuar");
        btnPronto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProntoActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar conversão");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnYesToAll.setText("Aceitar todas as sugestões a partir de agora");
        btnYesToAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnYesToAllActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(2, 2, 2)
                .addComponent(btnPronto)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnYesToAll)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(2, 2, 2)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnPronto)
                    .addComponent(btnCancelar)
                    .addComponent(btnYesToAll))
                .addGap(2, 2, 2))
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(5, 5, 5))
        );

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

    private void btnProntoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProntoActionPerformed
        Opcoes.Resultado = conversorOpcoes.resultOfQuestion.resOk;
        Opcoes.OPC = conversorDrawer1.getSelectedIndex();
        Opcoes.LastPosi = this.getLocation();
        setVisible(false);
    }//GEN-LAST:event_btnProntoActionPerformed

    private void btnYesToAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnYesToAllActionPerformed
        Opcoes.Resultado = conversorOpcoes.resultOfQuestion.resOkToAll;
        Opcoes.OPC = Opcoes.opcDefault;
        Opcoes.LastPosi = this.getLocation();
        setVisible(false);
    }//GEN-LAST:event_btnYesToAllActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        Opcoes.Resultado = conversorOpcoes.resultOfQuestion.respCancel;
        Opcoes.OPC = -1;
        Opcoes.LastPosi = this.getLocation();
        setVisible(false);
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnMostrarOrigemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMostrarOrigemActionPerformed
        origem.getEditor().setSelected(origem);
    }//GEN-LAST:event_btnMostrarOrigemActionPerformed

    private void btnMostrarDestinoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMostrarDestinoActionPerformed
        destino.getEditor().setSelected(destino);
    }//GEN-LAST:event_btnMostrarDestinoActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JToggleButton btnMostrarDestino;
    private javax.swing.JToggleButton btnMostrarOrigem;
    private javax.swing.JButton btnPronto;
    private javax.swing.JButton btnYesToAll;
    private javax.swing.ButtonGroup buttonGroup1;
    private controlador.conversor.conversorDrawer conversorDrawer1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables

    private Diagrama origem = null;
    private Diagrama destino = null;

    public void Inicializar(DiagramaConceitual ori, DiagramaLogico dest, conversorOpcoes opcoes, BufferedImage dig_img) {
        origem = ori;
        destino = dest;
        Opcoes = opcoes;
        Opcoes.OPC = -1;
        conversorDrawer1.setDiagramas(origem, destino);
        prepareQuestao(dig_img);
    }

    public conversorDrawer getDrawer() {
        return conversorDrawer1;
    }

    private conversorOpcoes Opcoes = null;

    private void prepareQuestao(BufferedImage dig_img) {
        conversorDrawer1.setObjAtivo(Opcoes.obj, dig_img);
        conversorDrawer1.Escreve(Opcoes);
        conversorDrawer1.revalidate();
        conversorDrawer1.repaint();
        //pack();
    }
}
