Cleanup and add missing functionality to InstanceSelector

This commit is contained in:
Cyberboss
2017-11-08 11:46:02 -05:00
parent cbcde73c51
commit 5f4c30b080
2 changed files with 103 additions and 14 deletions
+32 -1
View File
@@ -36,6 +36,8 @@
this.RenameInstanceButton = new System.Windows.Forms.Button();
this.DetachInstanceButton = new System.Windows.Forms.Button();
this.RefreshButton = new System.Windows.Forms.Button();
this.ConnectButton = new System.Windows.Forms.Button();
this.EnabledCheckBox = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// InstanceListBox
@@ -46,6 +48,7 @@
this.InstanceListBox.Name = "InstanceListBox";
this.InstanceListBox.Size = new System.Drawing.Size(339, 238);
this.InstanceListBox.TabIndex = 0;
this.InstanceListBox.SelectedIndexChanged += new System.EventHandler(this.InstanceListBox_SelectedIndexChanged);
//
// CreateInstanceButton
//
@@ -94,7 +97,7 @@
// RefreshButton
//
this.RefreshButton.Anchor = System.Windows.Forms.AnchorStyles.Right;
this.RefreshButton.Location = new System.Drawing.Point(358, 226);
this.RefreshButton.Location = new System.Drawing.Point(358, 195);
this.RefreshButton.Name = "RefreshButton";
this.RefreshButton.Size = new System.Drawing.Size(148, 25);
this.RefreshButton.TabIndex = 19;
@@ -102,12 +105,37 @@
this.RefreshButton.UseVisualStyleBackColor = true;
this.RefreshButton.Click += new System.EventHandler(this.RefreshButton_Click);
//
// ConnectButton
//
this.ConnectButton.Anchor = System.Windows.Forms.AnchorStyles.Right;
this.ConnectButton.Location = new System.Drawing.Point(358, 226);
this.ConnectButton.Name = "ConnectButton";
this.ConnectButton.Size = new System.Drawing.Size(148, 25);
this.ConnectButton.TabIndex = 20;
this.ConnectButton.Text = "Connect";
this.ConnectButton.UseVisualStyleBackColor = true;
this.ConnectButton.Click += new System.EventHandler(this.ConnectButton_Click);
//
// EnabledCheckBox
//
this.EnabledCheckBox.AutoSize = true;
this.EnabledCheckBox.ForeColor = System.Drawing.Color.White;
this.EnabledCheckBox.Location = new System.Drawing.Point(396, 149);
this.EnabledCheckBox.Name = "EnabledCheckBox";
this.EnabledCheckBox.Size = new System.Drawing.Size(65, 17);
this.EnabledCheckBox.TabIndex = 21;
this.EnabledCheckBox.Text = "Enabled";
this.EnabledCheckBox.UseVisualStyleBackColor = true;
this.EnabledCheckBox.CheckedChanged += new System.EventHandler(this.EnabledCheckBox_CheckedChanged);
//
// InstanceSelector
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(39)))), ((int)(((byte)(40)))), ((int)(((byte)(34)))));
this.ClientSize = new System.Drawing.Size(518, 261);
this.Controls.Add(this.EnabledCheckBox);
this.Controls.Add(this.ConnectButton);
this.Controls.Add(this.RefreshButton);
this.Controls.Add(this.DetachInstanceButton);
this.Controls.Add(this.RenameInstanceButton);
@@ -119,6 +147,7 @@
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Server Instances";
this.ResumeLayout(false);
this.PerformLayout();
}
@@ -130,5 +159,7 @@
private System.Windows.Forms.Button RenameInstanceButton;
private System.Windows.Forms.Button DetachInstanceButton;
private System.Windows.Forms.Button RefreshButton;
private System.Windows.Forms.Button ConnectButton;
private System.Windows.Forms.CheckBox EnabledCheckBox;
}
}
+71 -13
View File
@@ -8,7 +8,7 @@ using TGServiceInterface.Components;
namespace TGControlPanel
{
/// <summary>
/// Form used for managing <see cref="TGServiceInterface.Components.ITGSService"/> <see cref="TGServiceInterface.Components.ITGInstance"/> manipulation functions
/// Form used for managing <see cref="ITGSService"/> <see cref="ITGInstance"/> manipulation functions
/// </summary>
partial class InstanceSelector : CountedForm
{
@@ -20,6 +20,11 @@ namespace TGControlPanel
/// List of <see cref="InstanceMetadata"/> from <see cref="masterInterface"/>
/// </summary>
IList<InstanceMetadata> InstanceData;
/// <summary>
/// Used for modifying <see cref="EnabledCheckBox"/> without invoking its side effects
/// </summary>
bool UpdatingEnabledCheckbox = false;
public InstanceSelector(IInterface I)
{
InitializeComponent();
@@ -48,15 +53,13 @@ namespace TGControlPanel
}
/// <summary>
/// Connects to a <see cref="TGServiceInterface.Components.ITGInstance"/> if it is double clicked in <see cref="InstanceListBox"/>
/// Connects to a <see cref="ITGInstance"/> if it is double clicked in <see cref="InstanceListBox"/>
/// </summary>
/// <param name="sender">The sender of the event</param>
/// <param name="e">The <see cref="MouseEventArgs"/></param>
void InstanceListBox_MouseDoubleClick(object sender, MouseEventArgs e)
{
var index = InstanceListBox.IndexFromPoint(e.Location);
if (index != ListBox.NoMatches)
TryConnectToInstance(InstanceData[index].Name);
TryConnectToIndexInstance(InstanceListBox.IndexFromPoint(e.Location));
}
/// <summary>
@@ -88,15 +91,26 @@ namespace TGControlPanel
});
foreach(var I in InstanceData)
InstanceListBox.Items.Add(String.Format("{0}: {1} - {2} - {3}", I.LoggingID, I.Name, I.Path, I.Enabled ? "ONLINE" : "OFFLINE"));
var HasServerAdmin = masterInterface.ConnectionStatus().HasFlag(ConnectivityLevel.Administrator);
CreateInstanceButton.Enabled = HasServerAdmin;
ImportInstanceButton.Enabled = HasServerAdmin;
RenameInstanceButton.Enabled = HasServerAdmin;
DetachInstanceButton.Enabled = HasServerAdmin;
EnabledCheckBox.Enabled = HasServerAdmin;
if(InstanceData.Count > 0)
InstanceListBox.SelectedIndex = 0;
}
/// <summary>
/// Tries to start a <see cref="ControlPanel"/> for a given <paramref name="instanceName"/>
/// Tries to start a <see cref="ControlPanel"/> for a given <see cref="InstanceListBox"/> <paramref name="index"/>
/// </summary>
/// <param name="instanceName">The name of the <see cref="ITGInstance"/> to connect to</param>
async void TryConnectToInstance(string instanceName)
/// <param name="index">The <see cref="ListBox.SelectedIndex"/> of <see cref="InstanceListBox"/> to connect to</param>
async void TryConnectToIndexInstance(int index)
{
if(ControlPanel.InstancesInUse.TryGetValue(instanceName, out ControlPanel activeCP))
if (index == ListBox.NoMatches)
return;
var instanceName = InstanceData[index].Name;
if (ControlPanel.InstancesInUse.TryGetValue(instanceName, out ControlPanel activeCP))
{
activeCP.BringToFront();
return;
@@ -136,7 +150,7 @@ namespace TGControlPanel
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 = masterInterface.GetServiceComponent<ITGInstanceManager>().DetachInstance(imd.Name); });
await WrapServerOp(() => res = masterInterface.GetServiceComponent<ITGInstanceManager>().DetachInstance(imd.Name));
if (res != null)
MessageBox.Show(res);
RefreshInstances();
@@ -168,7 +182,7 @@ namespace TGControlPanel
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 = masterInterface.GetServiceComponent<ITGInstanceManager>().RenameInstance(imd.Name, new_name); });
await WrapServerOp(() => res = masterInterface.GetServiceComponent<ITGInstanceManager>().RenameInstance(imd.Name, new_name));
if (res != null)
MessageBox.Show(res);
RefreshInstances();
@@ -185,7 +199,7 @@ namespace TGControlPanel
if (instance_path == null)
return;
string res = null;
await WrapServerOp(() => { res = masterInterface.GetServiceComponent<ITGInstanceManager>().ImportInstance(instance_path); });
await WrapServerOp(() => res = masterInterface.GetServiceComponent<ITGInstanceManager>().ImportInstance(instance_path));
if (res != null)
MessageBox.Show(res);
RefreshInstances();
@@ -205,10 +219,54 @@ namespace TGControlPanel
if (instance_path == null)
return;
string res = null;
await WrapServerOp(() => { res = masterInterface.GetServiceComponent<ITGInstanceManager>().CreateInstance(instance_name, instance_path); });
await WrapServerOp(() => res = masterInterface.GetServiceComponent<ITGInstanceManager>().CreateInstance(instance_name, instance_path));
if (res != null)
MessageBox.Show(res);
RefreshInstances();
}
/// <summary>
/// Attempts to connect the user to an <see cref="ITGInstance"/> based on the <see cref="ListBox.SelectedIndex"/> of <see cref="InstanceListBox"/>
/// </summary>
/// <param name="sender">The sender of the event</param>
/// <param name="e">The <see cref="EventArgs"/></param>
private void ConnectButton_Click(object sender, EventArgs e)
{
TryConnectToIndexInstance(InstanceListBox.SelectedIndex);
}
/// <summary>
/// Prompts the user if they want to call <see cref="ITGInstanceManager.SetInstanceEnabled(string, bool)"/> to either online or offline an <see cref="ITGInstance"/> based on its current state
/// </summary>
/// <param name="sender">The sender of the event</param>
/// <param name="e">The <see cref="EventArgs"/></param>
async void EnabledCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (UpdatingEnabledCheckbox)
return;
var enabling = EnabledCheckBox.Checked;
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 = masterInterface.GetServiceComponent<ITGInstanceManager>().SetInstanceEnabled(InstanceData[InstanceListBox.SelectedIndex].Name, enabling));
if (res != null)
MessageBox.Show(res);
}
/// <summary>
/// Update <see cref="EnabledCheckBox"/> based on the selected <see cref="ITGInstance"/>'s <see cref="InstanceMetadata.Enabled"/> property
/// </summary>
/// <param name="sender">The sender of the event</param>
/// <param name="e">The <see cref="EventArgs"/></param>
void InstanceListBox_SelectedIndexChanged(object sender, EventArgs e)
{
var index = InstanceListBox.SelectedIndex;
if (index != ListBox.NoMatches)
{
UpdatingEnabledCheckbox = true;
EnabledCheckBox.Checked = InstanceData[index].Enabled;
UpdatingEnabledCheckbox = false;
}
}
}
}