/*

 * 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.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
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.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
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 group object. */
public class JaxoGroupOptionsPanel extends JPanel implements ActionListener,
    ItemListener {
    private ResourceBundle language =
        ResourceBundle.getBundle(JaxoPrefs.getPref(JaxoPrefs.PREF_LANGUAGE));
    private Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    private SpinnerNumberModel modelampin;
    private JaxoGroup theGroup;
    private String sGroup;
    private Icon theIcon;
    private ImageIcon colorPreviewIcon;
    private JButton colorbt;
    private JPanel colorPanel;
    private TitledBorder colorPanelTitle;
    private Color newColor;
    private Image colorPreview;
    private JComboBox fscomb;
    private JPanel fsPanel;
    private TitledBorder fsPanelTitle;
    private JPanel textFinalFontPanel;
    private TitledBorder textFontTitle;
    private JPanel textFamilyPanel;
    private JPanel textSizePanel;
    private JPanel textStylePanel;
    private JComboBox famcomb;
    private JComboBox sizecomb;
    private JComboBox stycomb;
    private JLabel textFamilyLabel;
    private JLabel textSizeLabel;
    private JLabel textStyleLabel;
    private double[] bbox;
    private int oldXin;
    private int oldYin;
    private int newXin;
    private int newYin;
    private float newSFactor;
    private float oldStroke;
    private float newStroke;
    private int oldAmp;
    private int newAmp;
    private int oldSize;
    private int newSize;
    private String fontChoice;
    private String sizeChoice;
    private String family;
    private Integer sizeCInt;
    private int sizeChoiceInt;
    private int styleC;
    private int styleChoice;
    private Font oldFont;
    private Font newFont;
    private String[] envFonts;
    private boolean changed = false;

    /** Constructor: pops up a panel that allows to change all the parameters
     *  of the vertex object.
     * @param group The vertex object that is edited.
     * @param fonts A string array holding the possible fonts.
     */
    public JaxoGroupOptionsPanel(JaxoGroup group, String[] fonts) {
        this.theGroup = group;
        this.envFonts = fonts;

        boolean ampOb = theGroup.areAmpObject();
        boolean texOb = theGroup.areTeXLabels();
        boolean psOb = theGroup.arePSLabels();

        sGroup = language.getString("Group_parameters");
        theIcon =
            new ImageIcon(JaxoGroupOptionsPanel.class.getClassLoader()
                                                     .getResource("japlot/jaxodraw/icons/groupobject.png"));

        bbox = theGroup.getBoundingBox();

        oldXin = (int) Math.round(bbox[JaObject.BB_MIN_X]);
        oldYin = (int) Math.round(bbox[JaObject.BB_MIN_Y]);

        newColor = theGroup.getColor();

        if (newColor != null) {
            colorPreview = getChooserImage(newColor);
            colorPreviewIcon = new ImageIcon(colorPreview);
        }

        oldStroke = theGroup.getStroke();
        oldAmp = theGroup.getAmp();
        oldSize = theGroup.getTeXSize();
        newSize = theGroup.getTeXSize();

        oldFont = theGroup.getTextFont();
        family = oldFont.getFamily();
        sizeCInt = new Integer(oldFont.getSize());
        styleC = oldFont.getStyle();

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

        SpinnerNumberModel modelXin =
            new SpinnerNumberModel(oldXin, 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(oldYin, 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(1.0, 0.0, 50.0, 0.05);
        JLabel rad =
            new JLabel(language.getString("Scale") + ": ", 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(
                    "Position/Scale_factor"));
        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(oldStroke, 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, 5, 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 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);

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

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

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

        if (ampOb) {
            JPanel pampin = new JPanel();
            GridBagLayout subgb2 = new GridBagLayout();
            GridBagConstraints subc2 = new GridBagConstraints();
            pampin.setLayout(subgb2);

            modelampin = new SpinnerNumberModel(oldAmp, -200.0, 200.0, 1);

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

            subc2.gridx = 0;
            subc2.gridy = 0;
            subc2.insets = new Insets(0, 5, 5, 5);
            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 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);

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

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

        if (texOb) {
            // Font Size options
            String[] allowed =
            {
                "tiny", "scriptsize", "footnotesize", "small", "normalsize",
                "large", "Large", "LARGE", "huge", "Huge"
            };

            fscomb = new JComboBox(allowed);
            fscomb.setSelectedIndex(oldSize);
            fscomb.addActionListener(this);

            fsPanel = new JPanel();
            fsPanelTitle =
                BorderFactory.createTitledBorder("TeX"
                    + language.getString("Font_size"));
            fsPanel.setBorder(fsPanelTitle);
            fsPanel.add(fscomb);
        }

        if (psOb) {
            // PS font options
            textFamilyPanel = new JPanel();
            textFamilyLabel = new JLabel();
            textFamilyLabel.setText(language.getString("Font"));
            textFamilyLabel.setHorizontalAlignment(JLabel.CENTER);

            textSizePanel = new JPanel();
            textSizeLabel = new JLabel();
            textSizeLabel.setText(language.getString("Size"));
            textSizeLabel.setHorizontalAlignment(JLabel.CENTER);

            textStylePanel = new JPanel();
            textStyleLabel = new JLabel();
            textStyleLabel.setText(language.getString("Style"));
            textStyleLabel.setHorizontalAlignment(JLabel.CENTER);

            famcomb = new JComboBox(envFonts);
            famcomb.addItemListener(this);
            famcomb.setSelectedIndex(getFamilyIndex(family));
            textFamilyPanel.add(famcomb);

            String[] sizes =
            {
                "4", "6", "8", "10", "12", "14", "16", "18", "20", "22", "24",
                "26", "28", "30", "32", "34", "36", "38", "40", "42", "44",
                "46", "48", "50", "52", "54", "56", "58", "60", "62", "64"
            };
            sizecomb = new JComboBox(sizes);
            sizecomb.addItemListener(this);
            sizecomb.setSelectedIndex(getSizeIndex(sizeCInt.toString()));
            textSizePanel.add(sizecomb);

            String[] styles = {"Plain", "Bold", "Italic", "Bold & Italic"};
            stycomb = new JComboBox(styles);
            stycomb.addItemListener(this);
            stycomb.setSelectedIndex(styleC);
            textStylePanel.add(stycomb);

            textFinalFontPanel = new JPanel();
            textFinalFontPanel.setLayout(new BorderLayout());
            textFontTitle =
                BorderFactory.createTitledBorder(language.getString(
                        "Font_properties"));

            textFinalFontPanel.setBorder(textFontTitle);
            textFinalFontPanel.add(BorderLayout.WEST, textFamilyPanel);
            textFinalFontPanel.add(BorderLayout.CENTER, textSizePanel);
            textFinalFontPanel.add(BorderLayout.EAST, textStylePanel);
        }

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

        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);
        colorPanel.setPreferredSize(new Dimension(100, 70));
        gridbag.setConstraints(colorPanel, c);
        add(colorPanel, c);

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

        if (ampOb) {
            c.gridx = 1;
            c.gridy = 1;
            gridbag.setConstraints(finalAwSPanel, c);
            add(finalAwSPanel, c);
        }

        if (psOb) {
            c.gridwidth = 2;
            c.gridx = 0;
            c.gridy = 2;
            c.insets = new Insets(5, 0, 0, 5);
            textFinalFontPanel.setPreferredSize(new Dimension(350, 60));
            gridbag.setConstraints(textFinalFontPanel, c);
            add(textFinalFontPanel, c);
        }

        if (texOb) {
            if (ampOb && !psOb) {
                c.gridx = 2;
                c.gridy = 0;
                c.insets = new Insets(0, 5, 5, 0);
                gridbag.setConstraints(fsPanel, c);
                add(fsPanel, c);
            } else if (ampOb && psOb) {
                c.gridx = 2;
                c.gridy = 0;
                gridbag.setConstraints(fsPanel, c);
                add(fsPanel, c);
            } else {
                c.gridx = 1;
                c.gridy = 1;
                gridbag.setConstraints(fsPanel, c);
                add(fsPanel, c);
            }
        }

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

        if (n == JOptionPane.OK_OPTION) {
            newXin = (int) modelXin.getNumber().intValue();
            newYin = (int) modelYin.getNumber().intValue();

            newSFactor = (float) Math.abs(modelrad.getNumber().floatValue());
            newStroke = (float) Math.abs(modelstin.getNumber().floatValue());

            if (ampOb) {
                newAmp = (int) modelampin.getNumber().floatValue();
            }

            if (newColor != null) {
                theGroup.setGroupColor(newColor);
                changed = true;
            }

            if (((newXin - oldXin) != 0) || ((newYin - oldYin) != 0)) {
                theGroup.setX(newXin);
                theGroup.setY(newYin);
                theGroup.moveBy(newXin - oldXin, newYin - oldYin);
                changed = true;
            }

            if (Math.abs(newSFactor - 1.f) > 0.0001) {
                theGroup.rescaleObject(newXin, newYin, newSFactor);
                changed = true;
            }

            if (Math.abs(newStroke - oldStroke) > 0.0001) {
                theGroup.setGroupStroke(newStroke);
                changed = true;
            }

            if (ampOb && ((newAmp - oldAmp) != 0)) {
                theGroup.setGroupAmp(newAmp);
                changed = true;
            }

            if (texOb && ((oldSize - newSize) != 0)) {
                theGroup.setGroupTeXSize(newSize);
                changed = true;
            }

            if (psOb) {
                if (!((sizeChoiceInt - sizeCInt.intValue()) == 0)
                        || !((styleChoice - styleC) == 0)
                        || (fontChoice != null)) {
                    newFont = new Font(fontChoice, styleChoice, sizeChoiceInt);
                    theGroup.setGroupTextFont(newFont);
                }
            }
        }
    }

    /** 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() == colorbt) {
            JaxoColorChooser colorChooser = new JaxoColorChooser();
            newColor = colorChooser.color(newColor, false);

            if (newColor != null) {
                colorPreviewIcon = new ImageIcon(getChooserImage(newColor));
                colorbt.setIcon(colorPreviewIcon);
            }
        } else if (evt.getSource() == fscomb) {
            newSize = fscomb.getSelectedIndex();
        }
    }

    /** Defines the font that corresponds to the selected font from the pull down lists.
     * @param e The ItemEvent
     */
    public final void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() != ItemEvent.SELECTED) {
            return;
        }

        Object list = e.getSource();

        if (list == famcomb) {
            fontChoice = (String) famcomb.getSelectedItem();
        } else if (list == stycomb) {
            styleChoice = stycomb.getSelectedIndex();
        } else {
            sizeChoice = (String) sizecomb.getSelectedItem();

            Integer thisSize = new Integer(sizeChoice);
            sizeChoiceInt = thisSize.intValue();
        }
    }

    /////////////////////////////////////////////////////////////////////////
    //
    //   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 static int getSizeIndex(String s) {
        String[] sizes =
        {
            "4", "6", "8", "10", "12", "14", "16", "18", "20", "22", "24", "26",
            "28", "30", "32", "34", "36", "38", "40", "42", "44", "46", "48",
            "50", "52", "54", "56", "58", "60", "62", "64"
        };
        int i = 0;

        while (sizes[i].compareTo(s) != 0) {
            i++;
        }

        if (sizes[i].compareTo(s) == 0) {
            return i;
        }

        return -1;
    }

    private int getFamilyIndex(String s) {
        int i = envFonts.length;

        for (int j = 0; j < i; j++) {
            if ((envFonts[j]).compareTo(s) == 0) {
                i = j;

                break;
            }
        }

        if (i < envFonts.length) {
            return i;
        } else {
            return 0;
        }
    }
}

 

Ads help maintain this website.