/*

 * 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.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.ResourceBundle;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;


/** Responsible for displaying and modifying the buttons of the
 * grid button menu.
 */
public class JaxoGridButtons extends JPanel implements ActionListener {
    private static final String[] GRID_BUTTON_ICONS =
    {"japlot/jaxodraw/icons/snap.png", "japlot/jaxodraw/icons/viewmag1.png", "japlot/jaxodraw/icons/exit.png"};
    private JaxoMainPanel thePanel;
    private ResourceBundle language =
        ResourceBundle.getBundle(JaxoPrefs.getPref(JaxoPrefs.PREF_LANGUAGE));
    private boolean isSnapActive = false;
    private int gnbts = 3;
    private JButton[] gridButtons = new JButton[gnbts];
    private String[] gridTips =
    {
        language.getString("Snap_points_to_the_grid"),
        language.getString("Dynamical_zoom"),
        language.getString("Exits_JaxoDraw")
    };
    private JaxoPopupListener snapPopupListener;
    private JaxoPopupListener zoomPopupListener;
    private Border raisedetched =
        BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
    private Border raisedbevel = BorderFactory.createRaisedBevelBorder();
    private Border loweredbevel = BorderFactory.createLoweredBevelBorder();

    /** Constructor: lays out the grid button panel.
     * @param mainPanel The JaxoMainPanel that holds this button panel.
     */
    public JaxoGridButtons(JaxoMainPanel mainPanel) {
        this.thePanel = mainPanel;

        JPanel gridButtonPanel = new JPanel();

        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();

        gridButtonPanel.setLayout(gridbag);

        for (int i = 0; i < gnbts; i++) {
            gridButtons[i] = new JButton();
            gridButtons[i].setIcon(new ImageIcon(JaxoGridButtons.class.getClassLoader()
                                                                      .getResource(GRID_BUTTON_ICONS[i])));
            gridButtons[i].setToolTipText(gridTips[i]);
            gridButtons[i].addActionListener(this);
            gridButtons[i].setMinimumSize(new Dimension(40, 40));
            gridButtons[i].setPreferredSize(new Dimension(40, 40));
            gridButtons[i].setBorder(raisedbevel);
            gridButtons[i].setFocusPainted(false);
        }

        for (int j = 0; j < 1; j++) {
            for (int i = 0; i < 3; i++) {
                c.gridx = i;
                c.gridy = j;
                gridbag.setConstraints(gridButtons[i + (3 * j)], c);
                gridButtonPanel.add(gridButtons[i + (3 * j)]);
            }
        }

        //        gridButtons[1].setEnabled(false);
        JPanel finalGridButtonPanel = new JPanel();
        finalGridButtonPanel.setLayout(new BorderLayout());
        finalGridButtonPanel.setBorder(raisedetched);
        finalGridButtonPanel.add(gridButtonPanel, BorderLayout.CENTER);

        setLayout(new GridLayout(0, 1));
        add(finalGridButtonPanel);

        snapPopupListener = new JaxoPopupListener(thePanel, 3);
        gridButtons[0].addMouseListener(snapPopupListener);
        zoomPopupListener = new JaxoPopupListener(thePanel, 2);
        gridButtons[1].addMouseListener(zoomPopupListener);
    }

    /** Executes the appropriate action when a button is pressed.
     * @param evt the action event to be processed.
     */
    public final void actionPerformed(ActionEvent evt) {
        if (evt.getSource() == gridButtons[0]) {
            isSnapActive = !isSnapActive;
            thePanel.setSnap(!thePanel.getSnap());

            if (isSnapActive) {
                setButtonBorder(0, loweredbevel);
                setButtonBackground(0, new Color(170, 170, 180));
            } else {
                setButtonBorder(0, raisedbevel);
                setButtonBackground(0, null);
            }
        } else if (evt.getSource() == gridButtons[1]) {
            thePanel.resetButtons();
            setButtonBorder(1, loweredbevel);
            setButtonBackground(1, new Color(170, 170, 180));
            thePanel.setMode(JaxoMainPanel.ZOOM);
            thePanel.updateZoom();
        } else if (evt.getSource() == gridButtons[2]) {
            thePanel.fileEvent(JaxoMainPanel.QUIT);
        }
    }

    /** Sets the zoom button as not pressed. */
    public final void reset() {
        setButtonBorder(1, raisedbevel);
        setButtonBackground(1, null);
    }

    /** Returns the number of buttons in the grid button menu.
     * @return The number of buttons in the grid button menu.
     */
    public final int getgnbts() {
        return gnbts;
    }

    /** Sets the background color of the button with index i
     * to the color bgColor.
     * @param i The index of the botton, whose background color is to be set.
     * @param bgColor The color to be set.
     */
    public final void setButtonBackground(int i, Color bgColor) {
        gridButtons[i].setBackground(bgColor);
    }

    /** Sets the border of the button with index i to the specified border.
     * @param i The index of the botton, whose border is to be set.
     * @param border The border to be set.
     */
    public final void setButtonBorder(int i, Border border) {
        gridButtons[i].setBorder(border);
    }

    /** Sets the specified button as pressed.
     * @param j The index of the button to be pressed.
     */
    public final void pressButton(int j) {
        setButtonBackground(j, new Color(170, 170, 180));
        setButtonBorder(j, loweredbevel);
        gridButtons[j].setFocusPainted(false);
    }

    /** Sets the tool tip text for the grid buttons in different languages.
     * @param languageString A string specifying the language to be set.
     */
    public final void setLanguage(String languageString) {
        language = ResourceBundle.getBundle(languageString);

        gridTips[0] = language.getString("Snap_points_to_the_grid");
        gridTips[1] = language.getString("Dynamical_zoom");
        gridTips[2] = language.getString("Exits_JaxoDraw");

        for (int i = 0; i < gnbts; i++) {
            gridButtons[i].setToolTipText(gridTips[i]);
        }

        snapPopupListener.setLanguage(languageString);
        zoomPopupListener.setLanguage(languageString);
    }

    /** Enables the grid button.
     * @param enable A boolean specifying if the grid button
     * should be enabled or not.
     */
    public final void enableGridBt(boolean enable) {
        gridButtons[0].setEnabled(enable);
    }

    /** Disables the grid button. */
    public final void disableSnap() {
        gridButtons[0].doClick();
    }
}

 

Ads help maintain this website.