<commit_before>﻿using DtxModeler.Ddl;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Xml.Serialization;

namespace DtxModeler.Xaml {
	/// <summary>
	/// Interaction logic for DmlExplorerControl.xaml
	/// </summary>
	public partial class ExplorerControl : UserControl {

		private List<Database> loaded_databases = new List<Database>();

		private bool first_rightclick = false;

		private Selection selected_type;

		public Selection SelectedType {
			get { return selected_type; }
		}

		private Table selected_table;
		private View selected_view;
		private Function selected_function;
		private Database selected_database;

		public Database[] ChangedDatabases {
			get {
				return loaded_databases.Where(db => db._Modified).ToArray();
			}
		}

		public Database SelectedDatabase {
			get { return selected_database; }
		}

		public Table SelectedTable {
			get { return selected_table; }
		}

		public event EventHandler<DatabaseEventArgs> DatabaseModified;
		public event EventHandler<SelectionChangedEventArgs> ChangedSelection;
		public event EventHandler<DatabaseEventArgs> LoadedDatabase;
		public event EventHandler<DatabaseEventArgs> UnloadedDatabase;


		public ExplorerControl() {
			selected_type = Selection.None;
			this.UseLayoutRounding = true;

			InitializeComponent();

			
		}

		/// <summary>
		/// Refreshes the control to display the current loaded databases and selectes the specified database.
		/// </summary>
		/// <param name="select_db">If a database is provided, it will be automatically selected in the tree.</param>
		private void Refresh() {
			

			string image_database = "pack://application:,,,/Xaml/Images/database.png";
			string image_table = "pack://application:,,,/Xaml/Images/table.png";
			string image_table_custom = "pack://application:,,,/Xaml/Images/table_gear.png";
			string image_tables = "pack://application:,,,/Xaml/Images/table_multiple.png";
			string image_view = "pack://application:,,,/Xaml/Images/table_chart.png";
			string image_function = "pack://application:,,,/Xaml/Images/function.png";

			foreach (var database in loaded_databases) {
				var db_root = createTreeViewItem(database.Name, image_database);
				db_root.Tag = database;
				db_root.IsExpanded = true;

				if (selected_database == database || _TreDatabaseLayout.SelectedItem == null) {
					db_root.IsSelected = true;
				}


				// Tables
				var tables_root = createTreeViewItem("Tables", image_tables);
				tables_root.Tag = typeof(Table[]);
				tables_root.IsExpanded = true;
				db_root.Items.Add(tables_root);

				// Display all the tables
				foreach (var table in database.Table) {
					var tree_table = createTreeViewItem(table.Name, (table.UseCustomSql)? image_table_custom : image_table);
					tree_table.Tag = table;
					tables_root.Items.Add(tree_table);
					tree_table.IsSelected = (selected_table == table);
				}

				// Views
				var views_root = createTreeViewItem("Views", image_view);
				views_root.Tag = typeof(View[]);
				db_root.Items.Add(views_root);

				// Functions
				var functions_root = createTreeViewItem("Functions", image_function);
				functions_root.Tag = typeof(Function[]);
				db_root.Items.Add(functions_root);

				_TreDatabaseLayout.Items.Clear();
				_TreDatabaseLayout.Items.Add(db_root);
				database._TreeRoot = db_root;
			}
		}

		/// <summary>
		/// Create a new database and prompt the user for a name.
		/// </summary><commit_msg>Fixed issue where user could only view the last loaded database in the explorer control.<commit_after>﻿using DtxModeler.Ddl;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Xml.Serialization;

namespace DtxModeler.Xaml {
	/// <summary>
	/// Interaction logic for DmlExplorerControl.xaml
	/// </summary>
	public partial class ExplorerControl : UserControl {

		private List<Database> loaded_databases = new List<Database>();

		private bool first_rightclick = false;

		private Selection selected_type;

		public Selection SelectedType {
			get { return selected_type; }
		}

		private Table selected_table;
		private View selected_view;
		private Function selected_function;
		private Database selected_database;

		public Database[] ChangedDatabases {
			get {
				return loaded_databases.Where(db => db._Modified).ToArray();
			}
		}

		public Database SelectedDatabase {
			get { return selected_database; }
		}

		public Table SelectedTable {
			get { return selected_table; }
		}

		public event EventHandler<DatabaseEventArgs> DatabaseModified;
		public event EventHandler<SelectionChangedEventArgs> ChangedSelection;
		public event EventHandler<DatabaseEventArgs> LoadedDatabase;
		public event EventHandler<DatabaseEventArgs> UnloadedDatabase;


		public ExplorerControl() {
			selected_type = Selection.None;
			this.UseLayoutRounding = true;

			InitializeComponent();

			
		}

		/// <summary>
		/// Refreshes the control to display the current loaded databases and selectes the specified database.
		/// </summary>
		/// <param name="select_db">If a database is provided, it will be automatically selected in the tree.</param>
		private void Refresh() {
			

			string image_database = "pack://application:,,,/Xaml/Images/database.png";
			string image_table = "pack://application:,,,/Xaml/Images/table.png";
			string image_table_custom = "pack://application:,,,/Xaml/Images/table_gear.png";
			string image_tables = "pack://application:,,,/Xaml/Images/table_multiple.png";
			string image_view = "pack://application:,,,/Xaml/Images/table_chart.png";
			string image_function = "pack://application:,,,/Xaml/Images/function.png";

			var last_selected_db = selected_database;
			_TreDatabaseLayout.Items.Clear();
			

			foreach (var database in loaded_databases) {
				var db_root = createTreeViewItem(database.Name, image_database);
				db_root.Tag = database;
				db_root.IsExpanded = true;

				if (last_selected_db == database || _TreDatabaseLayout.SelectedItem == null) {
					db_root.IsSelected = true;
				}


				// Tables
				var tables_root = createTreeViewItem("Tables", image_tables);
				tables_root.Tag = typeof(Table[]);
				tables_root.IsExpanded = true;
				db_root.Items.Add(tables_root);

				// Display all the tables
				foreach (var table in database.Table) {
					var tree_table = createTreeViewItem(table.Name, (table.UseCustomSql)? image_table_custom : image_table);
					tree_table.Tag = table;
					tables_root.Items.Add(tree_table);
					tree_table.IsSelected = (selected_table == table);
				}

				// Views
				var views_root = createTreeViewItem("Views", image_view);
				views_root.Tag = typeof(View[]);
				db_root.Items.Add(views_root);

				// Functions
				var functions_root = createTreeViewItem("Functions", image_function);
				functions_root.Tag = typeof(Function[]);
				db_root.Items.Add(functions_root);

				_TreDatabaseLayout.Items.Add(db_root);
				database._TreeRoot = db_root;
			}
		}

		/// <summary>
		/// Create a new database and prompt the user for a name.
		/// </summary>
{'fraction_non_alphanumeric': 0.1079681778002273, 'fraction_numerical': 0.0005051142821063266, 'mean_word_length': 4.173089483997387, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 33, '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': '7376931', 'n_tokens_mistral': 2658, 'n_tokens_neox': 2265, 'n_words': 540}