~ubuntu-branches/debian/sid/latexdraw/sid

« back to all changes in this revision

Viewing changes to latexDraw/ui/DrawContainer.java

  • Committer: Bazaar Package Importer
  • Author(s): Stuart Prescott
  • Date: 2009-07-15 23:35:52 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090715233552-2bblktyf2lmrkyo3
Tags: 2.0.3+1-1
* New upstream release.
* Add additional Recommended packages for new export features.
* Fix typo in long description, with thanks to Kai Weber (Closes: #529195).
* Bump standards to 3.8.2 (no changes).

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
import java.awt.print.PageFormat;
11
11
import java.awt.print.Printable;
12
12
import java.awt.print.PrinterException;
13
 
import java.io.File;
14
 
import java.io.FileOutputStream;
15
 
import java.io.IOException;
16
 
import java.io.ObjectInputStream;
 
13
import java.io.*;
 
14
import java.util.Arrays;
 
15
import java.util.Hashtable;
17
16
import java.util.Locale;
18
17
import java.util.Vector;
19
18
 
24
23
import javax.imageio.plugins.bmp.BMPImageWriteParam;
25
24
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
26
25
import javax.imageio.stream.ImageOutputStream;
27
 
import javax.swing.JComponent;
28
 
import javax.swing.JViewport;
29
 
import javax.swing.Scrollable;
30
 
import javax.swing.SwingConstants;
 
26
import javax.swing.*;
31
27
 
32
28
import latexDraw.figures.*;
33
29
import latexDraw.figures.properties.Arrowable;
 
30
import latexDraw.filters.PSFilter;
 
31
import latexDraw.filters.TeXFilter;
34
32
import latexDraw.parsers.svg.SVGAttributes;
35
33
import latexDraw.parsers.svg.SVGDocument;
36
34
import latexDraw.parsers.svg.SVGElements;
171
169
        protected MagneticGrid grid;
172
170
        
173
171
        
 
172
        
 
173
        
174
174
        /**
175
175
         * The constructor by default.
176
176
         */
974
974
        
975
975
        
976
976
        
 
977
        public String getLatexDocument()
 
978
        {
 
979
                String packages         = getParentPanel().getCodePanel().getDocumentPackages();
 
980
                String eol                      = System.getProperty("line.separator");//$NON-NLS-1$
 
981
                StringBuffer doc        = new StringBuffer();
 
982
                parentPanel.updateCode();
 
983
                String code             = parentPanel.getCodePanel().getEditor().getCode(false, false);
 
984
                LaTeXDrawPoint2D se = bordersComplete.getTheSEPoint();
 
985
 
 
986
                if(packages==null)
 
987
                        packages = "";
 
988
                
 
989
                doc.append("\\documentclass{article}").append(eol).append("\\pagestyle{empty}").append(eol).append(packages).append(eol).append(
 
990
                        "\\usepackage[left=0cm,top=0.1cm,right=0cm,nohead,nofoot,paperwidth=").append(
 
991
                        se.x/pixelsPerCm).append("cm,paperheight=").append(
 
992
                        se.y/pixelsPerCm+0.3).append("cm]{geometry}").append(
 
993
                        eol).append("\\usepackage[usenames,dvipsnames]{pstricks}").append(//$NON-NLS-1$//$NON-NLS-2$
 
994
                        eol).append("\\usepackage{pstricks-add}").append(eol).append("\\usepackage{epsfig}").append(//$NON-NLS-1$//$NON-NLS-2$
 
995
                        eol).append("\\usepackage{pst-grad}").append(eol).append("\\usepackage{pst-plot}").append(eol).append(//$NON-NLS-1$//$NON-NLS-2$
 
996
                         "\\begin{document}").append(eol).append(
 
997
                        "\\addtolength{\\oddsidemargin}{-0.2in}").append(eol).append("\\addtolength{\\evensidemargin}{-0.2in}").append(
 
998
                        eol).append(code).append(eol).append("\\end{document}");//$NON-NLS-1$//$NON-NLS-2$
 
999
 
 
1000
                return doc.toString();
 
1001
        }
 
1002
        
 
1003
        
 
1004
        
 
1005
        public File createLatexFile(String pathExportTex)
 
1006
        {
 
1007
                if(pathExportTex==null)
 
1008
                        return null;
 
1009
                
 
1010
                try {
 
1011
                        String doc                              = getLatexDocument();
 
1012
                        FileOutputStream fos    = new FileOutputStream(pathExportTex);
 
1013
                        OutputStreamWriter osw  = new OutputStreamWriter(fos);
 
1014
                        
 
1015
                        osw.append(doc);
 
1016
                        osw.flush();
 
1017
                        osw.close();
 
1018
                        fos.flush();
 
1019
                        fos.close();
 
1020
                        
 
1021
                        return new File(pathExportTex);
 
1022
                }
 
1023
                catch(Exception e) { return null; }
 
1024
        }
 
1025
        
 
1026
        
 
1027
        private void showErrorDialogLatexCompilation(String log) {
 
1028
                String text = log==null || log.length()==0 ? "Check the path of your latex distribution in your preferences." : log;
 
1029
                ExceptionFrameDialog dialog = new ExceptionFrameDialog(new IllegalAccessException(text));
 
1030
                
 
1031
                dialog.setTitle("Document not created");
 
1032
                dialog.setInfoLabel("The document cannot be created.");
 
1033
                dialog.getEditor().setText(text);
 
1034
                dialog.getEditor().setCaretPosition(0);
 
1035
                dialog.setVisible(true);
 
1036
        }
 
1037
        
 
1038
        
 
1039
        /**
 
1040
         * Create a .ps file that corresponds to the compiled latex document containing
 
1041
         * the pstricks drawing.
 
1042
         * @param latexDistribPath The path locating latex.
 
1043
         * @param pathExportPs The path of the .ps file to create (MUST ends with .ps).
 
1044
         * @return The create file or null.
 
1045
         * @throws IllegalArgumentException If a problem occurs.
 
1046
         * @since 2.1
 
1047
         */
 
1048
        public File createPSFile(String latexDistribPath, String pathExportPs)
 
1049
        {
 
1050
                if(latexDistribPath==null)
 
1051
                        return null;
 
1052
                
 
1053
                String dirBin   = parentPanel.getFrameParent().getPathDistribLatex();
 
1054
                String pathTmp  = System.getProperty("java.io.tmpdir");                                                 //$NON-NLS-1$
 
1055
                String sep              = System.getProperty("file.separator");                                                 //$NON-NLS-1$
 
1056
                String path             = pathTmp + sep + "latexdrawTmp" + System.currentTimeMillis();  //$NON-NLS-1$
 
1057
                File texFile    = createLatexFile(path + TeXFilter.TEX_EXTENSION);
 
1058
                String log;
 
1059
                File finalPS;
 
1060
                LaTeXDrawPoint2D nw = bordersComplete.getTheNWPoint(), se = bordersComplete.getTheSEPoint();
 
1061
                float dec               = 0.2f;
 
1062
                
 
1063
                if(texFile==null || !texFile.exists())
 
1064
                        return null;
 
1065
                
 
1066
                dirBin = dirBin==null ? "" : dirBin.endsWith(sep) || dirBin.length()==0 ? dirBin : dirBin + sep;
 
1067
                log    = execute(dirBin + "latex --interaction=nonstopmode --output-directory=" + pathTmp + " " + texFile.getAbsolutePath());//$NON-NLS-1$//$NON-NLS-2$
 
1068
                log   += execute(dirBin + "dvips -T " + ((se.x-nw.x)/pixelsPerCm+dec) + "cm," + ((se.y-nw.y)/pixelsPerCm+dec) + "cm "  + path + ".dvi -o " + pathExportPs);                                              //$NON-NLS-1$//$NON-NLS-2$
 
1069
                
 
1070
                texFile.delete();
 
1071
                new File(path + ".dvi").delete();                                       //$NON-NLS-1$
 
1072
                new File(path + ".log").delete();                                       //$NON-NLS-1$
 
1073
                new File(path + ".aux").delete();                                       //$NON-NLS-1$
 
1074
                
 
1075
                finalPS = new File(pathExportPs);
 
1076
                
 
1077
                if(!finalPS.exists()) {
 
1078
                        showErrorDialogLatexCompilation(log);
 
1079
                        finalPS = null;
 
1080
                }
 
1081
                
 
1082
                return finalPS;
 
1083
        }
 
1084
        
 
1085
        
 
1086
        
 
1087
        /**
 
1088
         * Create a .pdf file that corresponds to the compiled latex document containing
 
1089
         * the pstricks drawing.
 
1090
         * @param latexDistribPath The path locating latex.
 
1091
         * @param pathExportPdf The path of the .pdf file to create (MUST ends with .pdf).
 
1092
         * @return The create file or null.
 
1093
         * @throws IllegalArgumentException If a problem occurs.
 
1094
         * @since 2.1
 
1095
         */
 
1096
        public File createPDFFile(String latexDistribPath, String pathExportPdf)
 
1097
        {
 
1098
                if(pathExportPdf==null)
 
1099
                        return null;
 
1100
                
 
1101
                String dirBin   = parentPanel.getFrameParent().getPathDistribLatex();
 
1102
                String pathTmp  = System.getProperty("java.io.tmpdir");                                                 //$NON-NLS-1$
 
1103
                String sep              = System.getProperty("file.separator");                                                 //$NON-NLS-1$
 
1104
                String path             = pathTmp + sep + "latexdrawTmp" + System.currentTimeMillis()*2;//$NON-NLS-1$
 
1105
                File psFile     = createPSFile(latexDistribPath, path + PSFilter.PS_EXTENSION);
 
1106
                String log;
 
1107
                File pdfFile;
 
1108
                
 
1109
                if(psFile==null)
 
1110
                        return null;
 
1111
                
 
1112
                dirBin  = dirBin==null ? "" : dirBin.endsWith(sep) || dirBin.length()==0 ? dirBin : dirBin + sep;
 
1113
                log     = execute(dirBin + "ps2pdf "  + psFile.getAbsolutePath()  + " " + pathExportPdf); //$NON-NLS-1$//$NON-NLS-2$
 
1114
                pdfFile = new File(pathExportPdf);
 
1115
                psFile.delete();
 
1116
                
 
1117
                if(!pdfFile.exists()) {
 
1118
                        showErrorDialogLatexCompilation(log);
 
1119
                        pdfFile = null;
 
1120
                }
 
1121
                                
 
1122
                return pdfFile;
 
1123
        }
 
1124
        
 
1125
        
 
1126
        
977
1127
        /**
978
1128
         * Allows to export the drawing as a eps file
979
1129
         * @param file The new eps file
1466
1616
        {
1467
1617
                if(g==null || figures==null) return;
1468
1618
 
1469
 
        g.setColor(Color.WHITE);
1470
 
        g.fillRect(0, 0, getWidth(), getHeight());
1471
1619
                Graphics2D g2 = (Graphics2D) g;
1472
1620
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antiAliasingValue);
1473
1621
                g2.setRenderingHint(RenderingHints.KEY_RENDERING, renderingValue);
1517
1665
        public void paintComponent(Graphics g)
1518
1666
    {           
1519
1667
        super.paintComponents(g);
 
1668
        
 
1669
        g.setColor(Color.WHITE);
 
1670
        g.fillRect(0, 0, getWidth(), getHeight());
 
1671
        
1520
1672
        paintFigures(g, true, true);
1521
1673
                
1522
1674
        try
2051
2203
                                if(size<2)
2052
2204
                                        return ;
2053
2205
                                
 
2206
                                Figure f;
2054
2207
                                int[] id = new int[size];
2055
 
                                Draw d = (Draw)selected.clone();
 
2208
                                Draw d   = new Draw(true, true);
 
2209
                                Hashtable<Integer, Figure> table = new Hashtable<Integer, Figure>();
2056
2210
                                
2057
 
                                for(i=0; i<size; i++)
2058
 
                                {
 
2211
                                for(i=0; i<size; i++) {
2059
2212
                                        id[i] = figures.indexOf(selected.getFigureAt(i));
2060
 
                                        figures.remove(id[i]);
 
2213
                                        table.put(id[i], figures.elementAt(id[i]));
2061
2214
                                }
2062
2215
                                
 
2216
                                Arrays.sort(id);
2063
2217
                                min = id[0];
2064
 
                                i = 1;
2065
2218
                                
2066
 
                                while(i<size)
2067
 
                                {
2068
 
                                        if(id[i]<min)
2069
 
                                                min = id[i];
2070
 
                                        i++;
 
2219
                                for(i=0; i<id.length; i++) {
 
2220
                                        f = table.get(id[i]);
 
2221
                                        d.addFigure(f);
 
2222
                                        figures.remove(f);
2071
2223
                                }
2072
2224
                                
2073
2225
                                if(undoManager!=null)
2949
3101
                                                setWhichCodeOutput(Double.valueOf(n.getTextContent()).shortValue());
2950
3102
                                        else if(name.endsWith(LaTeXDrawNamespace.XML_DISPLAY_GRID))
2951
3103
                                                displayGrid = Boolean.valueOf(n.getTextContent()).booleanValue();
2952
 
                                        else if(name.endsWith(LaTeXDrawNamespace.XML_DRAW_BORDERS))
2953
 
                                                setDrawBorders(Boolean.valueOf(n.getTextContent()).booleanValue());
2954
3104
                                }
2955
3105
                                catch(Exception e) { System.out.println(name + ": invalid value."); } //$NON-NLS-1$
2956
3106
                        }
3016
3166
        {
3017
3167
                this.drawBorders = drawBorders;
3018
3168
        }
 
3169
        
 
3170
        
 
3171
        
 
3172
        
 
3173
        private static String execute(String cmd)
 
3174
        {
 
3175
                if(cmd==null)
 
3176
                        return null;
 
3177
                
 
3178
                String log = "";
 
3179
                
 
3180
                try
 
3181
                {
 
3182
                        Runtime runtime = Runtime.getRuntime();
 
3183
                        Process process = runtime.exec(cmd);
 
3184
                        String eol              = System.getProperty("line.separator");
 
3185
                        boolean ok = true;
 
3186
                        int cpt    = 1;
 
3187
                        int exit   = 0;
 
3188
                        
 
3189
                        synchronized(runtime) {
 
3190
                                while(ok && cpt<10)
 
3191
                                        try { 
 
3192
                                                exit = process.exitValue(); 
 
3193
                                                ok = false; 
 
3194
                                        }
 
3195
                                        catch(IllegalThreadStateException e) { 
 
3196
                                                runtime.wait(10); 
 
3197
                                                cpt++; 
 
3198
                                        }
 
3199
                        }
 
3200
                        
 
3201
                        InputStreamReader isr = new InputStreamReader(process.getInputStream());
 
3202
                        BufferedReader br     = new BufferedReader(isr);
 
3203
                        String line               = br.readLine();
 
3204
                        
 
3205
                        while(line!=null) {
 
3206
                                log += line + eol;
 
3207
                                line = br.readLine();
 
3208
                        }
 
3209
                        
 
3210
                        br.close();
 
3211
                        isr.close();
 
3212
                        
 
3213
                        if(exit!=0)
 
3214
                                throw new IllegalArgumentException(log);
 
3215
                        
 
3216
                        return log;
 
3217
                }
 
3218
                catch(Exception e) { return log; }
 
3219
        }
 
3220
        
3019
3221
}