Skip to content

Commit

Permalink
Merge #1939 Use OpenFileDialog instead of FolderBrowserDialog in inst…
Browse files Browse the repository at this point in the history
…ance selector
  • Loading branch information
politas committed Dec 27, 2016
2 parents 5ce5a09 + 3215c15 commit 31057f3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 51 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ All notable changes to this project will be documented in this file.
- [GUI] Modlist hides epochs by default (#1942 by: politas; reviewed: ayan4m1)
- [Core/GUI] Let users select compatible KSP versions (#1957 by: grzegrzk; reviewed: dbent, politas)
- [Core] Add IntersectWith method to KspVersionRange (#1958 by: dbent; reviewed: grzegrzk, politas)
- [GUI] Display all mod versions in ModInfo panel (#1961 by grzegrzk; reviewed: politas)
- [GUI] Display all mod versions in ModInfo panel (#1961 by: grzegrzk; reviewed: politas)
- [GUI] Use OpenFileDialog instead of FolderBrowserDialog in instance selector (#1939 by: ayan4m1; reviewed: politas)

## v1.20.1

Expand Down
116 changes: 66 additions & 50 deletions GUI/ChooseKSPInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,42 @@ namespace CKAN
{
public partial class ChooseKSPInstance : Form
{
private FolderBrowserDialog browseKspFolder;
private RenameInstanceDialog renameInstanceDialog;
private readonly KSPManager manager;
private readonly KSPManager _manager;
private RenameInstanceDialog _renameInstanceDialog;
private readonly OpenFileDialog _instanceDialog = new OpenFileDialog()
{
AddExtension = false,
CheckFileExists = false,
CheckPathExists = false,
InitialDirectory = Environment.CurrentDirectory,
Filter = "Build metadata file (buildID*.txt)|buildID*.txt",
Multiselect = false
};

public bool HasSelections => KSPInstancesListView.SelectedItems.Count > 0;

public ChooseKSPInstance()
{
manager = Main.Instance.Manager;
_manager = Main.Instance.Manager;
InitializeComponent();

StartPosition = FormStartPosition.CenterScreen;

browseKspFolder = new FolderBrowserDialog();

if (!manager.Instances.Any())
if (!_manager.Instances.Any())
{
manager.FindAndRegisterDefaultInstance();
_manager.FindAndRegisterDefaultInstance();
}

UpdateInstancesList();

SetButtonsEnabled(false);
UpdateButtonState();
}

private void UpdateInstancesList()
{
SetButtonsEnabled(false);
KSPInstancesListView.Items.Clear();
UpdateButtonState();

foreach (var instance in manager.Instances)
foreach (var instance in _manager.Instances)
{
var item = new ListViewItem { Text = instance.Key, Tag = instance.Key };

Expand All @@ -49,24 +56,25 @@ private void UpdateInstancesList()

private void AddNewButton_Click(object sender, EventArgs e)
{
if (browseKspFolder.ShowDialog() == DialogResult.OK)
if (_instanceDialog.ShowDialog() != DialogResult.OK) return;
if (!File.Exists(_instanceDialog.FileName)) return;

KSP instance;
var path = Path.GetDirectoryName(_instanceDialog.FileName);
try
{
KSP instance;
string path = browseKspFolder.SelectedPath;
try
{
instance = new KSP(path, GUI.user);
}
catch (NotKSPDirKraken){
GUI.user.displayError("Directory {0} is not valid KSP directory.", new object[] {path});
return;
}

string instanceName = Path.GetFileName(path);
instanceName = manager.GetNextValidInstanceName(instanceName);
manager.AddInstance(instanceName, instance);
UpdateInstancesList();
instance = new KSP(path, GUI.user);
}
catch (NotKSPDirKraken)
{
GUI.user.displayError("Directory {0} is not valid KSP directory.", new object[] { path });
return;
}

var instanceName = Path.GetFileName(path);
instanceName = _manager.GetNextValidInstanceName(instanceName);
_manager.AddInstance(instanceName, instance);
UpdateInstancesList();
}

private void SelectButton_Click(object sender, EventArgs e)
Expand All @@ -76,55 +84,63 @@ private void SelectButton_Click(object sender, EventArgs e)

private void UseSelectedInstance()
{
var instance = (string) KSPInstancesListView.SelectedItems[0].Tag;
if (KSPInstancesListView.SelectedItems.Count == 0)
{
return;
}

var selected = KSPInstancesListView.SelectedItems[0];
var instName = selected?.Tag as string;
if (instName == null)
{
return;
}

if (SetAsDefaultCheckbox.Checked)
{
manager.SetAutoStart(instance);
_manager.SetAutoStart(instName);
}

manager.SetCurrentInstance(instance);
_manager.SetCurrentInstance(instName);
DialogResult = DialogResult.OK;
Close();
}

private void KSPInstancesListView_SelectedIndexChanged(object sender, EventArgs e)
{
var has_instance = KSPInstancesListView.SelectedItems.Count != 0;
SetButtonsEnabled(has_instance);
UpdateButtonState();
}

private void KSPInstancesListView_DoubleClick(object sender, EventArgs r)
{
var has_instance = KSPInstancesListView.SelectedItems.Count != 0;
if(has_instance)
UseSelectedInstance();
if (HasSelections) UseSelectedInstance();
}

private void RenameButton_Click(object sender, EventArgs e)
{
var instance = (string) KSPInstancesListView.SelectedItems[0].Tag;
var instance = (string)KSPInstancesListView.SelectedItems[0].Tag;

renameInstanceDialog = new RenameInstanceDialog();
if (renameInstanceDialog.ShowRenameInstanceDialog(instance) == DialogResult.OK)
{
manager.RenameInstance(instance, renameInstanceDialog.GetResult());
UpdateInstancesList();
}
// show the dialog, and only continue if the user selected "OK"
_renameInstanceDialog = new RenameInstanceDialog();
if (_renameInstanceDialog.ShowRenameInstanceDialog(instance) != DialogResult.OK) return;

// proceed with instance rename
_manager.RenameInstance(instance, _renameInstanceDialog.GetResult());
UpdateInstancesList();
}

private void Forget_Click(object sender, EventArgs e)
{
var instance = (string)KSPInstancesListView.SelectedItems[0].Tag;
manager.RemoveInstance(instance);
UpdateInstancesList();

foreach (var instance in KSPInstancesListView.SelectedItems.OfType<ListViewItem>().Select(item => item.Tag as string))
{
_manager.RemoveInstance(instance);
UpdateInstancesList();
}
}

private void SetButtonsEnabled(bool has_instance)
private void UpdateButtonState()
{
ForgetButton.Enabled = RenameButton.Enabled = SelectButton.Enabled = SetAsDefaultCheckbox.Enabled = has_instance;
ForgetButton.Enabled = RenameButton.Enabled = SelectButton.Enabled = SetAsDefaultCheckbox.Enabled = HasSelections;
}

}
}

0 comments on commit 31057f3

Please sign in to comment.