<commit_before>/*******************************************************************************
 *  Copyright 2013 <NAME>                                                *
 *                                                                             *
 *  Licensed under the Apache License, Version 2.0 (the "License");            *
 *  you may not use this file except in compliance with the License.           *
 *  You may obtain a copy of the License at                                    *
 *                                                                             *
 *      http://www.apache.org/licenses/LICENSE-2.0                             *
 *                                                                             *
 *  Unless required by applicable law or agreed to in writing, software        *
 *  distributed under the License is distributed on an "AS IS" BASIS,          *
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
 *  See the License for the specific language governing permissions and        *
 *  limitations under the License.                                             *
 *******************************************************************************/

package com.vanomaly.superd.controller;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.util.Comparator;
import java.util.regex.Pattern;

import net.snakedoc.jutils.ConfigException;

import com.vanomaly.superd.Config;
import com.vanomaly.superd.core.FileScanner;
import com.vanomaly.superd.core.SimpleFileProperty;
import com.vanomaly.superd.view.BaselineLeftStringCenterOverrunTableCell;
import com.vanomaly.superd.view.BaselineRightStringTableCell;
import com.vanomaly.superd.view.ThemedStageFactory;

import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import javafx.util.Callback;

/**
 * @author <NAME>
 *
 */
public class MainWindowController {
    
    @FXML private TableView<SimpleFileProperty> table;
    @FXML private TableColumn<SimpleFileProperty, String> fileCol;
    @FXML private TableColumn<SimpleFileProperty, String> hashCol;
    @FXML private TableColumn<SimpleFileProperty, String> sizeCol;
    final private ObservableList<SimpleFileProperty> tableData = FXCollections.observableArrayList();
    
    @FXML private Label targetLabel;
    @FXML private TextField targetText;
    
    @FXML private Label delimiterLabel;
    @FXML private TextField delimiterText;
    
    @FXML private Label hashMethodLabel;
    @FXML private Slider hashSlider;
    @FXML private TextField hashMethodDescText;
    
    @FXML private Button addButton;
    @FXML private Button actionButton;
    
    private String CURRENT_HASHALGO;
    private String CURRENT_DELIMITER;
    private String LAST_DELIMITER;
    
    private MainWindowController() {
    }
    
    private static class MainWindowControllerator {
        private static final MainWindowController INSTANCE = new MainWindowController();
    }
    
    public static MainWindowController getInstance() {
        return MainWindowControllerator.INSTANCE;
    }
    
    /**
     * Initializes the controller class. This method is automatically called
     * after the fxml file has been loaded.
     */
    @FXML
    private void initialize() {
        hashSlider.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(final ObservableValue<? extends Number> ov,
                    final Number old_val, final Number new_val) {
                try {
                    setAndUpdateHashAlgo(new_val.intValue());
                } catch (ConfigException e) {
                    // don't care
                }
            }
        });
        hashSlider.setValue(initializeSlider());
        delimiterText.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(final ObservableValue<? extends String> ov,
                                    final String old_val, final String new_Val) {
                if (!"".equals(new_Val) && new_Val != null) {
                    refreshDelimiter();
                }
            }
        });
        initializeDelimiterText();
        initializeColumns();
        table.setItems(tableData);
    }
    
    private void initializeColumns() {
        fileCol.setCellValueFactory(
                new PropertyValueFactory<SimpleFileProperty, String>("path")
                );
        fileCol.setCellFactory(
                new Callback<TableColumn<SimpleFileProperty, String>, TableCell<SimpleFileProperty, String>>() {
                    @Override
                    public TableCell<SimpleFileProperty, String> call(TableColumn<SimpleFileProperty, String> t) {
                        return new BaselineLeftStringCenterOverrunTableCell();
                    }
                });
        hashCol.setCellValueFactory(
                new PropertyValueFactory<SimpleFileProperty, String>("hash")
                );
        hashCol.setCellFactory(
                new Callback<TableColumn<SimpleFileProperty, String>, TableCell<SimpleFileProperty, String>>() {
                    @Override
                    public TableCell<SimpleFileProperty, String> call(TableColumn<SimpleFileProperty, String> t) {
                        return new BaselineLeftStringCenterOverrunTableCell();
                    }
                });
        sizeCol.setCellValueFactory(
                new PropertyValueFactory<SimpleFileProperty, String>("size")
                );
        sizeCol.setCellFactory(
                new Callback<TableColumn<SimpleFileProperty,String>, TableCell<SimpleFileProperty, String>>() {
                    @Override
                    public TableCell<SimpleFileProperty, String> call(TableColumn<SimpleFileProperty, String> t) {
                        return new BaselineRightStringTableCell();
                    }
                });
        sizeCol.setComparator(new Comparator<String>() {

            @Override
            public int compare(String arg0, String arg1) {
                // TODO Auto-generated method stub
                return 0;
            }
            
        });
    }
    
    public void addTableRow(final SimpleFileProperty data) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                tableData.add(data);
            }
        });
        table.scrollTo(tableData.size() + 1);
    }
    
    private void refreshDelimiter() {
        if (!"".equals(getTargetText()) && getTargetText() != null) {
            setDelimiter();
            if (isDelimiterChanged() && LAST_DELIMITER != null) {<commit_msg>added comparator for the size column, allows sorting by file size<commit_after>/*******************************************************************************
 *  Copyright 2013 <NAME>                                                *
 *                                                                             *
 *  Licensed under the Apache License, Version 2.0 (the "License");            *
 *  you may not use this file except in compliance with the License.           *
 *  You may obtain a copy of the License at                                    *
 *                                                                             *
 *      http://www.apache.org/licenses/LICENSE-2.0                             *
 *                                                                             *
 *  Unless required by applicable law or agreed to in writing, software        *
 *  distributed under the License is distributed on an "AS IS" BASIS,          *
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
 *  See the License for the specific language governing permissions and        *
 *  limitations under the License.                                             *
 *******************************************************************************/

package com.vanomaly.superd.controller;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.util.Comparator;
import java.util.regex.Pattern;

import net.snakedoc.jutils.ConfigException;

import com.vanomaly.superd.Config;
import com.vanomaly.superd.core.FileScanner;
import com.vanomaly.superd.core.SimpleFileProperty;
import com.vanomaly.superd.view.BaselineLeftStringCenterOverrunTableCell;
import com.vanomaly.superd.view.BaselineRightStringTableCell;
import com.vanomaly.superd.view.ThemedStageFactory;

import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import javafx.util.Callback;

/**
 * @author <NAME>
 *
 */
public class MainWindowController {
    
    @FXML private TableView<SimpleFileProperty> table;
    @FXML private TableColumn<SimpleFileProperty, String> fileCol;
    @FXML private TableColumn<SimpleFileProperty, String> hashCol;
    @FXML private TableColumn<SimpleFileProperty, String> sizeCol;
    final private ObservableList<SimpleFileProperty> tableData = FXCollections.observableArrayList();
    
    @FXML private Label targetLabel;
    @FXML private TextField targetText;
    
    @FXML private Label delimiterLabel;
    @FXML private TextField delimiterText;
    
    @FXML private Label hashMethodLabel;
    @FXML private Slider hashSlider;
    @FXML private TextField hashMethodDescText;
    
    @FXML private Button addButton;
    @FXML private Button actionButton;
    
    private String CURRENT_HASHALGO;
    private String CURRENT_DELIMITER;
    private String LAST_DELIMITER;
    
    private MainWindowController() {
    }
    
    private static class MainWindowControllerator {
        private static final MainWindowController INSTANCE = new MainWindowController();
    }
    
    public static MainWindowController getInstance() {
        return MainWindowControllerator.INSTANCE;
    }
    
    /**
     * Initializes the controller class. This method is automatically called
     * after the fxml file has been loaded.
     */
    @FXML
    private void initialize() {
        hashSlider.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(final ObservableValue<? extends Number> ov,
                    final Number old_val, final Number new_val) {
                try {
                    setAndUpdateHashAlgo(new_val.intValue());
                } catch (ConfigException e) {
                    // don't care
                }
            }
        });
        hashSlider.setValue(initializeSlider());
        delimiterText.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(final ObservableValue<? extends String> ov,
                                    final String old_val, final String new_Val) {
                if (!"".equals(new_Val) && new_Val != null) {
                    refreshDelimiter();
                }
            }
        });
        initializeDelimiterText();
        initializeColumns();
        table.setItems(tableData);
    }
    
    private void initializeColumns() {
        fileCol.setCellValueFactory(
                new PropertyValueFactory<SimpleFileProperty, String>("path")
                );
        fileCol.setCellFactory(
                new Callback<TableColumn<SimpleFileProperty, String>, TableCell<SimpleFileProperty, String>>() {
                    @Override
                    public TableCell<SimpleFileProperty, String> call(TableColumn<SimpleFileProperty, String> t) {
                        return new BaselineLeftStringCenterOverrunTableCell();
                    }
                });
        hashCol.setCellValueFactory(
                new PropertyValueFactory<SimpleFileProperty, String>("hash")
                );
        hashCol.setCellFactory(
                new Callback<TableColumn<SimpleFileProperty, String>, TableCell<SimpleFileProperty, String>>() {
                    @Override
                    public TableCell<SimpleFileProperty, String> call(TableColumn<SimpleFileProperty, String> t) {
                        return new BaselineLeftStringCenterOverrunTableCell();
                    }
                });
        sizeCol.setCellValueFactory(
                new PropertyValueFactory<SimpleFileProperty, String>("size")
                );
        sizeCol.setCellFactory(
                new Callback<TableColumn<SimpleFileProperty,String>, TableCell<SimpleFileProperty, String>>() {
                    @Override
                    public TableCell<SimpleFileProperty, String> call(TableColumn<SimpleFileProperty, String> t) {
                        return new BaselineRightStringTableCell();
                    }
                });
        sizeCol.setComparator(new Comparator<String>() {

            @Override
            public int compare(String arg0, String arg1) {
                return getValue(arg0).compareTo(getValue(arg1));
            }
            
            private BigDecimal getValue(final String str) {
                char[] origArr = str.toCharArray();
                char[] destArr = new char[origArr.length];
                int j = 0;
                for (int i = 0; i < origArr.length; i++) {
                    char c = origArr[i];
                    if (c >= 48 && c <= 57) {
                        destArr[j++] = c;
                    }
                }
                return new BigDecimal(new String(destArr, 0, j));
            }
            
        });
    }
    
    public void addTableRow(final SimpleFileProperty data) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                tableData.add(data);
            }
        });
        table.scrollTo(tableData.size() + 1);
    }
    
    private void refreshDelimiter() {
        if (!"".equals(getTargetText()) && getTargetText() != null) {
            setDelimiter();
            if (isDelimiterChanged() && LAST_DELIMITER != null) {
{'fraction_non_alphanumeric': 0.10012936610608021, 'fraction_numerical': 0.002069857697283312, 'mean_word_length': 2.078653922739944, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6887443', 'n_tokens_mistral': 3771, 'n_tokens_neox': 3649, 'n_words': 878}