/*

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


/** A class that displays a pop-up menu if the user right-clicks on a tab. */
public class JaxoCanvasPopupMenu extends JPopupMenu implements ActionListener,
    MouseListener {
    private ResourceBundle language =
        ResourceBundle.getBundle(JaxoPrefs.getPref(JaxoPrefs.PREF_LANGUAGE));
    private JaxoMainPanel thePanel;
    private JMenuItem newTabMI;
    private JMenuItem renameMI;
    private JMenuItem refreshMI;
    private JMenuItem clearMI;
    private JMenuItem closeTabMI;
    private JMenuItem pasteMI;
    private JMenuItem gridSizeMI;
    private JMenu gridM;
    private JRadioButtonMenuItem gridBType1;
    private JRadioButtonMenuItem gridBType2;
    private ButtonGroup gridGroup;
    private int keyMask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
    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 JaxoCanvasPopupMenu(JaxoMainPanel mainPanel) {
        this.thePanel = mainPanel;

        newTabMI = new JMenuItem(language.getString("New_tab"));
        newTabMI.addActionListener(this);
        newTabMI.setActionCommand("New");
        newTabMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, keyMask));

        renameMI = new JMenuItem(language.getString("Rename_tab"));
        renameMI.addActionListener(this);
        renameMI.addMouseListener(this);
        renameMI.setActionCommand("Rename");

        refreshMI = new JMenuItem(language.getString("Refresh"));
        refreshMI.addActionListener(this);
        refreshMI.setActionCommand("Refresh");
        refreshMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
                ActionEvent.ALT_MASK));

        clearMI = new JMenuItem(language.getString("Clear"));
        clearMI.addActionListener(this);
        clearMI.setActionCommand("Clear");
        clearMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
                ActionEvent.ALT_MASK));

        pasteMI = new JMenuItem(language.getString("Paste"));
        pasteMI.addActionListener(this);
        pasteMI.setActionCommand("Paste");
        pasteMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
                ActionEvent.ALT_MASK));

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

        closeTabMI = new JMenuItem(language.getString("Close"));
        closeTabMI.addActionListener(this);
        closeTabMI.setActionCommand("Close");
        closeTabMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, keyMask));

        this.add(newTabMI);
        this.add(renameMI);
        this.addSeparator();
        this.add(gridSizeMI);
        this.add(gridM);
        this.addSeparator();
        this.add(refreshMI);
        this.add(clearMI);
        this.add(pasteMI);
        this.addSeparator();
        this.add(closeTabMI);
    }

    /** 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("New")) {
            thePanel.fileEvent(JaxoMainPanel.NEWT);
        } else if (source.getActionCommand().equals("Rename")) {
            renameTab(theCanvas);
        } else 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();
        } else if (source.getActionCommand().equals("Refresh")) {
            thePanel.actionEvent(JaxoMainPanel.REFRESH);
        } else if (source.getActionCommand().equals("Clear")) {
            thePanel.actionEvent(JaxoMainPanel.CLEAR);
        } else if (source.getActionCommand().equals("Paste")) {
            thePanel.actionEvent(JaxoMainPanel.PASTE);
        } else if (source.getActionCommand().equals("Close")) {
            thePanel.fileEvent(JaxoMainPanel.CLOSE);
        }
    }

    private void renameTab(JaxoCanvas canvas) {
        String newTabName = getNewTabName(canvas.getCanvasTick());

        if ((newTabName.length() != 0)
                && !(newTabName.equals(canvas.getCanvasTick()))) {
            thePanel.getCanvasPanel().removeTitle(thePanel.getCanvasPanel()
                                                          .indexOfComponent(canvas),
                thePanel.getCanvasPanel()
                        .getComponentAt(thePanel.getCanvasPanel()
                                                .indexOfComponent(canvas))
                        .getName());

            (thePanel.getCanvasPanel()
                     .getComponentAt(thePanel.getCanvasPanel()
                                             .indexOfComponent(canvas)))
            .setName(newTabName);

            thePanel.getCanvasPanel().registerTitle(newTabName);

            canvas.setCanvasTick(thePanel.getCanvasPanel().getIndexedTitle(newTabName));
            thePanel.getCanvasPanel().setTitleAt(thePanel.getCanvasPanel()
                                                         .indexOfComponent(canvas),
                canvas.getCanvasTick());
        }
    }

    /** Pops up a JOptionPane to allow the user to enter some text.
     * @return The entered text string.
     */
    private String getNewTabName(String oldName) {
        String text = "";
        String msg1 = language.getString("Please_enter_the_new_tab_name");
        JTextField textField = new JTextField(oldName, 20);

        Object[] array = {msg1, textField};

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

        JOptionPane jPane =
            new JOptionPane(array, JOptionPane.PLAIN_MESSAGE,
                JOptionPane.OK_CANCEL_OPTION, null, options, btn1);
        JDialog dialog =
            jPane.createDialog(this, language.getString("Grid_size"));

        Dimension dialogSize = dialog.getSize();
        dialog.setLocation(xPos + (dialogSize.width / 4),
            yPos + (dialogSize.height / 2));

        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) {
            text = textField.getText();
        }

        return text;
    }

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

        newTabMI.setText(language.getString("New_tab"));
        clearMI.setText(language.getString("Clear"));
        refreshMI.setText(language.getString("Refresh"));
        renameMI.setText(language.getString("Rename_tab"));
        pasteMI.setText(language.getString("Paste"));
        closeTabMI.setText(language.getString("Close"));
        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; // - (2 * e.getX());
        yPos = tapLocation.y; //e.getY();
    }

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