/*

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

import java.io.File;

import java.util.ArrayList;
import java.util.ResourceBundle;

import javax.swing.AbstractButton;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.filechooser.FileNameExtensionFilter;

import jhplot.jadraw.JaLatexText;
import jhplot.jadraw.JaObject;

// FREEHEP 
//import org.freehep.util.export.ExportDialog;
//import org.freehep.util.export.ExportFileType;


/** The file menu items. */
public class JaxoFileMenu extends JMenu implements ActionListener {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JaxoMainPanel thePanel;
    private ResourceBundle language =
        ResourceBundle.getBundle(JaxoPrefs.getPref(JaxoPrefs.PREF_LANGUAGE));
    private JMenu newM;
    private JMenuItem openMI;
    private JMenuItem opendataMI;
    private JMenuItem closeMI;
    private JMenuItem saveMI;
    private JMenuItem saveasMI;
    private JMenuItem exportMI;
    private JMenuItem printMI;
    private JMenuItem quitMI;
    private JMenuItem importMI;
    private ArrayList recentList;
    private int[] keyList =
    {KeyEvent.VK_1, KeyEvent.VK_2, KeyEvent.VK_3, KeyEvent.VK_4};
    private JMenuItem newGraphMI;
    private JMenuItem newGraphTabMI;
    private int keyMask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();

    /** Creates the file menu from the specified JaxoMainPanel.
     * @param mainPanel The specified JaxoMainPanel.
     */
    public JaxoFileMenu(JaxoMainPanel mainPanel) {
        super(ResourceBundle.getBundle(JaxoPrefs.getPref(
                    JaxoPrefs.PREF_LANGUAGE)).getString("File"));

        this.thePanel = mainPanel;

        recentList = new ArrayList(4);

        newM = new JMenu(language.getString("New"));
        newM.addActionListener(this);

        newGraphMI = new JMenuItem(language.getString("New_graph"));
        newGraphMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, keyMask));
        newGraphMI.addActionListener(this);
        newGraphMI.setActionCommand("NewG");
        newGraphTabMI = new JMenuItem(language.getString("New_tab"));
        newGraphTabMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,
                keyMask));
        newGraphTabMI.addActionListener(this);
        newGraphTabMI.setActionCommand("NewT");

        newM.add(newGraphMI);
        newM.add(newGraphTabMI);
        this.add(newM);

        /*
        openMI = new JMenuItem(language.getString("Open"));
        this.add(openMI);
        openMI.addActionListener(this);
        openMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, keyMask));
        openMI.setActionCommand("Open");
        */

        opendataMI = new JMenuItem(language.getString("Opendata"));
        this.add(opendataMI);
        opendataMI.addActionListener(this);
        //opendataMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, keyMask));
        opendataMI.setActionCommand("Opendata");
 
        closeMI = new JMenuItem(language.getString("Close"));
        this.add(closeMI);
        closeMI.addActionListener(this);
        closeMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, keyMask));
        closeMI.setActionCommand("Close");
 
        /*
        saveMI = new JMenuItem(language.getString("Save"));
        this.add(saveMI);
        saveMI.addActionListener(this);
        saveMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, keyMask));
        saveMI.setActionCommand("Save");

        saveasMI = new JMenuItem(language.getString("Save_as"));
        this.add(saveasMI);
        saveasMI.addActionListener(this);
        saveasMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, keyMask));
        saveasMI.setActionCommand("Save_as");

        addSeparator();
        */

        // importMI = new JMenuItem(language.getString("Import"));
        // this.add(importMI);
        // importMI.addActionListener(this);
        // importMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, keyMask));
        // importMI.setActionCommand("Import");

        exportMI = new JMenuItem(language.getString("Export"));
        this.add(exportMI);
        exportMI.addActionListener(this);
        exportMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, keyMask));
        exportMI.setActionCommand("Export");

        /*
        printMI = new JMenuItem(language.getString("Print"));
        this.add(printMI);
        printMI.addActionListener(this);
        printMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, keyMask));
        printMI.setActionCommand("Print");
        */

        addSeparator();
        addRecentFiles();
        addSeparator();

        quitMI = new JMenuItem(language.getString("Quit"));
        this.add(quitMI);
        quitMI.addActionListener(this);
        quitMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, keyMask));
        quitMI.setActionCommand("Quit");
    }

    /** Executes the corresponding action when an item from the file menu
     * is chosen.
     * @param evt The action event to be processed.
     */
    public final void actionPerformed(ActionEvent evt) {
        AbstractButton source = (AbstractButton) evt.getSource();

        if (source.getActionCommand().equals("NewG")) {
            thePanel.fileEvent(JaxoMainPanel.NEWG);
        } else if (source.getActionCommand().equals("NewT")) {
            thePanel.fileEvent(JaxoMainPanel.NEWT);
        } else if (source.getActionCommand().equals("Open")) {
            thePanel.fileEvent(JaxoMainPanel.OPEN);
        } else if (source.getActionCommand().equals("Opendata")) {
            thePanel.fileEvent(JaxoMainPanel.OPENDATA);
        } else if (source.getActionCommand().equals("Import")) {
            thePanel.fileEvent(JaxoMainPanel.IMPORT);
        } else if (source.getActionCommand().equals("Close")) {
            thePanel.fileEvent(JaxoMainPanel.CLOSE);
        } else if (source.getActionCommand().equals("Save")) {
            thePanel.fileEvent(JaxoMainPanel.SAVE);
        } else if (source.getActionCommand().equals("Save_as")) {
            thePanel.fileEvent(JaxoMainPanel.SAVE_AS);
        } else if (source.getActionCommand().equals("Export")) {
            thePanel.fileEvent(JaxoMainPanel.EXPORT);
        } else if (source.getActionCommand().equals("Print")) {
            thePanel.fileEvent(JaxoMainPanel.PRINT);
        } else if (source.getActionCommand().equals("Quit")) {
            thePanel.fileEvent(JaxoMainPanel.QUIT);
        } else {
            boolean xml = checkFileExtension(source.getName());

            if (xml) {
                JaxoGraph ng = JaxoIO.open(source.getName());

                if (ng != null) {
                    reorderRFPanel(source.getName(), true);
                    open(ng);
                } else {
                    reorderRFPanel(source.getName(), false);
                }
            } else {
                JaxoGraph ng = JaxoIO.importLatex(source.getName());

                if (ng != null) {
                    reorderRFPanel(source.getName(), true);
                    open(ng);
                } else {
                    reorderRFPanel(source.getName(), false);
                }
            }
        }
    }

    /** Starts a new graph, if the current graph is not saved, a warning message is
     * displayed.
     */
    public final void aNewG() {
        JaxoGraph graph = thePanel.getCanvas().getCanvasGraph();
        boolean listIsEmpty = graph.getObjectList().isEmpty();

        if (!listIsEmpty) {
            String s1 = language.getString("Continue");
            String s2 = language.getString("Cancel");
            Object[] options = {s1, s2};
            int n =
                JOptionPane.showOptionDialog(null,
                    language.getString(
                        "All_drawn_objects_will_be_destroyed!_Continue?"),
                    language.getString("Warning"), JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null, options, s1);

            if (n == JOptionPane.YES_OPTION) {
                thePanel.getCanvas().saveBackupImage();
                graph.setBackupSelectedObject(graph.getSelectedObject());
                graph.setBackupList(graph.copyFrom(graph.getObjectList()));

                // Remove all elements from the list and everything else
                graph.clearAll();
                thePanel.getCanvas().clearOffScreenImage();

                // No object in the list, no object is selected
                graph.setSelectedObject(null);
                thePanel.setFlagPanelFileName("");

                // Repaint to reflect the change
                thePanel.repaintCanvas();
            }
        } else {
            thePanel.getCanvas().saveBackupImage();
            graph.setBackupSelectedObject(graph.getSelectedObject());
            graph.setBackupList(graph.copyFrom(graph.getObjectList()));

            // Remove all elements from the list and everything else
            graph.clearAll();
            thePanel.getCanvas().clearOffScreenImage();

            // No object in the list, no object is selected
            graph.setSelectedObject(null);
            thePanel.setFlagPanelFileName("");

            // Repaint to reflect the change
            thePanel.repaintCanvas();
        }
    }

    /** Create a new canvas tab. */
    public final void aNewT() {
        JaxoGraph newGraph = new JaxoGraph();
        newGraph.setBackupSelectedObject(null);
        newGraph.setSelectedObject(null);

        JaxoCanvas newCanvas = new JaxoCanvas(thePanel);
        newCanvas.setCanvasGraph(newGraph);

        newCanvas.addMouseMotionListener(newCanvas);
        newCanvas.addMouseListener(newCanvas);
        thePanel.getCanvasPanel().registerTitle(newCanvas.getCanvasTick());
        newCanvas.setCanvasTick(thePanel.getCanvasPanel().getIndexedTitle(newCanvas
                .getCanvasTick()));
        (thePanel.getCanvasPanel().add(newCanvas.getCanvasTick(), newCanvas))
        .setName(language.getString("Untitled"));
        thePanel.getCanvasPanel().setSelectedComponent(newCanvas);

        if (thePanel.isMiscMode()) {
            if (thePanel.getMode() > 33) {
                newCanvas.setCursor(Cursor.getPredefinedCursor(
                        Cursor.TEXT_CURSOR));
            }
        } else if (thePanel.isEditMode()) {
            if (thePanel.getMode() < 53) {
                newCanvas.setCursor(Cursor.getPredefinedCursor(
                        Cursor.CROSSHAIR_CURSOR));
            } else {
                newCanvas.setCursor(Cursor.getPredefinedCursor(
                        Cursor.HAND_CURSOR));
            }
        }

        newCanvas.clearOffScreenImage();
        thePanel.updateZoom();
    }

    /** Brings up a file chooser dialog to open a file saved in a previous session. */
    public final void open() {
        JaxoGraph newGraph = JaxoIO.open();
        open(newGraph);
    }


  /** Brings up a file chooser dialog to open a file saved in a previous session. */
    public final void opendata() {
     
              JFileChooser fileChooser = jhplot.gui.CommonGUI.openDataFileChooser(this); 
              int ret = fileChooser.showDialog(this, "Open Data file");
              if (ret == JFileChooser.APPROVE_OPTION) {
            	  jhplot.io.BrowserData br = new jhplot.io.BrowserData(fileChooser.getSelectedFile().getAbsolutePath(), null );
                  //System.out.println("Read ="+Integer.toString(br.getMap().size()));
                  thePanel.fillBrowser(br.getMap());
              }
              
              
    }

    /** Brings up a file chooser dialog to import a latex file. */
    public final void importLatex() {
        JaxoGraph newGraph = JaxoIO.importLatex();
        open(newGraph);
    }

    /** Closes the current graph. */
    public final void close() {
        JaxoCanvas theCanvas = (JaxoCanvas) thePanel.getCanvas();
        int index = thePanel.getCanvasPanel().indexOfComponent(theCanvas);

        if (thePanel.getCanvasPanel().getTabCount() > 1) {
            if (theCanvas.getCanvasGraph().isSaved()) {
                thePanel.getCanvasPanel().removeTitle(index,
                    thePanel.getCanvasPanel().getComponentAt(index).getName());

                thePanel.getCanvasPanel().remove(theCanvas);
            } else {
                String s1 = language.getString("Yes");
                String s2 = language.getString("No");
                String s3 = language.getString("Save_and_close");

                Object[] options = {s1, s2, s3};
                int n =
                    JOptionPane.showOptionDialog(null,
                        language.getString("Graph").concat(theCanvas.getCanvasTick()
                                                                    .concat(language
                                .getString("_has_been_modified!")))
                        + language.getString("_Close_anyway?"),
                        language.getString("Close_Confirmation"),
                        JOptionPane.DEFAULT_OPTION,
                        JOptionPane.QUESTION_MESSAGE, null, options, null);

                switch (n) {
                    case 0:
                        thePanel.getCanvasPanel().removeTitle(index,
                            thePanel.getCanvasPanel().getComponentAt(index)
                                    .getName());
                        thePanel.getCanvasPanel().remove(theCanvas);

                        break;
                    case 1:
                        break;
                    case 2:
                        save();
                        thePanel.getCanvasPanel().removeTitle(index,
                            thePanel.getCanvasPanel().getComponentAt(index)
                                    .getName());
                        thePanel.getCanvasPanel().remove(theCanvas);

                        break;
                    default:
                        break;
                }
            }
        } else {
            if (theCanvas.getCanvasGraph().isSaved()) {
                thePanel.getCanvasPanel().removeTitle(index,
                    thePanel.getCanvasPanel().getComponentAt(index).getName());
                thePanel.fileEvent(JaxoMainPanel.NEWT);
                thePanel.getCanvasPanel().remove(theCanvas);
            } else {
                String s1 = language.getString("Yes");
                String s2 = language.getString("No");
                String s3 = language.getString("Save_and_close");

                Object[] options = {s1, s2, s3};
                int n =
                    JOptionPane.showOptionDialog(null,
                        language.getString("Graph").concat(theCanvas.getCanvasTick()
                                                                    .concat(language
                                .getString("_has_been_modified!")))
                        + language.getString("_Close_anyway?"),
                        language.getString("Close_Confirmation"),
                        JOptionPane.DEFAULT_OPTION,
                        JOptionPane.QUESTION_MESSAGE, null, options, null);

                switch (n) {
                    case 0:
                        thePanel.getCanvasPanel().removeTitle(index,
                            thePanel.getCanvasPanel().getComponentAt(index)
                                    .getName());
                        thePanel.fileEvent(JaxoMainPanel.NEWT);
                        thePanel.getCanvasPanel().remove(theCanvas);

                        break;
                    case 1:
                        break;
                    case 2:
                        save();
                        thePanel.getCanvasPanel().removeTitle(index,
                            thePanel.getCanvasPanel().getComponentAt(index)
                                    .getName());
                        thePanel.fileEvent(JaxoMainPanel.NEWT);
                        thePanel.getCanvasPanel().remove(theCanvas);

                        break;
                    default:
                        break;
                }
            }
        }

        if (!theCanvas.getCanvasGraph().getSaveFileName().equals("")) {
            thePanel.setFlagPanelFileName(thePanel.getCanvas().getCanvasGraph()
                                                  .getSaveFileName());
        }
    }

    /** If the current JaxoGraph has no save file name associated to it, brings up a file
     * chooser dialog to save the current JaxoGraph to a file. Otherwise saves it to
     * the default file.
     */
    public final void save() {
        JaxoCanvas theCanvas = thePanel.getCanvas();
        String currentTab =
            thePanel.getCanvasPanel().getSelectedComponent().getName();
        JaxoIO.save(theCanvas.getCanvasGraph(), currentTab);

        if (theCanvas.getCanvasGraph().getSaveFileName().length() != 0) {
            addRecentFile(theCanvas.getCanvasGraph());
            thePanel.getCanvasPanel().removeTitle(thePanel.getCanvasPanel()
                                                          .indexOfComponent(theCanvas),
                thePanel.getCanvasPanel()
                        .getComponentAt(thePanel.getCanvasPanel()
                                                .indexOfComponent(theCanvas))
                        .getName());

            (thePanel.getCanvasPanel()
                     .getComponentAt(thePanel.getCanvasPanel()
                                             .indexOfComponent(theCanvas)))
            .setName(removeExtension(theCanvas.getCanvasGraph().getShortName()));

            thePanel.getCanvasPanel().registerTitle(removeExtension(
                    theCanvas.getCanvasGraph().getShortName()));
            theCanvas.setCanvasTick(thePanel.getCanvasPanel().getIndexedTitle(removeExtension(
                        theCanvas.getCanvasGraph().getShortName())));
            thePanel.getCanvasPanel().setTitleAt(thePanel.getCanvasPanel()
                                                         .indexOfComponent(theCanvas),
                theCanvas.getCanvasTick());

            thePanel.getCanvasPanel().removeModifiedIcon(thePanel.getCanvasPanel()
                                                                 .indexOfComponent(theCanvas));
        } else {
            thePanel.getCanvasPanel().setTitleAt(thePanel.getCanvasPanel()
                                                         .indexOfComponent(theCanvas),
                theCanvas.getCanvasTick());
        }

        thePanel.setFlagPanelFileName(theCanvas.getCanvasGraph()
                                               .getSaveFileName());
    }

    /** Brings up a file chooser dialog to save the current JaxoGraph to a file. */
    public final void saveAs() {
        JaxoCanvas theCanvas = thePanel.getCanvas();
        String currentTab =
            thePanel.getCanvasPanel().getSelectedComponent().getName();
        JaxoIO.saveAs(theCanvas.getCanvasGraph(), currentTab);
        thePanel.setFlagPanelFileName(theCanvas.getCanvasGraph()
                                               .getSaveFileName());

        if (theCanvas.getCanvasGraph().getSaveFileName().length() != 0) {
            addRecentFile(theCanvas.getCanvasGraph());

            thePanel.getCanvasPanel().removeTitle(thePanel.getCanvasPanel()
                                                          .indexOfComponent(theCanvas),
                thePanel.getCanvasPanel()
                        .getComponentAt(thePanel.getCanvasPanel()
                                                .indexOfComponent(theCanvas))
                        .getName());

            (thePanel.getCanvasPanel()
                     .getComponentAt(thePanel.getCanvasPanel()
                                             .indexOfComponent(theCanvas)))
            .setName(removeExtension(theCanvas.getCanvasGraph().getShortName()));

            thePanel.getCanvasPanel().registerTitle(removeExtension(
                    theCanvas.getCanvasGraph().getShortName()));
            theCanvas.setCanvasTick(thePanel.getCanvasPanel().getIndexedTitle(removeExtension(
                        theCanvas.getCanvasGraph().getShortName())));
            thePanel.getCanvasPanel().setTitleAt(thePanel.getCanvasPanel()
                                                         .indexOfComponent(theCanvas),
                theCanvas.getCanvasTick());

            thePanel.getCanvasPanel().removeModifiedIcon(thePanel.getCanvasPanel()
                                                                 .indexOfComponent(theCanvas));
        }
    }

    /** Brings up a dialog that allows to export the current JaxoGraph to several file
     * formats: postscript portrait and landscape, EPS and LaTex are supported.
     */
    public final void export() {


             thePanel.exportImage(); 
/*
        Dimension d =
            new Dimension(thePanel.getCanvas().getWidth(),
                thePanel.getCanvas().getHeight());
        String tab =
            thePanel.getCanvasPanel().getSelectedComponent().getName();
        JaxoExport.ExportFile = null;
        JaxoExportPanel e = new JaxoExportPanel();
        e.setCurrentTab(tab);
        e.export(thePanel.getCanvas().getCanvasGraph(), d);
*/



    }

    /** Brings up the standart Java print dialog to print the current JaxoGraph directly
     * to a printer or to a postscript file.
     */
    public final void print() {
        Dimension d =
            new Dimension(thePanel.getCanvas().getWidth(),
                thePanel.getCanvas().getHeight());

        JaxoPrint p = new JaxoPrint();
        p.print(thePanel.getCanvas().getCanvasGraph(), d);
    }

    /** Exits japlot.jaxodraw. */
    public final void quit() {
        boolean allSaved = true;

        for (int i = 0; i < thePanel.getCanvasPanel().getTabCount(); i++) {
            if (!(
                        ((JaxoCanvas) thePanel.getCanvasPanel().getComponentAt(i)).getCanvasGraph()
                             .isSaved()
                    )) {
                allSaved = false;

                break;
            }
        }

 
//  chekanov. This is annoing.. 
        if (allSaved) {
            JaxoPrefs.saveRecentFiles();
            thePanel.shutdown();
        } else {
            String s1 = language.getString("Quit");
            String s2 = language.getString("Cancel");
            Object[] options = {s1, s2};
            int n =
            JOptionPane.showOptionDialog(null, language.getString("Do_you_really_want_to_quit?"), language.getString("Quit_Confirmation"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, null); 
/*
                JOptionPane.showOptionDialog(null,
                    language.getString("There_are_unsaved_changes!")
                    + language.getString("Do_you_really_want_to_quit?"),
                    language.getString("Quit_Confirmation"),
                    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
                    null, options, null);
*/

            switch (n) {
                case JOptionPane.YES_OPTION:
                    JaxoPrefs.saveRecentFiles();
                    thePanel.shutdown();
                    break;
                case JOptionPane.CANCEL_OPTION:
                    break;
                default:
                    break;
            }
        }

    }

    private String removeExtension(String fileName) {
        int lp = fileName.lastIndexOf(".");

        if (lp != -1) {
            return fileName.substring(0, lp);
        } else {
            return fileName;
        }
    }

    private boolean checkFileExtension(String fileName) {
        boolean xml = true;
        int lp = fileName.lastIndexOf(".");

        if (fileName.substring(lp + 1, fileName.length()).equals("tex")) {
            xml = false;
        }

        return xml;
    }

    /** Opens a new graph without opening the fileChooser dialog.
     * @param graph The current graph.
     */
    public void open(JaxoGraph graph) {
        if (graph != null) {
            if ((thePanel.getCanvas().hasBeenUsed())
                    || (
                        thePanel.getCanvas().getCanvasGraph().getShortName()
                                    .length() > 0
                    )) {
                JaxoCanvas newCanvasTab = new JaxoCanvas(thePanel);
                newCanvasTab.setCanvasGraph(graph);
                newCanvasTab.addMouseMotionListener(newCanvasTab);
                newCanvasTab.addMouseListener(newCanvasTab);

                for (int i = 0; i < graph.listSize(); i++) {
                    JaObject jaxoOb = (JaObject) graph.listElementAt(i);

                    if (jaxoOb instanceof JaLatexText) {
                        ((JaLatexText) jaxoOb).createLatexPanel();
                        newCanvasTab.add(((JaLatexText) jaxoOb)
                            .getLatexPanel());
                        ((JaLatexText) jaxoOb).getLatexPanel().setVisible(false);
                    }
                }

                (thePanel.getCanvasPanel()
                         .add(removeExtension(graph.getShortName()),
                    newCanvasTab)).setName(removeExtension(
                        graph.getShortName()));
                thePanel.getCanvasPanel().registerTitle(removeExtension(
                        newCanvasTab.getCanvasGraph().getShortName()));
                newCanvasTab.setCanvasTick(thePanel.getCanvasPanel()
                                                   .getIndexedTitle(removeExtension(
                            newCanvasTab.getCanvasGraph().getShortName())));
                thePanel.getCanvasPanel().setTitleAt(thePanel.getCanvasPanel()
                                                             .indexOfComponent(newCanvasTab),
                    newCanvasTab.getCanvasTick());
                thePanel.getCanvasPanel().setSelectedComponent(newCanvasTab);
                thePanel.setFlagPanelFileName(graph.getSaveFileName());
            } else {
                JaxoCanvas oldCanvasTab =
                    (JaxoCanvas) thePanel.getCanvasPanel()
                                         .getSelectedComponent();
                thePanel.getCanvasPanel().removeTitle(thePanel.getCanvasPanel()
                                                              .indexOfComponent(oldCanvasTab),
                    (thePanel.getCanvasPanel()
                             .getComponentAt(thePanel.getCanvasPanel()
                                                     .indexOfComponent(oldCanvasTab)))
                    .getName());

                JaxoCanvas newCanvasTab = new JaxoCanvas(thePanel);
                newCanvasTab.setCanvasGraph(graph);
                newCanvasTab.addMouseMotionListener(newCanvasTab);
                newCanvasTab.addMouseListener(newCanvasTab);

                (thePanel.getCanvasPanel()
                         .add(removeExtension(graph.getShortName()),
                    newCanvasTab)).setName(removeExtension(
                        graph.getShortName()));

                for (int i = 0; i < graph.listSize(); i++) {
                    JaObject jaxoOb = (JaObject) graph.listElementAt(i);

                    if (jaxoOb instanceof JaLatexText) {
                        ((JaLatexText) jaxoOb).createLatexPanel();
                        newCanvasTab.add(((JaLatexText) jaxoOb)
                            .getLatexPanel());
                        ((JaLatexText) jaxoOb).getLatexPanel().setVisible(false);
                    }
                }

                thePanel.getCanvasPanel().registerTitle(removeExtension(
                        newCanvasTab.getCanvasGraph().getShortName()));
                newCanvasTab.setCanvasTick(thePanel.getCanvasPanel()
                                                   .getIndexedTitle(removeExtension(
                            newCanvasTab.getCanvasGraph().getShortName())));
                thePanel.getCanvasPanel().setTitleAt(thePanel.getCanvasPanel()
                                                             .indexOfComponent(newCanvasTab),
                    newCanvasTab.getCanvasTick());

                thePanel.getCanvasPanel().remove(oldCanvasTab);
                thePanel.setFlagPanelFileName(graph.getSaveFileName());
            }

            JaxoCanvas newCanvas = thePanel.getCanvas();

            if (thePanel.isEditMode()) {
                if (thePanel.getMode() < 53) {
                    newCanvas.setCursor(Cursor.getPredefinedCursor(
                            Cursor.CROSSHAIR_CURSOR));
                } else {
                    newCanvas.setCursor(Cursor.getPredefinedCursor(
                            Cursor.HAND_CURSOR));
                }

                newCanvas.drawWithHandles(true);
                newCanvas.drawVisualAid(false);
                thePanel.getEbtPanel().pressButton(thePanel.getEbtPanel()
                                                           .getIndex(thePanel
                        .getMode()));
            } else if (thePanel.isParticleMode()) {
                thePanel.getPbtPanel().pressButton(thePanel.getPbtPanel()
                                                           .getIndex(thePanel
                        .getMode()));
                newCanvas.drawWithHandles(false);
                newCanvas.drawVisualAid(false);
            } else if (thePanel.isMiscMode()) {
                if (thePanel.getMode() > 33) {
                    newCanvas.setCursor(Cursor.getPredefinedCursor(
                            Cursor.TEXT_CURSOR));
                }

                thePanel.getMbtPanel().pressButton(thePanel.getMbtPanel()
                                                           .getIndex(thePanel
                        .getMode()));
                newCanvas.drawWithHandles(false);
                newCanvas.drawVisualAid(false);
            }

            thePanel.reset();
        }
    }

    private void addRecentFile(JaxoGraph newGraph) {
        JMenuItem mi = new JMenuItem(newGraph.getShortName());
        mi.setName(newGraph.getSaveFileName());

        boolean maxReached = false;
        boolean notthere = true;

        for (int i = 0; i < recentList.size(); i++) {
            String name;
            name = ((JMenuItem) recentList.get(i)).getText();

            int p = name.indexOf(". ");

            if (p != -1) {
                name = name.substring(p + 2, name.length());
            }

            if ((mi.getText()).equals(name)) {
                notthere = false;

                break;
            }
        }

        if (notthere) {
            recentList.add(0, mi);
            copyRecentArray();

            if (recentList.size() > 4) {
                maxReached = true;

                for (int i = 4; i < recentList.size(); i++) {
                    recentList.remove(i);
                }
            }

            for (int i = 0; i < recentList.size(); i++) {
                Integer in = new Integer(i + 1);
                String name;
                name = ((JMenuItem) recentList.get(i)).getText();

                int p = name.indexOf(". ");

                if (p != -1) {
                    name = name.substring(p + 2, name.length());
                }

                ((JMenuItem) recentList.get(i)).setText((in.toString() + ". ")
                    .concat(name));
                ((JMenuItem) recentList.get(i)).removeActionListener(this);
                ((JMenuItem) recentList.get(i)).addActionListener(this);
                ((JMenuItem) recentList.get(i)).setAccelerator(KeyStroke
                    .getKeyStroke(keyList[i], keyMask));
                this.add(((JMenuItem) recentList.get(i)), 10 + i);
            }

            if (maxReached) {
                this.remove(this.getItemCount() - 3);
            }
        }
    }

    private void copyRecentArray() {
        for (int j = 0; j < 4; j++) {
            if (j < recentList.size()) {
                JaxoPrefs.setRecentFile(j,
                    ((JMenuItem) recentList.get(j)).getName());
            } else {
                JaxoPrefs.setRecentFile(j, "");
            }
        }
    }

    private String getBaseName(String absPath) {
        int last = absPath.lastIndexOf(File.separator);

        return absPath.substring(last + 1);
    }

    private void addRecentFiles() {
        String recentFile = "";

        for (int j = 3; j >= 0; j--) {
            recentFile = JaxoPrefs.getRecentFile(j);

            if (recentFile.length() > 0) {
                JMenuItem mi = new JMenuItem(getBaseName(recentFile));
                mi.setName(recentFile);

                recentList.add(0, mi);

                for (int i = 0; i < recentList.size(); i++) {
                    Integer in = new Integer(i + 1);
                    String name;
                    name = ((JMenuItem) recentList.get(i)).getText();

                    int p = name.indexOf(". ");

                    if (p != -1) {
                        name = name.substring(p + 2, name.length());
                    }

                    ((JMenuItem) recentList.get(i)).setText((in.toString()
                        + ". ").concat(name));
                    ((JMenuItem) recentList.get(i)).removeActionListener(this);
                    ((JMenuItem) recentList.get(i)).addActionListener(this);
                    ((JMenuItem) recentList.get(i)).setAccelerator(KeyStroke
                        .getKeyStroke(keyList[i], keyMask));
                    this.add(((JMenuItem) recentList.get(i)), 10 + i);
                }
            }
        }
    }

    private void reorderRFPanel(String file, boolean found) {
        String sName = getBaseName(file);
        int index = 10;

        for (int i = 0; i < recentList.size(); i++) {
            String name;
            name = ((JMenuItem) recentList.get(i)).getText();

            int p = name.indexOf(". ");

            if (p != -1) {
                name = name.substring(p + 2, name.length());
            }

            if ((name).equals(sName)) {
                recentList.remove(i);
                index = index + i;

                break;
            }
        }

        this.remove(index);

        if (found) {
            JMenuItem mi = new JMenuItem(sName);
            mi.setName(file);
            recentList.add(0, mi);
        }

        for (int i = 0; i < recentList.size(); i++) {
            Integer in = new Integer(i + 1);
            String name;
            name = ((JMenuItem) recentList.get(i)).getText();

            int p = name.indexOf(". ");

            if (p != -1) {
                name = name.substring(p + 2, name.length());
            }

            ((JMenuItem) recentList.get(i)).setText((in.toString() + ". ")
                .concat(name));
            ((JMenuItem) recentList.get(i)).removeActionListener(this);
            ((JMenuItem) recentList.get(i)).addActionListener(this);
            ((JMenuItem) recentList.get(i)).setAccelerator(KeyStroke
                .getKeyStroke(keyList[i], keyMask));
            this.add(((JMenuItem) recentList.get(i)), 10 + i);
        }

        copyRecentArray();
    }

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

        super.setText(language.getString("File"));

        newM.setText(language.getString("New"));
        newGraphMI.setText(language.getString("New_graph"));
        newGraphTabMI.setText(language.getString("New_tab"));
        // openMI.setText(language.getString("Open"));
        opendataMI.setText(language.getString("Opendata"));
        closeMI.setText(language.getString("Close"));
        // saveMI.setText(language.getString("Save"));
        // saveasMI.setText(language.getString("Save_as"));
        // importMI.setText(language.getString("Import"));
        exportMI.setText(language.getString("Export"));
        // printMI.setText(language.getString("Print"));
        quitMI.setText(language.getString("Quit"));
    }
}

 

Ads help maintain this website.