/*

 * 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.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.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.TitledBorder;

import jhplot.jadraw.JaLatexText;


/** A panel to change the parameters of a text object. */
public class JaxoLatexTextOptionsPanel extends JPanel implements ActionListener {
    private ResourceBundle language =
        ResourceBundle.getBundle(JaxoPrefs.getPref(JaxoPrefs.PREF_LANGUAGE));
    private Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    private JaLatexText theLatexText;
    private String sText;
    private Icon theIcon;
    private ImageIcon colorPreviewIcon;
    private String textStringNames = "LaTeX";
    private JLabel textStringLabels = new JLabel();
    private JTextField textStringFields = new JTextField();
    private JPanel textStringNamesPanel;
    private JPanel textStringFieldsPanel;
    private JPanel textFinalStringPanel;
    private TitledBorder textStringTitle;

    //    private JPanel textFinalFontPanel;
    //    private TitledBorder textFontTitle;
    //    private JPanel textFamilyPanel;
    //    private JPanel textSizePanel;
    //    private JPanel textStylePanel;
    private JButton colorbt;
    private JPanel colorPanel;
    private TitledBorder colorPanelTitle;
    private JComboBox allicomb;
    private JPanel alliPanel;
    private TitledBorder alliPanelTitle;
    private JComboBox fscomb;
    private JPanel fsPanel;

    //    private JPanel topPanel;
    private TitledBorder fsPanelTitle;
    private Color newColor;
    private Image colorPreview;
    private int newX;
    private int newY;
    private String newTextString;
    private int newAlli;
    private int newLatexFS;
    private boolean changed = false;
    private int newRot;

    /** Constructor: pops up a panel that allows to change all the parameters
     *  of the text object.
     * @param latexText The arc object that is edited.
     */
    public JaxoLatexTextOptionsPanel(JaLatexText latexText) {
        this.theLatexText = latexText;

        sText = language.getString("LaTeX_text_parameters");
        theIcon =
            new ImageIcon(JaxoLatexTextOptionsPanel.class.getClassLoader()
                                                         .getResource("japlot/jaxodraw/icons/latex.png"));
        newTextString = theLatexText.getTextString();

        newX = theLatexText.getX();
        newY = theLatexText.getY();
        newColor = theLatexText.getColor();
        newAlli = theLatexText.getAllign();
        newLatexFS = theLatexText.getLatexFS();
        newRot = -theLatexText.getRotAngle();

        colorPreview = getChooserImage(newColor);
        colorPreviewIcon = new ImageIcon(colorPreview);

        // String options
        textStringNamesPanel = new JPanel();
        textStringNamesPanel.setLayout(new GridLayout(0, 2));
        textStringFieldsPanel = new JPanel();
        textStringFieldsPanel.setLayout(new GridLayout(1, 0));

        String textStringPar = newTextString;

        textStringLabels = new JLabel(textStringNames + ": ", JLabel.RIGHT);
        textStringNamesPanel.add(textStringLabels);
        textStringFields = new JTextField(textStringPar);
        textStringNamesPanel.add(textStringFields);

        textFinalStringPanel = new JPanel();
        textFinalStringPanel.setLayout(new BorderLayout());
        textStringTitle =
            BorderFactory.createTitledBorder(language.getString("Text"));
        textFinalStringPanel.setBorder(textStringTitle);
        textFinalStringPanel.add(textStringNamesPanel, BorderLayout.NORTH);
        textFinalStringPanel.add(textStringFieldsPanel, BorderLayout.SOUTH);

        // Alignment options
        String[] possible =
        {
            language.getString("Left-Top"), language.getString("Left-Center"),
            language.getString("Left-Bottom"), language.getString("Center-Top"),
            language.getString("Center-Center"),
            language.getString("Center-Bottom"),
            language.getString("Right-Top"), language.getString("Right-Center"),
            language.getString("Right-Bottom")
        };
        allicomb = new JComboBox(possible);
        allicomb.setSelectedIndex(newAlli);
        allicomb.addActionListener(this);

        alliPanel = new JPanel();
        alliPanelTitle =
            BorderFactory.createTitledBorder(language.getString("Alignment"));
        alliPanel.setBorder(alliPanelTitle);
        alliPanel.add(allicomb);

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

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

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

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

        SpinnerNumberModel modelXin =
            new SpinnerNumberModel(newX, 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(newY, 0, d.height, 1);
        JLabel yin = new JLabel("Y: ", JLabel.RIGHT);
        pyin.add(yin);

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

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

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

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

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

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

        // Rotation options
        JPanel protin = new JPanel();
        GridBagLayout subgb1 = new GridBagLayout();
        GridBagConstraints subc1 = new GridBagConstraints();
        protin.setLayout(subgb1);

        SpinnerNumberModel modelrotin =
            new SpinnerNumberModel(newRot, -360, 360, 1);
        JLabel rotin =
            new JLabel(language.getString("Rot._Angle") + ": ", JLabel.RIGHT);
        JSpinner sprotin = new JSpinner(modelrotin);

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

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

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

        sc2.gridx = 0;
        sc2.gridy = 0;
        gb2.setConstraints(protin, sc2);
        rotrSPanel.add(protin, sc2);

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

        TitledBorder rotrSTitle =
            BorderFactory.createTitledBorder(language.getString("Rotate"));
        finalRotrSPanel.setBorder(rotrSTitle);
        finalRotrSPanel.add(rotrSPanel);

        // Color options
        colorbt = new JButton(colorPreviewIcon);
        colorbt.addActionListener(this);
        colorPanel = new JPanel();
        colorPanelTitle =
            BorderFactory.createTitledBorder(language.getString("Text_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, 5);
        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, 5, 5);
        gridbag.setConstraints(finalRotrSPanel, c);
        add(finalRotrSPanel, c);

        c.gridx = 1;
        c.gridy = 1;
        c.insets = new Insets(5, 0, 5, 5);
        alliPanel.setPreferredSize(new Dimension(150, 60));
        gridbag.setConstraints(alliPanel, c);
        add(alliPanel, c);

        c.gridx = 2;
        c.gridy = 1;
        c.insets = new Insets(5, 5, 5, 5);
        fsPanel.setPreferredSize(new Dimension(150, 60));
        gridbag.setConstraints(fsPanel, c);
        add(fsPanel, c);

        c.gridwidth = 3;
        c.gridx = 0;
        c.gridy = 2;
        c.insets = new Insets(5, 0, 0, 5);
        textFinalStringPanel.setPreferredSize(new Dimension(260, 50));
        gridbag.setConstraints(textFinalStringPanel, c);
        add(textFinalStringPanel, c);

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

        if (n == JOptionPane.OK_OPTION) {
            if (newColor != null) {
                theLatexText.setColor(newColor);
            }

            newTextString = textStringFields.getText();
            theLatexText.setTextString(newTextString);

            newX = (int) modelXin.getNumber().intValue();
            newY = (int) modelYin.getNumber().intValue();

            theLatexText.setX(newX);
            theLatexText.setY(newY);

            theLatexText.setAllign(newAlli);
            theLatexText.setLatexFS(newLatexFS);

            newRot = (int) modelrotin.getNumber().intValue();
            theLatexText.setRotAngle(-newRot);

            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 or whether one of
     *  the JComboBoxes has changed.
     * @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() == allicomb) {
            newAlli = allicomb.getSelectedIndex();
        } else if (evt.getSource() == fscomb) {
            newLatexFS = fscomb.getSelectedIndex();
        }
    }

    /////////////////////////////////////////////////////////////////////////
    //
    //   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.