<commit_before>/*******************************************************************************
 * Copyright (c) 2012 <NAME>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     <NAME> - initial API and implementation
 *******************************************************************************/
package bndtools.release.ui;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Pattern;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Widget;

import aQute.bnd.differ.Baseline;
import aQute.bnd.differ.Baseline.Info;
import aQute.bnd.version.Version;
import bndtools.release.nl.Messages;

public class BundleTree extends Composite {

	public final static String VERSION_STRING = "(\\d+)(\\.(\\d+)(\\.(\\d+)(\\.([-_\\da-zA-Z]+))?)?)?"; //$NON-NLS-1$
	public final static Pattern VERSION = Pattern.compile(VERSION_STRING);

	protected SashForm sashForm;
	protected Button showAll;

	protected TreeViewer infoViewer;
	protected Composite infoViewerComposite;

	protected TreeViewer bundleTreeViewer;
	protected Composite bundleTreeViewerComposite;

	protected TreeContentProvider bundleTreeViewerProvider = new TreeContentProvider();
	protected InfoContentProvider infoTreeViewerProvider = new InfoContentProvider();

	public BundleTree(Composite composite) {
		this(composite, SWT.NONE);
	}

	public BundleTree(Composite composite, int style) {
		super(composite, style);
		createControl();
	}

	public void createControl() {

		sashForm = new SashForm(this, SWT.VERTICAL); 
		sashForm.setLayout(new FillLayout());
		sashForm.setSashWidth(10);

		createInfoViewer(sashForm);
		createBundleTreeViewer(sashForm);

		showAll = createButtons(this);

		setLayout(new BundleTreeLayout());

		sashForm.setWeights(new int[] { 30, 70 });

	}

	private void createInfoViewer(Composite container) {

		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		gridLayout.horizontalSpacing = 0;
		gridLayout.verticalSpacing = 0;
		gridLayout.marginWidth = 0;
		gridLayout.marginHeight = 0;

		infoViewerComposite = new Composite(container, SWT.NONE);
		infoViewerComposite.setLayout(gridLayout);

		infoViewer = new TreeViewer(infoViewerComposite, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
		infoViewer.setUseHashlookup(true);

		GridData gd = new GridData(GridData.FILL, GridData.FILL, true, true);

		infoViewer.getTree().setLayoutData(gd);
		infoViewer.getTree().setHeaderVisible(true);

		TreeViewerColumn treeColumn = new TreeViewerColumn(infoViewer, SWT.NONE);
		treeColumn.getColumn().setText(Messages.bundleAndPackageName);
		treeColumn.getColumn().setWidth(450);
		treeColumn.setLabelProvider(new InfoLabelProvider());

		TreeViewerColumn currentVersion = new TreeViewerColumn(infoViewer, SWT.NONE);
		currentVersion.getColumn().setText(Messages.version2);
		currentVersion.getColumn().setWidth(80);
		currentVersion.setLabelProvider(new ColumnLabelProvider() {
			@Override
            public String getText(Object element) {
                if (element instanceof Baseline) {
                    return ((Baseline) element).getOlderVersion().toString();
                }
				if (element instanceof Info) {
					return ((Info) element).olderVersion.toString();
				}
				return ""; //$NON-NLS-1$
			}
		});
		TreeViewerColumn suggestedVersion = new TreeViewerColumn(infoViewer, SWT.NONE);
		suggestedVersion.getColumn().setText(Messages.newVersion);
		suggestedVersion.getColumn().setWidth(80);
		suggestedVersion.setLabelProvider(new ColumnLabelProvider() {
			@Override
            public String getText(Object element) {
                if (element instanceof Baseline) {
                    return ((Baseline) element).getSuggestedVersion().toString();
                }
				if (element instanceof Info) {
					return ((Info) element).suggestedVersion.toString();
				}
				return ""; //$NON-NLS-1$
			}
		});
		suggestedVersion.setEditingSupport(new InlineComboEditingSupport(infoViewer));

		infoViewer.setContentProvider(infoTreeViewerProvider);
		infoViewer.setAutoExpandLevel(2);

	}

	private void createBundleTreeViewer(Composite container) {

		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		gridLayout.horizontalSpacing = 0;
		gridLayout.verticalSpacing = 0;
		gridLayout.marginWidth = 0;
		gridLayout.marginHeight = 0;

		bundleTreeViewerComposite = new Composite(container, SWT.NONE);
		bundleTreeViewerComposite.setLayout(gridLayout);

		bundleTreeViewer = new TreeViewer(bundleTreeViewerComposite, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
		bundleTreeViewer.setUseHashlookup(true);

		GridData gd = new GridData(GridData.FILL, GridData.FILL, true, true);

		bundleTreeViewer.getTree().setLayoutData(gd);<commit_msg>Fix: NPE when package was removed<commit_after>/*******************************************************************************
 * Copyright (c) 2012 <NAME>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     <NAME> - initial API and implementation
 *******************************************************************************/
package bndtools.release.ui;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Pattern;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Widget;

import aQute.bnd.differ.Baseline;
import aQute.bnd.differ.Baseline.Info;
import aQute.bnd.version.Version;
import bndtools.release.nl.Messages;

public class BundleTree extends Composite {

	public final static String VERSION_STRING = "(\\d+)(\\.(\\d+)(\\.(\\d+)(\\.([-_\\da-zA-Z]+))?)?)?"; //$NON-NLS-1$
	public final static Pattern VERSION = Pattern.compile(VERSION_STRING);

	protected SashForm sashForm;
	protected Button showAll;

	protected TreeViewer infoViewer;
	protected Composite infoViewerComposite;

	protected TreeViewer bundleTreeViewer;
	protected Composite bundleTreeViewerComposite;

	protected TreeContentProvider bundleTreeViewerProvider = new TreeContentProvider();
	protected InfoContentProvider infoTreeViewerProvider = new InfoContentProvider();

	public BundleTree(Composite composite) {
		this(composite, SWT.NONE);
	}

	public BundleTree(Composite composite, int style) {
		super(composite, style);
		createControl();
	}

	public void createControl() {

		sashForm = new SashForm(this, SWT.VERTICAL);
		sashForm.setLayout(new FillLayout());
		sashForm.setSashWidth(10);

		createInfoViewer(sashForm);
		createBundleTreeViewer(sashForm);

		showAll = createButtons(this);

		setLayout(new BundleTreeLayout());

		sashForm.setWeights(new int[] { 30, 70 });

	}

	private void createInfoViewer(Composite container) {

		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		gridLayout.horizontalSpacing = 0;
		gridLayout.verticalSpacing = 0;
		gridLayout.marginWidth = 0;
		gridLayout.marginHeight = 0;

		infoViewerComposite = new Composite(container, SWT.NONE);
		infoViewerComposite.setLayout(gridLayout);

		infoViewer = new TreeViewer(infoViewerComposite, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
		infoViewer.setUseHashlookup(true);

		GridData gd = new GridData(GridData.FILL, GridData.FILL, true, true);

		infoViewer.getTree().setLayoutData(gd);
		infoViewer.getTree().setHeaderVisible(true);

		TreeViewerColumn treeColumn = new TreeViewerColumn(infoViewer, SWT.NONE);
		treeColumn.getColumn().setText(Messages.bundleAndPackageName);
		treeColumn.getColumn().setWidth(450);
		treeColumn.setLabelProvider(new InfoLabelProvider());

		TreeViewerColumn currentVersion = new TreeViewerColumn(infoViewer, SWT.NONE);
		currentVersion.getColumn().setText(Messages.version2);
		currentVersion.getColumn().setWidth(80);
		currentVersion.setLabelProvider(new ColumnLabelProvider() {
			@Override
            public String getText(Object element) {
                if (element instanceof Baseline) {
                    return ((Baseline) element).getOlderVersion().toString();
                }
				if (element instanceof Info) {
					return ((Info) element).olderVersion.toString();
				}
				return ""; //$NON-NLS-1$
			}
		});
		TreeViewerColumn suggestedVersion = new TreeViewerColumn(infoViewer, SWT.NONE);
		suggestedVersion.getColumn().setText(Messages.newVersion);
		suggestedVersion.getColumn().setWidth(80);
		suggestedVersion.setLabelProvider(new ColumnLabelProvider() {
			@Override
            public String getText(Object element) {
                if (element instanceof Baseline) {
                    return ((Baseline) element).getSuggestedVersion().toString();
                }
				if (element instanceof Info) {
					return ((Info) element).suggestedVersion != null ? ((Info) element).suggestedVersion.toString() : ""; //$NON-NLS-1$
				}
				return ""; //$NON-NLS-1$
			}
		});
		suggestedVersion.setEditingSupport(new InlineComboEditingSupport(infoViewer));

		infoViewer.setContentProvider(infoTreeViewerProvider);
		infoViewer.setAutoExpandLevel(2);

	}

	private void createBundleTreeViewer(Composite container) {

		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		gridLayout.horizontalSpacing = 0;
		gridLayout.verticalSpacing = 0;
		gridLayout.marginWidth = 0;
		gridLayout.marginHeight = 0;

		bundleTreeViewerComposite = new Composite(container, SWT.NONE);
		bundleTreeViewerComposite.setLayout(gridLayout);

		bundleTreeViewer = new TreeViewer(bundleTreeViewerComposite, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
		bundleTreeViewer.setUseHashlookup(true);

		GridData gd = new GridData(GridData.FILL, GridData.FILL, true, true);

		bundleTreeViewer.getTree().setLayoutData(gd);
{'fraction_non_alphanumeric': 0.1347972972972973, 'fraction_numerical': 0.006165540540540541, 'mean_word_length': 4.986349848331648, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5559959', 'n_tokens_mistral': 3912, 'n_tokens_neox': 3549, 'n_words': 676}