/*
 * 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 View;

/**
 *
 * @author Sneyder G
 */
import Controller.Launcher;
import Model.Shapefile.ShapeFile;
import Util.Punto;
import java.awt.Dimension;
import java.io.File;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author Sneyder G
 */
public class GUIMain extends javax.swing.JFrame {

    /**
     * Creates new form GUIFrame
     * @param name nombre de el frame principal
     */
    public GUIMain(String name) {
        initComponents();
        ListModel = new DefaultListModel();
        ListShapeFiles.setModel(ListModel);
        about =  new AboutFrame();
        this.setTitle(name);
        this.setIconImage(new ImageIcon(
                getClass().getResource("/Resources/Globe-32.png")).getImage());
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            SwingUtilities.updateComponentTreeUI(this);
            this.pack();
            this.setVisible(true);
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUIMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
    }

    /**
     * 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">                          
    private void initComponents() {

        canvasTemp = new View.Canvas();
        ButtonAdd = new javax.swing.JButton();
        ScrollPaneList = new javax.swing.JScrollPane();
        ListShapeFiles = new javax.swing.JList();
        ButtonDelLayer = new javax.swing.JButton();
        ButtonDelAll = new javax.swing.JButton();
        ButtonZoomExtend = new javax.swing.JButton();
        ButtonZoomLayer = new javax.swing.JButton();
        ButtonZoomIn = new javax.swing.JButton();
        ButtonZoomOut = new javax.swing.JButton();
        ButtonZoomMouse = new javax.swing.JButton();
        ButtonPaneo = new javax.swing.JButton();
        ButtonCoordinate = new javax.swing.JButton();
        MenuBar = new javax.swing.JMenuBar();
        MenuFile = new javax.swing.JMenu();
        MenuItemAdd = new javax.swing.JMenuItem();
        MenuItemDelSelected = new javax.swing.JMenuItem();
        MenuItemDelAll = new javax.swing.JMenuItem();
        SeparatorMenuFile = new javax.swing.JPopupMenu.Separator();
        MenuItemExit = new javax.swing.JMenuItem();
        MenuHelp = new javax.swing.JMenu();
        MenuItemAbout = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Visor GIS UD V 0.5");

        canvasTemp.setBackground(new java.awt.Color(255, 255, 255));
        canvasTemp.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));

        javax.swing.GroupLayout canvasTempLayout = new javax.swing.GroupLayout(canvasTemp);
        canvasTemp.setLayout(canvasTempLayout);
        canvasTempLayout.setHorizontalGroup(
                canvasTempLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 0, Short.MAX_VALUE)
        );
        canvasTempLayout.setVerticalGroup(
                canvasTempLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 0, Short.MAX_VALUE)
        );

        ButtonAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/Add-Shape-32.png"))); // NOI18N
        ButtonAdd.setToolTipText("Agregar una Capa");
        ButtonAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonAddActionPerformed(evt);
            }
        });

        ListShapeFiles.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        ListShapeFiles.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        ScrollPaneList.setViewportView(ListShapeFiles);

        ButtonDelLayer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/Delete-Shape-32.png"))); // NOI18N
        ButtonDelLayer.setToolTipText("Borrar Capa Seleccionada");
        ButtonDelLayer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonDelLayerActionPerformed(evt);
            }
        });

        ButtonDelAll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/Delete-All-Shape-32.png"))); // NOI18N
        ButtonDelAll.setToolTipText("Borrar Todas las Capas");
        ButtonDelAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonDelAllActionPerformed(evt);
            }
        });

        ButtonZoomExtend.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/Zoom-extend-black-32.png"))); // NOI18N
        ButtonZoomExtend.setToolTipText("Zoom Extendido");
        ButtonZoomExtend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonZoomExtendActionPerformed(evt);
            }
        });

        ButtonZoomLayer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/Zoom-capa-32.png"))); // NOI18N
        ButtonZoomLayer.setToolTipText("Zoom a la Capa Seleccionada");
        ButtonZoomLayer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonZoomLayerActionPerformed(evt);
            }
        });

        ButtonZoomIn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/Zoom-In-black-32.png"))); // NOI18N
        ButtonZoomIn.setToolTipText("Zoom In");
        ButtonZoomIn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonZoomInActionPerformed(evt);
            }
        });

        ButtonZoomOut.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/Zoom-Out-black-32.png"))); // NOI18N
        ButtonZoomOut.setToolTipText("Zoom Out");
        ButtonZoomOut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonZoomOutActionPerformed(evt);
            }
        });

        ButtonZoomMouse.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/Zoom-mouse-black-32.png"))); // NOI18N
        ButtonZoomMouse.setToolTipText("Zoom por Mouse");
        ButtonZoomMouse.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        ButtonZoomMouse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonZoomMouseActionPerformed(evt);
            }
        });

        ButtonPaneo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/Paneo-32.png"))); // NOI18N
        ButtonPaneo.setToolTipText("Paneo");
        ButtonPaneo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonPaneoActionPerformed(evt);
            }
        });

        ButtonCoordinate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/consulta-coordenada-black-32.png"))); // NOI18N
        ButtonCoordinate.setToolTipText("Consultar Coordenada por Mouse");
        ButtonCoordinate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonCoordinateActionPerformed(evt);
            }
        });

        MenuFile.setText("Archivo");

        MenuItemAdd.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        MenuItemAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/Add-Shape-32.png"))); // NOI18N
        MenuItemAdd.setText("Adicionar Capa");
        MenuItemAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MenuItemAddActionPerformed(evt);
            }
        });
        MenuFile.add(MenuItemAdd);

        MenuItemDelSelected.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_W, java.awt.event.InputEvent.CTRL_MASK));
        MenuItemDelSelected.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/Delete-Shape-32.png"))); // NOI18N
        MenuItemDelSelected.setText("Eliminar Capa Seleccionada");
        MenuItemDelSelected.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MenuItemDelSelectedActionPerformed(evt);
            }
        });
        MenuFile.add(MenuItemDelSelected);

        MenuItemDelAll.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.CTRL_MASK));
        MenuItemDelAll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/Delete-All-Shape-32.png"))); // NOI18N
        MenuItemDelAll.setText("Eliminar Todas las Capas");
        MenuItemDelAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MenuItemDelAllActionPerformed(evt);
            }
        });
        MenuFile.add(MenuItemDelAll);
        MenuFile.add(SeparatorMenuFile);

        MenuItemExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
        MenuItemExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/Lock-32.png"))); // NOI18N
        MenuItemExit.setText("Salir");
        MenuItemExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MenuItemExitActionPerformed(evt);
            }
        });
        MenuFile.add(MenuItemExit);

        MenuBar.add(MenuFile);

        MenuHelp.setText("Ayuda");

        MenuItemAbout.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_H, java.awt.event.InputEvent.CTRL_MASK));
        MenuItemAbout.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/About-32.png"))); // NOI18N
        MenuItemAbout.setText("Acerca de...");
        MenuItemAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MenuItemAboutActionPerformed(evt);
            }
        });
        MenuHelp.add(MenuItemAbout);

        MenuBar.add(MenuHelp);

        setJMenuBar(MenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                        .addComponent(ScrollPaneList, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(canvasTemp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGroup(layout.createSequentialGroup()
                                        .addGap(4, 4, 4)
                                        .addComponent(ButtonAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(ButtonDelLayer, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(ButtonDelAll, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(ButtonZoomExtend, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(ButtonZoomLayer, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(ButtonZoomIn, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(ButtonZoomOut, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(ButtonZoomMouse, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(ButtonPaneo, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(ButtonCoordinate, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(0, 672, Short.MAX_VALUE)))
                        .addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(ButtonAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(ButtonDelLayer, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                                .addComponent(ButtonDelAll, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                                .addComponent(ButtonZoomExtend, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                                .addComponent(ButtonZoomLayer, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                                .addComponent(ButtonZoomIn, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                                .addComponent(ButtonZoomOut, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                                .addComponent(ButtonZoomMouse, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                                .addComponent(ButtonPaneo, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                                .addComponent(ButtonCoordinate, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                        .addGap(9, 9, 9)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(canvasTemp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(ScrollPaneList, javax.swing.GroupLayout.DEFAULT_SIZE, 621, Short.MAX_VALUE))
                        .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    private void MenuItemAboutActionPerformed(java.awt.event.ActionEvent evt) {
        about.setLocationRelativeTo(this);
        about.setVisible(true);
    }

    private void ButtonZoomInActionPerformed(java.awt.event.ActionEvent evt) {
        Launcher.getInstance().setActiveTool(Canvas.ZOOM_IN);
    }

    private void ButtonZoomOutActionPerformed(java.awt.event.ActionEvent evt) {
        Launcher.getInstance().setActiveTool(Canvas.ZOOM_OUT);
    }

    private void ButtonZoomMouseActionPerformed(java.awt.event.ActionEvent evt) {
        Launcher.getInstance().setActiveTool(Canvas.ZOOM_MOUSE);
    }

    private void ButtonPaneoActionPerformed(java.awt.event.ActionEvent evt) {
        Launcher.getInstance().setActiveTool(Canvas.PANEO);
    }

    private void ButtonCoordinateActionPerformed(java.awt.event.ActionEvent evt) {
        Launcher.getInstance().setActiveTool(Canvas.COORDINATE);
    }

    private void ButtonAddActionPerformed(java.awt.event.ActionEvent evt) {
        addShapeFile();
    }

    private void MenuItemExitActionPerformed(java.awt.event.ActionEvent evt) {
        System.exit(0);
    }

    private void MenuItemAddActionPerformed(java.awt.event.ActionEvent evt) {
        addShapeFile();
    }

    private void MenuItemDelAllActionPerformed(java.awt.event.ActionEvent evt) {
        Launcher.getInstance().deleteAllShapes();
        ListModel.clear();
    }

    private void ButtonDelAllActionPerformed(java.awt.event.ActionEvent evt) {
        Launcher.getInstance().deleteAllShapes();
        ListModel.clear();
    }

    private void ButtonDelLayerActionPerformed(java.awt.event.ActionEvent evt) {
        ShapeFile shapeSelected = (ShapeFile) ListShapeFiles.getSelectedValue();
        if (shapeSelected != null) {
            Launcher.getInstance().deleteShape(shapeSelected);
            ListModel.removeElement(shapeSelected);
        }

    }

    private void MenuItemDelSelectedActionPerformed(java.awt.event.ActionEvent evt) {
        ShapeFile shapeSelected = (ShapeFile) ListShapeFiles.getSelectedValue();
        if (shapeSelected != null) {
            Launcher.getInstance().deleteShape(shapeSelected);
            ListModel.removeElement(shapeSelected);
        }
    }

    private void ButtonZoomLayerActionPerformed(java.awt.event.ActionEvent evt) {
        ShapeFile shapeSelected = (ShapeFile) ListShapeFiles.getSelectedValue();
        if (shapeSelected != null) {
            Launcher.getInstance().ZoomToLayer(shapeSelected.getEnvoltura());
        }
    }

    private void ButtonZoomExtendActionPerformed(java.awt.event.ActionEvent evt) {
        Launcher.getInstance().ZoomExtend();
    }

    private void addShapeFile() {
        JFileChooser FC = new JFileChooser();
        FC.setFileSelectionMode(JFileChooser.FILES_ONLY);
        if (!FC.isMultiSelectionEnabled()) {
            FC.setMultiSelectionEnabled(true);
        }
        FC.setFileFilter(new FileNameExtensionFilter("ShapeFiles", "shp"));
        FC.showOpenDialog(null);

        File tempFiles[] = FC.getSelectedFiles();

        if (tempFiles.length == 0) {
            return;
        }
        ShapeFile shapeTemp;
        for (File file : tempFiles) {

            if (!ListModel.contains(file)) {
                shapeTemp = new ShapeFile(file);
                ListModel.addElement(shapeTemp);
                Launcher.getInstance().addShapeFile(shapeTemp);
            }
        }

    }

    public Dimension getDimension() {
        return canvasTemp.getSize();
    }

    public void setActiveTool(final int t) {
        canvasTemp.setActiveTool(t);

    }

    public void setRubberBand(final Util.BoundBox rb) {
        canvasTemp.setRubberBand(rb);
    }

    public void repaintCanvas() {
        //canvasTemp.repaint();
        canvasTemp.paintImmediately(canvasTemp.getVisibleRect());
    }

    public void showQueryCoordinates(Punto puntoQuery) {
        JOptionPane.showMessageDialog(null, puntoQuery.toString());
    }

    private DefaultListModel ListModel;

    // Variables declaration - do not modify                     
    private javax.swing.JButton ButtonAdd;
    private javax.swing.JButton ButtonCoordinate;
    private javax.swing.JButton ButtonDelAll;
    private javax.swing.JButton ButtonDelLayer;
    private javax.swing.JButton ButtonPaneo;
    private javax.swing.JButton ButtonZoomExtend;
    private javax.swing.JButton ButtonZoomIn;
    private javax.swing.JButton ButtonZoomLayer;
    private javax.swing.JButton ButtonZoomMouse;
    private javax.swing.JButton ButtonZoomOut;
    private javax.swing.JList ListShapeFiles;
    private javax.swing.JMenuBar MenuBar;
    private javax.swing.JMenu MenuFile;
    private javax.swing.JMenu MenuHelp;
    private javax.swing.JMenuItem MenuItemAbout;
    private javax.swing.JMenuItem MenuItemAdd;
    private javax.swing.JMenuItem MenuItemDelAll;
    private javax.swing.JMenuItem MenuItemDelSelected;
    private javax.swing.JMenuItem MenuItemExit;
    private javax.swing.JScrollPane ScrollPaneList;
    private javax.swing.JPopupMenu.Separator SeparatorMenuFile;
    private View.Canvas canvasTemp;
    private AboutFrame about;
    // End of variables declaration                   
}
