/*
 * 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/>.
 *
 * 
 * 
 * WandoraToolManagerPanel.java
 *
 * Created on 30. tammikuuta 2006, 11:56
 */

package org.wandora.application;

import static org.wandora.utils.Tuples.t2;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JDialog;

import org.wandora.application.gui.UIConstants;
import org.wandora.application.gui.WandoraOptionPane;
import org.wandora.topicmap.TopicMapException;
import org.wandora.utils.Tuples.T2;
/**
 *
 * @author  olli
 */
public class WandoraToolManagerPanel extends javax.swing.JPanel {


	private static final long serialVersionUID = 1L;

	
	public static class ListWrapper {
        public T2<WandoraTool,String> tool;
        public ListWrapper(T2<WandoraTool,String> tool){
            this.tool=tool;
        }
        public String toString(){
            return tool.e2;
        }
    }
    public static class ComboboxWrapper{
        public WandoraTool tool;
        public ComboboxWrapper(WandoraTool tool){
            this.tool=tool;
        }
        public String toString(){
            return tool.getName()+" ("+tool.getClass().getName()+")";
        }
    }
    
    private Wandora admin;
    private WandoraToolManager manager;
    private DefaultListModel listModel;
    private JDialog newDialog;
    private JDialog parent;
    private JDialog renameDialog;
    private boolean renameCancelled;
    
    /** Creates new form AdminToolManagerPanel */
    public WandoraToolManagerPanel(WandoraToolManager manager, JDialog parent, Wandora admin) {
        this.admin = admin;
        this.manager = manager;
        this.parent = parent;
        listModel = new DefaultListModel();
        initComponents();
        refreshTools();
        
        newDialog=new JDialog(parent,true);
        newDialog.getContentPane().add(newToolPanel);
        newDialog.setTitle("New tool");
        
        renameDialog=new JDialog(parent,true);
        renameDialog.getContentPane().add(renamePanel);
        renameDialog.setTitle("Rename tool");
    }
    
    
    public void refreshTools(){
        listModel.removeAllElements();
        Vector<T2<WandoraTool,String>> tools=manager.getTools(typeComboBox.getSelectedItem().toString());
        for(T2<WandoraTool,String> tool : tools){
            listModel.addElement(new ListWrapper(tool));
        }        
        toolList.repaint();
    }
    
    
    
    private String getCurrentType() {
        return typeComboBox.getSelectedItem().toString();
    }
    
    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        newToolPanel = new javax.swing.JPanel();
        toolLabel = new org.wandora.application.gui.simple.SimpleLabel();
        toolComboBox = new org.wandora.application.gui.simple.SimpleComboBox();
        nameLabel = new org.wandora.application.gui.simple.SimpleLabel();
        nameTextField = new org.wandora.application.gui.simple.SimpleField();
        jSeparator2 = new javax.swing.JSeparator();
        descriptionLabel = new org.wandora.application.gui.simple.SimpleLabel();
        descriptionScrollPane = new javax.swing.JScrollPane();
        descriptionTextPane = new javax.swing.JTextPane();
        jPanel2 = new javax.swing.JPanel();
        newOKButton = new org.wandora.application.gui.simple.SimpleButton();
        newCancelButton = new org.wandora.application.gui.simple.SimpleButton();
        renamePanel = new javax.swing.JPanel();
        jLabel3 = new org.wandora.application.gui.simple.SimpleLabel();
        renameTextField = new org.wandora.application.gui.simple.SimpleField();
        jPanel3 = new javax.swing.JPanel();
        renameOKButton = new org.wandora.application.gui.simple.SimpleButton();
        renameCancelButton = new org.wandora.application.gui.simple.SimpleButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        toolList = new javax.swing.JList(listModel);
        buttonPanel = new javax.swing.JPanel();
        newButton = new org.wandora.application.gui.simple.SimpleButton();
        removeButton = new org.wandora.application.gui.simple.SimpleButton();
        configureButton = new org.wandora.application.gui.simple.SimpleButton();
        upButton = new org.wandora.application.gui.simple.SimpleButton();
        downButton = new org.wandora.application.gui.simple.SimpleButton();
        renameButton = new org.wandora.application.gui.simple.SimpleButton();
        okButton = new org.wandora.application.gui.simple.SimpleButton();
        typePanel = new javax.swing.JPanel();
        jLabel4 = new org.wandora.application.gui.simple.SimpleLabel();
        typeComboBox = new org.wandora.application.gui.simple.SimpleComboBox(org.wandora.application.WandoraToolManager.toolTypes);
        jSeparator1 = new javax.swing.JSeparator();

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

        toolLabel.setText("Tool class");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(7, 7, 0, 5);
        newToolPanel.add(toolLabel, gridBagConstraints);

        toolComboBox.setPreferredSize(new java.awt.Dimension(29, 21));
        toolComboBox.setEditable(false);
        toolComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toolComboBoxActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(7, 5, 0, 7);
        newToolPanel.add(toolComboBox, gridBagConstraints);

        nameLabel.setText("Name");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 7, 5, 5);
        newToolPanel.add(nameLabel, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(10, 5, 5, 7);
        newToolPanel.add(nameTextField, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(10, 7, 0, 7);
        newToolPanel.add(jSeparator2, gridBagConstraints);

        descriptionLabel.setText("Description");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 7, 2, 5);
        newToolPanel.add(descriptionLabel, gridBagConstraints);

        descriptionScrollPane.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(153, 153, 153)));
        descriptionScrollPane.setMaximumSize(new java.awt.Dimension(3, 50));
        descriptionScrollPane.setMinimumSize(new java.awt.Dimension(3, 50));
        descriptionScrollPane.setPreferredSize(new java.awt.Dimension(3, 50));

        descriptionTextPane.setBorder(null);
        descriptionTextPane.setEditable(false);
        descriptionTextPane.setFont(UIConstants.smallButtonLabelFont);
        descriptionTextPane.setMaximumSize(new java.awt.Dimension(1, 40));
        descriptionTextPane.setMinimumSize(new java.awt.Dimension(1, 40));
        descriptionTextPane.setPreferredSize(new java.awt.Dimension(1, 40));
        descriptionScrollPane.setViewportView(descriptionTextPane);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 2, 7);
        newToolPanel.add(descriptionScrollPane, gridBagConstraints);

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

        newOKButton.setText("OK");
        newOKButton.setMargin(new java.awt.Insets(2, 6, 2, 6));
        newOKButton.setMaximumSize(new java.awt.Dimension(65, 23));
        newOKButton.setPreferredSize(new java.awt.Dimension(65, 23));
        newOKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newOKButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        jPanel2.add(newOKButton, gridBagConstraints);

        newCancelButton.setText("Cancel");
        newCancelButton.setMargin(new java.awt.Insets(2, 6, 2, 6));
        newCancelButton.setMaximumSize(new java.awt.Dimension(65, 23));
        newCancelButton.setPreferredSize(new java.awt.Dimension(65, 23));
        newCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newCancelButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 8, 0, 0);
        jPanel2.add(newCancelButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 7, 7, 7);
        newToolPanel.add(jPanel2, gridBagConstraints);

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

        jLabel3.setText("New name");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(7, 7, 5, 5);
        renamePanel.add(jLabel3, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(7, 5, 5, 7);
        renamePanel.add(renameTextField, gridBagConstraints);

        renameOKButton.setText("OK");
        renameOKButton.setMargin(new java.awt.Insets(2, 6, 2, 6));
        renameOKButton.setMaximumSize(new java.awt.Dimension(65, 23));
        renameOKButton.setPreferredSize(new java.awt.Dimension(65, 23));
        renameOKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                renameOKButtonActionPerformed(evt);
            }
        });
        jPanel3.add(renameOKButton);

        renameCancelButton.setText("Cancel");
        renameCancelButton.setMargin(new java.awt.Insets(2, 6, 2, 6));
        renameCancelButton.setMaximumSize(new java.awt.Dimension(65, 23));
        renameCancelButton.setPreferredSize(new java.awt.Dimension(65, 23));
        renameCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                renameCancelButtonActionPerformed(evt);
            }
        });
        jPanel3.add(renameCancelButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 5, 2);
        renamePanel.add(jPanel3, gridBagConstraints);

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

        toolList.setFont(UIConstants.labelFont);
        toolList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        toolList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                toolListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(toolList);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jScrollPane1, gridBagConstraints);

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

        newButton.setText("New");
        newButton.setMaximumSize(new java.awt.Dimension(90, 23));
        newButton.setMinimumSize(new java.awt.Dimension(90, 23));
        newButton.setPreferredSize(new java.awt.Dimension(90, 23));
        newButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 3, 5);
        buttonPanel.add(newButton, gridBagConstraints);

        removeButton.setText("Remove");
        removeButton.setMaximumSize(new java.awt.Dimension(90, 23));
        removeButton.setMinimumSize(new java.awt.Dimension(90, 23));
        removeButton.setPreferredSize(new java.awt.Dimension(90, 23));
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 3, 5);
        buttonPanel.add(removeButton, gridBagConstraints);

        configureButton.setText("Configure");
        configureButton.setEnabled(false);
        configureButton.setMaximumSize(new java.awt.Dimension(90, 23));
        configureButton.setMinimumSize(new java.awt.Dimension(90, 23));
        configureButton.setPreferredSize(new java.awt.Dimension(90, 23));
        configureButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                configureButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        buttonPanel.add(configureButton, gridBagConstraints);

        upButton.setText("Up");
        upButton.setMaximumSize(new java.awt.Dimension(90, 23));
        upButton.setMinimumSize(new java.awt.Dimension(90, 23));
        upButton.setPreferredSize(new java.awt.Dimension(90, 23));
        upButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                upButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 3, 5);
        buttonPanel.add(upButton, gridBagConstraints);

        downButton.setText("Down");
        downButton.setMaximumSize(new java.awt.Dimension(90, 23));
        downButton.setMinimumSize(new java.awt.Dimension(90, 23));
        downButton.setPreferredSize(new java.awt.Dimension(90, 23));
        downButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                downButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
        buttonPanel.add(downButton, gridBagConstraints);

        renameButton.setText("Rename");
        renameButton.setMaximumSize(new java.awt.Dimension(90, 23));
        renameButton.setMinimumSize(new java.awt.Dimension(90, 23));
        renameButton.setPreferredSize(new java.awt.Dimension(90, 23));
        renameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                renameButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
        buttonPanel.add(renameButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        add(buttonPanel, gridBagConstraints);

        okButton.setText("OK");
        okButton.setMaximumSize(new java.awt.Dimension(90, 23));
        okButton.setMinimumSize(new java.awt.Dimension(90, 23));
        okButton.setPreferredSize(new java.awt.Dimension(90, 23));
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(okButton, gridBagConstraints);

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

        jLabel4.setText("Tool type");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        typePanel.add(jLabel4, gridBagConstraints);

        typeComboBox.setPreferredSize(new java.awt.Dimension(29, 21));
        typeComboBox.setEditable(false);
        typeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                typeComboBoxActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        typePanel.add(typeComboBox, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(typePanel, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jSeparator1, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void typeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_typeComboBoxActionPerformed
        refreshTools();
        boolean enableButtons=true;
        if("configurable".equals(getCurrentType())) {
            enableButtons=false;
        }
        renameButton.setEnabled(enableButtons);
        newButton.setEnabled(enableButtons);
        removeButton.setEnabled(enableButtons);
        upButton.setEnabled(enableButtons);
        downButton.setEnabled(enableButtons);
    }//GEN-LAST:event_typeComboBoxActionPerformed

    private void renameCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_renameCancelButtonActionPerformed
        renameCancelled=true;
        renameDialog.setVisible(false);
    }//GEN-LAST:event_renameCancelButtonActionPerformed

    private void renameOKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_renameOKButtonActionPerformed
        renameCancelled=false;
        renameDialog.setVisible(false);
    }//GEN-LAST:event_renameOKButtonActionPerformed

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

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

    private void toolComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toolComboBoxActionPerformed
        if(toolComboBox.getSelectedItem() != null) {
            WandoraTool tool = ((ComboboxWrapper)toolComboBox.getSelectedItem()).tool;
            if(tool != null) {
                nameTextField.setText(tool.getName());
                descriptionTextPane.setText(tool.getDescription());
            }
        }
    }//GEN-LAST:event_toolComboBoxActionPerformed

    private void newOKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newOKButtonActionPerformed
        String name=nameTextField.getText();
        if(name.trim().length()==0) {
            WandoraOptionPane.showMessageDialog(this, "You must enter a name for the tool!");
            return;
        }
        boolean added=manager.addTool(((ComboboxWrapper)toolComboBox.getSelectedItem()).tool,name,getCurrentType());
        if(!added) {
            WandoraOptionPane.showMessageDialog(this, "That name is already used for another tool!");
            return;
        }
        refreshTools();
        newDialog.setVisible(false);
    }//GEN-LAST:event_newOKButtonActionPerformed

    private void toolListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_toolListValueChanged
        ListWrapper wrapper=(ListWrapper)toolList.getSelectedValue();
        if(wrapper==null) {
            configureButton.setEnabled(false);
            return;
        }
        WandoraTool tool=wrapper.tool.e1;
        configureButton.setEnabled(tool.isConfigurable());
    }//GEN-LAST:event_toolListValueChanged

    private void downButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downButtonActionPerformed
        int sel=toolList.getSelectedIndex();
        Vector<T2<WandoraTool,String>> tools=manager.getTools(getCurrentType());
        if(sel==-1 || sel==tools.size()-1) return;
        T2<WandoraTool,String> temp=tools.get(sel+1);
        tools.setElementAt(tools.get(sel), sel+1);
        tools.setElementAt(temp,sel);
        refreshTools();
        toolList.setSelectedIndex(sel+1);
        manager.rewriteOptions();
        manager.getAdmin().toolsChanged();
    }//GEN-LAST:event_downButtonActionPerformed

    private void upButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_upButtonActionPerformed
        int sel=toolList.getSelectedIndex();
        if(sel==-1 || sel==0) return;
        Vector<T2<WandoraTool,String>> tools=manager.getTools(getCurrentType());
        T2<WandoraTool,String> temp=tools.get(sel-1);
        tools.setElementAt(tools.get(sel), sel-1);
        tools.setElementAt(temp,sel);
        refreshTools();
        toolList.setSelectedIndex(sel-1);
        manager.rewriteOptions();
        manager.getAdmin().toolsChanged();        
    }//GEN-LAST:event_upButtonActionPerformed

    private void configureButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_configureButtonActionPerformed
        ListWrapper wrapper=(ListWrapper)toolList.getSelectedValue();
        if(wrapper==null){
            configureButton.setEnabled(false);
            return;
        }
        WandoraTool tool=wrapper.tool.e1;
        int counter=toolList.getSelectedIndex();
        if(tool.isConfigurable()) {
            try{
                String type=getCurrentType();
                tool.configure(manager.getAdmin(),manager.getAdmin().getOptions(),"tools."+type+".item"+counter+".options.");
            }catch(TopicMapException tme){
                tme.printStackTrace(); // TODO EXCEPTION
            }
        }
    }//GEN-LAST:event_configureButtonActionPerformed

    private void renameButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_renameButtonActionPerformed
        int sel=toolList.getSelectedIndex();
        if(sel==-1) return;
        renameDialog.setSize(400,100);
        if(admin != null) admin.centerWindow(renameDialog);
        renameTextField.setText(manager.getTools(getCurrentType()).get(sel).e2);
        renameTextField.setSelectionStart(0); renameTextField.setSelectionEnd(renameTextField.getText().length());
        renameDialog.setVisible(true);
        if(renameCancelled) return;
        String name=renameTextField.getText();
        while(!manager.checkName(name)){
            WandoraOptionPane.showMessageDialog(this, "That name is already used by another tool.");
            renameDialog.setSize(400,100);
            org.wandora.utils.swing.GuiTools.centerWindow(renameDialog,this);
            renameTextField.setText(name);
            renameTextField.setSelectionStart(0); renameTextField.setSelectionEnd(renameTextField.getText().length());
            renameDialog.setVisible(true);
            if(renameCancelled) return;
            name=renameTextField.getText();
        }
        Vector<T2<WandoraTool,String>> tools=manager.getTools(getCurrentType());
        T2<WandoraTool,String> tool=tools.get(sel);
        tools.setElementAt(t2(tool.e1,name),sel);
        refreshTools();
        manager.rewriteOptions();
        manager.getAdmin().toolsChanged();                
    }//GEN-LAST:event_renameButtonActionPerformed

    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
        int sel=toolList.getSelectedIndex();
        if(sel==-1) return;
        Vector<T2<WandoraTool,String>> tools=manager.getTools(getCurrentType());
        T2<WandoraTool,String> tool=tools.get(sel);
        manager.removeTool(tool.e1, tool.e2, getCurrentType());
        refreshTools();
        manager.rewriteOptions();
        manager.getAdmin().toolsChanged();
    }//GEN-LAST:event_removeButtonActionPerformed

    private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButtonActionPerformed

        List<WandoraTool> tools=new Vector<WandoraTool>();
        for(WandoraTool tool : manager.getToolList(getCurrentType())) {
            tools.add(tool);
        }
        Collections.sort(tools,new Comparator<WandoraTool>() {
            public int compare(WandoraTool a1,WandoraTool a2){
                return a1.getName().compareTo(a2.getName());
            }
        });
        toolComboBox.removeAllItems();
        for(WandoraTool tool : tools) {
            toolComboBox.addItem(new ComboboxWrapper(tool));
        }
        
        newDialog.setSize(600,190);
        if(admin != null) admin.centerWindow(newDialog);
        newDialog.setVisible(true);
    }//GEN-LAST:event_newButtonActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton configureButton;
    private javax.swing.JLabel descriptionLabel;
    private javax.swing.JScrollPane descriptionScrollPane;
    private javax.swing.JTextPane descriptionTextPane;
    private javax.swing.JButton downButton;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JButton newButton;
    private javax.swing.JButton newCancelButton;
    private javax.swing.JButton newOKButton;
    private javax.swing.JPanel newToolPanel;
    private javax.swing.JButton okButton;
    private javax.swing.JButton removeButton;
    private javax.swing.JButton renameButton;
    private javax.swing.JButton renameCancelButton;
    private javax.swing.JButton renameOKButton;
    private javax.swing.JPanel renamePanel;
    private javax.swing.JTextField renameTextField;
    private javax.swing.JComboBox toolComboBox;
    private javax.swing.JLabel toolLabel;
    private javax.swing.JList toolList;
    private javax.swing.JComboBox typeComboBox;
    private javax.swing.JPanel typePanel;
    private javax.swing.JButton upButton;
    // End of variables declaration//GEN-END:variables
    
}
