<commit_before>/*
 * ARX: Powerful Data Anonymization
 * Copyright (C) 2012 - 2014 <NAME>, <NAME>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package org.deidentifier.arx.gui.view.impl.explore;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import org.deidentifier.arx.ARXLattice;
import org.deidentifier.arx.ARXLattice.ARXNode;
import org.deidentifier.arx.gui.Controller;
import org.deidentifier.arx.gui.model.Model;
import org.deidentifier.arx.gui.model.ModelEvent;
import org.deidentifier.arx.gui.model.ModelEvent.ModelPart;
import org.deidentifier.arx.gui.model.ModelNodeFilter;
import org.deidentifier.arx.gui.resources.Resources;
import org.deidentifier.arx.gui.view.SWTUtil;
import org.deidentifier.arx.gui.view.def.IView;
import org.deidentifier.arx.metric.InformationLoss;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;

import cern.colt.Arrays;
import de.linearbits.tiles.DecoratorColor;
import de.linearbits.tiles.DecoratorListener;
import de.linearbits.tiles.DecoratorString;
import de.linearbits.tiles.Filter;
import de.linearbits.tiles.Gradient;
import de.linearbits.tiles.GradientHeatscale;
import de.linearbits.tiles.TileLayoutDynamic;
import de.linearbits.tiles.Tiles;

/**
 * This class implements a tiles view on selected nodes.
 * 
 * @author prasser
 */
public class ViewTiles implements IView {

    /** The controller. */
    private final Controller     controller;

    /** The tiles. */
    private final Tiles<ARXNode> tiles;

    /** The model. */
    private Model                model;

    /** The format. */
    private final NumberFormat   format = new DecimalFormat("##0.000"); //$NON-NLS-1$

    /**
     * Init.
     *
     * @param parent
     * @param controller
     */
    public ViewTiles(final Composite parent, final Controller controller) {

        // Listen
        controller.addListener(ModelPart.SELECTED_NODE, this);
        controller.addListener(ModelPart.FILTER, this);
        controller.addListener(ModelPart.MODEL, this);
        controller.addListener(ModelPart.RESULT, this);

        this.controller = controller;

        tiles = new Tiles<ARXNode>(parent, SWT.NONE);
        tiles.setLayoutData(SWTUtil.createFillGridData());

        // Set layout
        tiles.setTileLayout(new TileLayoutDynamic(10, 20, 5, 5));
        tiles.setComparator(new Comparator<ARXNode>() {
            public int compare(ARXNode o1, ARXNode o2) {
                int c1 = o1.getMinimumInformationLoss().compareTo(o2.getMinimumInformationLoss());
                return c1 != 0 ? c1 : o1.getMaximumInformationLoss().compareTo(o2.getMaximumInformationLoss());
            }
        });
        tiles.setFilter(new Filter<ARXNode>() {
            public boolean accepts(ARXNode arg0) {
                return true;
            }
        });
        tiles.setDecoratorLabel(new DecoratorString<ARXNode>() {
            @Override
            public String decorate(ARXNode node) {
                return Arrays.toString(node.getTransformation());
            }
        });
        tiles.setDecoratorBackgroundColor(createDecoratorBackgroundColor());
        tiles.setDecoratorTooltip(new DecoratorString<ARXNode>() {
            @Override
            public String decorate(ARXNode node) {
                final StringBuffer b = new StringBuffer();
                b.append(Resources.getMessage("LatticeView.1")); //$NON-NLS-1$
                b.append(format.format(asRelativeValue(node.getMinimumInformationLoss())));
                b.append(" - "); //$NON-NLS-1$
                b.append(format.format(asRelativeValue(node.getMaximumInformationLoss())));
                b.append(" [%]\n"); //$NON-NLS-1$
                if (model.getOutputDefinition() != null) {
                    for (final String qi : node.getQuasiIdentifyingAttributes()) {

                        // Determine height of hierarchy
                        int height = model.getOutputDefinition().isHierarchyAvailable(qi) ?
                                model.getOutputDefinition().getHierarchy(qi)[0].length : 0;
                        b.append(" * "); //$NON-NLS-1$
                        b.append(qi);
                        b.append(": "); //$NON-NLS-1$
                        b.append(format.format(asRelativeValue(node.getGeneralization(qi), height - 1)));
                        b.append(" [%]\n"); //$NON-NLS-1$
                    }
                }
                b.setLength(b.length() - 1);
                return b.toString();

            }
        });
        tiles.update();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.deidentifier.arx.gui.view.def.IView#dispose()
     */
    @Override
    public void dispose() {
        controller.removeListener(this);
    }

    /**
     * Resets the view.
     */
    @Override
    public void reset() {
        tiles.setRedraw(false);
        tiles.setItems(new ArrayList<ARXNode>());
        tiles.setRedraw(true);
        tiles.setFilter(new Filter<ARXNode>() {
            public boolean accepts(ARXNode arg0) {
                return true;
            }
        });
        tiles.update();            final int index = (levels[level].length - 1 - levelsize[level]);
        SWTUtil.disable(tiles);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.deidentifier.arx.gui.view.def.IView#update(org.deidentifier.arx.gui.model.ModelEvent)<commit_msg>Remove crap<commit_after>/*
 * ARX: Powerful Data Anonymization
 * Copyright (C) 2012 - 2014 <NAME>, <NAME>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package org.deidentifier.arx.gui.view.impl.explore;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import org.deidentifier.arx.ARXLattice;
import org.deidentifier.arx.ARXLattice.ARXNode;
import org.deidentifier.arx.gui.Controller;
import org.deidentifier.arx.gui.model.Model;
import org.deidentifier.arx.gui.model.ModelEvent;
import org.deidentifier.arx.gui.model.ModelEvent.ModelPart;
import org.deidentifier.arx.gui.model.ModelNodeFilter;
import org.deidentifier.arx.gui.resources.Resources;
import org.deidentifier.arx.gui.view.SWTUtil;
import org.deidentifier.arx.gui.view.def.IView;
import org.deidentifier.arx.metric.InformationLoss;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;

import cern.colt.Arrays;
import de.linearbits.tiles.DecoratorColor;
import de.linearbits.tiles.DecoratorListener;
import de.linearbits.tiles.DecoratorString;
import de.linearbits.tiles.Filter;
import de.linearbits.tiles.Gradient;
import de.linearbits.tiles.GradientHeatscale;
import de.linearbits.tiles.TileLayoutDynamic;
import de.linearbits.tiles.Tiles;

/**
 * This class implements a tiles view on selected nodes.
 * 
 * @author prasser
 */
public class ViewTiles implements IView {

    /** The controller. */
    private final Controller     controller;

    /** The tiles. */
    private final Tiles<ARXNode> tiles;

    /** The model. */
    private Model                model;

    /** The format. */
    private final NumberFormat   format = new DecimalFormat("##0.000"); //$NON-NLS-1$

    /**
     * Init.
     *
     * @param parent
     * @param controller
     */
    public ViewTiles(final Composite parent, final Controller controller) {

        // Listen
        controller.addListener(ModelPart.SELECTED_NODE, this);
        controller.addListener(ModelPart.FILTER, this);
        controller.addListener(ModelPart.MODEL, this);
        controller.addListener(ModelPart.RESULT, this);

        this.controller = controller;

        tiles = new Tiles<ARXNode>(parent, SWT.NONE);
        tiles.setLayoutData(SWTUtil.createFillGridData());

        // Set layout
        tiles.setTileLayout(new TileLayoutDynamic(10, 20, 5, 5));
        tiles.setComparator(new Comparator<ARXNode>() {
            public int compare(ARXNode o1, ARXNode o2) {
                int c1 = o1.getMinimumInformationLoss().compareTo(o2.getMinimumInformationLoss());
                return c1 != 0 ? c1 : o1.getMaximumInformationLoss().compareTo(o2.getMaximumInformationLoss());
            }
        });
        tiles.setFilter(new Filter<ARXNode>() {
            public boolean accepts(ARXNode arg0) {
                return true;
            }
        });
        tiles.setDecoratorLabel(new DecoratorString<ARXNode>() {
            @Override
            public String decorate(ARXNode node) {
                return Arrays.toString(node.getTransformation());
            }
        });
        tiles.setDecoratorBackgroundColor(createDecoratorBackgroundColor());
        tiles.setDecoratorTooltip(new DecoratorString<ARXNode>() {
            @Override
            public String decorate(ARXNode node) {
                final StringBuffer b = new StringBuffer();
                b.append(Resources.getMessage("LatticeView.1")); //$NON-NLS-1$
                b.append(format.format(asRelativeValue(node.getMinimumInformationLoss())));
                b.append(" - "); //$NON-NLS-1$
                b.append(format.format(asRelativeValue(node.getMaximumInformationLoss())));
                b.append(" [%]\n"); //$NON-NLS-1$
                if (model.getOutputDefinition() != null) {
                    for (final String qi : node.getQuasiIdentifyingAttributes()) {

                        // Determine height of hierarchy
                        int height = model.getOutputDefinition().isHierarchyAvailable(qi) ?
                                model.getOutputDefinition().getHierarchy(qi)[0].length : 0;
                        b.append(" * "); //$NON-NLS-1$
                        b.append(qi);
                        b.append(": "); //$NON-NLS-1$
                        b.append(format.format(asRelativeValue(node.getGeneralization(qi), height - 1)));
                        b.append(" [%]\n"); //$NON-NLS-1$
                    }
                }
                b.setLength(b.length() - 1);
                return b.toString();

            }
        });
        tiles.update();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.deidentifier.arx.gui.view.def.IView#dispose()
     */
    @Override
    public void dispose() {
        controller.removeListener(this);
    }

    /**
     * Resets the view.
     */
    @Override
    public void reset() {
        tiles.setRedraw(false);
        tiles.setItems(new ArrayList<ARXNode>());
        tiles.setRedraw(true);
        tiles.setFilter(new Filter<ARXNode>() {
            public boolean accepts(ARXNode arg0) {
                return true;
            }
        });
        tiles.update();
        SWTUtil.disable(tiles);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.deidentifier.arx.gui.view.def.IView#update(org.deidentifier.arx.gui.model.ModelEvent)
{'fraction_non_alphanumeric': 0.11645299145299146, 'fraction_numerical': 0.0071499013806706115, 'mean_word_length': 2.3662517289073306, 'pattern_counts': {'":': 2, '<': 25, '<?xml version=': 0, '>': 25, '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': '5451534', 'n_tokens_mistral': 3635, 'n_tokens_neox': 3479, 'n_words': 736}