/*

 * Title: japlot.jaxodraw
 * Description: Graphical user interface for drawing Feynman diagrams
 * @author Daniele Binosi 
 * @author Lukas Theussl  

 Copyright (C) 2003-2006,  Daniele Binosi and Lukas Theussl

 See the file LICENSE in the source distribution home directory
 for a full copy of the GPL (GNU General Public License).

    This file is part of japlot.jaxodraw.

    japlot.jaxodraw 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 2 of the License, or
    (at your option) any later version.

    japlot.jaxodraw 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, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


 */
package japlot.jaxodraw;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.image.MemoryImageSource;

import java.util.ResourceBundle;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.TitledBorder;

import jhplot.jadraw.JaArcObject;
import jhplot.jadraw.JaFArc;
import jhplot.jadraw.JaGArc;
import jhplot.jadraw.JaGlArc;
import jhplot.jadraw.JaPArc;
import jhplot.jadraw.JaSArc;


/** A panel to change the parameters of an arc object. */
public class JaxoArcOptionsPanel extends JPanel implements ItemListener,
    ActionListener {
    private ResourceBundle language =
        ResourceBundle.getBundle(JaxoPrefs.getPref(JaxoPrefs.PREF_LANGUAGE));
    private JaArcObject theArc;
    private String theParticle;
    private Icon theIcon;
    private ImageIcon colorPreviewIcon;
    private Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    private SpinnerNumberModel modelampin;
    private SpinnerNumberModel modelwigin;
    private JButton colorbt;
    private JPanel colorPanel;
    private TitledBorder colorPanelTitle;
    private JCheckBox arrowcb;
    private JCheckBox flipcb;
    private JPanel arcArrowPanel;
    private TitledBorder arcArrowTitle;
    private Color newColor;
    private Image colorPreview;
    private boolean newFlip;
    private boolean newArrow;
    private boolean newEnd;
    private int newAmp;
    private float newFreq;
    private int newWiggles;
    private float newStroke;
    private float newDash;
    private boolean changed = false;
    private boolean newDLine;
    private float newDLSep;
    private JCheckBox dlcb;
    private JLabel stsep;
    private JSpinner spsep;
    private int nx1;
    private int nx2;
    private int nx3;
    private int ny1;
    private int ny2;
    private int ny3;
    private JRadioButton middlerb;
    private JRadioButton endrb;
    private JPanel arcMiddlePanel;
    private TitledBorder arcMiddleTitle;

    ////

    /** Constructor: pops up a panel that allows to change all the parameters
     *  of the arc object.
     * @param newArc The arc object that is edited.
     */
    public JaxoArcOptionsPanel(JaArcObject newArc) {
        this.theArc = newArc;

        nx1 = (int) Math.round(theArc.getX1());
        ny1 = (int) Math.round(theArc.getY1());
        nx2 = (int) Math.round(theArc.getX2());
        ny2 = (int) Math.round(theArc.getY2());
        nx3 = theArc.getX();
        ny3 = theArc.getY();

        double[] pars = theArc.getArcParameters();

        newColor = theArc.getColor();
        colorPreview = getChooserImage(newColor);
        colorPreviewIcon = new ImageIcon(colorPreview);

        newArrow = theArc.getArrow();
        newFlip = theArc.getFlip();
        newEnd = theArc.getEnd();
        newAmp = theArc.getAmp();

        newStroke = theArc.getStroke();
        newDash = theArc.getDash();

        newFreq = theArc.getFreq();

        newDLine = theArc.getDoubleLine();
        newDLSep = theArc.getDLSeparation();

        if (newDLSep < 0.5) {
            newDLSep = JaxoPrefs.getFloatPref(JaxoPrefs.PREF_DLSEP);
        }

        if (theArc instanceof JaFArc) {
            theParticle = language.getString("Fermion_arc_parameters");
            theIcon =
                new ImageIcon(JaxoArcOptionsPanel.class.getClassLoader()
                                                       .getResource("japlot/jaxodraw/icons/farc.gif"));
        } else if (theArc instanceof JaSArc) {
            theParticle = language.getString("Scalar_arc_parameters");
            theIcon =
                new ImageIcon(JaxoArcOptionsPanel.class.getClassLoader()
                                                       .getResource("japlot/jaxodraw/icons/sarc.gif"));
        } else if (theArc instanceof JaGArc) {
            theParticle = language.getString("Ghost_arc_parameters");
            theIcon =
                new ImageIcon(JaxoArcOptionsPanel.class.getClassLoader()
                                                       .getResource("japlot/jaxodraw/icons/garc.gif"));
        } else if (theArc instanceof JaPArc) {
            theParticle = language.getString("Photon_arc_parameters");
            theIcon =
                new ImageIcon(JaxoArcOptionsPanel.class.getClassLoader()
                                                       .getResource("japlot/jaxodraw/icons/photonarc.gif"));
            newWiggles =
                (int) Math.round(((pars[2] * 2 * Math.abs(pars[3] / 2)) / newFreq));
        } else if (theArc instanceof JaGlArc) {
            theParticle = language.getString("Gluon_arc_parameters");
            theIcon =
                new ImageIcon(JaxoArcOptionsPanel.class.getClassLoader()
                                                       .getResource("japlot/jaxodraw/icons/gluonarc.gif"));
            newWiggles =
                (int) Math.round((
                        (pars[2] * 2 * Math.abs(pars[3] / 2)) - newFreq
                    ) / newFreq / 0.6f);
        }

        // Dimension options
        JPanel px1 = new JPanel();
        px1.setLayout(new GridLayout(0, 2));

        SpinnerNumberModel modelX1 =
            new SpinnerNumberModel(nx1, 0, d.width, 1);
        JLabel lx1 = new JLabel("X1: ", JLabel.RIGHT);
        px1.add(lx1);

        JSpinner spx1 = new JSpinner(modelX1);
        px1.add(spx1);

        JPanel px2 = new JPanel();
        px2.setLayout(new GridLayout(0, 2));

        SpinnerNumberModel modelX2 =
            new SpinnerNumberModel(nx2, 0, d.width, 1);
        JLabel lx2 = new JLabel("X2: ", JLabel.RIGHT);
        px2.add(lx2);

        JSpinner spx2 = new JSpinner(modelX2);
        px2.add(spx2);

        JPanel px3 = new JPanel();
        px3.setLayout(new GridLayout(0, 2));

        SpinnerNumberModel modelX3 =
            new SpinnerNumberModel(nx3, 0, d.width, 1);
        JLabel lx3 = new JLabel("X3: ", JLabel.RIGHT);
        px3.add(lx3);

        JSpinner spx3 = new JSpinner(modelX3);
        px3.add(spx3);

        JPanel py1 = new JPanel();
        py1.setLayout(new GridLayout(0, 2));

        SpinnerNumberModel modelY1 =
            new SpinnerNumberModel(ny1, 0, d.height, 1);
        JLabel ly1 = new JLabel("Y1: ", JLabel.RIGHT);
        py1.add(ly1);

        JSpinner spy1 = new JSpinner(modelY1);
        py1.add(spy1);

        JPanel py2 = new JPanel();
        py2.setLayout(new GridLayout(0, 2));

        SpinnerNumberModel modelY2 =
            new SpinnerNumberModel(ny2, 0, d.height, 1);
        JLabel ly2 = new JLabel("Y2: ", JLabel.RIGHT);
        py2.add(ly2);

        JSpinner spy2 = new JSpinner(modelY2);
        py2.add(spy2);

        JPanel py3 = new JPanel();
        py3.setLayout(new GridLayout(0, 2));

        SpinnerNumberModel modelY3 =
            new SpinnerNumberModel(ny3, 0, d.height, 1);
        JLabel ly3 = new JLabel("Y3: ", JLabel.RIGHT);
        py3.add(ly3);

        JSpinner spy3 = new JSpinner(modelY3);
        py3.add(spy3);

        JPanel dimSPanel = new JPanel();
        GridBagLayout gb = new GridBagLayout();
        GridBagConstraints sc = new GridBagConstraints();
        dimSPanel.setLayout(gb);

        sc.gridx = 0;
        sc.gridy = 0;
        px1.setPreferredSize(new Dimension(120, 20));
        gb.setConstraints(px1, sc);
        dimSPanel.add(px1, sc);

        sc.gridx = 0;
        sc.gridy = 1;
        sc.insets = new Insets(3, 0, 3, 0);
        py1.setPreferredSize(new Dimension(120, 20));
        gb.setConstraints(py1, sc);
        dimSPanel.add(py1, sc);

        sc.gridx = 1;
        sc.gridy = 0;
        px2.setPreferredSize(new Dimension(120, 20));
        gb.setConstraints(px2, sc);
        dimSPanel.add(px2, sc);

        sc.gridx = 1;
        sc.gridy = 1;
        sc.insets = new Insets(3, 0, 3, 0);
        py2.setPreferredSize(new Dimension(120, 20));
        gb.setConstraints(py2, sc);
        dimSPanel.add(py2, sc);

        sc.gridx = 2;
        sc.gridy = 0;
        px3.setPreferredSize(new Dimension(120, 20));
        gb.setConstraints(px3, sc);
        dimSPanel.add(px3, sc);

        sc.gridx = 2;
        sc.gridy = 1;
        sc.insets = new Insets(3, 0, 3, 0);
        py3.setPreferredSize(new Dimension(120, 20));
        gb.setConstraints(py3, sc);
        dimSPanel.add(py3, sc);

        JPanel finalDimSPanel = new JPanel();
        finalDimSPanel.setLayout(new BoxLayout(finalDimSPanel, BoxLayout.X_AXIS));

        TitledBorder dimSTitle =
            BorderFactory.createTitledBorder(language.getString(
                    "Points_coordinates"));

        //language.getString("Center/Radius"));
        finalDimSPanel.setBorder(dimSTitle);
        finalDimSPanel.add(dimSPanel);

        // Stroke options
        JPanel pstin = new JPanel();
        GridBagLayout subgb = new GridBagLayout();
        GridBagConstraints subc = new GridBagConstraints();
        pstin.setLayout(subgb);

        SpinnerNumberModel modelstin =
            new SpinnerNumberModel(newStroke, 0.0, 100.0, 0.2);
        JLabel stin =
            new JLabel(language.getString("Line_Width"), JLabel.RIGHT);
        JSpinner spstin = new JSpinner(modelstin);

        subc.gridx = 0;
        subc.gridy = 0;
        subc.insets = new Insets(0, 5, 0, 5);
        subgb.setConstraints(stin, subc);
        pstin.add(stin, subc);

        subc.gridx = 1;
        subc.gridy = 0;
        spstin.setPreferredSize(new Dimension(50, 20));
        subgb.setConstraints(spstin, subc);
        pstin.add(spstin, subc);

        JPanel pdsin = new JPanel();
        GridBagLayout subgb1 = new GridBagLayout();
        GridBagConstraints subc1 = new GridBagConstraints();
        pdsin.setLayout(subgb);

        SpinnerNumberModel modeldsin =
            new SpinnerNumberModel(newDash, 0.0, 50.0, 0.2);
        JLabel dsin =
            new JLabel(" " + language.getString("Dash_Size"), JLabel.RIGHT);
        pdsin.add(dsin);

        JSpinner spdsin = new JSpinner(modeldsin);

        subc1.gridx = 0;
        subc1.gridy = 0;
        subc1.insets = new Insets(0, 5, 5, 5);
        subgb1.setConstraints(dsin, subc1);
        pdsin.add(dsin, subc1);

        subc1.gridx = 1;
        subc1.gridy = 0;
        spdsin.setPreferredSize(new Dimension(50, 20));
        subgb1.setConstraints(spdsin, subc1);
        pdsin.add(spdsin, subc1);

        if (theArc instanceof JaFArc || theArc instanceof JaPArc
                || theArc instanceof JaGlArc) {
            dsin.setEnabled(false);
            spdsin.setEnabled(false);
        }

        JPanel strSPanel = new JPanel();
        GridBagLayout gb1 = new GridBagLayout();
        GridBagConstraints sc1 = new GridBagConstraints();
        strSPanel.setLayout(gb1);

        sc1.gridx = 0;
        sc1.gridy = 0;
        gb1.setConstraints(pstin, sc1);
        strSPanel.add(pstin, sc1);

        sc1.gridx = 0;
        sc1.gridy = 1;
        sc1.insets = new Insets(5, 0, 0, 0);
        gb1.setConstraints(pdsin, sc1);
        strSPanel.add(pdsin, sc1);

        JPanel finalStrSPanel = new JPanel();
        finalStrSPanel.setLayout(new BoxLayout(finalStrSPanel, BoxLayout.X_AXIS));

        TitledBorder strSTitle =
            BorderFactory.createTitledBorder(language.getString(
                    "Width/Dashing"));
        finalStrSPanel.setBorder(strSTitle);
        finalStrSPanel.add(strSPanel);

        // Color options
        colorbt = new JButton(colorPreviewIcon);
        colorbt.addActionListener(this);
        colorPanel = new JPanel();
        colorPanelTitle =
            BorderFactory.createTitledBorder(language.getString("Color"));
        colorPanel.setBorder(colorPanelTitle);
        colorPanel.add(colorbt);

        // Amplitude and wiggles options
        JPanel finalAwSPanel = new JPanel();

        if (theArc instanceof JaPArc || theArc instanceof JaGlArc) {
            JPanel pampin = new JPanel();
            GridBagLayout subgb2 = new GridBagLayout();
            GridBagConstraints subc2 = new GridBagConstraints();
            pampin.setLayout(subgb2);

            modelampin = new SpinnerNumberModel(newAmp, -500.0, 500.0, 1);

            JLabel ampin =
                new JLabel(language.getString("Amplitude"), JLabel.RIGHT);
            JSpinner spampin = new JSpinner(modelampin);

            subc2.gridx = 0;
            subc2.gridy = 0;
            subgb2.setConstraints(ampin, subc2);
            pampin.add(ampin, subc2);

            subc2.gridx = 1;
            subc2.gridy = 0;
            spampin.setPreferredSize(new Dimension(60, 20));
            subgb2.setConstraints(spampin, subc2);
            pampin.add(spampin, subc2);

            JPanel pwigin = new JPanel();
            GridBagLayout subgb3 = new GridBagLayout();
            GridBagConstraints subc3 = new GridBagConstraints();
            pampin.setLayout(subgb3);

            modelwigin = new SpinnerNumberModel(newWiggles, 0.0, 1000.0, 1);

            JLabel wigin =
                new JLabel("  " + language.getString("Wiggles"), JLabel.RIGHT);
            JSpinner spwigin = new JSpinner(modelwigin);

            subc3.gridx = 0;
            subc3.gridy = 0;
            subgb3.setConstraints(wigin, subc3);
            pwigin.add(wigin, subc3);

            subc3.gridx = 1;
            subc3.gridy = 0;
            spwigin.setPreferredSize(new Dimension(60, 20));
            subgb3.setConstraints(spwigin, subc3);
            pwigin.add(spwigin, subc3);

            JPanel awSPanel = new JPanel();
            GridBagLayout gb2 = new GridBagLayout();
            GridBagConstraints sc2 = new GridBagConstraints();
            awSPanel.setLayout(gb2);

            sc2.gridx = 0;
            sc2.gridy = 0;
            gb2.setConstraints(pampin, sc2);
            awSPanel.add(pampin, sc2);

            sc2.gridx = 0;
            sc2.gridy = 1;
            sc2.insets = new Insets(0, 0, 0, 0);
            gb2.setConstraints(pwigin, sc2);
            awSPanel.add(pwigin, sc2);

            finalAwSPanel.setLayout(new BoxLayout(finalAwSPanel,
                    BoxLayout.X_AXIS));

            TitledBorder awSTitle =
                BorderFactory.createTitledBorder(language.getString(
                        "Amplitude/Wiggles"));
            finalAwSPanel.setBorder(awSTitle);
            finalAwSPanel.add(awSPanel);
        }

        // Arrow options
        if (theArc instanceof JaFArc || theArc instanceof JaSArc
                || theArc instanceof JaGArc) {
            arrowcb = new JCheckBox(language.getString("Arrow"));
            arrowcb.setSelected(theArc.getArrow());
            arrowcb.addItemListener(this);
            flipcb = new JCheckBox(language.getString("Flip"));
            flipcb.setSelected(theArc.getFlip());
            flipcb.addItemListener(this);
            arcArrowPanel = new JPanel();
            arcArrowPanel.setLayout(new GridLayout(0, 1));
            arcArrowTitle =
                BorderFactory.createTitledBorder(language.getString("Arrow"));
            arcArrowPanel.setBorder(arcArrowTitle);
            arcArrowPanel.add(arrowcb);
            arcArrowPanel.add(flipcb);
        }

        // Middle options
        if (theArc instanceof JaFArc || theArc instanceof JaSArc
                || theArc instanceof JaGArc) {
            middlerb = new JRadioButton(language.getString("Middle"));
            middlerb.setSelected(!newEnd);
            middlerb.addItemListener(this);
            endrb = new JRadioButton(language.getString("End"));
            endrb.setSelected(newEnd);

            if (theArc instanceof JaSArc || theArc instanceof JaGArc) {
                endrb.setEnabled(false);
            }

            endrb.addItemListener(this);

            ButtonGroup bg = new ButtonGroup();
            bg.add(middlerb);
            bg.add(endrb);

            arcMiddlePanel = new JPanel();
            arcMiddlePanel.setLayout(new GridLayout(0, 1));
            arcMiddleTitle =
                BorderFactory.createTitledBorder(language.getString(
                        "Arrow_Position"));
            arcMiddlePanel.setBorder(arcMiddleTitle);
            arcMiddlePanel.add(middlerb);
            arcMiddlePanel.add(endrb);
        }

        // Double Line options
        dlcb = new JCheckBox(language.getString("Double_line"));
        dlcb.setSelected(newDLine);
        dlcb.addItemListener(this);

        SpinnerNumberModel modelsep =
            new SpinnerNumberModel(newDLSep, 0.5, 5.0, 0.5);
        stsep = new JLabel(language.getString("Separation") + ": ");
        spsep = new JSpinner(modelsep);
        stsep.setEnabled(newDLine);
        spsep.setEnabled(newDLine);

        JPanel dlP = new JPanel();
        GridBagLayout subdl = new GridBagLayout();
        GridBagConstraints subcdl = new GridBagConstraints();
        dlP.setLayout(subdl);

        subcdl.gridx = 0;
        subcdl.gridy = 0;
        subdl.setConstraints(dlcb, subcdl);
        dlP.add(dlcb, subcdl);

        subcdl.gridx = 0;
        subcdl.gridy = 1;
        subcdl.insets = new Insets(5, 0, 5, 0);
        subdl.setConstraints(stsep, subcdl);
        dlP.add(stsep, subcdl);

        subcdl.gridx = 1;
        subcdl.gridy = 1;
        subcdl.insets = new Insets(5, 0, 5, 5);
        spsep.setPreferredSize(new Dimension(50, 20));
        subdl.setConstraints(spsep, subcdl);
        dlP.add(spsep, subcdl);

        JPanel dlPanel = new JPanel();

        dlPanel.setLayout(new BoxLayout(dlPanel, BoxLayout.X_AXIS));

        TitledBorder dlTitle =
            BorderFactory.createTitledBorder(language.getString("Double_line"));
        dlPanel.setBorder(dlTitle);
        dlPanel.add(dlP);

        ////
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();

        setLayout(gridbag);

        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(0, 0, 5, 5);
        gridbag.setConstraints(finalDimSPanel, c);
        add(finalDimSPanel, c);

        c.gridx = 0;
        c.gridy = 1;
        c.insets = new Insets(5, 0, 0, 5);
        gridbag.setConstraints(finalStrSPanel, c);
        add(finalStrSPanel, c);

        c.gridx = 1;
        c.gridy = 1;
        c.insets = new Insets(5, 5, 0, 0);
        colorPanel.setPreferredSize(new Dimension(100, 70));
        gridbag.setConstraints(colorPanel, c);
        add(colorPanel, c);

        if (theArc instanceof JaFArc || theArc instanceof JaSArc
                || theArc instanceof JaGArc) {
            c.gridx = 1;
            c.gridy = 0;
            c.insets = new Insets(0, 5, 5, 0);
            arcArrowPanel.setPreferredSize(new Dimension(100, 70));
            gridbag.setConstraints(arcArrowPanel, c);
            add(arcArrowPanel, c);

            c.gridx = 2;
            c.gridy = 0;
            c.insets = new Insets(0, 5, 5, 0);
            arcMiddlePanel.setPreferredSize(new Dimension(150, 70));
            gridbag.setConstraints(arcMiddlePanel, c);
            add(arcMiddlePanel, c);

            c.gridx = 2;
            c.gridy = 1;
            gridbag.setConstraints(dlPanel, c);
            add(dlPanel, c);
        } else {
            c.gridx = 1;
            c.gridy = 0;
            c.insets = new Insets(0, 5, 5, 5);
            gridbag.setConstraints(finalAwSPanel, c);
            add(finalAwSPanel, c);

            c.gridx = 2;
            c.gridy = 0;
            gridbag.setConstraints(dlPanel, c);
            add(dlPanel, c);
        }

        String[] optionButtonNames =
        {language.getString("Accept"), language.getString("Cancel")};
        int n =
            JOptionPane.showOptionDialog(this, this, theParticle,
                JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE,
                theIcon, optionButtonNames, optionButtonNames[0]);

        if (n == JOptionPane.OK_OPTION) {
            theArc.setArrow(newArrow);
            theArc.setEnd(newEnd);
            theArc.setFlip(newFlip);

            if (newColor != null) {
                theArc.setColor(newColor);
            }

            int x1 = modelX1.getNumber().intValue();
            int y1 = modelY1.getNumber().intValue();
            int x2 = modelX2.getNumber().intValue();
            int y2 = modelY2.getNumber().intValue();
            int x3 = modelX3.getNumber().intValue();
            int y3 = modelY3.getNumber().intValue();

            theArc.setArcPts(x1, y1, x2, y2, x3, y3);

            double[] par = theArc.getArcParameters();

            newStroke = (float) Math.abs(modelstin.getNumber().floatValue());
            newDash = (float) Math.abs(modeldsin.getNumber().floatValue());

            if (theArc instanceof JaPArc || theArc instanceof JaGlArc) {
                newAmp = (int) modelampin.getNumber().floatValue();
                newWiggles = (int) modelwigin.getNumber().floatValue();
            }

            if (theArc instanceof JaPArc) {
                newFreq =
                    (float) (
                        (par[2] * 2 * Math.abs(par[3] / 2)) / newWiggles
                    );
            }

            if (theArc instanceof JaGlArc) {
                newFreq =
                    (float) (
                        (par[2] * 2 * Math.abs(par[3] / 2)) / (
                            (newWiggles * 0.6f) + 1.0f
                        )
                    );
            }

            theArc.setDash(newDash);
            theArc.setStroke(newStroke);

            theArc.setDoubleLine(newDLine);
            newDLSep = modelsep.getNumber().floatValue();
            theArc.setDLSeparation(newDLSep);

            theArc.setAmp(newAmp);
            theArc.setFreq(newFreq);

            changed = true;
        }
    }

    /** Indicates whether the user actually changed any of the parameters.
     * @return True if any of the properties have been changed.
     */
    public final boolean hasChanged() {
        return changed;
    }

    /** Checks for ItemEvents that have ocurred on the panel,
     *  presently only whether the arrow or flip check boxes
     *  have been clicked.
     * @param e  The ItemEvent that has ocurred.
     */
    public final void itemStateChanged(ItemEvent e) {
        Object source = e.getItemSelectable();

        if (source == arrowcb) {
            newArrow = !newArrow;
            flipcb.setEnabled(newArrow);
            middlerb.setEnabled(newArrow);

            if (theArc instanceof JaFArc) {
                endrb.setEnabled(newArrow);
            }
        } else if (source == flipcb) {
            newFlip = !newFlip;
        } else if (source == middlerb) {
            newEnd = false;
        } else if (source == endrb) {
            newEnd = true;
        } else if (source == dlcb) {
            newDLine = !newDLine;
            stsep.setEnabled(newDLine);
            spsep.setEnabled(newDLine);
        }
    }

    /** Checks for ActionEvents that have ocurred on the panel,
     *  presently only whether the color button has been pressed in which
     *  case a new color chooser panel is popped up.
     * @param evt The ActionEvent that has ocurred.
     */
    public final void actionPerformed(ActionEvent evt) {
        if (evt.getSource() == colorbt) {
            JaxoColorChooser colorChooser = new JaxoColorChooser();
            newColor = colorChooser.color(newColor, false);

            if (newColor != null) {
                colorPreviewIcon = new ImageIcon(getChooserImage(newColor));
                colorbt.setIcon(colorPreviewIcon);
            }
        }
    }

    /////////////////////////////////////////////////////////////////////////
    //
    //   private auxiliary methods  //
    //
    /////////////////////////////////////////////////////////////////////////
    private Image getChooserImage(Color iconColor) {
        MemoryImageSource source;
        int[] pixels;
        int width = 40;
        int height = 15;
        int size = width * height;
        pixels = new int[size];

        for (int i = 0; i < size; i++) {
            pixels[i] = iconColor.getRGB();
        }

        source = new MemoryImageSource(width, height, pixels, 0, width);

        Image jaxColorBox = createImage(source);

        return jaxColorBox;
    }
}

 

Ads help maintain this website.