<commit_before>package org.reldb.dbrowser;

import java.io.File;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.LinkedList;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Shell;
import org.reldb.dbrowser.hooks.OpenDocumentEventProcessor;
import org.reldb.dbrowser.ui.DbTab;
import org.reldb.dbrowser.ui.MainPanel;
import org.reldb.dbrowser.ui.ManageRecentlyUsedDialog;
import org.reldb.dbrowser.ui.RemoteDatabaseDialog;
import org.reldb.dbrowser.ui.RemoteDatabaseDialog.RemoteDatabaseDialogResponse;
import org.reldb.dbrowser.ui.preferences.PreferencePageGeneral;
import org.reldb.dbrowser.ui.preferences.Preferences;

/** Root of RelUI. */
public class Core {
	private final static String recentlyUsedDatabaseListPreference = "recentlyUsedDatabaseList";

	private static MainPanel mainPanel;
	private static DirectoryDialog openDatabaseDialog;
	private static DirectoryDialog newDatabaseDialog;
	private static RemoteDatabaseDialog remoteDatabaseDialog;

	private static OpenDocumentEventProcessor openDocProcessor = new OpenDocumentEventProcessor();

	private static boolean noLocalRel = true;

	private static String defaultDatabasePath = Paths.get(System.getProperty("user.home"), "DefaultRelDb").toString();
	
	public static Shell getShell() {
		return mainPanel.getShell();
	}

	public static boolean isNoLocalRel() {
		return noLocalRel;
	}

	public static void launch(String[] applicationArguments, Composite parent) {
		parent.getDisplay().addListener(SWT.OpenDocument, openDocProcessor);
		parent.setLayout(new FillLayout());
		mainPanel = new MainPanel(parent, SWT.None);
		initialise(applicationArguments);
	}

	public static void setStatus(String s) {
		mainPanel.setStatus(s);
	}

	private static void initialise(String[] applicationArguments) {
		openDatabaseDialog = new DirectoryDialog(getShell());
		openDatabaseDialog.setText("Open Database");
		openDatabaseDialog.setMessage("Select a folder that contains a database.");
		openDatabaseDialog.setFilterPath(System.getProperty("user.home"));

		newDatabaseDialog = new DirectoryDialog(getShell());
		newDatabaseDialog.setText("Create Database");
		newDatabaseDialog.setMessage(
				"Select a folder to hold a new database.  If a database already exists there, it will be opened.");
		newDatabaseDialog.setFilterPath(System.getProperty("user.home"));

		remoteDatabaseDialog = new RemoteDatabaseDialog(getShell());

		Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
			private int failureCount = 0;

			public void uncaughtException(Thread t, Throwable e) {
				if (failureCount > 1) {
					System.err
							.println("SYSTEM ERROR!  It's gotten even worse.  This is a last-ditch attempt to escape.");
					failureCount++;
					Thread.setDefaultUncaughtExceptionHandler(null);
					System.exit(1);
					return;
				}
				if (failureCount > 0) {
					System.err.println(
							"SYSTEM ERROR!  Things have gone so horribly wrong that we can't recover or even pop up a message.  I hope someone sees this...\nShutting down now, if we can.");
					failureCount++;
					System.exit(1);
					return;
				}
				failureCount++;
				if (e instanceof OutOfMemoryError) {
					System.err.println("Out of memory!");
					e.printStackTrace();
					mainPanel.dispose();
					MessageDialog.openError(getShell(), "OUT OF MEMORY", "Out of memory!  Shutting down NOW!");
				} else {
					System.err.println("Unknown error: " + t);
					e.printStackTrace();
					mainPanel.dispose();
					MessageDialog.openError(getShell(), "Unexpected Error", e.toString());
				}
				System.exit(1);
			}
		});

		try {
			Class.forName("org.reldb.rel.Rel");
			noLocalRel = false;
		} catch (ClassNotFoundException cnfe) {
			noLocalRel = true;
		}

		DbTab dbTab = new DbTab();
		if (!Preferences.getPreferenceBoolean(PreferencePageGeneral.SKIP_DEFAULT_DB_LOAD))
			dbTab.openDefaultDatabase(defaultDatabasePath);

		openDocProcessor.addFilesToOpen(applicationArguments);
		String[] filesToOpen = openDocProcessor.retrieveFilesToOpen();
		for (String fname : filesToOpen)
			openFile(fname);

		Core.setSelection(0);
	}

	public static CTabFolder getTabFolder() {
		return mainPanel.getTabFolder();
	}

	public static DbTab getCurrentDbTab() {
		return (DbTab) mainPanel.getTabFolder().getSelection();
	}

	public static void setSelection(int i) {
		mainPanel.getTabFolder().setSelection(i);
	}

	public static void newDatabase() {
		String result = newDatabaseDialog.open();
		if (result != null)
			getCurrentDbTab().newDatabase(result.toString());
	}

	public static void openLocalDatabase() {
		String result = openDatabaseDialog.open();
		if (result != null)
			getCurrentDbTab().openLocalDatabase(result.toString());
	}

	public static void openRemoteDatabase() {
		RemoteDatabaseDialogResponse result = remoteDatabaseDialog.open();
		if (result != null)
			getCurrentDbTab().openRemoteDatabase(result.toString());
	}

	public static void createNewTabIfNeeded() {
		CTabItem[] tabs = mainPanel.getTabFolder().getItems();
		if (tabs.length == 0 || ((DbTab) tabs[tabs.length - 1]).isOpenOnADatabase())
			new DbTab();
	}

	public static DbTab selectEmptyTab() {
		CTabItem[] tabs = mainPanel.getTabFolder().getItems();
		DbTab lastTab = (DbTab) tabs[tabs.length - 1];
		Core.setSelection(tabs.length - 1);
		return lastTab;
	}
<commit_msg>Active database tab is last database loaded.<commit_after>package org.reldb.dbrowser;

import java.io.File;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.LinkedList;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Shell;
import org.reldb.dbrowser.hooks.OpenDocumentEventProcessor;
import org.reldb.dbrowser.ui.DbTab;
import org.reldb.dbrowser.ui.MainPanel;
import org.reldb.dbrowser.ui.ManageRecentlyUsedDialog;
import org.reldb.dbrowser.ui.RemoteDatabaseDialog;
import org.reldb.dbrowser.ui.RemoteDatabaseDialog.RemoteDatabaseDialogResponse;
import org.reldb.dbrowser.ui.preferences.PreferencePageGeneral;
import org.reldb.dbrowser.ui.preferences.Preferences;

/** Root of RelUI. */
public class Core {
	private final static String recentlyUsedDatabaseListPreference = "recentlyUsedDatabaseList";

	private static MainPanel mainPanel;
	private static DirectoryDialog openDatabaseDialog;
	private static DirectoryDialog newDatabaseDialog;
	private static RemoteDatabaseDialog remoteDatabaseDialog;

	private static OpenDocumentEventProcessor openDocProcessor = new OpenDocumentEventProcessor();

	private static boolean noLocalRel = true;

	private static String defaultDatabasePath = Paths.get(System.getProperty("user.home"), "DefaultRelDb").toString();
	
	public static Shell getShell() {
		return mainPanel.getShell();
	}

	public static boolean isNoLocalRel() {
		return noLocalRel;
	}

	public static void launch(String[] applicationArguments, Composite parent) {
		parent.getDisplay().addListener(SWT.OpenDocument, openDocProcessor);
		parent.setLayout(new FillLayout());
		mainPanel = new MainPanel(parent, SWT.None);
		initialise(applicationArguments);
	}

	public static void setStatus(String s) {
		mainPanel.setStatus(s);
	}

	private static void initialise(String[] applicationArguments) {
		openDatabaseDialog = new DirectoryDialog(getShell());
		openDatabaseDialog.setText("Open Database");
		openDatabaseDialog.setMessage("Select a folder that contains a database.");
		openDatabaseDialog.setFilterPath(System.getProperty("user.home"));

		newDatabaseDialog = new DirectoryDialog(getShell());
		newDatabaseDialog.setText("Create Database");
		newDatabaseDialog.setMessage(
				"Select a folder to hold a new database.  If a database already exists there, it will be opened.");
		newDatabaseDialog.setFilterPath(System.getProperty("user.home"));

		remoteDatabaseDialog = new RemoteDatabaseDialog(getShell());

		Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
			private int failureCount = 0;

			public void uncaughtException(Thread t, Throwable e) {
				if (failureCount > 1) {
					System.err
							.println("SYSTEM ERROR!  It's gotten even worse.  This is a last-ditch attempt to escape.");
					failureCount++;
					Thread.setDefaultUncaughtExceptionHandler(null);
					System.exit(1);
					return;
				}
				if (failureCount > 0) {
					System.err.println(
							"SYSTEM ERROR!  Things have gone so horribly wrong that we can't recover or even pop up a message.  I hope someone sees this...\nShutting down now, if we can.");
					failureCount++;
					System.exit(1);
					return;
				}
				failureCount++;
				if (e instanceof OutOfMemoryError) {
					System.err.println("Out of memory!");
					e.printStackTrace();
					mainPanel.dispose();
					MessageDialog.openError(getShell(), "OUT OF MEMORY", "Out of memory!  Shutting down NOW!");
				} else {
					System.err.println("Unknown error: " + t);
					e.printStackTrace();
					mainPanel.dispose();
					MessageDialog.openError(getShell(), "Unexpected Error", e.toString());
				}
				System.exit(1);
			}
		});

		try {
			Class.forName("org.reldb.rel.Rel");
			noLocalRel = false;
		} catch (ClassNotFoundException cnfe) {
			noLocalRel = true;
		}

		DbTab dbTab = new DbTab();
		if (!Preferences.getPreferenceBoolean(PreferencePageGeneral.SKIP_DEFAULT_DB_LOAD))
			dbTab.openDefaultDatabase(defaultDatabasePath);

		openDocProcessor.addFilesToOpen(applicationArguments);
		String[] filesToOpen = openDocProcessor.retrieveFilesToOpen();
		for (String fname : filesToOpen)
			openFile(fname);

		Core.setSelectionToLastDatabaseTab();
	}

	public static CTabFolder getTabFolder() {
		return mainPanel.getTabFolder();
	}

	public static DbTab getCurrentDbTab() {
		return (DbTab) mainPanel.getTabFolder().getSelection();
	}

	public static void setSelection(int i) {
		mainPanel.getTabFolder().setSelection(i);
	}

	public static void setSelectionToLastDatabaseTab() {
		int tabCount = mainPanel.getTabFolder().getItemCount();
		if (tabCount <= 1)
			setSelection(0);
		else
			setSelection(tabCount - 2);
	}
	
	public static void newDatabase() {
		String result = newDatabaseDialog.open();
		if (result != null)
			getCurrentDbTab().newDatabase(result.toString());
	}

	public static void openLocalDatabase() {
		String result = openDatabaseDialog.open();
		if (result != null)
			getCurrentDbTab().openLocalDatabase(result.toString());
	}

	public static void openRemoteDatabase() {
		RemoteDatabaseDialogResponse result = remoteDatabaseDialog.open();
		if (result != null)
			getCurrentDbTab().openRemoteDatabase(result.toString());
	}

	public static void createNewTabIfNeeded() {
		CTabItem[] tabs = mainPanel.getTabFolder().getItems();
		if (tabs.length == 0 || ((DbTab) tabs[tabs.length - 1]).isOpenOnADatabase())
			new DbTab();
	}

	public static DbTab selectEmptyTab() {
		CTabItem[] tabs = mainPanel.getTabFolder().getItems();
		DbTab lastTab = (DbTab) tabs[tabs.length - 1];
		Core.setSelection(tabs.length - 1);
		return lastTab;
	}

{'fraction_non_alphanumeric': 0.11145024853928664, 'fraction_numerical': 0.0020929624138833172, 'mean_word_length': 4.887063655030801, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 7, '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': '6947603', 'n_tokens_mistral': 3801, 'n_tokens_neox': 3340, 'n_words': 770}