using Perforce.Helper; using Perforce.P4; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace Perforce.ViewModel { public class DirectoryItemViewModel : ViewModelBase { public enum ItemStatus { Untracked, Add, Edit, Delete, Move, Mapped, Mixed } private FileMetaData _metadata; public FileMetaData Metadata { get { return _metadata; } set { _metadata = value; RefreshAllBindings(); } } public void UpdateMetadata() { ListingHelper.UpdateDirectoryItem(this); } public void RefreshAllBindings() { OnPropertyChanged(""); } private ResourceDictionary _resources; private string _labelText; private bool _isDirectory; private bool _isSelected; private int _viewerColumn; private ItemStatus _status; private ObservableCollection<FrameworkElement> _contextMenu; public Brush DefaultBackground { get { return UIHelper.GetCurrentSelector().ContentBackground; } } public ResourceDictionary Resources { get { return _resources; } set { _resources = value; } } public string DepotPath { get { return _metadata.DepotPath != null ? _metadata.DepotPath.Path : null; } } public string ClientPath { get { return _metadata.ClientPath != null ? _metadata.ClientPath.Path : null; } } public bool IsEdit { get { return _metadata.Action.Equals(FileAction.Edit); } } public string Icon { get { StringBuilder resource = new StringBuilder(); if (_isDirectory) { resource.Append("Folder"); if (_metadata.IsMapped) { resource.Append("Mapped"); } else { resource.Append("Untracked"); } } else { resource.Append("File"); if (IsMapped) { var other = ""; var action = _metadata.Action; if (_metadata.OtherActions != null && _metadata.OtherActions.Count > 0) { other = "Other"; action = _metadata.OtherActions[0]; } switch (action) { case FileAction.None: break; case FileAction.Add: resource.Append("Add"); break; case FileAction.Edit: resource.Append("Edit"); break; case FileAction.Delete: resource.Append("Delete"); break; case FileAction.Move: resource.Append("Move"); break; case FileAction.MoveAdd: resource.Append("MoveAdd"); break; case FileAction.MoveDelete: resource.Append("MoveDelete"); break; } resource.Append(other); } else { resource.Append("Untracked"); } } if (_isSelected) { resource.Append("Selected"); } var img = _resources[resource.ToString()] as Image; if (img != null) { return img.Source.ToString(); } else { return "/Perforce;component/Resources/Icons/IconFile.png"; } } } public bool IsMapped { get { return _metadata.IsMapped; } } public bool IsStale { get { return _metadata.IsStale; } } public string LabelText { get { return _labelText; } set { _labelText = value; OnPropertyChanged("LabelText"); } } public bool IsDirectory { get { return _isDirectory; } set { _isDirectory = value; OnPropertyChanged("IsDirectory"); } } public bool IsSelected { get { return _isSelected; } set { _isSelected = value; OnPropertyChanged("IsSelected"); OnPropertyChanged("Icon"); } } public ItemStatus Status { get { return _status; } set { _status = value; OnPropertyChanged("ItemStatus"); } } public int ViewerColumn { get { return _viewerColumn; } set { _viewerColumn = value; OnPropertyChanged("ViewerColumn"); } } public void RefreshData() { //var x = new FileMetaData(); //var helper = Utility.GetProperty(Constants.PERFORCE_HELPER) as PerforceHelper; //if (IsDirectory) { // // since directories don't have metadata in Perforce, we just have to // // get basic information about them // IsMapped = helper.IsDirectoryMapped(DepotPath); //} else { // // files have tons of metadata, so let's go to town // var paths = new List<string>(); // paths.Add(DepotPath); // var list = helper.GetFileMetaData(paths); // if (list != null && list.Count == 1) { // var md = list[0]; // ClientPath = md.ClientPath.Path; // IsMapped = md.IsMapped; // switch (md.Action) { // case Perforce.P4.FileAction.Add: // Status = ItemStatus.Add; // break; // case Perforce.P4.FileAction.Edit: // Status = ItemStatus.Edit; // break; // default: // Console.WriteLine("### ACTION: " + md.Action.ToString()); // break; // } // } //} } public ObservableCollection<FrameworkElement> ItemContextMenu { get { _contextMenu = new ObservableCollection<FrameworkElement>(); _contextMenu.Add(new MenuItem { Header = LabelText, IsEnabled = false }); _contextMenu.Add(new Separator()); var currentSelector = UIHelper.GetCurrentSelector(); if (currentSelector != null) { switch (currentSelector.SelectorType) { case SELECTOR_TYPE.WORKSPACE: ConfigureWorkspaceContextMenu(); break; case SELECTOR_TYPE.SERVER: ConfigureServerContextMenu(); break; case SELECTOR_TYPE.PENDING: ConfigurePendingContextMenu(); break; case SELECTOR_TYPE.RECENT: ConfigureRecentContextMenu(); break; case SELECTOR_TYPE.TRASH: ConfigureTrashContextMenu(); break; case SELECTOR_TYPE.FAVORITE: ConfigureFavoriteContextMenu(); break; case SELECTOR_TYPE.TAG: ConfigureTagContextMenu(); break; } } return _contextMenu; } } private void ConfigureWorkspaceContextMenu() { _contextMenu.Add(new MenuItem { Header = "Open", IsEnabled = false }); _contextMenu.Add(new MenuItem { Header = "Open readonly", IsEnabled = false }); if (_isDirectory) { _contextMenu.Add(new MenuItem { Header = "Edit all files", IsEnabled = false }); } else { if (IsEdit) { _contextMenu.Add(new MenuItem { Header = "Revert changes", IsEnabled = true, Command = new DelegateCommand(RevertFile) }); } else { _contextMenu.Add(new MenuItem { Header = "Edit file", IsEnabled = true, Command = new DelegateCommand(CheckoutFile) }); } } _contextMenu.Add(new Separator()); _contextMenu.Add(new MenuItem { Header = "Show in Explorer", IsEnabled = IsMapped, Command = new DelegateCommand(ShowInExplorer) }); _contextMenu.Add(new MenuItem { Header = "Show Versions", IsEnabled = false }); _contextMenu.Add(new MenuItem { Header = "Copy link", IsEnabled = true, Command = new DelegateCommand(CopyLink) }); _contextMenu.Add(new Separator()); _contextMenu.Add(new MenuItem { Header = "Trash", IsEnabled = false }); } private void ConfigureServerContextMenu() { if (_isDirectory) { if (IsMapped) { _contextMenu.Add(new MenuItem { Header = "Unsync from computer", IsEnabled = true, Command = new DelegateCommand(UnsyncFromComputer) }); } else { _contextMenu.Add(new MenuItem { Header = "Sync with computer", IsEnabled = true, Command = new DelegateCommand(SyncWithComputer) }); } _contextMenu.Add(new MenuItem { Header = "Copy link", IsEnabled = true, Command = new DelegateCommand(CopyLink) }); } else { _contextMenu.Add(new MenuItem { Header = "Open", IsEnabled = false }); _contextMenu.Add(new MenuItem { Header = "Open readonly", IsEnabled = false }); _contextMenu.Add(new MenuItem { Header = "Open file from server", IsEnabled = true, Command = new DelegateCommand(OpenFromServer) }); _contextMenu.Add(new Separator()); _contextMenu.Add(new MenuItem { Header = "Show in Explorer", IsEnabled = IsMapped, Command = new DelegateCommand(ShowInExplorer) }); _contextMenu.Add(new MenuItem { Header = "Show Versions", IsEnabled = false }); _contextMenu.Add(new MenuItem { Header = "Copy link", IsEnabled = true, Command = new DelegateCommand(CopyLink) }); } } private void ConfigurePendingContextMenu() { _contextMenu.Add(new MenuItem { Header = "Open", IsEnabled = false }); _contextMenu.Add(new MenuItem { Header = "Open readonly", IsEnabled = false }); _contextMenu.Add(new MenuItem { Header = "Submit", IsEnabled = false }); _contextMenu.Add(new Separator()); _contextMenu.Add(new MenuItem { Header = "Show in Explorer", IsEnabled = false }); _contextMenu.Add(new MenuItem { Header = "Show versions", IsEnabled = false }); _contextMenu.Add(new MenuItem { Header = "Copy link", IsEnabled = true, Command = new DelegateCommand(CopyLink) }); _contextMenu.Add(new MenuItem { Header = "Revert changes", IsEnabled = false }); _contextMenu.Add(new Separator()); _contextMenu.Add(new MenuItem { Header = "Trash", IsEnabled = false }); } private void ConfigureRecentContextMenu() { _contextMenu.Add(new MenuItem { Header = "Open", IsEnabled = false }); _contextMenu.Add(new MenuItem { Header = "Open readonly", IsEnabled = false }); _contextMenu.Add(new MenuItem { Header = "Submit", IsEnabled = false }); _contextMenu.Add(new Separator()); _contextMenu.Add(new MenuItem { Header = "Show in Explorer", IsEnabled = false }); _contextMenu.Add(new MenuItem { Header = "Show versions", IsEnabled = false }); _contextMenu.Add(new MenuItem { Header = "Copy link", IsEnabled = true, Command = new DelegateCommand(CopyLink) }); _contextMenu.Add(new MenuItem { Header = "Revert changes", IsEnabled = false }); _contextMenu.Add(new Separator()); _contextMenu.Add(new MenuItem { Header = "Trash", IsEnabled = false }); } private void ConfigureTrashContextMenu() { _contextMenu.Add(new MenuItem { Header = "Open", IsEnabled = false }); _contextMenu.Add(new MenuItem { Header = "Open readonly", IsEnabled = false }); _contextMenu.Add(new MenuItem { Header = "Submit", IsEnabled = false }); _contextMenu.Add(new Separator()); _contextMenu.Add(new MenuItem { Header = "Show in Explorer", IsEnabled = false }); _contextMenu.Add(new MenuItem { Header = "Show versions", IsEnabled = false }); _contextMenu.Add(new MenuItem { Header = "Copy link", IsEnabled = false }); _contextMenu.Add(new MenuItem { Header = "Undo delete", IsEnabled = false }); } private void ConfigureFavoriteContextMenu() { } private void ConfigureTagContextMenu() { } #region COMMANDS private void SyncWithComputer() { UIHelper.SetBusyState(); var helper = Utility.GetProperty(Constants.PERFORCE_HELPER) as PerforceHelper; helper.IncludeInClient(DepotPath + "/..."); RefreshData(); OnPropertyChanged("ItemContextMenu"); } private void UnsyncFromComputer() { UIHelper.SetBusyState(); var helper = Utility.GetProperty(Constants.PERFORCE_HELPER) as PerforceHelper; helper.RemoveFromClient(DepotPath + "/..."); RefreshData(); OnPropertyChanged("ItemContextMenu"); } private void OpenFromServer() { CommandHelper.OpenFileFromServer(DepotPath); } private void ShowInExplorer() { CommandHelper.ShowFileInExplorer(ClientPath); } private void CheckoutFile() { var helper = Utility.GetProperty(Constants.PERFORCE_HELPER) as PerforceHelper; helper.CheckoutFiles(false, "DepotPath"); UpdateMetadata(); } private void DeleteFile() { var helper = Utility.GetProperty(Constants.PERFORCE_HELPER) as PerforceHelper; helper.DeleteFiles(DepotPath); UpdateMetadata(); } private void RevertFile() { var helper = Utility.GetProperty(Constants.PERFORCE_HELPER) as PerforceHelper; helper.RevertFiles(DepotPath); UpdateMetadata(); } private void CopyLink() { if (DepotPath != null) { Clipboard.SetText(string.Format("p4:{0}", DepotPath)); } } #endregion } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 15071 | alan_petersen |
Populate -o //guest/perforce_software/piper/... //guest/alan_petersen/piper/.... |
||
//guest/perforce_software/piper/windows/main/Perforce/ViewModel/DirectoryItemViewModel.cs | |||||
#1 | 11255 | alan_petersen | Rename/move file(s) | ||
//guest/perforce_software/piper/windows/Perforce/ViewModel/DirectoryItemViewModel.cs | |||||
#1 | 10761 | alan_petersen |
initial drop of Piper for Windows.... this version still has _many_ bugs (er... i mean "unintended features") but I will be updating it over the next week as more stability is added. |