#region License
// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
// This source code is made available under the terms of the GNU Lesser General Public License v3.0
// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using MiniSqlQuery.Core;
namespace MiniSqlQuery
{
/// The options form.
public partial class OptionsForm : Form
{
/// The _configuration objects.
private readonly List _configurationObjects = new List();
/// The _host.
private readonly IHostWindow _host;
/// The _property grid.
private readonly PropertyGrid _propertyGrid;
/// The _services.
private readonly IApplicationServices _services;
/// Initializes a new instance of the class.
/// The application services.
/// The host window.
public OptionsForm(IApplicationServices applicationServices, IHostWindow hostWindow)
{
InitializeComponent();
// add a grid to the panel
_propertyGrid = new PropertyGrid();
_propertyGrid.Dock = DockStyle.Fill;
groupBox1.Controls.Add(_propertyGrid);
_services = applicationServices;
_host = hostWindow;
}
/// Gets ConfigurationObject.
private IConfigurationObject ConfigurationObject
{
get
{
if (lstSettingsProviders.SelectedIndex > -1)
{
return _configurationObjects[lstSettingsProviders.SelectedIndex];
}
return null;
}
}
/// The ask to save changes.
///
private DialogResult AskToSaveChanges()
{
return _host.DisplayMessageBox(null, "所做的配置更改,是否保存?", "保存改变?", MessageBoxButtons.YesNo,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
MessageBoxOptions.ServiceNotification, null, null);
}
/// The config object property changed.
/// The sender.
/// The e.
private void ConfigObjectPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "IsDirty")
{
string title = "选项";
if (((IConfigurationObject)sender).IsDirty)
{
title += "*";
}
Text = title;
}
}
/// The options form_ form closing.
/// The sender.
/// The e.
private void OptionsForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (ConfigurationObject != null)
{
if (ConfigurationObject.IsDirty)
{
DialogResult result = AskToSaveChanges();
if (result == DialogResult.Yes)
{
ConfigurationObject.Save();
}
else if (result == DialogResult.Cancel)
{
e.Cancel = true;
}
}
}
}
/// The options form_ load.
/// The sender.
/// The e.
private void OptionsForm_Load(object sender, EventArgs e)
{
var cofigTypes = _services.GetConfigurationObjectTypes();
// build a list of config instances
foreach (Type cofigType in cofigTypes)
{
_configurationObjects.Add(_services.Resolve(cofigType.FullName));
}
// add the config editors to the list and watch them for changes
foreach (var configObject in _configurationObjects)
{
configObject.PropertyChanged += ConfigObjectPropertyChanged;
lstSettingsProviders.Items.Add(configObject.Name);
}
// select first
if (lstSettingsProviders.Items.Count > 0)
{
lstSettingsProviders.SelectedIndex = 0;
}
}
/// The btn o k_ click.
/// The sender.
/// The e.
private void btnOK_Click(object sender, EventArgs e)
{
if (ConfigurationObject != null)
{
ConfigurationObject.Save();
}
Close();
}
/// The list box 1_ selected value changed.
/// The sender.
/// The e.
private void listBox1_SelectedValueChanged(object sender, EventArgs e)
{
if (ConfigurationObject != null)
{
bool change = true;
if (ConfigurationObject.IsDirty)
{
DialogResult result = AskToSaveChanges();
if (result == DialogResult.Yes)
{
ConfigurationObject.Save();
}
else
{
change = false;
}
}
if (change)
{
_propertyGrid.SelectedObject = ConfigurationObject.Settings;
}
}
}
}
}