/*

 * 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.image.MemoryImageSource;

import java.util.ResourceBundle;

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

import jhplot.jadraw.JaObject;


/** A panel to change the parameters of a vertex type 3 object (a filled square). */
public class JaxoVertexT3OptionsPanel extends JPanel implements ActionListener {
    private ResourceBundle language =
        ResourceBundle.getBundle(JaxoPrefs.getPref(JaxoPrefs.PREF_LANGUAGE));
    private JaObject theVertexT3;
    private Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    private String sVertexT3;
    private Icon theIcon;
    private ImageIcon colorFillPreviewIcon;
    private JButton colorFillbt;
    private JPanel colorFillPanel;
    private TitledBorder colorFillPanelTitle;
    private Color newFillColor;
    private Image colorFillPreview;
    private int newWidth;
    private int newHeight;
    private int newRelWidth;
    private int newRelHeight;
    private int newXin;
    private int newYin;
    private double newLength;
    private boolean changed = false;

    /** Constructor: pops up a panel that allows to change all the parameters
     *  of the vertex object.
     * @param vertexT3 The vertex object that is edited.
     */
    public JaxoVertexT3OptionsPanel(JaObject vertexT3) {
        this.theVertexT3 = vertexT3;

        sVertexT3 = language.getString("Vertex_parameters");
        theIcon =
            new ImageIcon(JaxoVertexT3OptionsPanel.class.getClassLoader()
                                                        .getResource("japlot/jaxodraw/icons/vertexT3.png"));

        newWidth = theVertexT3.getSize().width;
        newHeight = theVertexT3.getSize().height;

        newRelWidth = theVertexT3.getRelSize().width;
        newRelHeight = theVertexT3.getRelSize().height;

        getCenter(theVertexT3.getX(), theVertexT3.getY(), newRelWidth,
            newRelHeight);

        newLength =
            (double) Math.sqrt((newWidth * newWidth) + (
                    newHeight * newHeight
                ));

        newFillColor = theVertexT3.getColor();
        colorFillPreview = getChooserImage(newFillColor);
        colorFillPreviewIcon = new ImageIcon(colorFillPreview);

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

        SpinnerNumberModel modelXin =
            new SpinnerNumberModel(newXin, 0, d.width, 1);
        JLabel xin = new JLabel("X: ", JLabel.RIGHT);
        pxin.add(xin);

        JSpinner spxin = new JSpinner(modelXin);
        pxin.add(spxin);

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

        SpinnerNumberModel modelYin =
            new SpinnerNumberModel(newYin, 0, d.height, 1);
        JLabel yin = new JLabel("Y: ", JLabel.RIGHT);
        pyin.add(yin);

        JSpinner spyin = new JSpinner(modelYin);
        pyin.add(spyin);

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

        SpinnerNumberModel modelrad =
            new SpinnerNumberModel(Math.round(newLength), 0,
                (int) Math.round(Math.sqrt((d.width * d.width)
                        + (d.height * d.height))), 1);
        JLabel rad = new JLabel(language.getString("Radius"), JLabel.RIGHT);
        prad.add(rad);

        JSpinner sprad = new JSpinner(modelrad);
        prad.add(sprad);

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

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

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

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

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

        TitledBorder dimSTitle =
            BorderFactory.createTitledBorder(language.getString(
                    "Center/Radius"));
        finalDimSPanel.setBorder(dimSTitle);
        finalDimSPanel.add(dimSPanel);

        // Color options
        colorFillbt = new JButton(colorFillPreviewIcon);
        colorFillbt.addActionListener(this);
        colorFillPanel = new JPanel();
        colorFillPanelTitle =
            BorderFactory.createTitledBorder(language.getString("Color"));
        colorFillPanel.setBorder(colorFillPanelTitle);
        colorFillPanel.add(colorFillbt);

        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 = 1;
        c.gridy = 0;
        c.insets = new Insets(0, 5, 5, 0);
        colorFillPanel.setPreferredSize(new Dimension(100, 70));
        gridbag.setConstraints(colorFillPanel, c);
        add(colorFillPanel, c);

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

        if (n == JOptionPane.OK_OPTION) {
            if (newFillColor != null) {
                theVertexT3.setColor(newFillColor);
            }

            newXin = (int) modelXin.getNumber().intValue();
            newYin = (int) modelYin.getNumber().intValue();
            newLength = (double) modelrad.getNumber().floatValue();
            setBox(newLength, newRelWidth, newRelHeight);
            newWidth = Math.abs(newRelWidth);
            newHeight = Math.abs(newRelHeight);
            theVertexT3.setRelWAndH(newRelWidth, newRelHeight);
            setNewCenter(newXin, newYin, newRelWidth, newRelHeight);
            theVertexT3.setX(newXin);
            theVertexT3.setY(newYin);

            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 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() == colorFillbt) {
            JaxoColorChooser colorChooser = new JaxoColorChooser();
            newFillColor = colorChooser.color(newFillColor, false);

            if (newFillColor != null) {
                colorFillPreviewIcon =
                    new ImageIcon(getChooserImage(newFillColor));
                colorFillbt.setIcon(colorFillPreviewIcon);
            }
        }
    }

    /////////////////////////////////////////////////////////////////////////
    //
    //   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;
    }

    private void getCenter(int x, int y, int relw, int relh) {
        if ((relw > 0) && (relh >= 0)) {
            newXin = x;
            newYin = y;
        }

        if ((relw < 0) && (relh <= 0)) {
            newXin = x + Math.abs(relw);
            newYin = y + Math.abs(relh);
        }

        if ((relw >= 0) && (relh < 0)) {
            newXin = x;
            newYin = y + Math.abs(relh);
        }

        if ((relw <= 0) && (relh > 0)) {
            newXin = x + Math.abs(relw);
            newYin = y;
        }
    }

    private void setNewCenter(int xin, int yin, int relw, int relh) {
        if ((newRelWidth > 0) && (newRelHeight >= 0)) {
            newXin = xin;
            newYin = yin;
        }

        if ((newRelWidth < 0) && (newRelHeight <= 0)) {
            newXin = xin + relw;
            newYin = yin + relh;
        }

        if ((newRelWidth >= 0) && (newRelHeight < 0)) {
            newXin = xin;
            newYin = yin + relh;
        }

        if ((newRelWidth <= 0) && (newRelHeight > 0)) {
            newXin = xin + relw;
            newYin = yin;
        }
    }

    private void setBox(double length, int relw, int relh) {
        double theta = Math.atan2((float) relh, (float) relw);

        newRelHeight = (int) Math.round((length * Math.sin(theta)));
        newRelWidth = (int) Math.round((length * Math.cos(theta)));
    }
}

 

Ads help maintain this website.