//
// Copyright 2014 Perforce Software Inc.
//
using Perforce.Helper;
using Perforce.ViewModel;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace Perforce.View {
///
/// Interaction logic for SidebarSelector.xaml
///
public partial class SidebarSelector : UserControl {
private SidebarSelectorViewModelBase _model;
private Brush _contentBackground;
private SidebarView _view;
public SidebarSelector() {
InitializeComponent();
}
public string LabelText {
set {
if(_model != null) _model.LabelText = value;
}
}
public SidebarView SidbebarView {
get { return _view; }
set { _view = value; }
}
public Brush ContentBackground {
get { return _contentBackground; }
set { _contentBackground = value; }
}
public bool ShowCount {
get { return _model.ShowCount; }
set { if(_model != null) _model.ShowCount = value; }
}
public bool IsEditable {
get { return _model.IsEditable; }
set { if (_model != null) _model.IsEditable = value; }
}
public string Type {
set {
switch (value.ToUpper()) {
case "WORKSPACE":
_view = new WorkspaceView();
_model = new WorkspaceSelectorViewModel(this);
break;
case "SERVER":
_view = new ServerView();
_model = new ServerSelectorViewModel(this);
break;
case "PENDING":
_view = new PendingView();
_model = new PendingSelectorViewModel(this);
break;
case "RECENT":
_view = new RecentView();
_model = new RecentSelectorViewModel(this);
break;
case "TRASH":
_view = new TrashView();
_model = new TrashSelectorViewModel(this);
break;
case "FAVORITE":
_model = new FavoritesViewModel(this);
_view = new FavoritesView(_model as FavoritesViewModel);
break;
case "SEARCH":
_model = new SearchSelectorViewModel(this);
_view = new SearchView(_model as SearchSelectorViewModel);
break;
}
this.DataContext = _model;
}
}
public SELECTOR_TYPE SelectorType {
get {
if (_model != null) {
return _model.SelectorType;
} else {
return SELECTOR_TYPE.NONE;
}
}
}
public SidebarSelectorViewModelBase Model {
get {
return _model;
}
set {
_model = value;
this.DataContext = _model;
}
}
private void Detected_MouseClick(object sender, RoutedEventArgs e) {
if (sender == null) {
return;
}
SidebarSelector currentSidebarItem = (sender as SidebarSelector);
var found = false;
var parent = VisualTreeHelper.GetParent(currentSidebarItem);
while (parent != null) {
if (parent.ReadLocalValue(GroupAttribute.NameProperty) != DependencyProperty.UnsetValue) {
found = true;
break;
}
parent = VisualTreeHelper.GetParent(parent);
}
if (found) {
foreach (SidebarSelector child in UIHelper.FindVisualChildren(parent)) {
child.Model.IsSelected = false;
}
}
currentSidebarItem.Model.IsSelected = true;
// notify application that label was clicked
MainWindow main = App.Current.MainWindow as MainWindow;
main.Sidebar_Label_Clicked(currentSidebarItem, e);
}
private void MenuItem_Click(object sender, RoutedEventArgs e) {
MenuItem mi = sender as MenuItem;
switch (mi.Header.ToString().ToUpper()) {
case ("EDIT"):
EditBox.Text = _model.LabelText;
EditBox.Visibility = System.Windows.Visibility.Visible;
MainLabel.Visibility = System.Windows.Visibility.Hidden;
break;
case ("DELETE"):
break;
}
}
private void EditBox_KeyDown(object sender, KeyEventArgs e) {
if (e.Key == Key.Return) {
var text = EditBox.Text.Trim();
if (text.Length > 0 && !_model.LabelText.Equals(text, System.StringComparison.CurrentCultureIgnoreCase)) {
_model.LabelText = text;
}
EditBox.Visibility = System.Windows.Visibility.Hidden;
MainLabel.Visibility = System.Windows.Visibility.Visible;
}
}
}
}