/*
 * WANDORA
 * Knowledge Extraction, Management, and Publishing Application
 * http://wandora.org
 * 
 * Copyright (C) 2004-2023 Wandora Team
 * 
 * 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/>.
 *
 * 
 * 
 * WandoraToolSelector.java
 *
 * Created on 23. helmikuuta 2009, 17:46
 */

package org.wandora.application.gui;


import java.awt.BorderLayout;
import java.util.ArrayList;

import org.wandora.application.Wandora;
import org.wandora.application.WandoraTool;


/**
 *
 * @author  akivela
 */
public class WandoraToolSelector extends javax.swing.JDialog {


	private static final long serialVersionUID = 1L;

	private Wandora wandora = null;
    private boolean selectToolAccepted = false;
    private WandoraToolTable toolTable = null;
    
    
    
    /** Creates new form WandoraToolSelector */
    public WandoraToolSelector(Wandora wandora) {
        super(wandora, true);
        this.wandora = wandora;
        initComponents();
        ArrayList<WandoraTool> allTools = wandora.getToolManager().getAllTools();
        toolTable = new WandoraToolTable(wandora);
        toolTable.initialize(allTools.toArray( new WandoraTool[] {} ));
        selectToolAccepted = false;
        
        toolSelectTablePanel.removeAll();
        toolSelectTablePanel.add(toolTable, BorderLayout.NORTH);
        toolSelectScrollPane.setColumnHeaderView(toolTable.getTableHeader());
//        toolSelectTitlePanel.add(toolTable.getTableHeader(), BorderLayout.CENTER);
        
        this.setSize(700, 350);
        wandora.centerWindow(this);
        this.setVisible(true);
    }

    
    public boolean selectToolAccepted() {
        return selectToolAccepted;
    }
    
    public WandoraTool getSelectedTool() {
        if(selectToolAccepted() && toolTable != null) {
            return toolTable.getSelectedTool();
        }
        return null;
    }
    
    
    /** 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() {
        java.awt.GridBagConstraints gridBagConstraints;

        toolSelectPanel = new javax.swing.JPanel();
        toolSelectLabel = new org.wandora.application.gui.simple.SimpleLabel();
        toolSelectTitlePanel = new javax.swing.JPanel();
        toolSelectScrollPane = new org.wandora.application.gui.simple.SimpleScrollPane();
        toolSelectTablePanel = new javax.swing.JPanel();
        toolSelectButtonPanel = new javax.swing.JPanel();
        toolSelectFillerPanel = new javax.swing.JPanel();
        toolSelectButton = new org.wandora.application.gui.simple.SimpleButton();
        toolSelectCancelButton = new org.wandora.application.gui.simple.SimpleButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tool Selector");

        toolSelectPanel.setLayout(new java.awt.GridBagLayout());

        toolSelectLabel.setText("Select tool and click Select");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        toolSelectPanel.add(toolSelectLabel, gridBagConstraints);

        toolSelectTitlePanel.setLayout(new java.awt.BorderLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        toolSelectPanel.add(toolSelectTitlePanel, gridBagConstraints);

        toolSelectTablePanel.setLayout(new java.awt.BorderLayout());
        toolSelectScrollPane.setViewportView(toolSelectTablePanel);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
        toolSelectPanel.add(toolSelectScrollPane, gridBagConstraints);

        toolSelectButtonPanel.setLayout(new java.awt.GridBagLayout());

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

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        toolSelectButtonPanel.add(toolSelectFillerPanel, gridBagConstraints);

        toolSelectButton.setText("Select");
        toolSelectButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        toolSelectButton.setMaximumSize(new java.awt.Dimension(70, 23));
        toolSelectButton.setMinimumSize(new java.awt.Dimension(70, 23));
        toolSelectButton.setPreferredSize(new java.awt.Dimension(70, 23));
        toolSelectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toolSelectButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 3);
        toolSelectButtonPanel.add(toolSelectButton, gridBagConstraints);

        toolSelectCancelButton.setText("Cancel");
        toolSelectCancelButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        toolSelectCancelButton.setMaximumSize(new java.awt.Dimension(70, 23));
        toolSelectCancelButton.setMinimumSize(new java.awt.Dimension(70, 23));
        toolSelectCancelButton.setPreferredSize(new java.awt.Dimension(70, 23));
        toolSelectCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toolSelectCancelButtonActionPerformed(evt);
            }
        });
        toolSelectButtonPanel.add(toolSelectCancelButton, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
        toolSelectPanel.add(toolSelectButtonPanel, gridBagConstraints);

        getContentPane().add(toolSelectPanel, java.awt.BorderLayout.CENTER);

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

private void toolSelectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toolSelectButtonActionPerformed
    this.selectToolAccepted = true;
    this.setVisible(false);
}//GEN-LAST:event_toolSelectButtonActionPerformed

private void toolSelectCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toolSelectCancelButtonActionPerformed
    this.selectToolAccepted = false;
    this.setVisible(false);
}//GEN-LAST:event_toolSelectCancelButtonActionPerformed



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton toolSelectButton;
    private javax.swing.JPanel toolSelectButtonPanel;
    private javax.swing.JButton toolSelectCancelButton;
    private javax.swing.JPanel toolSelectFillerPanel;
    private javax.swing.JLabel toolSelectLabel;
    private javax.swing.JPanel toolSelectPanel;
    private javax.swing.JScrollPane toolSelectScrollPane;
    private javax.swing.JPanel toolSelectTablePanel;
    private javax.swing.JPanel toolSelectTitlePanel;
    // End of variables declaration//GEN-END:variables

}
