<commit_before>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.resultsView;

import entity.Literal;
import entity.Model;
import gui.AbstractComponentPanel;
import gui.Lang;
import gui.State;
import java.awt.Color;
import java.awt.Component;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import java.util.ListIterator;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

import solution.NotSatisfiableException;
import solution.SolverExecutionException;

/**
 *
 * @author Skander
 */
public class ResultsPanel extends AbstractComponentPanel {

    class RegexListener implements DocumentListener {
        
         @Override
        public void insertUpdate(DocumentEvent e) {
            setResult();
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            setResult();
        }
        
        @Override
        public void changedUpdate(DocumentEvent e) {
            setResult();
        }
    }
    
    class ResultTableCellRenderer extends DefaultTableCellRenderer {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            Component composant =  super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            if(row%2 == 0){
                composant.setBackground(Color.WHITE);
            }
            else {
                composant.setBackground(new Color(236,247,249));
            }
            return composant;
        }
    }
    
    class ResultTableModel extends DefaultTableModel
    {
        public ResultTableModel(Object []o, int n){
            super(o,n);
        }
        @Override
        public boolean isCellEditable(int rowIndex,int columnIndex){
		return false;
	}
    }
    
    private int currentModelIndex = 0;
    ListIterator<Model> iter;
    Model actModel;
    ExportDialog exportDialog;

    /**
     * Creates new form ResultsPanel
     */
    public ResultsPanel() {
        exportDialog = new ExportDialog();
        initComponents();
    }

    /**
     * Update the models iterator
     */
    public void updateIterator(ListIterator<Model> iter) {
        this.iter = iter;
    }
    
    public void setActModel(Model m) {
        actModel = m;
    }

    public void setResult() {
        
        boolean falseLiterals = falseCheckBox.isSelected();
        boolean trueLiterals = trueCheckBox.isSelected();
        
        String regex = searchTextField.getText();
        Pattern pattern = null;
        try {
            pattern = Pattern.compile(regex);
        } catch (PatternSyntaxException e) {
            regex = "";
        }
        
        
        ResultTableModel model = (ResultTableModel) jTable1.getModel();
        model.setNumRows(0);
        ArrayList<Literal> literals = (ArrayList<Literal>) actModel.literals;
        for(int i = 0; i < literals.size(); i++) {
            String name = literals.get(i).getLiteral();
            boolean value = literals.get(i).isLiteral_positivity();
            
            if(regex!="" && !pattern.matcher(name).find()){
                continue;
            }
            if(falseLiterals && !value){
                model.addRow(new String[]{name,"False"});
            } else if(trueLiterals && value){
                model.addRow(new String[]{name,"True"});
            }
        }
    }
    
    public void setEmpty(){
        ResultTableModel model = (ResultTableModel) jTable1.getModel();
        model.setNumRows(0);
    }
    
    public void exportModel() throws IOException {
        final JFileChooser fc = new JFileChooser();
        fc.setFileFilter(new FileNameExtensionFilter("Text files(txt, text)","txt","text"));
        fc.addChoosableFileFilter(new FileNameExtensionFilter("Latex files(latex)","tex"));
        fc.setAcceptAllFileFilterUsed(false);
        int returnVal = fc.showOpenDialog(this);
        
        if(returnVal == JFileChooser.APPROVE_OPTION){
            String filename = fc.getSelectedFile().getName();
            String extension = (filename.contains(".")?filename.substring(filename.lastIndexOf("."),filename.length()):"txt");
            Object[] options1 = { "Try This Number", "Choose A Random Number",
                "Quit" };
            int result = JOptionPane.showConfirmDialog(null, exportDialog,"Format d'export",JOptionPane.DEFAULT_OPTION);
            if(result == JOptionPane.YES_OPTION){
                StringBuilder sb = new StringBuilder();
                
                String prefix = exportDialog.getPrefixValue();
                String separator = exportDialog.getSeparatorValue();
                String suffix = exportDialog.getSuffixValue();
                System.err.println(prefix);
                
                ArrayList<Literal> literals = (ArrayList<Literal>) actModel.literals;
                for(int i = 0; i < literals.size(); i++) {
                    sb.append(prefix+literals.get(i).getLiteral()+separator+(literals.get(i).isLiteral_positivity()?"true":"false")+suffix+"\n");
                }

                BufferedWriter out = new BufferedWriter(new FileWriter(fc.getSelectedFile().getAbsolutePath()));
                out.write(sb.toString());
                out.close();
            }
            
        }
    }

    /**
     * Enable the next and previous buttons depending on the frame state.
     */
    public void applyRestrictions() {<commit_msg>#76 Fixed a System.err.println of debug Fixed checkbox use for syntax definition.<commit_after>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.resultsView;

import entity.Literal;
import entity.Model;
import gui.AbstractComponentPanel;
import gui.Lang;
import gui.State;
import java.awt.Color;
import java.awt.Component;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import java.util.ListIterator;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

import solution.NotSatisfiableException;
import solution.SolverExecutionException;

/**
 *
 * @author Skander
 */
public class ResultsPanel extends AbstractComponentPanel {

    class RegexListener implements DocumentListener {
        
         @Override
        public void insertUpdate(DocumentEvent e) {
            setResult();
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            setResult();
        }
        
        @Override
        public void changedUpdate(DocumentEvent e) {
            setResult();
        }
    }
    
    class ResultTableCellRenderer extends DefaultTableCellRenderer {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            Component composant =  super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            if(row%2 == 0){
                composant.setBackground(Color.WHITE);
            }
            else {
                composant.setBackground(new Color(236,247,249));
            }
            return composant;
        }
    }
    
    class ResultTableModel extends DefaultTableModel
    {
        public ResultTableModel(Object []o, int n){
            super(o,n);
        }
        @Override
        public boolean isCellEditable(int rowIndex,int columnIndex){
		return false;
	}
    }
    
    private int currentModelIndex = 0;
    ListIterator<Model> iter;
    Model actModel;
    ExportDialog exportDialog;

    /**
     * Creates new form ResultsPanel
     */
    public ResultsPanel() {
        exportDialog = new ExportDialog();
        initComponents();
    }

    /**
     * Update the models iterator
     */
    public void updateIterator(ListIterator<Model> iter) {
        this.iter = iter;
    }
    
    public void setActModel(Model m) {
        actModel = m;
    }

    public void setResult() {
        
        boolean falseLiterals = falseCheckBox.isSelected();
        boolean trueLiterals = trueCheckBox.isSelected();
        
        String regex = searchTextField.getText();
        Pattern pattern = null;
        try {
            pattern = Pattern.compile(regex);
        } catch (PatternSyntaxException e) {
            regex = "";
        }
        
        
        ResultTableModel model = (ResultTableModel) jTable1.getModel();
        model.setNumRows(0);
        ArrayList<Literal> literals = (ArrayList<Literal>) actModel.literals;
        for(int i = 0; i < literals.size(); i++) {
            String name = literals.get(i).getLiteral();
            boolean value = literals.get(i).isLiteral_positivity();
            
            if(regex!="" && !pattern.matcher(name).find()){
                continue;
            }
            if(falseLiterals && !value){
                model.addRow(new String[]{name,"False"});
            } else if(trueLiterals && value){
                model.addRow(new String[]{name,"True"});
            }
        }
    }
    
    public void setEmpty(){
        ResultTableModel model = (ResultTableModel) jTable1.getModel();
        model.setNumRows(0);
    }
    
    public void exportModel() throws IOException {
        final JFileChooser fc = new JFileChooser();
        fc.setFileFilter(new FileNameExtensionFilter("Text files(txt, text)","txt","text"));
        fc.addChoosableFileFilter(new FileNameExtensionFilter("Latex files(latex)","tex"));
        fc.setAcceptAllFileFilterUsed(false);
        int returnVal = fc.showOpenDialog(this);
        
        if(returnVal == JFileChooser.APPROVE_OPTION){
            String filename = fc.getSelectedFile().getName();
            String extension = (filename.contains(".")?filename.substring(filename.lastIndexOf("."),filename.length()):"txt");
            Object[] options1 = { "Try This Number", "Choose A Random Number",
                "Quit" };
            int result = JOptionPane.showConfirmDialog(null, exportDialog,"Format d'export",JOptionPane.DEFAULT_OPTION);
            if(result == JOptionPane.YES_OPTION){
                StringBuilder sb = new StringBuilder();
                
                String prefix = exportDialog.getPrefixValue();
                String separator = exportDialog.getSeparatorValue();
                String suffix = exportDialog.getSuffixValue();
                
                
                ArrayList<Literal> literals = (ArrayList<Literal>) actModel.literals;
                for(int i = 0; i < literals.size(); i++) {
                    String left = exportDialog.getLeftValue()=="litteral"?literals.get(i).getLiteral():(literals.get(i).isLiteral_positivity()?"true":"false");
                    String right = exportDialog.getRightValue()=="litteral"?literals.get(i).getLiteral():(literals.get(i).isLiteral_positivity()?"true":"false");
                    sb.append(prefix+left+separator+right+suffix+"\n");
                }

                BufferedWriter out = new BufferedWriter(new FileWriter(fc.getSelectedFile().getAbsolutePath()));
                out.write(sb.toString());
                out.close();
            }
            
        }
    }

    /**
     * Enable the next and previous buttons depending on the frame state.
     */
    public void applyRestrictions() {
{'fraction_non_alphanumeric': 0.10357573322619526, 'fraction_numerical': 0.0032141422257934912, 'mean_word_length': 2.3738140417457307, 'pattern_counts': {'":': 3, '<': 19, '<?xml version=': 0, '>': 15, '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': '6639972', 'n_tokens_mistral': 3320, 'n_tokens_neox': 3212, 'n_words': 720}