<commit_before>/**
 * Copyright (C) 2014 JBoss Inc
 *
 * 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 org.dashbuilder.client.displayer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.gwt.user.client.ui.Composite;
import org.dashbuilder.model.dataset.group.ColumnGroup;
import org.dashbuilder.model.dataset.group.DataSetGroup;
import org.dashbuilder.model.dataset.group.GroupStrategy;
import org.dashbuilder.model.dataset.sort.ColumnSort;
import org.dashbuilder.model.dataset.sort.DataSetSort;
import org.dashbuilder.model.dataset.sort.SortOrder;
import org.dashbuilder.model.displayer.DataDisplayer;

/**
 * Base class for implementing custom viewers.
 * <p>Any derived class must implement:
 * <ul>
 *     <li>The draw() & redraw() methods.</li>
 *     <li>The capture of events coming from the DataViewerListener interface.</li>
 * </ul>
 */
public abstract class AbstractDataViewer<T extends DataDisplayer> extends Composite implements DataViewer<T> {

    protected DataSetHandler dataSetHandler;
    protected T dataDisplayer;
    protected List<DataViewerListener> listenerList = new ArrayList<DataViewerListener>();
    protected Map<String,List<String>> columnSelectionMap = new HashMap<String,List<String>>();

    public T getDataDisplayer() {
        return dataDisplayer;
    }

    public void setDataDisplayer(T dataDisplayer) {
        this.dataDisplayer = dataDisplayer;
    }

    public DataSetHandler getDataSetHandler() {
        return dataSetHandler;
    }

    public void setDataSetHandler(DataSetHandler dataSetHandler) {
        this.dataSetHandler = dataSetHandler;
    }

    public void addListener(DataViewerListener listener) {
        listenerList.add(listener);
    }

    // CAPTURE EVENTS RECEIVED FROM OTHER VIEWERS

    public void onGroupIntervalsSelected(DataViewer viewer, DataSetGroup groupOp) {
        dataSetHandler.addGroupOperation(groupOp);
        redraw();
    }

    public void onGroupIntervalsReset(DataViewer viewer, DataSetGroup groupOp) {
        dataSetHandler.removeGroupOperation(groupOp);
        redraw();
    }

    // DATA COLUMN VALUES SELECTION, FILTER & NOTIFICATION

    /**
     * Get the set of columns being filtered.
     */
    protected Set<String> filterColumns() {
        return columnSelectionMap.keySet();
    }

    /**
     * Get the current filter values for the given data set column.
     *
     * @param columnId The column identifier-
     * @return A list of distinct values currently selected.
     */
    protected List<String> filterValues(String columnId) {
        return columnSelectionMap.get(columnId);
    }

    /**
     * Updates the current filter values for the given data set column.
     *
     * @param columnId The column to filter for.
     * @param valueSelected The value to add/remove from the current filter.
     * @param numberOfRows The total number of available column values.
     */
    protected void filterUpdate(String columnId, String valueSelected, int numberOfRows) {
        List<String> selectedValues = columnSelectionMap.get(columnId);
        if (selectedValues == null) {
            selectedValues = new ArrayList<String>();
            selectedValues.add(valueSelected);
            columnSelectionMap.put(columnId, selectedValues);
            filterApply(columnId, selectedValues);
        }
        else if (selectedValues.contains(valueSelected)) {
            selectedValues.remove(valueSelected);
            if (!selectedValues.isEmpty()) {
                filterApply(columnId, selectedValues);
            } else {
                filterReset(columnId);
            }
        } else {
            selectedValues.add(valueSelected);
            if (numberOfRows > 0 && selectedValues.size() < numberOfRows) {
                filterApply(columnId, selectedValues);
            } else {
                filterReset(columnId);
            }
        }
    }

    /**
     * Filter the values of the given column.
     *
     * @param columnId The name of the column to filter.
     * @param values A list of values to filter for.
     */
    protected void filterApply(String columnId, List<String> values) {

        // For string column filters, create and notify a group interval selection operation.
        DataSetGroup groupOp = dataSetHandler.getGroupOperation(columnId);
        DataSetGroup _groupSelect = null;
        if (groupOp != null && groupOp.getColumnGroup() != null) {
            _groupSelect = groupOp.cloneInstance();<commit_msg>Auto reset filter only when specified<commit_after>/**
 * Copyright (C) 2014 JBoss Inc
 *
 * 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 org.dashbuilder.client.displayer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.gwt.user.client.ui.Composite;
import org.dashbuilder.model.dataset.group.ColumnGroup;
import org.dashbuilder.model.dataset.group.DataSetGroup;
import org.dashbuilder.model.dataset.group.GroupStrategy;
import org.dashbuilder.model.dataset.sort.ColumnSort;
import org.dashbuilder.model.dataset.sort.DataSetSort;
import org.dashbuilder.model.dataset.sort.SortOrder;
import org.dashbuilder.model.displayer.DataDisplayer;

/**
 * Base class for implementing custom viewers.
 * <p>Any derived class must implement:
 * <ul>
 *     <li>The draw() & redraw() methods.</li>
 *     <li>The capture of events coming from the DataViewerListener interface.</li>
 * </ul>
 */
public abstract class AbstractDataViewer<T extends DataDisplayer> extends Composite implements DataViewer<T> {

    protected DataSetHandler dataSetHandler;
    protected T dataDisplayer;
    protected List<DataViewerListener> listenerList = new ArrayList<DataViewerListener>();
    protected Map<String,List<String>> columnSelectionMap = new HashMap<String,List<String>>();

    public T getDataDisplayer() {
        return dataDisplayer;
    }

    public void setDataDisplayer(T dataDisplayer) {
        this.dataDisplayer = dataDisplayer;
    }

    public DataSetHandler getDataSetHandler() {
        return dataSetHandler;
    }

    public void setDataSetHandler(DataSetHandler dataSetHandler) {
        this.dataSetHandler = dataSetHandler;
    }

    public void addListener(DataViewerListener listener) {
        listenerList.add(listener);
    }

    // CAPTURE EVENTS RECEIVED FROM OTHER VIEWERS

    public void onGroupIntervalsSelected(DataViewer viewer, DataSetGroup groupOp) {
        dataSetHandler.addGroupOperation(groupOp);
        redraw();
    }

    public void onGroupIntervalsReset(DataViewer viewer, DataSetGroup groupOp) {
        dataSetHandler.removeGroupOperation(groupOp);
        redraw();
    }

    // DATA COLUMN VALUES SELECTION, FILTER & NOTIFICATION

    /**
     * Get the set of columns being filtered.
     */
    protected Set<String> filterColumns() {
        return columnSelectionMap.keySet();
    }

    /**
     * Get the current filter values for the given data set column.
     *
     * @param columnId The column identifier-
     * @return A list of distinct values currently selected.
     */
    protected List<String> filterValues(String columnId) {
        return columnSelectionMap.get(columnId);
    }

    /**
     * Updates the current filter values for the given data set column.
     *
     * @param columnId The column to filter for.
     * @param valueSelected The value to add/remove from the current filter.
     * @param numberOfRows The total number of available column values.
     */
    protected void filterUpdate(String columnId, String valueSelected, int numberOfRows) {
        List<String> selectedValues = columnSelectionMap.get(columnId);
        if (selectedValues == null) {
            selectedValues = new ArrayList<String>();
            selectedValues.add(valueSelected);
            columnSelectionMap.put(columnId, selectedValues);
            filterApply(columnId, selectedValues);
        }
        else if (selectedValues.contains(valueSelected)) {
            selectedValues.remove(valueSelected);
            if (!selectedValues.isEmpty()) {
                filterApply(columnId, selectedValues);
            } else {
                filterReset(columnId);
            }
        } else {
            selectedValues.add(valueSelected);
            if (numberOfRows > 0 && selectedValues.size() >= numberOfRows) {
                filterReset(columnId);
            } else {
                filterApply(columnId, selectedValues);
            }
        }
    }

    /**
     * Filter the values of the given column.
     *
     * @param columnId The name of the column to filter.
     * @param values A list of values to filter for.
     */
    protected void filterApply(String columnId, List<String> values) {

        // For string column filters, create and notify a group interval selection operation.
        DataSetGroup groupOp = dataSetHandler.getGroupOperation(columnId);
        DataSetGroup _groupSelect = null;
        if (groupOp != null && groupOp.getColumnGroup() != null) {
            _groupSelect = groupOp.cloneInstance();
{'fraction_non_alphanumeric': 0.08640767198356003, 'fraction_numerical': 0.0017614247969468637, 'mean_word_length': 3.0047021943573666, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4633455', 'n_tokens_mistral': 2688, 'n_tokens_neox': 2641, 'n_words': 798}