/*

 * 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.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


/** A panel to change the parameters of a line object. */
public abstract class JaxoOptionsPanel extends JPanel implements ActionListener {
    private ResourceBundle language =
        ResourceBundle.getBundle(JaxoPrefs.getPref(JaxoPrefs.PREF_LANGUAGE));
    private Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    private String panelTitle = "";
    private Icon theIcon;
    private ImageIcon colorPreviewIcon;
    private Color newColor;
    private int newXin;
    private int newYin;
    private int newXfin;
    private int newYfin;
    private GridBagLayout gbLayout = new GridBagLayout();
    private GridBagConstraints gbConstraints = new GridBagConstraints();
    private Insets insets = new Insets(0, 5, 5, 0);

    /** 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() instanceof JButton) {
            JButton cb = (JButton) evt.getSource();

            if (cb.getName().equals("colorbt")) {
                JaxoColorChooser colorChooser = new JaxoColorChooser();
                newColor = colorChooser.color(newColor, false);

                if (newColor != null) {
                    colorPreviewIcon.setImage(getChooserImage(newColor));
                    cb.setIcon(colorPreviewIcon);
                }
            }
        }
    }

    private Image getChooserImage(Color iconColor) {
        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();
        }

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

        return jaxColorBox;
    }

    /** Sets the icon for this option panel.
     * @param icon A string with the path to the icon file.
     */
    public final void setPanelIcon(String icon) {
        theIcon =
            new ImageIcon(JaxoLineOptionsPanel.class.getClassLoader()
                                                    .getResource(icon));
    }

    /** Returns the icon for this option panel.
     * @return The current icon of this option panel.
     */
    public final Icon getPanelIcon() {
        return theIcon;
    }

    /** Sets the title for this option panel.
     * @param title A string with the panel title (this string gets
     * internationalized here, so make sure it is in the properties files).
     */
    public final void setPanelTitle(String title) {
        panelTitle = language.getString(title);
    }

    /** Returns the current title of this option panel.
     * @return The current title of this option panel.
     */
    public final String getPanelTitle() {
        return panelTitle;
    }

    /** Sets the insets for this option panel.
     * @param top The top inset.
     * @param left The left inset.
     * @param bottom The bottom inset.
     * @param right The right inset.
     */
    public final void setInsets(int top, int left, int bottom, int right) {
        insets.top = top;
        insets.left = left;
        insets.right = right;
        insets.bottom = bottom;
    }

    /** Sets the newColor property.
     * @param color The new color.
     */
    public final void setColor(Color color) {
        newColor = color;
        colorPreviewIcon = new ImageIcon(getChooserImage(newColor));
    }

    /** Returns the GridBagLayout of this option panel.
     * @return The GridBagLayout of this option panel.
     */
    public final GridBagLayout getGBLayout() {
        return gbLayout;
    }

    /** Applies the current GridBagConstraints to the specified JPanel.
     * @param panel The panel to apply the layout to.
     */
    public final void setGBLayout(JPanel panel) {
        gbLayout.setConstraints(panel, gbConstraints);
    }

    /** Returns the GridBagConstraints of this option panel.
     * @return The GridBagConstraints of this option panel.
     */
    public final GridBagConstraints getGBConstraints() {
        return gbConstraints;
    }

    /** Sets the GridBagConstraints and insets.
     * @param gx The x coordinate of the GridBagConstraint.
     * @param gy The y coordinate of the GridBagConstraint.
     */
    public final void setGBConstraints(int gx, int gy) {
        gbConstraints.gridx = gx;
        gbConstraints.gridy = gy;
        gbConstraints.insets = insets;
    }

    /** Returns the current insets of this option panel.
     * @return The current insets of this option panel.
     */
    public final Insets getInsets() {
        return insets;
    }

    /** Returns the new color.
     * @return The new color.
     */
    public final Color getColor() {
        return newColor;
    }

    /** Returns the new Xin.
     * @return The new Xin.
     */
    public final int getXin() {
        return newXin;
    }

    /** Returns the new Xfin.
     * @return The new Xfin.
     */
    public final int getXfin() {
        return newXfin;
    }

    /** Returns the new Yin.
     * @return The new Yin.
     */
    public final int getYin() {
        return newYin;
    }

    /** Returns the new Yfin.
     * @return The new Yfin.
     */
    public final int getYfin() {
        return newYfin;
    }

    /** Sets the Xin, Xfin, Yin and Yfin coordinates.
     * @param x The x coordinate.
     * @param y The y coordinate.
     * @param relw The relative width.
     * @param relh The relative height.
     */
    public final void setXYLineCoord(int x, int y, int relw, int relh) {
        if ((relw > 0) && (relh >= 0)) {
            newXin = x;
            newYin = y;
            newXfin = x + Math.abs(relw);
            newYfin = y + Math.abs(relh);
        } else if ((relw <= 0) && (relh <= 0)) {
            newXfin = x;
            newYfin = y;
            newXin = x + Math.abs(relw);
            newYin = y + Math.abs(relh);
        } else if ((relw > 0) && (relh < 0)) {
            newXin = x;
            newYin = y + Math.abs(relh);
            newXfin = x + Math.abs(relw);
            newYfin = y;
        } else if ((relw <= 0) && (relh > 0)) {
            newXfin = x;
            newYfin = y + Math.abs(relh);
            newXin = x + Math.abs(relw);
            newYin = y;
        }
    }

    private JPanel getColorPanel() {
        JButton colorbt = new JButton(colorPreviewIcon);
        colorbt.setName("colorbt");
        colorbt.addActionListener(this);

        JPanel colorPanel = new JPanel();
        TitledBorder colorPanelTitle =
            BorderFactory.createTitledBorder(language.getString("Color"));
        colorPanel.setBorder(colorPanelTitle);
        colorPanel.add(colorbt);

        return colorPanel;
    }

    /** Adds the color panel to this OptionPanel.
     * @param gridx The x position of the color panel.
     * @param gridy The y position of the color panel.
     */
    public final void addColorPanel(int gridx, int gridy) {
        setInsets(5, 5, 0, 0);
        setGBConstraints(gridx, gridy);

        JPanel colorPanel = getColorPanel();
        colorPanel.setPreferredSize(new Dimension(100, 70));
        setGBLayout(colorPanel);
        add(colorPanel, gbConstraints);
    }

    private JPanel getFinalDimSSPanel() {
        SpinnerListener spinnerListener = new SpinnerListener();

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

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

        JSpinner spxin = new JSpinner(modelXin);
        spxin.addChangeListener(spinnerListener);
        spxin.setName("spxin");
        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("Yin: ", JLabel.RIGHT);
        pyin.add(yin);

        JSpinner spyin = new JSpinner(modelYin);
        spyin.addChangeListener(spinnerListener);
        spyin.setName("spyin");
        pyin.add(spyin);

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

        SpinnerNumberModel modelXfin =
            new SpinnerNumberModel(newXfin, 0, d.width, 1);
        JLabel xfin = new JLabel("Xfin: ", JLabel.RIGHT);
        pxfin.add(xfin);

        JSpinner spxfin = new JSpinner(modelXfin);
        spxfin.addChangeListener(spinnerListener);
        spxfin.setName("spxfin");
        pxfin.add(spxfin);

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

        SpinnerNumberModel modelYfin =
            new SpinnerNumberModel(newYfin, 0, d.height, 1);
        JLabel yfin = new JLabel("Yfin: ", JLabel.RIGHT);
        pyfin.add(yfin);

        JSpinner spyfin = new JSpinner(modelYfin);
        spyfin.addChangeListener(spinnerListener);
        spyfin.setName("spyfin");
        pyfin.add(spyfin);

        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.insets = new Insets(0, 0, 0, 5);
        pxfin.setPreferredSize(new Dimension(120, 20));
        gb.setConstraints(pxfin, sc);
        dimSPanel.add(pxfin, sc);

        sc.gridx = 1;
        sc.gridy = 1;
        sc.insets = new Insets(5, 0, 5, 5);
        pyfin.setPreferredSize(new Dimension(120, 20));
        gb.setConstraints(pyfin, sc);
        dimSPanel.add(pyfin, 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);

        return finalDimSPanel;
    }

    /** Adds the position panel to this OptionPanel.
     * @param gridx The x position of the color panel.
     * @param gridy The y position of the color panel.
     */
    public final void addFinalDimSPanel(int gridx, int gridy) {
        setInsets(0, 0, 5, 5);
        setGBConstraints(gridx, gridy);

        JPanel finalDimSPanel = getFinalDimSSPanel();
        setGBLayout(finalDimSPanel);
        add(finalDimSPanel, gbConstraints);
    }

    /** An abstract method that displays the EditPanel.
     * @return A boolean variable that indicates if the object has been modified.
     */
    public abstract boolean showPanel();

    class SpinnerListener implements ChangeListener {
        public final void stateChanged(ChangeEvent evt) {
            JSpinner spinner = (JSpinner) evt.getSource();
            SpinnerNumberModel model = (SpinnerNumberModel) spinner.getModel();
            String name = spinner.getName();

            if (name.equals("spxin")) {
                newXin = model.getNumber().intValue();
            } else if (name.equals("spyin")) {
                newYin = model.getNumber().intValue();
            } else if (name.equals("spxfin")) {
                newXfin = model.getNumber().intValue();
            } else if (name.equals("spyfin")) {
                newYfin = model.getNumber().intValue();
            }
        }
    }
}

 

Ads help maintain this website.