<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.wizards;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import de.tobject.findbugs.FindbugsPlugin;
import de.tobject.findbugs.marker.FindBugsMarker;
import de.tobject.findbugs.preferences.FindBugsConstants;
import de.tobject.findbugs.util.Util;
import edu.umd.cs.findbugs.BugPattern;
import edu.umd.cs.findbugs.I18N;

/**
 * @author Andrei
 */
public class ExportWizardPage extends WizardPage {

	private static final int BY_NAME = 0;
	private static final int BY_NOT_FILTERED_COUNT = 1;
	private static final int BY_OVERALL_COUNT = 2;
	private static final String SEPARATOR = ",";
	private Composite comp;
	private int sortBy;
	private Text filteredBugIdsText;

	/**
	 * @param pageName
	 * @param title
	 * @param titleImage
	 */
	protected ExportWizardPage(String pageName, String title, String descr,
			String imagePath) {
		super(pageName, title, AbstractUIPlugin.imageDescriptorFromPlugin(FindbugsPlugin
				.getDefault().getBundle().getSymbolicName(), imagePath));
		setDescription(descr);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public void createControl(Composite parent) {
		comp = new Composite(parent, SWT.BORDER);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		comp.setLayout(layout);
		GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
		comp.setLayoutData(gd);
		setControl(comp);

		Label label = new Label(comp, SWT.NONE);
		label.setText("Sort by:");

		final Combo sortByCombo = new Combo(comp, SWT.READ_ONLY);
		final String[] items = new String[] { "Name", "Not filtered bug count", "Overall bug count" };
		sortByCombo.setItems(items);

		String sortOrder = FindbugsPlugin.getDefault().getPreferenceStore().getString(
				FindBugsConstants.EXPORT_SORT_ORDER);
		if(FindBugsConstants.ORDER_BY_NOT_FILTERED_BUGS_COUNT.equals(sortOrder)) {
			sortByCombo.select(1);
		} else if(FindBugsConstants.ORDER_BY_OVERALL_BUGS_COUNT.equals(sortOrder)) {
			sortByCombo.select(2);
		} else {
			sortByCombo.select(0);
		}

		sortByCombo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				widgetSelected(e);
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				sortBy = sortByCombo.getSelectionIndex();
			}
		});

		label = new Label(comp, SWT.NONE);
		label.setText("Filter bug ids:");
		filteredBugIdsText = new Text(comp, SWT.SHADOW_IN | SWT.BORDER);
		GridData layoutData = new GridData(GridData.FILL_HORIZONTAL
				| GridData.GRAB_HORIZONTAL);
		filteredBugIdsText.setLayoutData(layoutData);
		filteredBugIdsText.setText(FindbugsPlugin.getDefault().getPreferenceStore().getString(
				FindBugsConstants.LAST_USED_EXPORT_FILTER));
		filteredBugIdsText.setToolTipText("Bug ids to filter, separated by comma or space");
	}

	@Override
	public void dispose() {
		comp.dispose();
		super.dispose();
	}

	public boolean finish() {
		String data = collectBugsData();
		copyToClipboard(data);
		String filters = getLastUsedExportFilters();
		FindbugsPlugin.getDefault().getPreferenceStore().setValue(
				FindBugsConstants.LAST_USED_EXPORT_FILTER, filters);
		String sortPref;
		switch (sortBy) {
		case BY_NOT_FILTERED_COUNT:
			sortPref = FindBugsConstants.ORDER_BY_NOT_FILTERED_BUGS_COUNT;
			break;
		case BY_OVERALL_COUNT:
			sortPref = FindBugsConstants.ORDER_BY_OVERALL_BUGS_COUNT;
			break;
		case BY_NAME:
		default:
			sortPref = FindBugsConstants.ORDER_BY_NAME;
			break;
		}
		FindbugsPlugin.getDefault().getPreferenceStore().setValue(
				FindBugsConstants.EXPORT_SORT_ORDER, sortPref);
		return true;
	}

	/**
	 * @return
	 */
	private String getLastUsedExportFilters() {
		String text = filteredBugIdsText.getText();
		if(text == null || text.trim().isEmpty()) {
			return "";
		}
		String[] split = text.split("[^a-zA-Z]+");
		Arrays.sort(split);
		StringBuilder sb = new StringBuilder();
		for (String string : split) {
			sb.append(string).append(SEPARATOR);
		}
		if(sb.length() > 0) {
			sb.setLength(sb.length() - 1);
		}
		return sb.toString();
	}

	/**
	 * @return
	 */
	private String collectBugsData() {
		IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
		List<Record> lines = new ArrayList<Record>();
		for (IProject project : projects) {
			Record line = createProjectLine(project);
			if(line != null) {
				lines.add(line);
			}
		}
		Collections.sort(lines);
		StringBuilder sb = new StringBuilder();

		createHeader(sb);
<commit_msg>fix Java 6 dependency<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.wizards;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import de.tobject.findbugs.FindbugsPlugin;
import de.tobject.findbugs.marker.FindBugsMarker;
import de.tobject.findbugs.preferences.FindBugsConstants;
import de.tobject.findbugs.util.Util;
import edu.umd.cs.findbugs.BugPattern;
import edu.umd.cs.findbugs.I18N;

/**
 * @author Andrei
 */
public class ExportWizardPage extends WizardPage {

	private static final int BY_NAME = 0;
	private static final int BY_NOT_FILTERED_COUNT = 1;
	private static final int BY_OVERALL_COUNT = 2;
	private static final String SEPARATOR = ",";
	private Composite comp;
	private int sortBy;
	private Text filteredBugIdsText;

	/**
	 * @param pageName
	 * @param title
	 * @param titleImage
	 */
	protected ExportWizardPage(String pageName, String title, String descr,
			String imagePath) {
		super(pageName, title, AbstractUIPlugin.imageDescriptorFromPlugin(FindbugsPlugin
				.getDefault().getBundle().getSymbolicName(), imagePath));
		setDescription(descr);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public void createControl(Composite parent) {
		comp = new Composite(parent, SWT.BORDER);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		comp.setLayout(layout);
		GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
		comp.setLayoutData(gd);
		setControl(comp);

		Label label = new Label(comp, SWT.NONE);
		label.setText("Sort by:");

		final Combo sortByCombo = new Combo(comp, SWT.READ_ONLY);
		final String[] items = new String[] { "Name", "Not filtered bug count", "Overall bug count" };
		sortByCombo.setItems(items);

		String sortOrder = FindbugsPlugin.getDefault().getPreferenceStore().getString(
				FindBugsConstants.EXPORT_SORT_ORDER);
		if(FindBugsConstants.ORDER_BY_NOT_FILTERED_BUGS_COUNT.equals(sortOrder)) {
			sortByCombo.select(1);
		} else if(FindBugsConstants.ORDER_BY_OVERALL_BUGS_COUNT.equals(sortOrder)) {
			sortByCombo.select(2);
		} else {
			sortByCombo.select(0);
		}

		sortByCombo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				widgetSelected(e);
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				sortBy = sortByCombo.getSelectionIndex();
			}
		});

		label = new Label(comp, SWT.NONE);
		label.setText("Filter bug ids:");
		filteredBugIdsText = new Text(comp, SWT.SHADOW_IN | SWT.BORDER);
		GridData layoutData = new GridData(GridData.FILL_HORIZONTAL
				| GridData.GRAB_HORIZONTAL);
		filteredBugIdsText.setLayoutData(layoutData);
		filteredBugIdsText.setText(FindbugsPlugin.getDefault().getPreferenceStore().getString(
				FindBugsConstants.LAST_USED_EXPORT_FILTER));
		filteredBugIdsText.setToolTipText("Bug ids to filter, separated by comma or space");
	}

	@Override
	public void dispose() {
		comp.dispose();
		super.dispose();
	}

	public boolean finish() {
		String data = collectBugsData();
		copyToClipboard(data);
		String filters = getLastUsedExportFilters();
		FindbugsPlugin.getDefault().getPreferenceStore().setValue(
				FindBugsConstants.LAST_USED_EXPORT_FILTER, filters);
		String sortPref;
		switch (sortBy) {
		case BY_NOT_FILTERED_COUNT:
			sortPref = FindBugsConstants.ORDER_BY_NOT_FILTERED_BUGS_COUNT;
			break;
		case BY_OVERALL_COUNT:
			sortPref = FindBugsConstants.ORDER_BY_OVERALL_BUGS_COUNT;
			break;
		case BY_NAME:
		default:
			sortPref = FindBugsConstants.ORDER_BY_NAME;
			break;
		}
		FindbugsPlugin.getDefault().getPreferenceStore().setValue(
				FindBugsConstants.EXPORT_SORT_ORDER, sortPref);
		return true;
	}

	/**
	 * @return
	 */
	private String getLastUsedExportFilters() {
		String text = filteredBugIdsText.getText();
		if(text == null || text.trim().length() == 0) {
			return "";
		}
		String[] split = text.split("[^a-zA-Z]+");
		Arrays.sort(split);
		StringBuilder sb = new StringBuilder();
		for (String string : split) {
			sb.append(string).append(SEPARATOR);
		}
		if(sb.length() > 0) {
			sb.setLength(sb.length() - 1);
		}
		return sb.toString();
	}

	/**
	 * @return
	 */
	private String collectBugsData() {
		IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
		List<Record> lines = new ArrayList<Record>();
		for (IProject project : projects) {
			Record line = createProjectLine(project);
			if(line != null) {
				lines.add(line);
			}
		}
		Collections.sort(lines);
		StringBuilder sb = new StringBuilder();

		createHeader(sb);

{'fraction_non_alphanumeric': 0.11128700332436282, 'fraction_numerical': 0.005382301725502612, 'mean_word_length': 4.7017148014440435, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '3956694', 'n_tokens_mistral': 4441, 'n_tokens_neox': 4008, 'n_words': 924}