/*

 * 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.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import java.util.ResourceBundle;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;


/** A class that displays a pop-up menu if the user right-clicks on a tab. */
public class JaxoGridPopupMenu extends JPopupMenu implements ActionListener,
    MouseListener {
    private ResourceBundle language =
        ResourceBundle.getBundle(JaxoPrefs.getPref(JaxoPrefs.PREF_LANGUAGE));
    private JaxoMainPanel thePanel;
    private JMenuItem gridSizeMI;
    private JMenu gridM;
    private JRadioButtonMenuItem gridBType1;
    private JRadioButtonMenuItem gridBType2;
    private ButtonGroup gridGroup;
    private int xPos = 0;
    private int yPos = 0;

    /** Constructor: sets all the menu entries and the action listeners.
     * @param mainPanel Instance of the main panel.
     */
    public JaxoGridPopupMenu(JaxoMainPanel mainPanel) {
        this.thePanel = mainPanel;

        gridSizeMI = new JMenuItem(language.getString("Grid_size"));
        gridSizeMI.addActionListener(this);
        gridSizeMI.addMouseListener(this);
        gridSizeMI.setActionCommand("Grid size");

        gridM = new JMenu(language.getString("Grid_type"));
        gridGroup = new ButtonGroup();

        gridBType1 =
            new JRadioButtonMenuItem(language.getString("Rectangular"));
        gridBType1.addActionListener(this);
        gridBType1.setActionCommand("Rectangular");
        gridBType2 = new JRadioButtonMenuItem(language.getString("Hexagonal"));
        gridBType2.addActionListener(this);
        gridBType2.setActionCommand("Hexagonal");

        gridGroup.add(gridBType1);
        gridGroup.add(gridBType2);
        gridM.add(gridBType1);
        gridM.add(gridBType2);

        this.add(gridSizeMI);
        this.add(gridM);
    }

    /** Sets the corresponding readio buttons in the grid type menu. */
    public final void setGridType() {
        if (thePanel.getCanvas().getGridType()) {
            gridBType2.setSelected(true);
        } else {
            gridBType1.setSelected(true);
        }
    }

    /** Performs the corresponding action if one of the menu entries
     * has been selected.
     * @param evt The action event that specifies the chosen menu item
     */
    public final void actionPerformed(ActionEvent evt) {
        AbstractButton source = (AbstractButton) evt.getSource();

        JaxoCanvas theCanvas = thePanel.getCanvas();

        if (source.getActionCommand().equals("Grid size")) {
            setGridSize(theCanvas);
        } else if (source.getActionCommand().equals("Rectangular")) {
            theCanvas.setGridType(false);

            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
            double[] newbb = {5, 5, d.width, d.height};
            theCanvas.setOldbb(newbb);
            theCanvas.updateOffScreenImage();
        } else if (source.getActionCommand().equals("Hexagonal")) {
            thePanel.getCanvas().setGridType(true);

            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
            double[] newbb = {5, 5, d.width, d.height};
            theCanvas.setOldbb(newbb);
            theCanvas.updateOffScreenImage();
        }
    }

    private void setGridSize(JaxoCanvas canvas) {
        int oldSize = canvas.getGridSize();
        int newSize = getNewGridSize(oldSize);

        if (newSize != oldSize) {
            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
            double[] newbb = {5, 5, d.width, d.height};
            canvas.setGridSize(newSize);
            canvas.setOldbb(newbb);
            canvas.updateOffScreenImage();
        }
    }

    private int getNewGridSize(int os) {
        int newSize = os;

        // Grid Size
        JPanel pgrid = new JPanel();
        pgrid.setLayout(new GridLayout(0, 2));

        SpinnerNumberModel modelgrid = new SpinnerNumberModel(os, 1, 75, 1);
        JLabel lgrid =
            new JLabel(language.getString("Size_:") + " ", JLabel.RIGHT);
        pgrid.add(lgrid);

        JSpinner spgrid = new JSpinner(modelgrid);
        pgrid.add(spgrid);

        JPanel gridSizePanel = new JPanel();
        gridSizePanel.add(pgrid);

        String btn1 = language.getString("OK");
        String btn2 = language.getString("Cancel");
        Object[] options = {btn1, btn2};

        JOptionPane jPane =
            new JOptionPane(gridSizePanel, JOptionPane.PLAIN_MESSAGE,
                JOptionPane.OK_CANCEL_OPTION, null, options, btn1);

        JDialog dialog =
            jPane.createDialog(this, language.getString("Grid_size"));
        dialog.setLocation(xPos, yPos);
        dialog.setVisible(true);

        Object selectedValue = jPane.getValue();

        int n = 0;

        for (int counter = 0, maxCounter = options.length;
                counter < maxCounter; counter++) {
            if (options[counter].equals(selectedValue)) {
                n = counter;
            }
        }

        if (n == JOptionPane.OK_OPTION) {
            newSize = modelgrid.getNumber().intValue();
        }

        return newSize;
    }

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

        gridSizeMI.setText(language.getString("Grid_size"));
        gridM.setText(language.getString("Grid_type"));
        gridBType1.setText(language.getString("Rectangular"));
        gridBType2.setText(language.getString("Hexagonal"));
    }

    /**
     * The action to be taken when the mouse is clicked.
     * @param e A mouse event.
     */
    public final void mouseClicked(MouseEvent e) {
    }

    /**
     * The action to be taken when the mouse is released.
     * @param e A mouse event.
     */
    public final void mouseReleased(MouseEvent e) {
    }

    /**
     * The action to be taken when the mouse is pressed.
     * @param e A mouse event.
     */
    public final void mousePressed(MouseEvent e) {
        Point tapLocation = this.getLocationOnScreen();
        xPos = tapLocation.x;
        yPos = tapLocation.y;
    }

    /**
     * The action to be taken when the mouse exits.
     * @param e A mouse event.
     */
    public final void mouseExited(MouseEvent e) {
    }

    /**
     * The action to be taken when the mouse enters.
     * @param e A mouse event.
     */
    public final void mouseEntered(MouseEvent e) {
    }
}

 

Ads help maintain this website.