/*

 * 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.Dimension;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import java.util.ResourceBundle;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;


/** Tries to create a pstscript file from a Latex source file via Latex - dvips.
 * This currently only works under Linux.
 */
public class JaxoExportLatexPS extends JaxoExport {
    private static ResourceBundle language =
        ResourceBundle.getBundle(JaxoPrefs.getPref(JaxoPrefs.PREF_LANGUAGE));

    /** Pops up a filechooser dialog and tries to export a postscript
     * file to the chosen file.
     * @param newGraph The current JaxoGraph.
     */
    public final void export(JaxoGraph newGraph) {
        setGraph(newGraph);

        checkforPSText();

        String exportFileName = getExportFileName();

        if (exportFileName.length() != 0) {
            int status = exportLatexPS(newGraph, exportFileName);

            if (status != 0) {
                System.err.println("Status in JaxoExportLatexPS: " + status);
            }
        }
    }

    /** Tries to export a postscript file to the given file.
     * @param graph The current JaxoGraph.
     * @param fileName The name of the file to be exported.
     * @return The sum of the return status of the latex and the dvips command;
     * zero if everything went fine.
     */
    public final int exportLatexPS(JaxoGraph graph, String fileName) {
        if (!fileName.trim().endsWith(".eps")) {
            fileName = fileName.trim() + ".eps";
        }

        int status = 0;

        String baseName = "Jaxo_tmp";

        String latexFileName = baseName + ".tex";
        String dviFileName = baseName + ".dvi";
        String logFileName = baseName + ".log";
        String auxFileName = baseName + ".aux";
        File latexFile = new File(latexFileName);
        latexFile.deleteOnExit();

        File dviFile = new File(dviFileName);
        dviFile.deleteOnExit();

        File logFile = new File(logFileName);
        logFile.deleteOnExit();

        File auxFile = new File(auxFileName);
        auxFile.deleteOnExit();

        JaxoExportLatex lat = new JaxoExportLatex();
        lat.exportLatex(graph, latexFileName);

        String latexExe = JaxoPrefs.getStringPref(JaxoPrefs.PREF_LATEXPATH);
        String dvipsExe = JaxoPrefs.getStringPref(JaxoPrefs.PREF_DVIPSPATH);

        String latexCommand =
            latexExe + " --interaction scrollmode " + latexFileName;
        String dvipsCommand =
            dvipsExe + " -E -o " + fileName + " " + dviFileName;

        int texStatus = 0;
        int dviStatus = 0;

        try {
            texStatus = executeCommand(latexCommand, true);

            if (texStatus == 0) {
                dviStatus = executeCommand(dvipsCommand, false);
            }
        } catch (IOException anIOException) {
            JOptionPane.showMessageDialog(null,
                language.getString("IOException:_Cannot_execute_command!")
                + language.getString(
                    "Please_try_'Export_LaTeX'_and_run_latex_manually."));

            if (JaxoPrefs.verbose()) {
                System.err.println(anIOException);
            }
        } catch (InterruptedException intexc) {
            JOptionPane.showMessageDialog(null,
                language.getString(
                    "InterruptedException:_Cannot_execute_command!")
                + language.getString(
                    "Please_try_'Export_LaTeX'_and_run_latex_manually."));

            if (JaxoPrefs.verbose()) {
                System.err.println(intexc);
            }
        }

        status = texStatus + dviStatus;

        return status;
    }

    private static int executeCommand(final String command, boolean follow)
        throws IOException, InterruptedException {
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(command);

        int exit = 0;

        if (follow) {
            JTextArea area = new JTextArea(80, 40);
            area.setLineWrap(true);
            area.setWrapStyleWord(true);

            JScrollPane areaScrollPane = new JScrollPane(area);
            areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            areaScrollPane.setPreferredSize(new Dimension(500, 300));
            areaScrollPane.setBorder(BorderFactory.createCompoundBorder(
                    BorderFactory.createCompoundBorder(
                        BorderFactory.createTitledBorder(language.getString(
                                "LaTeX_output")),
                        BorderFactory.createEmptyBorder(5, 5, 5, 5)),
                    areaScrollPane.getBorder()));

            JFrame frame = new JFrame();
            frame.getContentPane().add(areaScrollPane, BorderLayout.CENTER);
            frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            frame.setTitle(command);

            //frame.setSize(600, 600);
            frame.pack();
            frame.setVisible(true);

            String lsStr;

            BufferedReader lsIn =
                new BufferedReader(new InputStreamReader(
                        process.getInputStream()));

            try {
                while ((lsStr = lsIn.readLine()) != null) {
                    area.append(lsStr + "\n");
                }
            } catch (IOException e) {
                if (JaxoPrefs.verbose()) {
                    System.err.println("IOException: " + e);
                }
            } finally {
                if (lsIn != null) {
                    lsIn.close();
                }
            }

            exit = process.waitFor();

            if (exit != 0) {
                JOptionPane.showMessageDialog(null,
                    language.getString("Error:_Execution_of_LaTeX_failed!")
                    + language.getString(
                        "Please_check_the_output_window_for_further_information."));
                process.destroy();
            } else {
                frame.dispose();
            }
        } else {
            exit = process.waitFor();

            if (exit != 0) {
                JOptionPane.showMessageDialog(null,
                    language.getString("Error:_Execution_of_dvips_failed!"));
                process.destroy();
            }
        }

        return exit;
    }
}

 

Ads help maintain this website.