/*

 * 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.util.HashMap;
import java.util.ResourceBundle;

import javax.swing.ImageIcon;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


/** A tabbed panel. */
public class JaxoTabbedCanvasPanel extends JTabbedPane
    implements ChangeListener {
    private JaxoPopupListener popupListener;
    private JaxoMainPanel thePanel;

    // hold indices
    private HashMap fileNames = new HashMap();
    private ResourceBundle language =
        ResourceBundle.getBundle(JaxoPrefs.getPref(JaxoPrefs.PREF_LANGUAGE));

    /** Constructor: adds MouseListener and ChangeListener to this TabbedPanel.
     * @param panel Instance of the main panel
     */
    public JaxoTabbedCanvasPanel(JaxoMainPanel panel) {
        this.thePanel = panel;
        popupListener = new JaxoPopupListener(thePanel, 0);
        this.addMouseListener(popupListener);
        this.addChangeListener(this);
    }

    /** Called if the state of the tabbed panel has changed.
     * @param evt The event that caused the change.
     */
    public final void stateChanged(ChangeEvent evt) {
        JaxoCanvas theCanvas = (JaxoCanvas) this.getSelectedComponent();

        //if (theCanvas.getCanvasGraph().getSaveFileName()!="")
        thePanel.setFlagPanelFileName(theCanvas.getCanvasGraph()
                                               .getSaveFileName());

        if (thePanel.isEditMode()) {
            thePanel.getEbtPanel().pressButton(thePanel.getEbtPanel().getIndex(thePanel
                    .getMode()));
            theCanvas.drawWithHandles(true);
        } else if (thePanel.isParticleMode()) {
            thePanel.getPbtPanel().pressButton(thePanel.getPbtPanel().getIndex(thePanel
                    .getMode()));
            theCanvas.drawWithHandles(false);
        } else if (thePanel.isMiscMode()) {
            thePanel.getMbtPanel().pressButton(thePanel.getMbtPanel().getIndex(thePanel
                    .getMode()));
            theCanvas.drawWithHandles(false);
        }

        //thePanel.updateZoom();
        theCanvas.drawVisualAid(false);
        theCanvas.repaint();

        if (theCanvas.getOffScreenImage() != null) {
            thePanel.updateZoom();
        }
    }

    // register a title in the list. If it hasn't been registered
    // yet, it is added to list, otherwise, the corresponding index
    // is increased by one.

    /** Sets the title of this tab.
     * @param title The new title.
     */
    public final void registerTitle(String title) {
        Integer integer = (Integer) fileNames.get(title);

        if (integer == null) {
            fileNames.put(title, new Integer(0));
        } else {
            fileNames.put(title, new Integer(integer.intValue() + 1));
        }
    }

    // get a title from the indexed titles list. If the title is found in
    // the list, and if the index is != 0, we add "_index" at the end of
    // the title.

    /** Returns the index of the tab with title title.
     * @param title The title of the tab.
     * @return The index of the wanted tab.
     */
    public final String getIndexedTitle(String title) {
        int val;
        Integer integer = (Integer) fileNames.get(title);

        if ((integer != null) && ((val = integer.intValue()) != 0)) {
            return (new StringBuffer(title)).append("_").append(val).toString();
        }

        return title;
    }

    // if specified title is title of another tab, we keep it in the list,
    // otherwise, it is removed

    /** Removes a title from a tab.
     * @param index The index of the tab.
     * @param title The title to be removed.
     */
    public final void removeTitle(int index, String title) {
        String name;
        boolean more = false;

        for (int i = 0; i < getTabCount(); i++) {
            if ((i != index) && (
                        (name = getComponentAt(i).getName()) != null
                    ) && name.equals(title)) {
                more = true;

                break;
            }
        }

        if (!more) {
            fileNames.remove(title);
        }
    }

    /** Removes all untitled tabs. */
    public final void removeAllUntitled() {
        for (int i = 0; i < fileNames.size(); i++) {
            fileNames.remove("Untitled");
            fileNames.remove("Sans titre");
            fileNames.remove("N.N.");
            fileNames.remove("Senza titolo");
            fileNames.remove("Sin t�tulo");
        }
    }

    /** Displayes an icon (a disc) in the tab to indicate that
     * the current graph has been changed and needs saving.
     * @param index The index of the tab to be marked.
     */
    public final void setModifiedIcon(int index) {
        ImageIcon modifiedCanvas =
            new ImageIcon(JaxoTabbedCanvasPanel.class.getClassLoader()
                                                     .getResource("japlot/jaxodraw/icons/modified.png"));
        setIconAt(index, modifiedCanvas);
    }

    /** Removes the icon of the indicated tab to reflect that the
     * current graph is saved.
     * @param index The index of the tab
     */
    public final void removeModifiedIcon(int index) {
        setIconAt(index, null);
    }

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

        popupListener.setLanguage(languageString);

        removeAllUntitled();

        for (int i = 0; i < getTabCount(); i++) {
            ((JaxoCanvas) getComponentAt(i)).setLanguage(languageString);

            String name;

            if (((name = getComponentAt(i).getName()) != null)
                    && (
                        name.equals("Untitled") || name.equals("Sans titre")
                        || name.equals("N.N.") || name.equals("Senza titolo")
                        || name.equals("Sin t�tulo")
                    )) {
                getComponentAt(i).setName(language.getString("Untitled"));
                registerTitle(language.getString("Untitled"));
                setTitleAt(i, getIndexedTitle(language.getString("Untitled")));
                ((JaxoCanvas) getComponentAt(i)).setCanvasTick(language
                    .getString("Untitled"));
            }
        }
    }
}

 

Ads help maintain this website.