/*

 * 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.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.TitledBorder;

import jhplot.jadraw.JaText;


/** A panel to change the parameters of a postscript text object. */
public class JaxoPSTextOptionsPanel extends JPanel implements ActionListener,
    ItemListener {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ResourceBundle language =
        ResourceBundle.getBundle(JaxoPrefs.getPref(JaxoPrefs.PREF_LANGUAGE));
    private Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    private JaText thePSText;
    private String sText;
    private Icon theIcon;
    private ImageIcon colorPreviewIcon;
    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 JPanel fsPanel;
    //    private JPanel topPanel;
    //    private TitledBorder fsPanelTitle;
    private JComboBox famcomb;
    private JComboBox sizecomb;
    private JComboBox stycomb;
    private Color newColor;
    private Image colorPreview;
    private int newX;
    private int newY;
    private int newRot;
    private String newTextString;
    private boolean changed = false;
    private JLabel textFamilyLabel;
    private JLabel textSizeLabel;
    private JLabel textStyleLabel;
    private String[] envFonts;
    private String fontChoice;
    private String sizeChoice;
    private String family;
    private Integer sizeCInt;
    private int sizeChoiceInt;
    private int styleC;
    private int styleChoice;
    private Font newFont;

    /** Constructor: pops up a panel that allows to change all the parameters
     *  of the text object.
     * @param psText The text object that is edited.
     * @param fonts A vector of fonts that can display greek characters.
     */
    public JaxoPSTextOptionsPanel(JaText psText, String[] fonts) {
        this.thePSText = psText;
        this.envFonts = fonts;

        sText = language.getString("Text_parameters");
        theIcon =
            new ImageIcon(JaxoPSTextOptionsPanel.class.getClassLoader()
                                                      .getResource("japlot/jaxodraw/icons/font_truetype.png"));
        newTextString = thePSText.getText();

        newX = thePSText.getX();
        newY = thePSText.getY();
        newColor = thePSText.getColor();
        newRot = -thePSText.getRotAngle();

        newFont = thePSText.getFont();
        family = newFont.getFamily();
        sizeCInt = new Integer(newFont.getSize());
        styleC = newFont.getStyle();

        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(language.getString("Text_string") + ": ", 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);

        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.setSize(new Dimension(80,24));
        

        //        int sC = newFont.getSize();
        //        Integer sCInt = new Integer(sC);
        sizecomb.setSelectedIndex(getSizeIndex(sizeCInt.toString()));
        textSizePanel.add(sizecomb);

        String[] styles = {"Plain", "Bold", "Italic", "B&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);

        // 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;
        gridbag.setConstraints(finalRotrSPanel, c);
        add(finalRotrSPanel, c);

        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);

        c.gridwidth = 2;
        c.gridx = 0;
        c.gridy = 3;
        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) {
                thePSText.setColor(newColor);
            }

            newTextString = textStringFields.getText();
            thePSText.setText(newTextString);
            newX = (int) modelXin.getNumber().intValue();
            newY = (int) modelYin.getNumber().intValue();
            thePSText.setX(newX);
            thePSText.setY(newY);
            newRot = (int) modelrotin.getNumber().intValue();
            thePSText.setRotAngle(-newRot);
            newFont = new Font(fontChoice, styleChoice, sizeChoiceInt);
            thePSText.setFont(newFont);
            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, true);

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

    /** 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 newSize = new Integer(sizeChoice);
            sizeChoiceInt = newSize.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.