/*

 * 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.beans.XMLDecoder;
import java.beans.XMLEncoder;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

import java.util.ResourceBundle;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import jhplot.jadraw.JaObject;


/** The class responsible for opening/saving JaxoGraphs. */
public final class JaxoIO {
    /** Empty private constructor to prevent the class from being
     * explicitly instantiated.
     */
    private JaxoIO() {
        // empty on purpose
    }

    /** Saves the specified JaxoGraph: if the current JaxoGraph has no
     * save file associated with it, asks for a save file name.
     * Othrewise saves the graph to the default file.
     * @param currentTab The name ofthe current tab
     * @param graph The graph to be saved.
     */
    public static void save(JaxoGraph graph, String currentTab) {
        String saveFileName = "";

        if (graph.getSaveFileName().length() == 0) {
            saveFileName = getSaveFileName(currentTab);

            if (saveFileName.length() != 0) {
                save(graph, saveFileName, currentTab);
                graph.setSaved(true);

                //graph.setSaveFileName(saveFileName);
            }
        } else {
            save(graph, graph.getSaveFileName(), currentTab);
            graph.setSaved(true);
        }
    }

    /** Saves the specified JaxoGraph: asks for a save file.
     * @param currentTab The name of the current tab
     * @param graph The graph to be saved.
     */
    public static void saveAs(JaxoGraph graph, String currentTab) {
        String saveFileName = getSaveFileName(currentTab);

        if (saveFileName.length() != 0) {
            save(graph, saveFileName, currentTab);
            graph.setSaved(true);

            //graph.setSaveFileName(saveFileName);
        }
    }

    /** Saves the specified JaxoGraph into the file fileName.
     * @param currentTab The name of the current tab
     * @param graph The graph to be saved
     * @param fileName The save file name.
     */
    public static void save(JaxoGraph graph, String fileName, String currentTab) {
        if (!fileName.trim().endsWith(".xml")) {
            fileName = fileName.trim() + ".xml";
        }

        graph.setSaveFileName(fileName);

        //         File saveFile = new File(fileName);
        //         int length = graph.getObjectList().size();
        try {
            // Serialize object into XML
            XMLEncoder encoder =
                new XMLEncoder(new BufferedOutputStream(
                        new FileOutputStream(fileName)));
            encoder.writeObject((Object) graph.getSaveGraph());
            encoder.close();
        } catch (FileNotFoundException e) {
            System.err.println(ResourceBundle.getBundle(JaxoPrefs.getPref(
                        JaxoPrefs.PREF_LANGUAGE)).getString("FileNotFoundException_in_XMLEncoder_!"));
        }
    }

    /** Brings up a file chooser dialog to choose a file for saving.
     * @return the chosen save file.
     * @param currentTab The name of the current tab
     */
    public static String getSaveFileName(String currentTab) {
        String[] suffix = {"xml"};
        JaxoChooseFile c = new JaxoChooseFile();
        String saveFileName =
            c.chooseFile(suffix,
                ResourceBundle.getBundle(JaxoPrefs.getPref(
                        JaxoPrefs.PREF_LANGUAGE)).getString("Save"),
                ResourceBundle.getBundle(JaxoPrefs.getPref(
                        JaxoPrefs.PREF_LANGUAGE)).getString("XML_data_file_(*.xml)"),
                currentTab);

        if (saveFileName.trim().length() > 0) {
            if (!saveFileName.trim().endsWith(".xml")) {
                saveFileName = saveFileName.trim() + ".xml";
            }

            File testfile = new File(saveFileName);

            if (testfile.exists()) { // Check if file exists
                String s1 =
                    ResourceBundle.getBundle(JaxoPrefs.getPref(
                            JaxoPrefs.PREF_LANGUAGE)).getString("Overwrite");
                String s2 =
                    ResourceBundle.getBundle(JaxoPrefs.getPref(
                            JaxoPrefs.PREF_LANGUAGE)).getString("Cancel");
                Object[] options = {s1, s2};
                int n =
                    JOptionPane.showOptionDialog(null,
                        saveFileName + " "
                        + ResourceBundle.getBundle(JaxoPrefs.getPref(
                                JaxoPrefs.PREF_LANGUAGE)).getString("_exists!_Overwrite?"),
                        ResourceBundle.getBundle(JaxoPrefs.getPref(
                                JaxoPrefs.PREF_LANGUAGE)).getString("Overwrite_Confirmation"),
                        JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE, null, options, s1);

                if (n != JOptionPane.YES_OPTION) {
                    saveFileName = "";
                }
            }
        } else {
            saveFileName = "";
        }

        return saveFileName;
    }

    /** Opens a new JaxoGraph: asks for an open file.
     * The current JaxoGraph is used to determine whether there are
     * drawn objects in this graph, they will be destroyed.
     * @return The new JaxoGraph.
     */
    public static JaxoGraph open() {
        JaxoGraph openedGraph = null;
        String openFileName = getOpenFileName();

        if (openFileName.length() != 0) {
            openedGraph = (JaxoGraph) open(openFileName);
            openedGraph.setSaved(true);
        }

        return openedGraph;
    }



    // open data file
    public static void opendata() {
        String openFileName = getOpenFileName();
        if (openFileName.length() != 0) {
        System.out.println("DataFile opened="+openFileName);         
        }

    }


    /** Imports a Graph from a latex file.
     * @return A the imported JaxoGraph or null, if no file was imported
     */
    public static JaxoGraph importLatex() {
        JaxoGraph openedGraph = null;
        String openFileName = getImportFileName();

        if (openFileName.length() != 0) {
            openedGraph = (JaxoGraph) importLatex(openFileName);
            openedGraph.setSaved(true);
        }

        return openedGraph;
    }

    /** Opens a new JaxoGraph from the specified file fileName.
     * @param fileName The file to be opened.
     * @return The new JaxoGraph.
     */
    public static JaxoGraph open(String fileName) {
        JaxoGraph openedGraph = new JaxoGraph();
        JaxoSaveGraph newSaveGraph = new JaxoSaveGraph();
        boolean exception = false;

        try {
            XMLDecoder decoder =
                new XMLDecoder(new BufferedInputStream(
                        new FileInputStream(fileName)));

            newSaveGraph = (JaxoSaveGraph) decoder.readObject();
            decoder.close();
        } catch (FileNotFoundException e) {
            exception = true;
            JOptionPane.showMessageDialog(null,
                ResourceBundle.getBundle(JaxoPrefs.getPref(
                        JaxoPrefs.PREF_LANGUAGE)).getString("Error:_No_such_file."),
                "IO error", JOptionPane.INFORMATION_MESSAGE);

            if (JaxoPrefs.verbose()) {
                System.err.println(ResourceBundle.getBundle(JaxoPrefs.getPref(
                            JaxoPrefs.PREF_LANGUAGE)).getString("FileNotFoundException_in_XMLDecoder_!"));
            }
        }

        if (!exception) {
            openedGraph.setSaveGraph(newSaveGraph);
            openedGraph.setSaved(true);
            openedGraph.setSaveFileName(fileName);

            return openedGraph;
        } else {
            return null;
        }
    }

    /** Imports the given Latex file.
     * @param fileName The file to be imported
     * @return The imported JaxoGraph or null, if no file was imported
     */
    public static JaxoGraph importLatex(String fileName) {
        JaxoGraph openedGraph = new JaxoGraph();
        JaxoSaveGraph newSaveGraph = new JaxoSaveGraph();

        JaxoAxodraw axo = new JaxoAxodraw();
        boolean exception = false;

        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader(fileName));

            String str;

            boolean[] isValid = axo.getValid();

            // first run: check required commands and set bounding box
            while ((str = in.readLine()) != null) {
                str = str.trim();
                axo.checkRequired(str);
                isValid = axo.getValid();

                if (isValid[2]) {
                    break;
                }
            }

            newSaveGraph.setPackageList(axo.getPackages());

            isValid = axo.getValid();

            if (!(isValid[0] && isValid[1] && isValid[2])) {
                String missing = "";
                if (!isValid[0]) {
                    missing = "\\documentclass";
                } else if (!isValid[1]) {
                    missing = "\\begin{document}";
                } else if (!isValid[2]) {
                    missing = "\\begin{picture}";
                }

                JOptionPane.showMessageDialog(null,
                    ResourceBundle.getBundle(JaxoPrefs.getPref(
                            JaxoPrefs.PREF_LANGUAGE)).getString("The_following_command_was_not_found_in_")
                    + fileName + ":\n" + missing);
            } else {
                // second run: read axodraw commands
                while ((str = in.readLine()) != null) {
                    str = str.trim();

                    JaObject ob = axo.getJaxoObject(str);

                    if (ob != null) {
                        newSaveGraph.addObject(ob);
                    }
                }
            }

            newSaveGraph.setDescription(axo.getComment());
            in.close();
        } catch (IOException e) {
            exception = true;
            JOptionPane.showMessageDialog(null,
                ResourceBundle.getBundle(JaxoPrefs.getPref(
                        JaxoPrefs.PREF_LANGUAGE)).getString("Error:_No_such_file."),
                "IO error", JOptionPane.INFORMATION_MESSAGE);

            if (JaxoPrefs.verbose()) {
                System.err.println(ResourceBundle.getBundle(JaxoPrefs.getPref(
                            JaxoPrefs.PREF_LANGUAGE)).getString("FileNotFoundException_in_XMLDecoder_!"));
            }
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ioEx) {
                    if (JaxoPrefs.verbose()) {
                        System.err.println(ioEx);
                    }
                }
            }
        }

        if (!exception) {
            openedGraph.setSaveGraph(newSaveGraph);
            openedGraph.setSaved(true);
            openedGraph.setSaveFileName(fileName);

            return openedGraph;
        } else {
            return null;
        }
    }

    /** Brings up a file chooser dialog to choose a file for opening.
     * Checks whether there is a non-empty current graph, it will be destroyed.
     * @return The chosen open file name.
     */
    public static String getOpenFileName() {
        String openFileName = "";
        String[] suffix = {"xml"};

        JaxoChooseFile c = new JaxoChooseFile();
        openFileName =
            c.chooseFile(suffix,
                ResourceBundle.getBundle(JaxoPrefs.getPref(
                        JaxoPrefs.PREF_LANGUAGE)).getString("Open"),
                ResourceBundle.getBundle(JaxoPrefs.getPref(
                        JaxoPrefs.PREF_LANGUAGE)).getString("XML_data_file_(*.xml)"),
                "");

        return openFileName;
    }

  

    /** Brings up a file chooser for the import.
     * @return The absolte path of the chosen file,
     * or an empty string if no file was selected
     */
    public static String getImportFileName() {
        String importFileName = "";
        String[] suffix = {"tex"};

        JaxoChooseFile c = new JaxoChooseFile();
        importFileName =
            c.chooseFile(suffix,
                ResourceBundle.getBundle(JaxoPrefs.getPref(
                        JaxoPrefs.PREF_LANGUAGE)).getString("Import"),
                ResourceBundle.getBundle(JaxoPrefs.getPref(
                        JaxoPrefs.PREF_LANGUAGE)).getString("LaTex_file_(*.tex)"),
                "");

        return importFileName;
    }
}

 

Ads help maintain this website.