<commit_before>/* 
 * Copyright (c) 2015 <NAME>
 * 
 * See the file LICENSE for copying permission.
 */
package latexstudio.editor.pdf;

import java.awt.Point;
import latexstudio.editor.runtime.CommandLineExecutor;
import java.util.logging.Logger;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import latexstudio.editor.EditorTopComponent;
import latexstudio.editor.files.FileService;
import latexstudio.editor.runtime.CommandLineBuilder;
import latexstudio.editor.util.ApplicationUtils;
import org.openide.util.Exceptions;

/**
 * A thread that is responsible for refreshing the PDF preview when necessary.
 * @author Sebastian
 */
public class PDFPreviewRefresher implements Runnable {
    
    private final JScrollPane jScrollPane;
    private final EditorTopComponent etc;
    private final PDFDisplay pdfDisplay;
    
    private static final Logger LOGGER = Logger.getLogger(PDFPreviewRefresher.class.getName());
    
    public PDFPreviewRefresher(JScrollPane jScrollPane, EditorTopComponent etc, PDFDisplay pdfDisplay) {
        this.jScrollPane = jScrollPane;
        this.etc = etc;
        this.pdfDisplay = pdfDisplay;
    }

    @Override
    public void run() {
        while(true) {
            if (etc.isDirty()) {
                compileTemporaryFile();
                drawPreview();
              //  etc.setDirty(false);
            }
            
            try {
                Thread.sleep(4000);
            } catch (InterruptedException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }
    
    public void drawPreview() {
        JPanel pdfImagePanel = pdfDisplay.drawPreviewOnJPanel();
       
        if (pdfImagePanel != null) {
            JViewport vp = jScrollPane.getViewport();
            Point p = vp.getViewPosition();
            vp.setView(pdfImagePanel);
            vp.setViewPosition(p);

        }
    }

    public void compileTemporaryFile() {        
        String fileLocation = ApplicationUtils.getTempSourceFile();
        String content = etc.getEditorContent();
        
        FileService.writeToFile(fileLocation, content);

        CommandLineExecutor.executeGeneratePDF(new CommandLineBuilder()
                .withPathToSource(ApplicationUtils.getTempSourceFile())
                .withOutputDirectory(ApplicationUtils.getAppDirectory())
                .withJobname("preview")
                .withWorkingFile(etc.getCurrentFile())
                .withLatexPath(etc.getLatexPath())
        );
    }

}<commit_msg>reverted undo rendo fix<commit_after>/* 
 * Copyright (c) 2015 <NAME>
 * 
 * See the file LICENSE for copying permission.
 */
package latexstudio.editor.pdf;

import java.awt.Point;
import latexstudio.editor.runtime.CommandLineExecutor;
import java.util.logging.Logger;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import latexstudio.editor.EditorTopComponent;
import latexstudio.editor.files.FileService;
import latexstudio.editor.runtime.CommandLineBuilder;
import latexstudio.editor.util.ApplicationUtils;
import org.openide.util.Exceptions;

/**
 * A thread that is responsible for refreshing the PDF preview when necessary.
 * @author Sebastian
 */
public class PDFPreviewRefresher implements Runnable {
    
    private final JScrollPane jScrollPane;
    private final EditorTopComponent etc;
    private final PDFDisplay pdfDisplay;
    
    private static final Logger LOGGER = Logger.getLogger(PDFPreviewRefresher.class.getName());
    
    public PDFPreviewRefresher(JScrollPane jScrollPane, EditorTopComponent etc, PDFDisplay pdfDisplay) {
        this.jScrollPane = jScrollPane;
        this.etc = etc;
        this.pdfDisplay = pdfDisplay;
    }

    @Override
    public void run() {
        while(true) {
            if (etc.isDirty()) {
                compileTemporaryFile();
                drawPreview();
                etc.setDirty(false);
            }
            
            try {
                Thread.sleep(4000);
            } catch (InterruptedException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }
    
    private void drawPreview() {
        JPanel pdfImagePanel = pdfDisplay.drawPreviewOnJPanel();
       
        if (pdfImagePanel != null) {
            JViewport vp = jScrollPane.getViewport();
            Point p = vp.getViewPosition();
            vp.setView(pdfImagePanel);
            vp.setViewPosition(p);

        }
    }

    private void compileTemporaryFile() {        
        String fileLocation = ApplicationUtils.getTempSourceFile();
        String content = etc.getEditorContent();
        
        FileService.writeToFile(fileLocation, content);

        CommandLineExecutor.executeGeneratePDF(new CommandLineBuilder()
                .withPathToSource(ApplicationUtils.getTempSourceFile())
                .withOutputDirectory(ApplicationUtils.getAppDirectory())
                .withJobname("preview")
                .withWorkingFile(etc.getCurrentFile())
                .withLatexPath(etc.getLatexPath())
        );
    }

}
{'fraction_non_alphanumeric': 0.087109375, 'fraction_numerical': 0.003125, 'mean_word_length': 2.4601351351351353, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5454892', 'n_tokens_mistral': 1400, 'n_tokens_neox': 1336, 'n_words': 277}