Skip to content

Commit 4d54bf1

Browse files
committed
Changed load file logic.
1 parent d7d6fff commit 4d54bf1

File tree

1 file changed

+70
-24
lines changed

1 file changed

+70
-24
lines changed

Forms/MainForm.cs

Lines changed: 70 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,44 @@ private void newClassToolStripButton_Click(object sender, EventArgs e)
171171

172172
private void openProjectToolStripMenuItem_Click(object sender, EventArgs e)
173173
{
174-
SelectAndLoadFileFromPath(null);
174+
try
175+
{
176+
var path = ShowOpenProjectFileDialog();
177+
if (path != null)
178+
{
179+
var project = new ReClassNetProject();
180+
181+
LoadFileFromPath(path, ref project);
182+
183+
// If the file is a ReClass.NET file remember the path.
184+
if (Path.GetExtension(path) == ReClassNetFile.FileExtension)
185+
{
186+
project.Path = path;
187+
}
188+
189+
SetProject(project);
190+
}
191+
}
192+
catch (Exception ex)
193+
{
194+
Program.Logger.Log(ex);
195+
}
175196
}
176197

177198
private void mergeWithProjectToolStripMenuItem_Click(object sender, EventArgs e)
178199
{
179-
SelectAndLoadFileFromPath(CurrentProject);
200+
try
201+
{
202+
var path = ShowOpenProjectFileDialog();
203+
if (path != null)
204+
{
205+
LoadFileFromPath(path, ref currentProject);
206+
}
207+
}
208+
catch (Exception ex)
209+
{
210+
Program.Logger.Log(ex);
211+
}
180212
}
181213

182214
private void clearProjectToolStripMenuItem_Click(object sender, EventArgs e)
@@ -388,7 +420,26 @@ private void MainForm_DragDrop(object sender, DragEventArgs e)
388420
var files = e.Data.GetData(DataFormats.FileDrop) as string[];
389421
if (files != null && files.Any())
390422
{
391-
LoadFileFromPath(files.First(), null);
423+
try
424+
{
425+
var path = files.First();
426+
427+
var project = new ReClassNetProject();
428+
429+
LoadFileFromPath(path, ref project);
430+
431+
// If the file is a ReClass.NET file remember the path.
432+
if (Path.GetExtension(path) == ReClassNetFile.FileExtension)
433+
{
434+
project.Path = path;
435+
}
436+
437+
SetProject(project);
438+
}
439+
catch (Exception ex)
440+
{
441+
Program.Logger.Log(ex);
442+
}
392443
}
393444
}
394445

@@ -527,7 +578,9 @@ private void AskAddOrInsertBytes(string title, Action<int> callback)
527578
}
528579
}
529580

530-
private void SelectAndLoadFileFromPath(ReClassNetProject project)
581+
/// <summary>Shows an <see cref="OpenFileDialog"/> with all valid file extensions.</summary>
582+
/// <returns>The path to the selected file or null if no file was selected.</returns>
583+
public static string ShowOpenProjectFileDialog()
531584
{
532585
using (var ofd = new OpenFileDialog())
533586
{
@@ -540,50 +593,43 @@ private void SelectAndLoadFileFromPath(ReClassNetProject project)
540593

541594
if (ofd.ShowDialog() == DialogResult.OK)
542595
{
543-
LoadFileFromPath(ofd.FileName, project);
596+
return ofd.FileName;
544597
}
545598
}
599+
600+
return null;
546601
}
547602

548-
private void LoadFileFromPath(string filePath, ReClassNetProject project)
603+
/// <summary>Loads the file into the given project.</summary>
604+
/// <param name="filePath">Full pathname of the file.</param>
605+
/// <param name="project">[in,out] The project.</param>
606+
private void LoadFileFromPath(string filePath, ref ReClassNetProject project)
549607
{
550608
Contract.Requires(filePath != null);
551-
552-
var loadProject = project ?? new ReClassNetProject();
609+
Contract.Requires(project != null);
553610

554611
IReClassImport import = null;
555612
switch (Path.GetExtension(filePath))
556613
{
557614
case ReClassNetFile.FileExtension:
558-
import = new ReClassNetFile(loadProject);
559-
loadProject.Path = filePath;
615+
import = new ReClassNetFile(project);
560616
break;
561617
case ReClassQtFile.FileExtension:
562-
import = new ReClassQtFile(loadProject);
618+
import = new ReClassQtFile(project);
563619
break;
564620
case ReClassFile.FileExtension:
565-
import = new ReClassFile(loadProject);
621+
import = new ReClassFile(project);
566622
break;
567623
case ReClass2007File.FileExtension:
568-
import = new ReClass2007File(loadProject);
624+
import = new ReClass2007File(project);
569625
break;
570626
default:
571627
Program.Logger.Log(LogLevel.Error, $"The file '{filePath}' has an unknown type.");
572628
break;
573629
}
574630
if (import != null)
575631
{
576-
try
577-
{
578-
import.Load(filePath, Program.Logger);
579-
580-
SetProject(loadProject);
581-
}
582-
catch (Exception ex)
583-
{
584-
Program.Logger.Log(ex);
585-
}
586-
632+
import.Load(filePath, Program.Logger);
587633
}
588634
}
589635
}

0 commit comments

Comments
 (0)