<commit_before>/*
 * ChipDataFilePreview.java
 *
 * Created on December 31, 2005, 5:10 PM
 */

package ch.unizh.ini.caviar.graphics;

import ch.unizh.ini.caviar.aemonitor.*;
import ch.unizh.ini.caviar.chip.*;
import ch.unizh.ini.caviar.chip.EventExtractor2D;
import ch.unizh.ini.caviar.event.*;
import ch.unizh.ini.caviar.eventio.*;
import ch.unizh.ini.caviar.util.EngineeringFormat;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;
import java.util.logging.*;
import javax.swing.*;

/**
 * Provides preview of recorded AE data selectedFile
 *
 *
 * @author tobi
 */
public class ChipDataFilePreview extends JPanel implements PropertyChangeListener{
    JFileChooser chooser;
    EventExtractor2D extractor;
    int oldScale=4;
    ChipCanvas canvas;
    AEChipRenderer renderer;
    JLabel infoLabel;
    AEChip chip;
    volatile boolean indexFileEnabled=false;
    Logger log=Logger.getLogger("AEViewer");
    private int packetTime=40000;
    
    /**
     * Creates new form ChipDataFilePreview
     */
    public ChipDataFilePreview(JFileChooser jfc, AEChip chip) {
        canvas=new RetinaCanvas(chip);
        setLayout(new BorderLayout());
        this.chooser=jfc;
        extractor=chip.getEventExtractor();
        renderer=chip.getRenderer();
        canvas.setScale(2);
        add(canvas.getCanvas(),BorderLayout.CENTER);
        canvas.getCanvas().addKeyListener(new KeyAdapter(){
            public void keyReleased(KeyEvent e) {
//                System.out.println(e+"\n");
                switch(e.getKeyCode()){
                    case KeyEvent.VK_S:
                        packetTime/=2;
                        break;
                    case KeyEvent.VK_F:
                        packetTime*=2;
                        break;
                }
            }
        });
//        infoLabel=new JLabel("");
//        infoLabel.setForeground(Color.white);
//        add(infoLabel,BorderLayout.NORTH);
    }
    
//    FileInputStream fis;
    File selectedFile, currentFile;
    boolean isIndexFile(File f){
        if(f==null) return false;
        if(f.getName().endsWith(".index")) return true; else return false;
    }
    
    public void propertyChange(PropertyChangeEvent evt) {
//            System.out.println(evt);
        selectedFile=chooser.getSelectedFile();
//        System.out.println("evt.getPropertyName()="+evt.getPropertyName());
//        System.out.println("evt.getNewValue()="+evt.getNewValue());
        if(evt.getPropertyName()=="SelectedFileChangedProperty"){
            showFile(selectedFile); // starts showing selectedFile
        }
    }
    AEFileInputStream ais;
    
    volatile boolean deleteIt=false;
    
    public void deleteCurrentFile(){
        if(indexFileEnabled){
            log.warning("won't try to delete this index file");
            return;
        }
        deleteIt=true;
    }
    
    public void renameCurrentFile(){
        log.warning("renaming not implemented yet for "+currentFile);
        
    }
    
    volatile boolean stop=false;
    
// gets called on property change, possibly with null file
    public void showFile(File file) {
        try {
//            if(fis!=null){ System.out.println("closing "+fis); fis.close();}
            if(file==null) {stop=true; return;}
//            fis=new FileInputStream(file);
            currentFile=file;
            indexFileEnabled=isIndexFile(file);
            if(!indexFileEnabled){
                if(ais!=null) {
//                    System.out.println("closing "+ais);
                    ais.close();
                    ais=null;
                    System.gc(); // try to make memory mapped file GC'ed so that user can delete it
                    System.runFinalization();
                    // see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4715154
                    // http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4724038
                }
                ais=new AEFileInputStream(new FileInputStream(file));
                try{ ais.rewind();}catch(IOException e){};
                fileSizeString=fmt.format(ais.size())+" events "+fmt.format(ais.getDurationUs()/1e6f)+" s";
            }else{
                indexFileString=getIndexFileCount(file);
            }
//        infoLabel.setText(fmt.format((int)ais.size()));
            stop=false;
            repaint();  // starts recursive repaint, finishes when paint returns without calling repaint itself
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    
    File indexFile=null;
    AEPacketRaw aeRaw;
    EventPacket ae;
    
    public void paint(Graphics g){
        if(stop || deleteIt) {
//            System.out.println("stop="+stop+" deleteIt="+deleteIt);
            try{
                if(ais!=null){
//                    System.out.println("closing "+ais);
                    ais.close();
                    ais=null;
                    System.gc();
                }
                if(deleteIt){
                    deleteIt=false;
                    if(currentFile!=null && currentFile.isFile()){
                        boolean deleted=currentFile.delete();
                        if(deleted){
                            log.info("succesffully deleted "+currentFile);
                            chooser.rescanCurrentDirectory();
                        }else{
                            log.warning("couldn't delete file "+currentFile);
                        }
                    }
                }
            }catch(IOException e){}
            return;
        }
        Graphics2D g2=(Graphics2D)canvas.getCanvas().getGraphics();
        g2.setColor(Color.black);
        g2.fillRect(0,0,getWidth(),getHeight());
       if(!indexFileEnabled){
            if(ais!=null){
                try{
                    aeRaw=ais.readPacketByTime(packetTime);
                }catch(EOFException e){
                    try{
                        ais.rewind();
                    }catch(IOException ioe){
                        System.err.println("IOException on rewind from EOF: "+ioe.getMessage());
                        ioe.printStackTrace();
                    }
                }catch(IOException e){
                    e.printStackTrace();
                    try{
                        ais.close();
                        if(ais!=null){
                            try {
                                ais.close();
                            } catch (IOException e2) {
                                e2.printStackTrace();
                            }
                        }
                    }catch(Exception e3){
                        e3.printStackTrace();
                    }
                }
                if(aeRaw!=null) ae=extractor.extractPacket(aeRaw);
            }<commit_msg>fixed flicker in preview that was caused by an extra call to fill(Color.black) that was redundant. the actual drawing happens on the page flip with buffer.show().<commit_after>/*
 * ChipDataFilePreview.java
 *
 * Created on December 31, 2005, 5:10 PM
 */

package ch.unizh.ini.caviar.graphics;

import ch.unizh.ini.caviar.aemonitor.*;
import ch.unizh.ini.caviar.chip.*;
import ch.unizh.ini.caviar.chip.EventExtractor2D;
import ch.unizh.ini.caviar.event.*;
import ch.unizh.ini.caviar.eventio.*;
import ch.unizh.ini.caviar.util.EngineeringFormat;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;
import java.util.logging.*;
import javax.swing.*;

/**
 * Provides preview of recorded AE data selectedFile
 *
 *
 * @author tobi
 */
public class ChipDataFilePreview extends JPanel implements PropertyChangeListener{
    JFileChooser chooser;
    EventExtractor2D extractor;
    int oldScale=4;
    ChipCanvas canvas;
    AEChipRenderer renderer;
    JLabel infoLabel;
    AEChip chip;
    volatile boolean indexFileEnabled=false;
    Logger log=Logger.getLogger("AEViewer");
    private int packetTime=40000;
    
    /**
     * Creates new form ChipDataFilePreview
     */
    public ChipDataFilePreview(JFileChooser jfc, AEChip chip) {
        canvas=new RetinaCanvas(chip);
        setLayout(new BorderLayout());
        this.chooser=jfc;
        extractor=chip.getEventExtractor();
        renderer=chip.getRenderer();
        canvas.setScale(2);
        add(canvas.getCanvas(),BorderLayout.CENTER);
        canvas.getCanvas().addKeyListener(new KeyAdapter(){
            public void keyReleased(KeyEvent e) {
//                System.out.println(e+"\n");
                switch(e.getKeyCode()){
                    case KeyEvent.VK_S:
                        packetTime/=2;
                        break;
                    case KeyEvent.VK_F:
                        packetTime*=2;
                        break;
                }
            }
        });
//        infoLabel=new JLabel("");
//        infoLabel.setForeground(Color.white);
//        add(infoLabel,BorderLayout.NORTH);
    }
    
//    FileInputStream fis;
    File selectedFile, currentFile;
    boolean isIndexFile(File f){
        if(f==null) return false;
        if(f.getName().endsWith(".index")) return true; else return false;
    }
    
    public void propertyChange(PropertyChangeEvent evt) {
//            System.out.println(evt);
        selectedFile=chooser.getSelectedFile();
//        System.out.println("evt.getPropertyName()="+evt.getPropertyName());
//        System.out.println("evt.getNewValue()="+evt.getNewValue());
        if(evt.getPropertyName()=="SelectedFileChangedProperty"){
            showFile(selectedFile); // starts showing selectedFile
        }
    }
    AEFileInputStream ais;
    
    volatile boolean deleteIt=false;
    
    public void deleteCurrentFile(){
        if(indexFileEnabled){
            log.warning("won't try to delete this index file");
            return;
        }
        deleteIt=true;
    }
    
    public void renameCurrentFile(){
        log.warning("renaming not implemented yet for "+currentFile);
        
    }
    
    volatile boolean stop=false;
    
// gets called on property change, possibly with null file
    public void showFile(File file) {
        try {
//            if(fis!=null){ System.out.println("closing "+fis); fis.close();}
            if(file==null) {stop=true; return;}
//            fis=new FileInputStream(file);
            currentFile=file;
            indexFileEnabled=isIndexFile(file);
            if(!indexFileEnabled){
                if(ais!=null) {
//                    System.out.println("closing "+ais);
                    ais.close();
                    ais=null;
                    System.gc(); // try to make memory mapped file GC'ed so that user can delete it
                    System.runFinalization();
                    // see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4715154
                    // http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4724038
                }
                ais=new AEFileInputStream(new FileInputStream(file));
                try{ ais.rewind();}catch(IOException e){};
                fileSizeString=fmt.format(ais.size())+" events "+fmt.format(ais.getDurationUs()/1e6f)+" s";
            }else{
                indexFileString=getIndexFileCount(file);
            }
//        infoLabel.setText(fmt.format((int)ais.size()));
            stop=false;
            repaint();  // starts recursive repaint, finishes when paint returns without calling repaint itself
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    
    File indexFile=null;
    AEPacketRaw aeRaw;
    EventPacket ae;
    
    public void paint(Graphics g){
        if(stop || deleteIt) {
//            System.out.println("stop="+stop+" deleteIt="+deleteIt);
            try{
                if(ais!=null){
//                    System.out.println("closing "+ais);
                    ais.close();
                    ais=null;
                    System.gc();
                }
                if(deleteIt){
                    deleteIt=false;
                    if(currentFile!=null && currentFile.isFile()){
                        boolean deleted=currentFile.delete();
                        if(deleted){
                            log.info("succesffully deleted "+currentFile);
                            chooser.rescanCurrentDirectory();
                        }else{
                            log.warning("couldn't delete file "+currentFile);
                        }
                    }
                }
            }catch(IOException e){}
            return;
        }
        Graphics2D g2=(Graphics2D)canvas.getCanvas().getGraphics();
//        g2.setColor(Color.black);
//        g2.fillRect(0,0,getWidth(),getHeight()); // rendering method already paints frame black, shouldn't do it here or we get flicker from black to image
       if(!indexFileEnabled){
            if(ais!=null){
                try{
                    aeRaw=ais.readPacketByTime(packetTime);
                }catch(EOFException e){
                    try{
                        ais.rewind();
                    }catch(IOException ioe){
                        System.err.println("IOException on rewind from EOF: "+ioe.getMessage());
                        ioe.printStackTrace();
                    }
                }catch(IOException e){
                    e.printStackTrace();
                    try{
                        ais.close();
                        if(ais!=null){
                            try {
                                ais.close();
                            } catch (IOException e2) {
                                e2.printStackTrace();
                            }
                        }
                    }catch(Exception e3){
                        e3.printStackTrace();
                    }
                }
                if(aeRaw!=null) ae=extractor.extractPacket(aeRaw);
            }
{'fraction_non_alphanumeric': 0.11241755090335533, 'fraction_numerical': 0.006739317464869515, 'mean_word_length': 1.5740911607307622, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5041649', 'n_tokens_mistral': 3824, 'n_tokens_neox': 3608, 'n_words': 697}