PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/controls/FindReplaceControl.cs

https://github.com/moscrif/ide
C# | 297 lines | 204 code | 65 blank | 28 comment | 43 complexity | 84bae70d44e6064f3591dc2be89d5541 MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using Moscrif.IDE.Editors;
  4. using Moscrif.IDE.Task;
  5. using System.Collections.Generic;
  6. using Moscrif.IDE.Workspace;
  7. using Moscrif.IDE.Iface.Entities;
  8. using Moscrif.IDE.Option;
  9. namespace Moscrif.IDE.Controls
  10. {
  11. public partial class FindReplaceControl : Gtk.Bin
  12. {
  13. private bool ignoreTextChange = false;
  14. List<string> textExtension = new List<string>();
  15. public FindReplaceControl()
  16. {
  17. this.Build();
  18. this.cbPlace.Active = 0;
  19. textExtension = MainClass.Tools.GetAllExtension(ExtensionSetting.OpenTyp.TEXT);
  20. }
  21. protected virtual void OnButton1Clicked(object sender, System.EventArgs e)
  22. {
  23. string expresion = this.entrExpresion.Text;
  24. if (!String.IsNullOrEmpty(expresion)) {
  25. SearchPattern sp = GetSearchPattern();
  26. if(cbPlace.Active == 0){
  27. MainClass.MainWindow.EditorNotebook.SearchNext();
  28. }
  29. else {
  30. sp.ReplaceExpresion = null;
  31. StartFindReplaceInFiles(sp);
  32. }
  33. }
  34. }
  35. private SearchPattern GetSearchPattern(){
  36. SearchPattern sp = new SearchPattern();
  37. sp.CaseSensitive = this.chbCaseSensitive.Active;
  38. sp.WholeWorlds = this.chbWholeWords.Active;
  39. sp.Expresion = this.entrExpresion.Text;
  40. sp.CloseFiles = new List<string>();
  41. sp.OpenFiles = new List<string>();
  42. switch (cbPlace.Active)
  43. {
  44. case 0:{
  45. sp.SearchTyp = SearchPattern.TypSearch.CurentDocument;
  46. break;
  47. }
  48. case 1:{
  49. sp.SearchTyp = SearchPattern.TypSearch.AllOpenDocument;
  50. sp.OpenFiles = new List<string>(MainClass.MainWindow.EditorNotebook.OpenFiles.ToArray());
  51. break;
  52. }
  53. case 2:{
  54. sp.SearchTyp = SearchPattern.TypSearch.CurentProject;
  55. if(MainClass.Workspace.ActualProject == null)
  56. return sp;
  57. MainClass.Workspace.ActualProject.GetAllFiles(ref sp.CloseFiles,MainClass.Workspace.ActualProject.AbsolutProjectDir,textExtension,true);
  58. break;
  59. }
  60. case 3:{
  61. sp.SearchTyp = SearchPattern.TypSearch.AllOpenProject;
  62. foreach (Project p in MainClass.Workspace.Projects)
  63. p.GetAllFiles(ref sp.CloseFiles,p.AbsolutProjectDir,textExtension,true);
  64. break;
  65. }
  66. }
  67. return sp;
  68. }
  69. private void SetSearch(){
  70. string expresion = this.entrExpresion.Text;
  71. if (!String.IsNullOrEmpty(expresion)) {
  72. SearchPattern sp = GetSearchPattern();
  73. if(cbPlace.Active == 0)
  74. MainClass.MainWindow.EditorNotebook.Search(sp);
  75. }
  76. }
  77. private void StartFindReplaceInFiles(SearchPattern sp){
  78. MainClass.MainWindow.FindOutput.Clear();
  79. // first - find/replace in open files
  80. List<string> notOpen = new List<string>(sp.CloseFiles);
  81. List<string> opened = new List<string>(sp.OpenFiles);
  82. List<string> allOpened = new List<string>(MainClass.MainWindow.EditorNotebook.OpenFiles);
  83. // files in not opened -vsetky subory rozdelime na otvorene a zavrete
  84. if(sp.CloseFiles.Count>0){
  85. // Except(sp.CloseFiles,allOpened);
  86. notOpen =new List<string>(sp.CloseFiles.Except(allOpened,StringComparer.CurrentCultureIgnoreCase).ToList().ToArray());
  87. opened = new List<string>(sp.CloseFiles.Except(notOpen,StringComparer.CurrentCultureIgnoreCase).ToList().ToArray());
  88. sp.CloseFiles = new List<string>(notOpen);
  89. sp.OpenFiles = new List<string>(opened);
  90. }
  91. TaskList tl = new TaskList();
  92. /*if(opened.Count>0){
  93. SearchPattern spO = sp.Clone();
  94. spO.OpenFiles = new List<string>(opened);
  95. FindInOpenFileTask rt = new FindInOpenFileTask();
  96. rt.Initialize(spO);
  97. tl.TasksList.Clear();
  98. tl.TasksList.Add(rt);
  99. sp.CloseFiles = new List<string>(notOpen);
  100. }*/
  101. // find replace in closed files
  102. FindReplaceTask ft = new FindReplaceTask();
  103. //ReplaceTask ft = new ReplaceTask();
  104. ft.Initialize(sp);
  105. tl.TasksList.Add(ft);
  106. MainClass.MainWindow.RunSecondaryTaskList(tl, MainClass.MainWindow.FindOutputWritte,false);
  107. }
  108. public void SetFocus(){
  109. entrExpresion.GrabFocus();
  110. }
  111. public void SetFindText(string text){
  112. if(!String.IsNullOrEmpty(text)){
  113. ignoreTextChange = true;
  114. entrExpresion.Text = text;
  115. }
  116. }
  117. protected virtual void OnEntrExpresionChanged(object sender, System.EventArgs e)
  118. {
  119. if(cbPlace.Active != 0) return;
  120. string expresion = entrExpresion.Text;
  121. if (!String.IsNullOrEmpty(expresion)) {
  122. SearchPattern sp = GetSearchPattern();
  123. if(cbPlace.Active == 0){
  124. if(!ignoreTextChange){
  125. MainClass.MainWindow.EditorNotebook.Search(sp);
  126. } else {
  127. MainClass.MainWindow.EditorNotebook.SetSearchExpresion(sp);
  128. ignoreTextChange = false;
  129. }
  130. }
  131. }
  132. }
  133. protected virtual void OnBtnReplaceClicked(object sender, System.EventArgs e)
  134. {
  135. string expresion = entrExpresion.Text;
  136. string replaceExpresion = entrReplaceText.Text;
  137. if (String.IsNullOrEmpty(expresion))
  138. return;
  139. if (String.IsNullOrEmpty(replaceExpresion))
  140. return;
  141. SearchPattern sp = GetSearchPattern();
  142. sp.ReplaceExpresion = replaceExpresion;
  143. if(cbPlace.Active == 0){
  144. MainClass.MainWindow.EditorNotebook.Replace(sp);
  145. }else {
  146. /* TaskList tl = new TaskList();
  147. FindTask ft = new FindTask();
  148. ft.Initialize(sp);
  149. tl.TasksList.Clear();
  150. tl.TasksList.Add(ft);
  151. MainClass.MainWindow.RunSecondaryTaskList(tl, MainClass.MainWindow.FindOutputWritte);*/
  152. }
  153. }
  154. protected virtual void OnBtnReplaceAllClicked(object sender, System.EventArgs e)
  155. {
  156. string expresion = entrExpresion.Text;
  157. string replaceExpresion = entrReplaceText.Text;
  158. if (String.IsNullOrEmpty(expresion))
  159. return;
  160. if (String.IsNullOrEmpty(replaceExpresion))
  161. return;
  162. SearchPattern sp = GetSearchPattern();
  163. sp.ReplaceExpresion = replaceExpresion;
  164. if(cbPlace.Active == 0){
  165. MainClass.MainWindow.EditorNotebook.ReplaceAll(sp);
  166. }else {
  167. StartFindReplaceInFiles(sp);
  168. }
  169. }
  170. protected virtual void OnEntrExpresionKeyReleaseEvent(object o, Gtk.KeyReleaseEventArgs args)
  171. {
  172. //if(cbPlace.Active != 0) return;
  173. if (args.Event.Key == Gdk.Key.Return) {
  174. string expresion = entrExpresion.Text;
  175. if (!String.IsNullOrEmpty(expresion)) {
  176. SearchPattern sp = GetSearchPattern();
  177. if(cbPlace.Active == 0){
  178. MainClass.MainWindow.EditorNotebook.SearchNext();
  179. }
  180. else {
  181. sp.ReplaceExpresion = null;
  182. StartFindReplaceInFiles(sp);
  183. }
  184. }
  185. }
  186. }
  187. protected void OnEntrReplaceTextKeyReleaseEvent (object o, Gtk.KeyReleaseEventArgs args)
  188. {
  189. if (String.IsNullOrEmpty(entrExpresion.Text))
  190. return;
  191. if (String.IsNullOrEmpty(entrReplaceText.Text))
  192. return;
  193. //if(cbPlace.Active != 0) return;
  194. if (args.Event.Key == Gdk.Key.Return) {
  195. string expresion = entrExpresion.Text;
  196. if (!String.IsNullOrEmpty(expresion)) {
  197. SearchPattern sp = GetSearchPattern();
  198. sp.ReplaceExpresion = entrReplaceText.Text;
  199. if(cbPlace.Active == 0){
  200. MainClass.MainWindow.EditorNotebook.Replace(sp);
  201. }
  202. else {
  203. StartFindReplaceInFiles(sp);
  204. }
  205. }
  206. }
  207. }
  208. protected virtual void OnChbWholeWordsToggled (object sender, System.EventArgs e)
  209. {
  210. SetSearch();
  211. }
  212. protected virtual void OnChbCaseSensitiveToggled (object sender, System.EventArgs e)
  213. {
  214. SetSearch();
  215. }
  216. protected void OnCbPlaceChanged (object sender, System.EventArgs e)
  217. {
  218. if(cbPlace.Active!= 0){
  219. btnReplace.Sensitive = false;
  220. } else {
  221. btnReplace.Sensitive = true;
  222. SetSearch();
  223. }
  224. }
  225. }
  226. }