package de.sofd.viskit.ui;

import de.sofd.draw2d.viewer.tools.DrawingViewerTool;
import de.sofd.draw2d.viewer.tools.EllipseTool;
import de.sofd.draw2d.viewer.tools.PolygonTool;
import de.sofd.draw2d.viewer.tools.RectangleTool;
import de.sofd.draw2d.viewer.tools.SelectorTool;
import de.sofd.util.BiHashMap;
import de.sofd.util.BiMap;

import javax.swing.JToggleButton;
import org.apache.log4j.Logger;

/**
 * Swing UI component that allows the user to interactively select a ROI drawing tool
 * (e.g. ellipse tool, rectangle tool, selector tool) in the UI. The selection
 * is represented as the corresponding {@link DrawingViewerTool} subclass, e.g.
 * {@link EllipseTool}, {@link RectangleTool} or {@link SelectorTool}, which is
 * exposed in the {@link #getToolClass()} property of the component. It may also
 * be set programmatically ({@link #setToolClass(Class)}).
 * <p>
 * In the UI, this class exposes the selection as a number of toggle buttons
 * corresponding to the tool classes, where the button that corresponds to the
 * selected tool class will be selected. These UI aspects aren't exposed through
 * the public API though, and may be changed by a subclass of this class.
 */
public class RoiToolPanel extends javax.swing.JPanel implements RoiToolPane {

    private final BiMap<JToggleButton, Class<? extends DrawingViewerTool> > toolClassesByButton =
            new BiHashMap<JToggleButton, Class<? extends DrawingViewerTool> >();

    private Class<? extends DrawingViewerTool> toolClass;

    /** Creates new form RoiJPanel */
    public RoiToolPanel() {
        initComponents();
        toolClassesByButton.put(selectorToggleButton, SelectorTool.class);
        toolClassesByButton.put(ellipseToggleButton, EllipseTool.class);
        toolClassesByButton.put(rectangleToggleButton, RectangleTool.class);
        toolClassesByButton.put(polygonToggleButton, PolygonTool.class);
    }

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

        roiButtonGroup = new javax.swing.ButtonGroup();
        selectorToggleButton = new javax.swing.JToggleButton();
        ellipseToggleButton = new javax.swing.JToggleButton();
        rectangleToggleButton = new javax.swing.JToggleButton();
        polygonToggleButton = new javax.swing.JToggleButton();

        roiButtonGroup.add(selectorToggleButton);
        selectorToggleButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/de/sofd/viskit/ui/selector.png"))); // NOI18N
        selectorToggleButton.setSelected(true);
        selectorToggleButton.setToolTipText("ROI Selector");
        selectorToggleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toolButtonClicked(evt);
            }
        });

        roiButtonGroup.add(ellipseToggleButton);
        ellipseToggleButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/de/sofd/viskit/ui/ellipse.png"))); // NOI18N
        ellipseToggleButton.setToolTipText("Draw ROI Ellipse");
        ellipseToggleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toolButtonClicked(evt);
            }
        });

        roiButtonGroup.add(rectangleToggleButton);
        rectangleToggleButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/de/sofd/viskit/ui/rectangle.png"))); // NOI18N
        rectangleToggleButton.setToolTipText("Draw ROI Rectangle");
        rectangleToggleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toolButtonClicked(evt);
            }
        });

        roiButtonGroup.add(polygonToggleButton);
        polygonToggleButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/de/sofd/viskit/ui/polygon.png"))); // NOI18N
        polygonToggleButton.setToolTipText("Draw ROI Polygon");
        polygonToggleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toolButtonClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(selectorToggleButton)
                .addComponent(ellipseToggleButton)
                .addComponent(rectangleToggleButton)
                .addComponent(polygonToggleButton))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(selectorToggleButton, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(ellipseToggleButton, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(rectangleToggleButton, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(polygonToggleButton, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void toolButtonClicked(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toolButtonClicked
        Class<? extends DrawingViewerTool> toolClass = toolClassesByButton.get(evt.getSource());
        setToolClass(toolClass);
}//GEN-LAST:event_toolButtonClicked

    /**
     * Get the value of toolClass
     *
     * @return the value of toolClass
     */
    @Override
    public Class<? extends DrawingViewerTool> getToolClass() {
        return toolClass;
    }

    /**
     * Set the value of toolClass
     *
     * @param toolClass new value of toolClass
     */
    @Override
    public void setToolClass(Class<? extends DrawingViewerTool> toolClass) {
        Class<? extends DrawingViewerTool> oldToolClass = this.toolClass;
        if (this.toolClass != toolClass) {
            JToggleButton button = toolClassesByButton.reverseGet(toolClass);
            button.setSelected(true);
            this.toolClass = toolClass;
            firePropertyChange(PROP_TOOLCLASS, oldToolClass, toolClass);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    javax.swing.JToggleButton ellipseToggleButton;
    javax.swing.JToggleButton polygonToggleButton;
    javax.swing.JToggleButton rectangleToggleButton;
    private javax.swing.ButtonGroup roiButtonGroup;
    javax.swing.JToggleButton selectorToggleButton;
    // End of variables declaration//GEN-END:variables

    static final Logger log4jLogger = Logger.getLogger(RoiToolPanel.class);

    /**
     * @return the ellipseToggleButton
     */
    public javax.swing.JToggleButton getEllipseToggleButton() {
        return ellipseToggleButton;
    }

    /**
     * @return the polygonToggleButton
     */
    public javax.swing.JToggleButton getPolygonToggleButton() {
        return polygonToggleButton;
    }

    /**
     * @return the rectangleToggleButton
     */
    public javax.swing.JToggleButton getRectangleToggleButton() {
        return rectangleToggleButton;
    }

    /**
     * @return the selectorToggleButton
     */
    public javax.swing.JToggleButton getSelectorToggleButton() {
        return selectorToggleButton;
    }
}
