//
// Copyright 2014 Perforce Software Inc.
//
using Perforce.Helper;
using Perforce.Model;
using Perforce.P4;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace Perforce.View {
///
/// Interaction logic for VersionInfo.xaml
///
public partial class VersionInfo : UserControl {
private string _filePath;
private FileMetaData _headMetaData;
public VersionInfo(string filePath) {
InitializeComponent();
_filePath = filePath;
var helper = Utility.GetPerforceHelper();
var fileHistory = helper.GetFileHistory(filePath);
var versionItems = new List();
if (fileHistory != null) {
foreach (var h in fileHistory) {
var md = helper.GetFileMetaData(h.DepotPath, h.Revision);
versionItems.Add(new VersionItem { History = h, MetaData = md });
}
}
RevisionList.ItemsSource = versionItems;
_headMetaData = helper.GetFileMetaData(path: filePath);
}
private void VersionInfo_Loaded(object sender, RoutedEventArgs e) {
UIHelper.ScrollToRight();
}
private void RevisionList_Selected(object sender, RoutedEventArgs e) {
Console.WriteLine(sender.GetType().ToString());
}
private void ReadOnly_Button_Click(object sender, RoutedEventArgs e) {
var item = RevisionList.SelectedItem as VersionItem;
CommandHelper.OpenFileFromServer(item.MetaData.DepotPath.Path, item.Revision);
}
private bool CanRollback {
get {
return MetadataHelper.CanEdit(_headMetaData);
}
}
private void Rollback_Button_Click(object sender, RoutedEventArgs e) {
var item = RevisionList.SelectedItem as VersionItem;
var message = string.Format("Are you sure you want to rollback\n{0}\nto revision {1}?", item.MetaData.DepotPath, item.Revision);
var main = App.Current.MainWindow as MainWindow;
var dialog = main.DialogManager.CreateMessageDialog(message, Technewlogic.WpfDialogManagement.DialogMode.OkCancel);
dialog.VerticalDialogAlignment = VerticalAlignment.Top;
dialog.Ok = () => { DoRollback(item); };
dialog.Cancel = RollbackCancelled;
dialog.Show();
}
private void DoRollback(VersionItem item) {
Utility.StopBackgroundProcesses();
var helper = Utility.GetPerforceHelper();
var results = helper.RollbackFileToRevision(item.MetaData.DepotPath.Path, item.Revision);
if (results != null) {
RevisionList.ItemsSource = helper.GetFileHistory(_filePath);
var message = string.Format("{0}\nrolled back to revision {1}", item.MetaData.DepotPath, item.Revision);
UIHelper.ShowTimedMessage(message, 0.5);
}
Utility.StartBackgroundProcesses();
}
private void RollbackCancelled() {
UIHelper.ShowTimedMessage("Rollback cancelled", 0.5);
}
}
public class TruncateTextConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
var original = value.ToString().Trim();
var truncated = original;
if (original.Length > 20) {
truncated = string.Format("{0}...", original.Substring(0, 20));
}
return truncated;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
var x = "TEST";
return null;
}
}
}