From 2f017058d488c9bbcadd6febb7ddf520d7ea0d4c Mon Sep 17 00:00:00 2001 From: TUPUNCO Date: Sun, 17 Oct 2021 21:55:35 +0800 Subject: [PATCH] `GUI-app` Support folder code batch conversion - Add `Convert` button `IsEnabled` property control in ViewModel. --- JavaToCSharpGui/JavaToCSharpGui.csproj | 1 + JavaToCSharpGui/ViewModels/ShellViewModel.cs | 217 +++++++++++++++++-- JavaToCSharpGui/Views/ShellView.xaml | 5 +- 3 files changed, 204 insertions(+), 19 deletions(-) diff --git a/JavaToCSharpGui/JavaToCSharpGui.csproj b/JavaToCSharpGui/JavaToCSharpGui.csproj index 3a1939f0..98533078 100644 --- a/JavaToCSharpGui/JavaToCSharpGui.csproj +++ b/JavaToCSharpGui/JavaToCSharpGui.csproj @@ -15,6 +15,7 @@ + diff --git a/JavaToCSharpGui/ViewModels/ShellViewModel.cs b/JavaToCSharpGui/ViewModels/ShellViewModel.cs index 0ce42cd8..d50260b6 100644 --- a/JavaToCSharpGui/ViewModels/ShellViewModel.cs +++ b/JavaToCSharpGui/ViewModels/ShellViewModel.cs @@ -1,13 +1,19 @@ -using System; +using System; +using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.IO; +using System.Linq; using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; + using Caliburn.Micro; + using JavaToCSharp; + using JavaToCSharpGui.Views; + using Microsoft.Win32; namespace JavaToCSharpGui.ViewModels @@ -24,11 +30,23 @@ public class ShellViewModel : Screen, IShell private bool _includeUsings = true; private bool _includeNamespace = true; private bool _useDebugAssertForAsserts; + private bool _isConvertEnabled = true; + + #region UseFolder + + private bool _useFolderConvert; + private IList _javaFiles; + private string _currentJavaFile; + + #endregion public ShellViewModel() { base.DisplayName = "Java to C# Converter"; + _isConvertEnabled = true; + _useFolderConvert = false; + _includeUsings = Properties.Settings.Default.UseUsingsPreference; _includeNamespace = Properties.Settings.Default.UseNamespacePreference; _useDebugAssertForAsserts = Properties.Settings.Default.UseDebugAssertPreference; @@ -138,6 +156,26 @@ public bool UseDebugAssertForAsserts } } + public bool IsConvertEnabled + { + get => _isConvertEnabled; + set + { + _isConvertEnabled = value; + NotifyOfPropertyChange(() => IsConvertEnabled); + } + } + + public bool UseFolderConvert + { + get => _useFolderConvert; + set + { + _useFolderConvert = value; + NotifyOfPropertyChange(() => UseFolderConvert); + } + } + public void AddUsing() { Usings.Add(_addUsingInput); @@ -166,20 +204,28 @@ public void Convert() options.WarningEncountered += Options_WarningEncountered; options.StateChanged += Options_StateChanged; + this.IsConvertEnabled = false; Task.Run(() => { try { - var csharp = JavaToCSharpConverter.ConvertText(JavaText, options); - - Dispatcher.CurrentDispatcher.Invoke(() => this.CSharpText = csharp); + if (_useFolderConvert) + { + FolderConvert(options); + } + else + { + var csharp = JavaToCSharpConverter.ConvertText(JavaText, options); + DispatcherInvoke(() => this.CSharpText = csharp); + } } catch (Exception ex) { - Dispatcher.CurrentDispatcher.Invoke(() => - { - MessageBox.Show("There was an error converting the text to C#: " + ex.Message); - }); + DispatcherInvoke(() => MessageBox.Show("There was an error converting the text to C#: " + ex.Message)); + } + finally + { + DispatcherInvoke(() => this.IsConvertEnabled = true); } }); } @@ -191,42 +237,170 @@ private void Options_StateChanged(object sender, ConversionStateChangedEventArgs case ConversionState.Starting: ConversionStateLabel = "Starting..."; break; + case ConversionState.ParsingJavaAst: ConversionStateLabel = "Parsing Java code..."; break; + case ConversionState.BuildingCSharpAst: ConversionStateLabel = "Building C# AST..."; break; + case ConversionState.Done: ConversionStateLabel = "Done!"; break; + default: break; } } - private static void Options_WarningEncountered(object sender, ConversionWarningEventArgs e) + private void Options_WarningEncountered(object sender, ConversionWarningEventArgs e) { - MessageBox.Show("Java Line " + e.JavaLineNumber + ": " + e.Message, "Warning Encountered"); + if (_useFolderConvert) + { + DispatcherInvoke(() => + { + this.CSharpText = $"{ this.CSharpText } \r\n==================\r\n[WARN]out.path: { _currentJavaFile },\r\n\t\tConversionWarning-JavaLine:[{ e.JavaLineNumber}]-Message:[{ e.Message}]\r\n"; + }); + } + else + { + MessageBox.Show($"Java Line {e.JavaLineNumber}: {e.Message}", "Warning Encountered"); + } } public void OpenFileDialog() { - var ofd = new OpenFileDialog + if (_useFolderConvert) + { + OpenFolderDialog(); + } + else { - Filter = "Java Files (*.java)|*.java", - Title = "Open Java File" + var ofd = new OpenFileDialog + { + Filter = "Java Files (*.java)|*.java", + Title = "Open Java File" + }; + + var result = ofd.ShowDialog(); + if (result.GetValueOrDefault()) + { + OpenPath = ofd.FileName; + JavaText = File.ReadAllText(ofd.FileName); + } + } + } + + #region FolderCodeConvert + + /// + /// Folder Browser OpenFolderDialog + /// + private void OpenFolderDialog() + { + var dlg = new FolderBrowserForWPF.Dialog() + { + Title = "Folder Browser", }; - var result = ofd.ShowDialog(); + if (!(dlg.ShowDialog() ?? false)) + return; + + var path = dlg.FileName; + var dir = new DirectoryInfo(path); + if (!dir.Exists) + { + OpenPath = string.Empty; + JavaText = string.Empty; + _javaFiles = null; + + return; + } + + var pDir = dir.Parent; + if (pDir == null) + { + MessageBox.Show("Fail: Root Directory !!!"); + return; + } + + OpenPath = path; + + Task.Run(() => + { + //list all subdir *.java + var files = dir.GetFiles("*.java", SearchOption.AllDirectories); + _javaFiles = files; + + //out java path + var subStartIndex = path.Length; + var javaTexts = string.Join("\r\n", files.Select(x => x.FullName.Substring(subStartIndex))); + + DispatcherInvoke(() => this.JavaText = javaTexts); + }); + } + + /// + /// Folder Code Convert + /// + /// + private void FolderConvert(JavaConversionOptions options) + { + if (_javaFiles == null || options == null) + return; + + var dir = new DirectoryInfo(_openPath); + var pDir = dir.Parent; + if (pDir == null) + throw new FileNotFoundException($"dir {_openPath} parent"); + + var dirName = dir.Name; + var outDirName = $"{dirName}_net_{DateTime.Now.Millisecond}"; + var outDir = pDir.CreateSubdirectory(outDirName); + if (outDir == null || !outDir.Exists) + throw new FileNotFoundException($"outDir {outDirName}"); - if (result.GetValueOrDefault()) + var outDirFullName = outDir.FullName; + var subStartIndex = dir.FullName.Length; + foreach (var jFile in _javaFiles) { - OpenPath = ofd.FileName; - JavaText = File.ReadAllText(ofd.FileName); + var jPath = jFile.Directory.FullName; + var jOutPath = outDirFullName + jPath.Substring(subStartIndex); + var jOutFileName = Path.GetFileNameWithoutExtension(jFile.Name) + ".cs"; + var jOutFileFullName = Path.Combine(jOutPath, jOutFileName); + + _currentJavaFile = jFile.FullName; + if (!Directory.Exists(jOutPath)) + Directory.CreateDirectory(jOutPath); + + var jText = File.ReadAllText(_currentJavaFile); + if (string.IsNullOrEmpty(jText)) + continue; + + try + { + var csText = JavaToCSharpConverter.ConvertText(jText, options); + File.WriteAllText(jOutFileFullName, csText); + + DispatcherInvoke(() => + { + this.CSharpText = $"{ this.CSharpText } \r\n==================\r\nout.path: { jOutPath },\r\n\t\tfile: {jOutFileName}"; + }); + } + catch (Exception ex) + { + DispatcherInvoke(() => + { + this.CSharpText = $"{ this.CSharpText } \r\n==================\r\n[ERROR]out.path: { jOutPath },\r\nex: { ex } \r\n"; + }); + } } } + #endregion + public void CopyOutput() { Clipboard.SetText(CSharpText); @@ -248,5 +422,14 @@ public void ForkMeOnGitHub() UseShellExecute = true }); } + + /// + /// Dispatcher.CurrentDispatcher.Invoke + /// + /// + private void DispatcherInvoke(System.Action callback) + { + Dispatcher.CurrentDispatcher.Invoke(callback); + } } } \ No newline at end of file diff --git a/JavaToCSharpGui/Views/ShellView.xaml b/JavaToCSharpGui/Views/ShellView.xaml index 8adb38aa..e7016468 100644 --- a/JavaToCSharpGui/Views/ShellView.xaml +++ b/JavaToCSharpGui/Views/ShellView.xaml @@ -1,4 +1,4 @@ -Include Usings in Output Include Namespace in Output Use Debug.Assert() for asserts + Use Folder Convert @@ -98,7 +99,7 @@ - +