PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/HgSccHelper/UI/TagsWindow.xaml.cs

https://bitbucket.org/zzsergant/hgsccpackage/
C# | 401 lines | 296 code | 70 blank | 35 comment | 56 complexity | bfcea0ec6b6c3e2e28025c0c5d782a20 MD5 | raw file
Possible License(s): GPL-2.0
  1. //=========================================================================
  2. // Copyright 2009 Sergey Antonov <sergant_@mail.ru>
  3. //
  4. // This software may be used and distributed according to the terms of the
  5. // GNU General Public License version 2 as published by the Free Software
  6. // Foundation.
  7. //
  8. // See the file COPYING.TXT for the full text of the license, or see
  9. // http://www.gnu.org/licenses/gpl-2.0.txt
  10. //
  11. //=========================================================================
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Windows;
  17. using System.Windows.Controls;
  18. using System.Windows.Data;
  19. using System.Windows.Documents;
  20. using System.Windows.Input;
  21. using System.Windows.Media;
  22. using System.Windows.Media.Imaging;
  23. using System.Windows.Shapes;
  24. using System.Windows.Threading;
  25. using HgSccHelper.CommandServer;
  26. namespace HgSccHelper
  27. {
  28. public partial class TagsWindow : Window
  29. {
  30. //-----------------------------------------------------------------------------
  31. public string WorkingDir { get; set; }
  32. //------------------------------------------------------------------
  33. public string TargetRevision { get; set; }
  34. //------------------------------------------------------------------
  35. public UpdateContext UpdateContext { get; private set; }
  36. //------------------------------------------------------------------
  37. HgClient HgClient
  38. {
  39. get { return UpdateContext.Cache.HgClient; }
  40. }
  41. DispatcherTimer tag_timer;
  42. DispatcherTimer rev_timer;
  43. RevLogChangeDesc RevDesc { get; set; }
  44. RevLogChangeDesc TagDesc { get; set; }
  45. Dictionary<string, TagInfo> tag_map;
  46. public const string CfgPath = @"GUI\TagsWindow";
  47. CfgWindowPosition wnd_cfg;
  48. //------------------------------------------------------------------
  49. public TagsWindow()
  50. {
  51. wnd_cfg = new CfgWindowPosition(CfgPath, this, CfgWindowPositionOptions.PositionOnly);
  52. InitializeComponent();
  53. HgSccHelper.UI.ThemeManager.Instance.Subscribe(this);
  54. // Since WPF combo box does not provide TextChanged event
  55. // register it from edit text box through combo box template
  56. comboTag.Loaded += delegate
  57. {
  58. TextBox editTextBox = comboTag.Template.FindName("PART_EditableTextBox", comboTag) as TextBox;
  59. if (editTextBox != null)
  60. {
  61. editTextBox.TextChanged += OnComboTextChanged;
  62. }
  63. };
  64. comboTag.Unloaded += delegate
  65. {
  66. TextBox editTextBox = comboTag.Template.FindName("PART_EditableTextBox", comboTag) as TextBox;
  67. if (editTextBox != null)
  68. {
  69. editTextBox.TextChanged -= OnComboTextChanged;
  70. }
  71. };
  72. tag_map = new Dictionary<string, TagInfo>();
  73. UpdateContext = new UpdateContext();
  74. }
  75. //------------------------------------------------------------------
  76. private void Window_Loaded(object sender, RoutedEventArgs e)
  77. {
  78. Title = string.Format("Tags: '{0}'", WorkingDir);
  79. tag_timer = new DispatcherTimer();
  80. tag_timer.Interval = TimeSpan.FromMilliseconds(200);
  81. tag_timer.Tick += OnTagTimerTick;
  82. rev_timer = new DispatcherTimer();
  83. rev_timer.Interval = TimeSpan.FromMilliseconds(200);
  84. rev_timer.Tick += OnRevTimerTick;
  85. string target_rev = TargetRevision;
  86. if (string.IsNullOrEmpty(target_rev))
  87. {
  88. var parents_info = UpdateContext.Cache.ParentsInfo;
  89. if (parents_info == null)
  90. parents_info = HgClient.Parents();
  91. if (parents_info == null)
  92. {
  93. // error
  94. Close();
  95. return;
  96. }
  97. target_rev = parents_info.Rev.ToString();
  98. }
  99. textRev.Text = target_rev;
  100. if (UpdateContext.Cache.Tags != null)
  101. UpdateTags(UpdateContext.Cache.Tags);
  102. else
  103. UpdateTags();
  104. RefreshRev();
  105. if (RevDesc != null)
  106. {
  107. foreach (var tag in RevDesc.Tags)
  108. {
  109. if (tag.Name != "tip")
  110. {
  111. // Selecting target revision tag in combo box
  112. for(int i = 0; i < comboTag.Items.Count; ++i)
  113. {
  114. var item = (TagsComboItem)comboTag.Items[i];
  115. if (item.Name == tag.Name)
  116. {
  117. comboTag.SelectedIndex = i;
  118. break;
  119. }
  120. }
  121. break;
  122. }
  123. }
  124. }
  125. RefreshTag();
  126. comboTag.Focus();
  127. }
  128. //------------------------------------------------------------------
  129. private void UpdateTags(List<TagInfo> tags)
  130. {
  131. var current_tag = comboTag.Text;
  132. tag_map.Clear();
  133. comboTag.Items.Clear();
  134. int counter = 0;
  135. foreach (var tag in tags)
  136. {
  137. if (tag.Name != "tip")
  138. {
  139. var item = new TagsComboItem();
  140. item.GroupText = "Tag";
  141. item.Name = tag.Name;
  142. item.Rev = tag.Rev;
  143. item.SHA1 = tag.SHA1.ShortSHA1();
  144. item.Misc = tag.IsLocal ? "Local" : "";
  145. comboTag.Items.Add(item);
  146. tag_map[tag.Name] = tag;
  147. if (tag.Name == current_tag)
  148. comboTag.SelectedIndex = counter;
  149. counter++;
  150. }
  151. }
  152. }
  153. //------------------------------------------------------------------
  154. private void UpdateTags()
  155. {
  156. var tags = HgClient.Tags();
  157. UpdateTags(tags);
  158. }
  159. //------------------------------------------------------------------
  160. private void RefreshTag()
  161. {
  162. var tag_name = comboTag.Text;
  163. TagDesc = null;
  164. if (comboTag.SelectedItem != null)
  165. {
  166. var item = (TagsComboItem)comboTag.SelectedItem;
  167. if (item.Name == tag_name)
  168. TagDesc = HgClient.GetRevisionDesc(item.SHA1);
  169. }
  170. textTagDesc.Text = TagDesc.GetDescription();
  171. bool is_tip = (tag_name == "tip");
  172. btnAdd.IsEnabled = (!String.IsNullOrEmpty(tag_name) && !is_tip);
  173. btnRemove.IsEnabled = (TagDesc != null && !is_tip);
  174. }
  175. //------------------------------------------------------------------
  176. private void RefreshRev()
  177. {
  178. RevDesc = HgClient.GetRevisionDesc(textRev.Text);
  179. textRevDesc.Text = RevDesc.GetDescription();
  180. var tag_name = comboTag.Text;
  181. bool is_tip = (tag_name == "tip");
  182. btnAdd.IsEnabled = (!String.IsNullOrEmpty(tag_name) && !is_tip);
  183. }
  184. //------------------------------------------------------------------
  185. private void OnTagTimerTick(object o, EventArgs e)
  186. {
  187. tag_timer.Stop();
  188. RefreshTag();
  189. }
  190. //------------------------------------------------------------------
  191. private void OnRevTimerTick(object o, EventArgs e)
  192. {
  193. rev_timer.Stop();
  194. RefreshRev();
  195. }
  196. //------------------------------------------------------------------
  197. private void Window_Closed(object sender, EventArgs e)
  198. {
  199. tag_timer.Stop();
  200. tag_timer.Tick -= OnTagTimerTick;
  201. rev_timer.Stop();
  202. rev_timer.Tick -= OnRevTimerTick;
  203. }
  204. //------------------------------------------------------------------
  205. private void btnAdd_Click(object sender, RoutedEventArgs e)
  206. {
  207. if (String.IsNullOrEmpty(comboTag.Text))
  208. {
  209. MessageBox.Show("Invalid tag name", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
  210. return;
  211. }
  212. if (RevDesc == null)
  213. {
  214. MessageBox.Show("Invalid revision", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
  215. return;
  216. }
  217. bool replace_tag = checkReplace.IsChecked == true;
  218. var tag_name = comboTag.Text;
  219. if ( tag_map.ContainsKey(tag_name)
  220. && !replace_tag
  221. )
  222. {
  223. var msg = String.Format("A tag named '{0}' allready exists.\nAre you sure to replace it ?", tag_name);
  224. var result = MessageBox.Show(msg, "Question", MessageBoxButton.OKCancel, MessageBoxImage.Question);
  225. if (result != MessageBoxResult.OK)
  226. return;
  227. replace_tag = true;
  228. }
  229. var options = HgTagOptions.None;
  230. if (replace_tag)
  231. options |= HgTagOptions.Force;
  232. var commit_message = "";
  233. if (checkLocal.IsChecked == true)
  234. {
  235. options = HgTagOptions.Local;
  236. }
  237. else
  238. {
  239. if (checkCustomMessage.IsChecked == true)
  240. commit_message = textCommitMessage.Text;
  241. }
  242. if (!HgClient.AddTag(tag_name, RevDesc.SHA1, options, commit_message))
  243. {
  244. var msg = String.Format("An error occured while adding tag '{0}'", tag_name);
  245. MessageBox.Show(msg, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
  246. }
  247. else
  248. {
  249. var msg = String.Format("Tag '{0}' has been added", tag_name);
  250. MessageBox.Show(msg, "Information", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);
  251. UpdateContext.IsTagsChanged = true;
  252. if ((options & HgTagOptions.Local) != HgTagOptions.Local)
  253. {
  254. UpdateContext.IsParentChanged = true;
  255. UpdateContext.IsBranchChanged = true;
  256. UpdateContext.IsBookmarksChanged = true;
  257. }
  258. UpdateTags();
  259. RefreshTag();
  260. RefreshRev();
  261. }
  262. }
  263. //------------------------------------------------------------------
  264. private void Cancel_Click(object sender, RoutedEventArgs e)
  265. {
  266. Close();
  267. }
  268. //------------------------------------------------------------------
  269. private void OnComboTextChanged(object sender, TextChangedEventArgs e)
  270. {
  271. tag_timer.Start();
  272. btnAdd.IsEnabled = false;
  273. btnRemove.IsEnabled = false;
  274. }
  275. //------------------------------------------------------------------
  276. private void btnRemove_Click(object sender, RoutedEventArgs e)
  277. {
  278. if (TagDesc == null)
  279. {
  280. MessageBox.Show("Invalid tag name", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
  281. return;
  282. }
  283. var tag_name = comboTag.Text;
  284. var options = HgTagOptions.None;
  285. var tag_info = tag_map[tag_name];
  286. if (tag_info.IsLocal)
  287. options |= HgTagOptions.Local;
  288. if (!HgClient.RemoveTag(tag_name, options))
  289. {
  290. var msg = String.Format("An error occured while removing tag '{0}'", tag_name);
  291. MessageBox.Show(msg, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
  292. }
  293. else
  294. {
  295. var msg = String.Format("Tag '{0}' has been removed", tag_name);
  296. MessageBox.Show(msg, "Information", MessageBoxButton.OK, MessageBoxImage.Information);
  297. UpdateContext.IsTagsChanged = true;
  298. if ((options & HgTagOptions.Local) != HgTagOptions.Local)
  299. {
  300. UpdateContext.IsParentChanged = true;
  301. UpdateContext.IsBranchChanged = true;
  302. UpdateContext.IsBookmarksChanged = true;
  303. }
  304. UpdateTags();
  305. RefreshTag();
  306. RefreshRev();
  307. }
  308. }
  309. //------------------------------------------------------------------
  310. private void targetRev_TextChanged(object sender, TextChangedEventArgs e)
  311. {
  312. rev_timer.Start();
  313. btnAdd.IsEnabled = false;
  314. }
  315. //------------------------------------------------------------------
  316. private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
  317. {
  318. if (e.Key == Key.Escape)
  319. Close();
  320. }
  321. }
  322. //------------------------------------------------------------------
  323. class TagsComboItem
  324. {
  325. public string GroupText { get; set; }
  326. public string Name { get; set; }
  327. public int Rev { get; set; }
  328. public string SHA1 { get; set; }
  329. public string Misc { get; set; }
  330. }
  331. }