Ingen beskrivning
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BranchesTab.cs 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEditor.IMGUI.Controls;
  5. using UnityEngine;
  6. using Codice.CM.Common;
  7. using Codice.Client.Common.Threading;
  8. using PlasticGui;
  9. using PlasticGui.WorkspaceWindow;
  10. using PlasticGui.WorkspaceWindow.QueryViews;
  11. using PlasticGui.WorkspaceWindow.QueryViews.Branches;
  12. using PlasticGui.WorkspaceWindow.Update;
  13. using Unity.PlasticSCM.Editor.AssetUtils;
  14. using Unity.PlasticSCM.Editor.UI;
  15. using Unity.PlasticSCM.Editor.UI.Progress;
  16. using Unity.PlasticSCM.Editor.UI.Tree;
  17. using Unity.PlasticSCM.Editor.Views.Branches.Dialogs;
  18. using Unity.PlasticSCM.Editor.Views.Changesets;
  19. namespace Unity.PlasticSCM.Editor.Views.Branches
  20. {
  21. internal class BranchesTab :
  22. IRefreshableView,
  23. IBranchMenuOperations,
  24. IQueryRefreshableView
  25. {
  26. internal BranchesListView Table { get { return mBranchesListView; } }
  27. internal IBranchMenuOperations Operations { get { return this; } }
  28. internal BranchesTab(
  29. WorkspaceInfo wkInfo,
  30. IWorkspaceWindow workspaceWindow,
  31. IViewSwitcher viewSwitcher,
  32. IMergeViewLauncher mergeViewLauncher,
  33. IUpdateReport updateReport,
  34. NewIncomingChangesUpdater developerNewIncomingChangesUpdater,
  35. EditorWindow parentWindow)
  36. {
  37. mWkInfo = wkInfo;
  38. mParentWindow = parentWindow;
  39. mProgressControls = new ProgressControlsForViews();
  40. mProgressControls = new ProgressControlsForViews();
  41. BuildComponents(
  42. wkInfo,
  43. workspaceWindow,
  44. viewSwitcher,
  45. mergeViewLauncher,
  46. updateReport,
  47. developerNewIncomingChangesUpdater,
  48. parentWindow);
  49. ((IRefreshableView)this).Refresh();
  50. }
  51. internal void Update()
  52. {
  53. mProgressControls.UpdateProgress(mParentWindow);
  54. }
  55. internal void OnGUI()
  56. {
  57. DoActionsToolbar(mProgressControls);
  58. DoBranchesArea(
  59. mBranchesListView,
  60. mProgressControls.IsOperationRunning());
  61. }
  62. internal void SetWorkingObjectInfo(WorkingObjectInfo homeInfo)
  63. {
  64. lock(mLock)
  65. {
  66. mLoadedBranchId = homeInfo.BranchInfo.BranchId;
  67. }
  68. mBranchesListView.SetLoadedBranchId(mLoadedBranchId);
  69. }
  70. static void DoBranchesArea(
  71. BranchesListView branchesListView,
  72. bool isOperationRunning)
  73. {
  74. EditorGUILayout.BeginVertical();
  75. GUI.enabled = !isOperationRunning;
  76. Rect rect = GUILayoutUtility.GetRect(0, 100000, 0, 100000);
  77. branchesListView.OnGUI(rect);
  78. GUI.enabled = true;
  79. EditorGUILayout.EndVertical();
  80. }
  81. internal void DrawSearchFieldForBranchesTab()
  82. {
  83. DrawSearchField.For(
  84. mSearchField,
  85. mBranchesListView,
  86. UnityConstants.SEARCH_FIELD_WIDTH);
  87. }
  88. internal void OnDisable()
  89. {
  90. mSearchField.downOrUpArrowKeyPressed -=
  91. SearchField_OnDownOrUpArrowKeyPressed;
  92. TreeHeaderSettings.Save(
  93. mBranchesListView.multiColumnHeader.state,
  94. UnityConstants.BRANCHES_TABLE_SETTINGS_NAME);
  95. }
  96. void IRefreshableView.Refresh()
  97. {
  98. string query = GetBranchesQuery(mDateFilter);
  99. FillBranches(mWkInfo,
  100. query,
  101. BranchesSelection.GetSelectedRepObjectInfos(mBranchesListView));
  102. }
  103. //IQueryRefreshableView
  104. public void RefreshAndSelect(RepObjectInfo repObj)
  105. {
  106. string query = GetBranchesQuery(mDateFilter);
  107. FillBranches(mWkInfo,
  108. query,
  109. new List<RepObjectInfo> { repObj });
  110. }
  111. void FillBranches(WorkspaceInfo wkInfo, string query, List<RepObjectInfo> branchesToSelect)
  112. {
  113. if (mIsRefreshing)
  114. return;
  115. mIsRefreshing = true;
  116. int defaultRow = TableViewOperations.
  117. GetFirstSelectedRow(mBranchesListView);
  118. ((IProgressControls)mProgressControls).ShowProgress(
  119. PlasticLocalization.GetString(
  120. PlasticLocalization.Name.LoadingBranches));
  121. ViewQueryResult queryResult = null;
  122. IThreadWaiter waiter = ThreadWaiter.GetWaiter();
  123. waiter.Execute(
  124. /*threadOperationDelegate*/ delegate
  125. {
  126. long loadedBranchId = GetLoadedBranchId(wkInfo);
  127. lock(mLock)
  128. {
  129. mLoadedBranchId = loadedBranchId;
  130. }
  131. queryResult = new ViewQueryResult(
  132. PlasticGui.Plastic.API.FindQuery(wkInfo, query));
  133. },
  134. /*afterOperationDelegate*/ delegate
  135. {
  136. try
  137. {
  138. if (waiter.Exception != null)
  139. {
  140. ExceptionsHandler.DisplayException(waiter.Exception);
  141. return;
  142. }
  143. UpdateBranchesList(
  144. mBranchesListView,
  145. queryResult,
  146. mLoadedBranchId);
  147. int branchesCount = GetBranchesCount(queryResult);
  148. if (branchesCount == 0)
  149. {
  150. return;
  151. }
  152. BranchesSelection.SelectBranches(
  153. mBranchesListView, branchesToSelect, defaultRow);
  154. }
  155. finally
  156. {
  157. ((IProgressControls)mProgressControls).HideProgress();
  158. mIsRefreshing = false;
  159. }
  160. });
  161. }
  162. static long GetLoadedBranchId(WorkspaceInfo wkInfo)
  163. {
  164. BranchInfo brInfo = PlasticGui.Plastic.API.GetWorkingBranch(wkInfo);
  165. if (brInfo != null)
  166. return brInfo.BranchId;
  167. return -1;
  168. }
  169. static void UpdateBranchesList(
  170. BranchesListView branchesListView,
  171. ViewQueryResult queryResult,
  172. long loadedBranchId)
  173. {
  174. branchesListView.BuildModel(
  175. queryResult, loadedBranchId);
  176. branchesListView.Refilter();
  177. branchesListView.Sort();
  178. branchesListView.Reload();
  179. }
  180. internal static int GetBranchesCount(
  181. ViewQueryResult queryResult)
  182. {
  183. if (queryResult == null)
  184. return 0;
  185. return queryResult.Count();
  186. }
  187. internal static string GetBranchesQuery(DateFilter dateFilter)
  188. {
  189. if (dateFilter.FilterType == DateFilter.Type.AllTime)
  190. return QueryConstants.BranchesBeginningQuery;
  191. string whereClause = QueryConstants.GetDateWhereClause(
  192. dateFilter.GetTimeAgo());
  193. return string.Format("{0} {1}",
  194. QueryConstants.BranchesBeginningQuery,
  195. whereClause);
  196. }
  197. internal void DrawDateFilter()
  198. {
  199. GUI.enabled = !mProgressControls.IsOperationRunning();
  200. EditorGUI.BeginChangeCheck();
  201. mDateFilter.FilterType = (DateFilter.Type)
  202. EditorGUILayout.EnumPopup(
  203. mDateFilter.FilterType,
  204. EditorStyles.toolbarDropDown,
  205. GUILayout.Width(100));
  206. if (EditorGUI.EndChangeCheck())
  207. {
  208. EnumPopupSetting<DateFilter.Type>.Save(
  209. mDateFilter.FilterType,
  210. UnityConstants.BRANCHES_DATE_FILTER_SETTING_NAME);
  211. ((IRefreshableView)this).Refresh();
  212. }
  213. GUI.enabled = true;
  214. }
  215. void SearchField_OnDownOrUpArrowKeyPressed()
  216. {
  217. mBranchesListView.SetFocusAndEnsureSelectedItem();
  218. }
  219. static void DoActionsToolbar(
  220. ProgressControlsForViews progressControls)
  221. {
  222. EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
  223. if (progressControls.IsOperationRunning())
  224. {
  225. DrawProgressForViews.ForIndeterminateProgress(
  226. progressControls.ProgressData);
  227. }
  228. GUILayout.FlexibleSpace();
  229. EditorGUILayout.EndHorizontal();
  230. }
  231. void BuildComponents(
  232. WorkspaceInfo wkInfo,
  233. IWorkspaceWindow workspaceWindow,
  234. IViewSwitcher viewSwitcher,
  235. IMergeViewLauncher mergeViewLauncher,
  236. IUpdateReport updateReport,
  237. NewIncomingChangesUpdater developerNewIncomingChangesUpdater,
  238. EditorWindow parentWindow)
  239. {
  240. mSearchField = new SearchField();
  241. mSearchField.downOrUpArrowKeyPressed += SearchField_OnDownOrUpArrowKeyPressed;
  242. DateFilter.Type dateFilterType =
  243. EnumPopupSetting<DateFilter.Type>.Load(
  244. UnityConstants.BRANCHES_DATE_FILTER_SETTING_NAME,
  245. DateFilter.Type.LastMonth);
  246. mDateFilter = new DateFilter(dateFilterType);
  247. BranchesListHeaderState headerState =
  248. BranchesListHeaderState.GetDefault();
  249. TreeHeaderSettings.Load(headerState,
  250. UnityConstants.BRANCHES_TABLE_SETTINGS_NAME,
  251. (int)BranchesListColumn.CreationDate, false);
  252. mBranchesListView = new BranchesListView(
  253. headerState,
  254. BranchesListHeaderState.GetColumnNames(),
  255. new BranchesViewMenu(this),
  256. sizeChangedAction: OnBranchesListViewSizeChanged);
  257. mBranchesListView.Reload();
  258. mBranchOperations = new BranchOperations(
  259. wkInfo,
  260. workspaceWindow,
  261. viewSwitcher,
  262. mergeViewLauncher,
  263. this,
  264. ViewType.BranchesView,
  265. mProgressControls,
  266. updateReport,
  267. new ContinueWithPendingChangesQuestionerBuilder(viewSwitcher, parentWindow),
  268. developerNewIncomingChangesUpdater);
  269. }
  270. void OnBranchesListViewSizeChanged()
  271. {
  272. if (!mShouldScrollToSelection)
  273. return;
  274. mShouldScrollToSelection = false;
  275. TableViewOperations.ScrollToSelection(mBranchesListView);
  276. }
  277. int IBranchMenuOperations.GetSelectedBranchesCount()
  278. {
  279. return BranchesSelection.GetSelectedBranchesCount(mBranchesListView);
  280. }
  281. void IBranchMenuOperations.CreateBranch()
  282. {
  283. RepositorySpec repSpec = BranchesSelection.GetSelectedRepository(mBranchesListView);
  284. BranchInfo branchInfo = BranchesSelection.GetSelectedBranch(mBranchesListView);
  285. BranchCreationData branchCreationData = CreateBranchDialog.CreateBranchFromLastParentBranchChangeset(
  286. mParentWindow,
  287. repSpec,
  288. branchInfo);
  289. mBranchOperations.CreateBranch(
  290. branchCreationData,
  291. RefreshAsset.BeforeLongAssetOperation,
  292. RefreshAsset.AfterLongAssetOperation);
  293. }
  294. void IBranchMenuOperations.CreateTopLevelBranch() { }
  295. void IBranchMenuOperations.SwitchToBranch()
  296. {
  297. RepositorySpec repSpec = BranchesSelection.GetSelectedRepository(mBranchesListView);
  298. BranchInfo branchInfo = BranchesSelection.GetSelectedBranch(mBranchesListView);
  299. mBranchOperations.SwitchToBranch(
  300. repSpec,
  301. branchInfo,
  302. RefreshAsset.BeforeLongAssetOperation,
  303. RefreshAsset.AfterLongAssetOperation);
  304. }
  305. void IBranchMenuOperations.MergeBranch() { }
  306. void IBranchMenuOperations.CherrypickBranch() { }
  307. void IBranchMenuOperations.MergeToBranch() { }
  308. void IBranchMenuOperations.PullBranch() { }
  309. void IBranchMenuOperations.PullRemoteBranch() { }
  310. void IBranchMenuOperations.SyncWithGit() { }
  311. void IBranchMenuOperations.PushBranch() { }
  312. void IBranchMenuOperations.DiffBranch() { }
  313. void IBranchMenuOperations.DiffWithAnotherBranch() { }
  314. void IBranchMenuOperations.ViewChangesets() { }
  315. void IBranchMenuOperations.RenameBranch()
  316. {
  317. RepositorySpec repSpec = BranchesSelection.GetSelectedRepository(mBranchesListView);
  318. BranchInfo branchInfo = BranchesSelection.GetSelectedBranch(mBranchesListView);
  319. BranchRenameData branchRenameData = RenameBranchDialog.GetBranchRenameData(
  320. repSpec,
  321. branchInfo,
  322. mParentWindow);
  323. mBranchOperations.RenameBranch(branchRenameData);
  324. }
  325. void IBranchMenuOperations.DeleteBranch()
  326. {
  327. RepositorySpec repSpec = BranchesSelection.GetSelectedRepository(mBranchesListView);
  328. List<RepositorySpec> repositories = BranchesSelection.GetSelectedRepositories(mBranchesListView);
  329. List<BranchInfo> branchesToDelete = BranchesSelection.GetSelectedBranches(mBranchesListView);
  330. mBranchOperations.DeleteBranch(repositories, branchesToDelete);
  331. }
  332. void IBranchMenuOperations.CreateCodeReview() { }
  333. void IBranchMenuOperations.ViewPermissions() { }
  334. SearchField mSearchField;
  335. bool mIsRefreshing;
  336. DateFilter mDateFilter;
  337. bool mShouldScrollToSelection;
  338. BranchesListView mBranchesListView;
  339. BranchOperations mBranchOperations;
  340. long mLoadedBranchId = -1;
  341. object mLock = new object();
  342. readonly WorkspaceInfo mWkInfo;
  343. readonly ProgressControlsForViews mProgressControls;
  344. readonly EditorWindow mParentWindow;
  345. }
  346. }