/*

 * 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.FlowLayout;
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.JToolBar;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;


/** Responsible for displaying the buttons in the toolbar. */

public class JaxoToolBar extends JToolBar implements ActionListener {
    private static final String[] TOOLBAR_BUTTON_ICONS =
    {
        "japlot/jaxodraw/icons/filenew.png", "japlot/jaxodraw/icons/folderclose.png", "japlot/jaxodraw/icons/closecb.png",
        "japlot/jaxodraw/icons/filesave.png", "japlot/jaxodraw/icons/filesaveas.png", "japlot/jaxodraw/icons/fileimport.png",
        "japlot/jaxodraw/icons/fileexport.png", "japlot/jaxodraw/icons/fileprint.png", "japlot/jaxodraw/icons/texpreview.png",
        "japlot/jaxodraw/icons/watchModeOff.png", "japlot/jaxodraw/icons/paste.png"
    };
    private static final int N_OF_TB_BUTTONS = TOOLBAR_BUTTON_ICONS.length;
    private static final String[] TOOLBAR_BUTTON_RO_ICONS =
    {
        "japlot/jaxodraw/icons/filenewro.png", "japlot/jaxodraw/icons/folderopen.png", "japlot/jaxodraw/icons/closecbro.png",
        "japlot/jaxodraw/icons/filesavero.png", "japlot/jaxodraw/icons/filesaveasro.png",
        "japlot/jaxodraw/icons/fileimportro.png", "japlot/jaxodraw/icons/fileexportro.png",
        "japlot/jaxodraw/icons/fileprintro.png", "japlot/jaxodraw/icons/texpreviewro.png",
        "japlot/jaxodraw/icons/watchModeOn.png", "japlot/jaxodraw/icons/pastero.png"
    };
    private static final int[] TOOLBAR_MODES =
    {
        JaxoMainPanel.NEWG, JaxoMainPanel.OPEN, JaxoMainPanel.CLOSE,
        JaxoMainPanel.SAVE, JaxoMainPanel.SAVE_AS, JaxoMainPanel.IMPORT,
        JaxoMainPanel.EXPORT, JaxoMainPanel.PRINT, JaxoMainPanel.PREVIEW, 0,
        JaxoMainPanel.PASTE
    };
    private JaxoMainPanel thePanel;
    private ResourceBundle language =
        ResourceBundle.getBundle(JaxoPrefs.getPref(JaxoPrefs.PREF_LANGUAGE));
    private JButton[] toolbarButtons = new JButton[N_OF_TB_BUTTONS];
    private JButton usrGuideButton = new JButton();
    private String[] toolbarTips =
    {
        language.getString("New"), language.getString("Open"),
        language.getString("Close"), language.getString("Save"),
        language.getString("Save_as"), language.getString("Import"),
        language.getString("Export"), language.getString("Print"),
        language.getString("Latex_Preview"),
        language.getString("Watch_file_mode"), language.getString("Paste")
    };
    private Border raisedetched =
        BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
    private ImageIcon watchModeOnIcon =
        new ImageIcon(JaxoToolBar.class.getClassLoader().getResource("japlot/jaxodraw/icons/watchModeOn.png"));
    private ImageIcon watchModeOffIcon =
        new ImageIcon(JaxoToolBar.class.getClassLoader().getResource("japlot/jaxodraw/icons/watchModeOff.png"));

    /** Constructor: lays out the toolbar.
     * @param mainPanel The JaxoMainPanel that holds this toolbar.
     */
    public JaxoToolBar(JaxoMainPanel mainPanel) {
        this.thePanel = mainPanel;

        JPanel toolBarPanel = new JPanel();

        toolBarPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
        this.setFloatable(false);
        this.setBorderPainted(true);
        this.setBorder(raisedetched);
        this.setLayout(new BorderLayout());

        for (int i = 0; i < N_OF_TB_BUTTONS; i++) {
            toolbarButtons[i] = new JButton();
            toolbarButtons[i].setIcon(new ImageIcon(JaxoToolBar.class.getClassLoader()
                                                                     .getResource(TOOLBAR_BUTTON_ICONS[i])));

            //if (i != 9) {
            toolbarButtons[i].setRolloverIcon(new ImageIcon(
                    JaxoToolBar.class.getClassLoader().getResource(TOOLBAR_BUTTON_RO_ICONS[i])));
            toolbarButtons[i].setPressedIcon(new ImageIcon(
                    JaxoToolBar.class.getClassLoader().getResource(TOOLBAR_BUTTON_RO_ICONS[i])));
            //}
            toolbarButtons[i].setToolTipText(toolbarTips[i]);
            toolbarButtons[i].addActionListener(this);
            toolbarButtons[i].setMinimumSize(new Dimension(28, 28));
            toolbarButtons[i].setPreferredSize(new Dimension(28, 28));
            toolbarButtons[i].setBorderPainted(false);
            toolbarButtons[i].setRolloverEnabled(true);
            toolbarButtons[i].setFocusPainted(false);
            toolbarButtons[i].setContentAreaFilled(false);

            toolBarPanel.add(toolbarButtons[i]);
        }

        usrGuideButton.setIcon(new ImageIcon(JaxoToolBar.class.getClassLoader()
                                                              .getResource("japlot/jaxodraw/icons/info.png")));
        usrGuideButton.setRolloverIcon(new ImageIcon(JaxoToolBar.class.getClassLoader()
                                                                      .getResource("japlot/jaxodraw/icons/inforo.png")));
        usrGuideButton.setPressedIcon(new ImageIcon(JaxoToolBar.class.getClassLoader()
                                                                     .getResource("japlot/jaxodraw/icons/inforo.png")));
        usrGuideButton.setToolTipText(language.getString("User_guide"));
        usrGuideButton.addActionListener(this);
        usrGuideButton.setMinimumSize(new Dimension(28, 28));
        usrGuideButton.setPreferredSize(new Dimension(28, 28));
        usrGuideButton.setBorderPainted(false);
        usrGuideButton.setRolloverEnabled(true);
        usrGuideButton.setFocusPainted(false);
        usrGuideButton.setContentAreaFilled(false);

        this.add(toolBarPanel, BorderLayout.WEST);
        this.add(usrGuideButton, BorderLayout.EAST);

        // Get current rollover mode
        //        boolean b = this.isRollover();
        // Enable rollover mode
        this.setRollover(true);
    }

    /** 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() == usrGuideButton) {
            thePanel.helpEvent();
        } else if (evt.getSource() == toolbarButtons[9]) {
            if (thePanel.isWatchMode()) {
                toolbarButtons[9].setIcon(watchModeOffIcon);
            } else {
                toolbarButtons[9].setIcon(watchModeOnIcon);
            }
            thePanel.setWatchMode(!thePanel.isWatchMode());
        } else {
            for (int i = 0; i < N_OF_TB_BUTTONS; i++) {
                if (evt.getSource() == toolbarButtons[i]) {
                    thePanel.fileEvent(TOOLBAR_MODES[i]);
                }
            }
        }
    }

    /** Returns the number of buttons in the toolbar.
     * @return The number of buttons in the toolbar.
     */
    public final int getnTBbts() {
        return N_OF_TB_BUTTONS;
    }

    /** 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) {
        toolbarButtons[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) {
        toolbarButtons[i].setBorder(border);
    }

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

        toolbarTips[0] = language.getString("New");
        toolbarTips[1] = language.getString("Open");
        toolbarTips[2] = language.getString("Close");
        toolbarTips[3] = language.getString("Save");
        toolbarTips[4] = language.getString("Save_as");
        toolbarTips[5] = language.getString("Import");
        toolbarTips[6] = language.getString("Export");
        toolbarTips[7] = language.getString("Print");
        toolbarTips[8] = language.getString("Latex_Preview");
        toolbarTips[9] = language.getString("Watch_file_mode");
        toolbarTips[10] = language.getString("Paste");

        for (int i = 0; i < N_OF_TB_BUTTONS; i++) {
            toolbarButtons[i].setToolTipText(toolbarTips[i]);
        }

        usrGuideButton.setToolTipText(language.getString("User_guide"));
    }
}

 

Ads help maintain this website.