PageRenderTime 4641ms CodeModel.GetById 46ms RepoModel.GetById 9ms app.codeStats 0ms

/SporeMaster/SporeMaster/FilesEditor.xaml.cs

https://gitlab.com/matt81093/sporemaster
C# | 282 lines | 261 code | 15 blank | 6 comment | 33 complexity | bdf349cefa8c6ac8ee3d963652515809 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.ComponentModel;
  16. using Path = System.IO.Path;
  17. namespace SporeMaster
  18. {
  19. /// <summary>
  20. /// Interaction logic for FilesEditor.xaml
  21. /// </summary>
  22. public partial class FilesEditor : UserControl
  23. {
  24. // MainWindow::loadConfiguration sets these directly:
  25. public HashSet<string> fullTextExtensions;
  26. public string WinMergeEXE;
  27. private DirectoryTree files = DirectoryTree.Tree;
  28. private DirectoryTreeWatcher[] watcher = new DirectoryTreeWatcher[2];
  29. private DirectoryTree lastSelectedFile = null;
  30. private string[] _Path = new string[2];
  31. private string LeftPath { get { return _Path[0]; } }
  32. private string RightPath { get { return _Path[1]; } }
  33. private Window Window { get { return MainWindow.Instance; } }
  34. IEditor currentEditor;
  35. public FilesEditor()
  36. {
  37. InitializeComponent();
  38. if (LicenseManager.UsageMode != LicenseUsageMode.Designtime)
  39. {
  40. this.Width = double.NaN;
  41. this.Height = double.NaN;
  42. }
  43. }
  44. public void SetPath(int side, string value, PleaseWait progress)
  45. {
  46. if (_Path[side] == value) return;
  47. _Path[side] = value;
  48. if (watcher[side] != null) watcher[side].Dispose();
  49. if (_Path[side] != null && Directory.Exists(_Path[side]))
  50. watcher[side] = new DirectoryTreeWatcher(files, side, _Path[side], Dispatcher, update, fullTextExtensions, progress);
  51. }
  52. public void SearchFiles(string search)
  53. {
  54. FileSearch.Text = search;
  55. update();
  56. }
  57. public string GetEditorSelection()
  58. {
  59. return currentEditor==null ? "" : currentEditor.GetSelectedText();
  60. }
  61. void update() {
  62. files.Search( new SearchSpec(FileSearch.Text) );
  63. if (ShowRightOnly.IsChecked == true)
  64. files.SearchRightPresent();
  65. DirTree.Data.ClearAll();
  66. DirTree.Data.AddRootItem(files);
  67. if (IsVisible)
  68. {
  69. if (lastSelectedFile != null && lastSelectedFile.Parent != null &&
  70. !lastSelectedFile.LeftPresent && !lastSelectedFile.RightPresent)
  71. {
  72. // File has been deleted or renamed. Check for renaming
  73. UInt32 instance = NameRegistry.Files.toHash(lastSelectedFile.BaseName);
  74. UInt32 group = NameRegistry.Groups.toHash(lastSelectedFile.Parent.Path);
  75. UInt32 ext = NameRegistry.Types.toHash(lastSelectedFile.FileType);
  76. string newpath = NameRegistry.Groups.toName(group) + "\\" + NameRegistry.Files.toName(instance) + "." + NameRegistry.Types.toName(ext);
  77. lastSelectedFile = files.getFile(newpath, false);
  78. }
  79. DirTree.SelectedItem = lastSelectedFile;
  80. if (DirTree.SelectedItem != null)
  81. {
  82. DirTree.ScrollIntoView(DirTree.SelectedItem);
  83. }
  84. /*if (editing != null && DirTree.SelectedItem != null &&
  85. editing == RightPath + "\\" + (DirTree.SelectedItem as DirectoryTree).Path)
  86. {
  87. if (System.IO.File.GetLastWriteTimeUtc(editing) >
  88. }*/
  89. updateEditorSearch();
  90. }
  91. }
  92. void FileSearch_TextChanged(object sender, TextChangedEventArgs e)
  93. {
  94. update();
  95. }
  96. private void editDocument(string path, bool read_only)
  97. {
  98. // Select an editor
  99. if (path != null && path.EndsWith(".png"))
  100. {
  101. currentEditor = ImageEditor;
  102. TabImageEditor.IsSelected = true;
  103. }
  104. else if (path != null && path.EndsWith(".rw4\\") && File.Exists(path + "model.mesh.xml"))
  105. {
  106. currentEditor = ModelEditor;
  107. TabModelEditor.IsSelected = true;
  108. }
  109. else
  110. {
  111. currentEditor = TextEditor;
  112. TabTextEditor.IsSelected = true;
  113. }
  114. currentEditor.Open(path, read_only);
  115. updateEditorSearch();
  116. }
  117. private void Tree_SelectedItemChanged(object sender, SelectionChangedEventArgs e)
  118. {
  119. var s = (sender as VTreeView.VTreeView).SelectedItem as DirectoryTree;
  120. SelectedFilePanel.IsEnabled = (s != null);
  121. if (s != null)
  122. {
  123. lastSelectedFile = s;
  124. SelectedFileLabel.Text = s.Path;
  125. SelectedFile_Open.IsEnabled = s.LeftPresent;
  126. SelectedFile_Erase.IsEnabled = s.RightPresent;
  127. var f = s.IsFolder ? s : s.Parent;
  128. SelectedFile_ExploreLeft.IsEnabled = f.LeftPresent;
  129. SelectedFile_ExploreRight.IsEnabled = f.RightPresent;
  130. SelectedFile_Save.IsEnabled = false;
  131. if (!s.LeftPresent && !s.RightPresent)
  132. {
  133. editDocument(null, true);
  134. }
  135. else if (s.RightPresent)
  136. {
  137. editDocument(this.RightPath + "\\" + s.Path + (s.IsFolder?"\\":""), false);
  138. }
  139. else
  140. {
  141. editDocument(this.LeftPath + "\\" + s.Path + (s.IsFolder ? "\\" : ""), true);
  142. if (!s.IsFolder)
  143. SelectedFile_Save.IsEnabled = true;
  144. }
  145. DirTree.ScrollIntoView(s);
  146. }
  147. else
  148. {
  149. if (!lastSelectedFile.IsSelected)
  150. lastSelectedFile = null;
  151. //SelectedFileLabel.Text = "";
  152. }
  153. }
  154. void updateEditorSearch()
  155. {
  156. if (currentEditor != null) currentEditor.Search(new SearchSpec(FileSearch.Text));
  157. }
  158. void SelectedFile_Open_Click(object sender, RoutedEventArgs e)
  159. {
  160. if (currentEditor != null) currentEditor.Save();
  161. var n = (DirTree.SelectedItem as DirectoryTree);
  162. var l = watcher[0].Path + "\\" + n.Path;
  163. var r = watcher[1].Path + "\\" + n.Path;
  164. if (!n.RightPresent)
  165. {
  166. Directory.CreateDirectory(Path.GetDirectoryName(r));
  167. System.Diagnostics.Process.Start(WinMergeEXE, "/u /wl /s \"" + l + "\" \"" + l + "\" \"" + r + "\"");
  168. } else if (!n.LeftPresent) {
  169. l = "Not Present in Spore";
  170. System.Diagnostics.Process.Start(WinMergeEXE, "/u /wl /s \"" + l + "\" \"" + r + "\"");
  171. } else
  172. System.Diagnostics.Process.Start(WinMergeEXE, "/u /wl /s \"" + l + "\" \"" + r + "\"" );
  173. }
  174. void SelectedFile_Save_Click(object sender, RoutedEventArgs e)
  175. {
  176. if (currentEditor != null) currentEditor.Save();
  177. var n = (DirTree.SelectedItem as DirectoryTree);
  178. if (n.LeftPresent && !n.RightPresent && !n.IsFolder)
  179. {
  180. var l = watcher[0].Path + "\\" + n.Path;
  181. var r = watcher[1].Path + "\\" + n.Path;
  182. Directory.CreateDirectory(Path.GetDirectoryName(r));
  183. System.IO.File.Copy(l, r);
  184. editDocument(r, false);
  185. }
  186. }
  187. void SelectedFile_Erase_Click(object sender, RoutedEventArgs e)
  188. {
  189. var n = (DirTree.SelectedItem as DirectoryTree);
  190. var r = watcher[1].Path + "\\" + n.Path;
  191. if (MessageBox.Show(this.Window, "Are you sure you want to delete " + r + "?", "Delete", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
  192. {
  193. if (n.IsFolder)
  194. System.IO.Directory.Delete(r, true);
  195. else
  196. {
  197. System.IO.File.Delete(r);
  198. if (File.Exists(r + ".search_index"))
  199. File.Delete(r + ".search_index");
  200. }
  201. }
  202. }
  203. void ShowRightOnly_Changed(object sender, RoutedEventArgs e)
  204. {
  205. update();
  206. }
  207. void SelectedFile_Explorer_Click(object sender, RoutedEventArgs e)
  208. {
  209. if (currentEditor != null) currentEditor.Save();
  210. var n = (DirTree.SelectedItem as DirectoryTree);
  211. int side = (sender == SelectedFile_ExploreLeft) ? 0 : 1;
  212. var p = watcher[side].Path + "\\" + n.Path;
  213. if (!n.IsFolder) p = Path.GetDirectoryName(p);
  214. System.Diagnostics.Process.Start(p);
  215. }
  216. void DirTree_KeyDown(object sender, KeyEventArgs e)
  217. {
  218. var n = (DirTree.SelectedItem as DirectoryTree);
  219. if (e.Key == Key.Up)
  220. {
  221. e.Handled = true;
  222. if (DirTree.SelectedIndex > 0)
  223. DirTree.SelectedIndex = DirTree.SelectedIndex - 1;
  224. }
  225. if (e.Key == Key.Down)
  226. {
  227. e.Handled = true;
  228. DirTree.SelectedIndex = DirTree.SelectedIndex + 1;
  229. }
  230. if (e.Key == Key.Right)
  231. {
  232. if (n != null && n.IsFolder && !n.IsExpanded)
  233. n.IsExpanded = true;
  234. e.Handled = true;
  235. }
  236. if (e.Key == Key.Left)
  237. {
  238. if (n != null)
  239. {
  240. if (n.IsExpanded)
  241. n.IsExpanded = false;
  242. else if (n.Parent != null)
  243. {
  244. if (n.Parent.Parent != null)
  245. n.Parent.IsExpanded = false;
  246. DirTree.SelectedItem = n.Parent;
  247. }
  248. }
  249. e.Handled = true;
  250. }
  251. }
  252. private void UserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  253. {
  254. update();
  255. if (currentEditor != null) currentEditor.Save();
  256. }
  257. }
  258. }