<commit_before>/*
 * Contributions to FindBugs
 * Copyright (C) 2008, <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package de.tobject.findbugs.view.explorer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.model.WorkbenchLabelProvider;
import org.eclipse.ui.navigator.ICommonContentExtensionSite;
import org.eclipse.ui.navigator.ICommonLabelProvider;
import org.eclipse.ui.navigator.IDescriptionProvider;

import de.tobject.findbugs.FindbugsPlugin;
import de.tobject.findbugs.marker.FindBugsMarker;
import edu.umd.cs.findbugs.SortedBugCollection;
import edu.umd.cs.findbugs.cloud.BugCollectionStorageCloud;
import edu.umd.cs.findbugs.cloud.Cloud;

/**
 * @author Andrei
 */
public class BugLabelProvider implements /* IStyledLabelProvider, */ILabelProvider, IDescriptionProvider, ICommonLabelProvider,
        IColorProvider {

    private final WorkbenchLabelProvider wbProvider;

    private BugContentProvider provider;

    public BugLabelProvider() {
        super();
        wbProvider = new WorkbenchLabelProvider();
    }

    public Image getImage(Object element) {
        if (element instanceof BugGroup) {
            BugGroup group = (BugGroup) element;
            if (group.getType() == GroupType.Class || group.getType() == GroupType.Package
                    || group.getType() == GroupType.Project || group.getType() == GroupType.Marker) {
                return wbProvider.getImage(group.getData());
            }
            FindBugsMarker.Priority prio = group.getPriority();
            ImageRegistry imageRegistry = FindbugsPlugin.getDefault().getImageRegistry();
            return imageRegistry.get(prio.iconName());
        }
        if (element instanceof IMarker) {
            IMarker marker = (IMarker) element;
            if (!marker.exists()) {
                return null;
            }
        }
        return wbProvider.getImage(element);
    }

    boolean isStandalone() {
        return provider == null;
    }

    /*
     * public StyledString getStyledText(Object element) { if (element
     * instanceof BugGroup) { BugGroup group = (BugGroup) element; String
     * cloudName = null; if (group.getType() == GroupType.Project) { IProject
     * project = (IProject) group.getData(); try { SortedBugCollection bc =
     * FindbugsPlugin.getBugCollection(project, null); Cloud cloud =
     * bc.getCloud(); if (!(cloud instanceof BugCollectionStorageCloud)) {
     * cloudName = cloud.getCloudName(); } } catch (CoreException e) { // ignore
     * } } if(isStandalone()){ return new
     * StyledString(group.getShortDescription()); } int filtered =
     * getFilteredMarkersCount(group); String filterCount = filtered > 0? "/" +
     * filtered + " filtered" : ""; String str = group.getShortDescription() +
     * " (" + (group.getMarkersCount() - filtered) + filterCount + ")";
     * StyledString ss = new StyledString(str); if (cloudName != null) {
     * ss.append(" [" + cloudName + "]", StyledString.DECORATIONS_STYLER); }
     * return ss; } if(element instanceof IMarker){ IMarker marker = (IMarker)
     * element; if(!marker.exists()){ return null; } } if(element instanceof
     * IStructuredSelection){ return new
     * StyledString(getDescriptionAndBugCount((
     * (IStructuredSelection)element).toArray())); } if(element instanceof
     * Object[]){ return new StyledString(getDescriptionAndBugCount((Object[])
     * element)); } return new StyledString(wbProvider.getText(element)); }
     */
    public String getText(Object element) {
        if (element instanceof BugGroup) {
            BugGroup group = (BugGroup) element;
            String cloudName = null;
            if (group.getType() == GroupType.Project) {
                IProject project = (IProject) group.getData();
                try {
                    SortedBugCollection bc = FindbugsPlugin.getBugCollection(project, null);
                    Cloud cloud = bc.getCloud();
                    if (!(cloud instanceof BugCollectionStorageCloud)) {
                        cloudName = cloud.getCloudName();
                    }
                } catch (CoreException e) {
                    // ignore
                }
            }
            if (isStandalone()) {
                return group.getShortDescription();
            }
            int filtered = getFilteredMarkersCount(group);
            String filterCount = filtered > 0 ? "/" + filtered + " filtered" : "";
            String str = group.getShortDescription() + " (" + (group.getMarkersCount() - filtered) + filterCount + ")";
            if (cloudName != null) {
                str += " - " + cloudName;
            }
            return str;
        }
        if (element instanceof IMarker) {
            IMarker marker = (IMarker) element;
            if (!marker.exists()) {
                return null;
            }
        }
        if (element instanceof IStructuredSelection) {
            return getDescriptionAndBugCount(((IStructuredSelection) element).toArray());
        }
        if (element instanceof Object[]) {
            return getDescriptionAndBugCount((Object[]) element);
        }
        return wbProvider.getText(element);
    }

    private int getFilteredMarkersCount(BugGroup group) {
        if (isStandalone() || !provider.isBugFilterActive()) {
            return 0;
        }
        return provider.getFilteredMarkersCount(group);
    }

    private String getDescriptionAndBugCount(Object[] objects) {
        if (objects.length == 0) {
            return "Nothing...";
        }
        if (objects.length == 1) {
            return getText(objects[0]);
        }
        int count = getBugCountsSum(objects);
        StringBuffer sb = new StringBuffer("Selection contains ");
        if (count == 1) {
            sb.append("exactly one single bug");
        } else if (count == 0) {
            sb.append("zero bugs (change filter settings to see more...)");
        } else {
            sb.append(count).append(" bugs");
        }
        if (isStandalone()) {
            sb.append(" (not filtered)");
        }<commit_msg>Get rid of silly label "(comments disabled)" if no real cloud is used<commit_after>/*
 * Contributions to FindBugs
 * Copyright (C) 2008, <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package de.tobject.findbugs.view.explorer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.model.WorkbenchLabelProvider;
import org.eclipse.ui.navigator.ICommonContentExtensionSite;
import org.eclipse.ui.navigator.ICommonLabelProvider;

import de.tobject.findbugs.FindbugsPlugin;
import de.tobject.findbugs.marker.FindBugsMarker;
import edu.umd.cs.findbugs.SortedBugCollection;
import edu.umd.cs.findbugs.cloud.Cloud;

/**
 * @author Andrei
 */
public class BugLabelProvider implements /* IStyledLabelProvider, */ ICommonLabelProvider,
        IColorProvider {

    private final WorkbenchLabelProvider wbProvider;

    private BugContentProvider provider;

    public BugLabelProvider() {
        super();
        wbProvider = new WorkbenchLabelProvider();
    }

    public Image getImage(Object element) {
        if (element instanceof BugGroup) {
            BugGroup group = (BugGroup) element;
            if (group.getType() == GroupType.Class || group.getType() == GroupType.Package
                    || group.getType() == GroupType.Project || group.getType() == GroupType.Marker) {
                return wbProvider.getImage(group.getData());
            }
            FindBugsMarker.Priority prio = group.getPriority();
            ImageRegistry imageRegistry = FindbugsPlugin.getDefault().getImageRegistry();
            return imageRegistry.get(prio.iconName());
        }
        if (element instanceof IMarker) {
            IMarker marker = (IMarker) element;
            if (!marker.exists()) {
                return null;
            }
        }
        return wbProvider.getImage(element);
    }

    boolean isStandalone() {
        return provider == null;
    }

    public String getText(Object element) {
        if (element instanceof BugGroup) {
            BugGroup group = (BugGroup) element;
            String cloudName = null;
            if (group.getType() == GroupType.Project) {
                IProject project = (IProject) group.getData();
                try {
                    SortedBugCollection bc = FindbugsPlugin.getBugCollection(project, null);
                    Cloud cloud = bc.getCloud();
                    if (cloud.isOnlineCloud()) {
                        cloudName = cloud.getCloudName();
                    }
                } catch (CoreException e) {
                    FindbugsPlugin.getDefault().logException(e, "Failed to load bug collection");
                }
            }
            if (isStandalone()) {
                return group.getShortDescription();
            }
            int filtered = getFilteredMarkersCount(group);
            String filterCount = filtered > 0 ? "/" + filtered + " filtered" : "";
            String str = group.getShortDescription() + " (" + (group.getMarkersCount() - filtered) + filterCount + ")";
            if (cloudName != null) {
                str += " - " + cloudName;
            }
            return str;
        }
        if (element instanceof IMarker) {
            IMarker marker = (IMarker) element;
            if (!marker.exists()) {
                return null;
            }
        }
        if (element instanceof IStructuredSelection) {
            return getDescriptionAndBugCount(((IStructuredSelection) element).toArray());
        }
        if (element instanceof Object[]) {
            return getDescriptionAndBugCount((Object[]) element);
        }
        return wbProvider.getText(element);
    }

    private int getFilteredMarkersCount(BugGroup group) {
        if (isStandalone() || !provider.isBugFilterActive()) {
            return 0;
        }
        return provider.getFilteredMarkersCount(group);
    }

    private String getDescriptionAndBugCount(Object[] objects) {
        if (objects.length == 0) {
            return "Nothing...";
        }
        if (objects.length == 1) {
            return getText(objects[0]);
        }
        int count = getBugCountsSum(objects);
        StringBuilder sb = new StringBuilder("Selection contains ");
        if (count == 1) {
            sb.append("exactly one single bug");
        } else if (count == 0) {
            sb.append("zero bugs (change filter settings to see more...)");
        } else {
            sb.append(count).append(" bugs");
        }
        if (isStandalone()) {
            sb.append(" (not filtered)");
        }
{'fraction_non_alphanumeric': 0.10558216102963185, 'fraction_numerical': 0.004115534271176295, 'mean_word_length': 2.5310435931307795, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, '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': '5014046', 'n_tokens_mistral': 3632, 'n_tokens_neox': 3488, 'n_words': 925}