using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
using TGS.Interface;
namespace TGS.ControlPanel
{
///
/// Form used for managing s
///
sealed partial class InstanceSelector : CountedForm
{
///
/// The we build instance connections from
///
readonly IServer server;
///
/// Used for modifying without invoking its side effects
///
bool UpdatingEnabledCheckbox = false;
///
/// Construct an
///
/// The value of
public InstanceSelector(IServer _server)
{
InitializeComponent();
InstanceListBox.MouseDoubleClick += InstanceListBox_MouseDoubleClick;
server = _server;
RefreshInstances();
}
///
/// Connects to a if it is double clicked in
///
/// The sender of the event
/// The
void InstanceListBox_MouseDoubleClick(object sender, MouseEventArgs e)
{
TryConnectToIndexInstance(InstanceListBox.IndexFromPoint(e.Location));
}
///
/// Returns the associated with 's current selected index
///
/// The associated with 's current selected index if it exists, otherwise
InstanceMetadata GetSelectedInstanceMetadata()
{
var index = (IInstance)InstanceListBox.SelectedItem;
return index?.Metadata;
}
///
/// Loads the using
///
void RefreshInstances()
{
InstanceListBox.Items.Clear();
foreach(var I in server.Instances)
InstanceListBox.Items.Add(I);
var HasServerAdmin = server.InstanceManager != null;
CreateInstanceButton.Enabled = HasServerAdmin;
ImportInstanceButton.Enabled = HasServerAdmin;
RenameInstanceButton.Enabled = HasServerAdmin;
DetachInstanceButton.Enabled = HasServerAdmin;
EnabledCheckBox.Enabled = HasServerAdmin;
if(InstanceListBox.Items.Count > 0)
InstanceListBox.SelectedIndex = 0;
}
///
/// Tries to start a for a given
///
/// The of to connect to
void TryConnectToIndexInstance(int index)
{
if (index == ListBox.NoMatches)
return;
var instance = (IInstance)InstanceListBox.Items[index];
if (ControlPanel.InstancesInUse.TryGetValue(instance.Metadata.Name, out ControlPanel activeCP))
{
activeCP.BringToFront();
return;
}
new ControlPanel(server, instance).Show();
}
///
/// Prompts the user for parameters to
///
/// The sender of the event
/// The
async void DetachInstanceButton_Click(object sender, EventArgs e)
{
var imd = GetSelectedInstanceMetadata();
if (imd == null)
return;
if (MessageBox.Show(String.Format("This will dissociate the server instance at \"{0}\"! Are you sure?", imd.Path), "Instance Detach", MessageBoxButtons.YesNo) != DialogResult.Yes)
return;
string res = null;
await WrapServerOp(() => res = server.InstanceManager.DetachInstance(imd.Name));
if (res != null)
MessageBox.Show(res);
RefreshInstances();
}
///
/// Calls
///
/// The sender of the event
/// The
void RefreshButton_Click(object sender, EventArgs e)
{
RefreshInstances();
}
///
/// Prompts the user for parameters to
///
/// The sender of the event
/// The
async void RenameInstanceButton_Click(object sender, EventArgs e)
{
var imd = GetSelectedInstanceMetadata();
if (imd == null)
return;
var new_name = Program.TextPrompt("Instance Rename", "Enter a new name for the instance:");
if (new_name == null)
return;
if (imd.Enabled && MessageBox.Show(String.Format("This will temporarily offline the server instance! Are you sure?", imd.Path), "Instance Restart", MessageBoxButtons.YesNo) != DialogResult.Yes)
return;
string res = null;
await WrapServerOp(() => res = server.InstanceManager.RenameInstance(imd.Name, new_name));
if (res != null)
MessageBox.Show(res);
RefreshInstances();
}
///
/// Prompts the user for parameters to
///
/// The sender of the event
/// The
async void ImportInstanceButton_Click(object sender, EventArgs e)
{
var instance_path = Program.TextPrompt("Instance Import", "Enter the full path to the instance:");
if (instance_path == null)
return;
string res = null;
await WrapServerOp(() => res = server.InstanceManager.ImportInstance(instance_path));
if (res != null)
MessageBox.Show(res);
RefreshInstances();
}
///
/// Prompts the user for parameters to
///
/// The sender of the event
/// The
async void CreateInstanceButton_Click(object sender, EventArgs e)
{
var instance_name = Program.TextPrompt("Instance Creation", "Enter the name of the instance:");
if (instance_name == null)
return;
var instance_path = Program.TextPrompt("Instance Creation", "Enter the full path to the instance:");
if (instance_path == null)
return;
string res = null;
await WrapServerOp(() => res = server.InstanceManager.CreateInstance(instance_name, instance_path));
if (res != null)
MessageBox.Show(res);
RefreshInstances();
}
///
/// Attempts to connect the user to an based on the of
///
/// The sender of the event
/// The
void ConnectButton_Click(object sender, EventArgs e)
{
TryConnectToIndexInstance(InstanceListBox.SelectedIndex);
}
///
/// Prompts the user if they want to call to either online or offline an based on its current state
///
/// The sender of the event
/// The
async void EnabledCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (UpdatingEnabledCheckbox)
return;
var enabling = EnabledCheckBox.Checked;
try
{
if (MessageBox.Show(String.Format("Are you sure you want to {0} this instance?", enabling ? "online" : "offline"), "Instance Status Change", MessageBoxButtons.YesNo) != DialogResult.Yes)
return;
string res = null;
await WrapServerOp(() => res = server.InstanceManager.SetInstanceEnabled(GetSelectedInstanceMetadata().Name, enabling));
if (res != null)
MessageBox.Show(res);
}
finally
{
RefreshInstances();
}
}
///
/// Update based on the selected 's property
///
/// The sender of the event
/// The
void InstanceListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (InstanceListBox.SelectedIndex != ListBox.NoMatches)
{
UpdatingEnabledCheckbox = true;
EnabledCheckBox.Checked = GetSelectedInstanceMetadata().Enabled;
UpdatingEnabledCheckbox = false;
}
}
}
}