using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Input; using System.Windows.Media; using BeWo.Core; using BeWo.ViewModel; using BS.Shared; using BS.Shared.DataContracts; using BS.Shared.DataContracts.ClientPartials; using BS.Shared.Extensions; namespace BeWo.View.Detail { public partial class SupportConceptGoalView : BeWoView { private SupportConceptVM _ViewModel; private TreeViewItem _SelectedGoalTreeNode; private ObservableCollection _SelectedGoals; public SupportConceptGoalView(SupportConceptVM pViewModel) { this.InitializeComponent(); SelectedGoals = GetSelectedGoalTree(pViewModel); treeview_Goals.ItemsSource = SelectedGoals; this.ViewModel = pViewModel; } private ObservableCollection GetSelectedGoalTree(SupportConceptVM pViewModel) { var goals = GetSelectedGoals(pViewModel); var copyList = new List(); foreach (var item in goals) { SupportConceptGoalTreeItem copy = CreateCopy(item); copy.IsExpanded = true; copyList.Add(copy); } ObservableCollection tree = CreateTree(copyList, pViewModel.CombinedGoalTree); return tree; } public ObservableCollection SelectedGoals { get { return _SelectedGoals; } set { _SelectedGoals = value; } } private ObservableCollection CreateTree(List copyList, ObservableCollection combinedGoalTree) { var tree = new ObservableCollection(); Dictionary totalOid2GoalDict = CreateGoalDictionary(combinedGoalTree); Dictionary resultTreeOid2GoalDict = new Dictionary(); foreach (var goal in copyList) { AddParentToTree(tree, goal, totalOid2GoalDict, resultTreeOid2GoalDict); } return tree; } private void AddParentToTree(ObservableCollection tree, SupportConceptGoalTreeItem goal, Dictionary totalOid2GoalDict, Dictionary resultTreeOid2GoalDict) { if (goal.GoalEntryDC.ParentOid.HasValue) { if (totalOid2GoalDict.ContainsKey(goal.GoalEntryDC.ParentOid.Value)) { SupportConceptGoalTreeItem parent = null; if (resultTreeOid2GoalDict.ContainsKey(goal.GoalEntryDC.ParentOid.Value)) { parent = resultTreeOid2GoalDict[goal.GoalEntryDC.ParentOid.Value]; } else { parent = CreateCopy(totalOid2GoalDict[goal.GoalEntryDC.ParentOid.Value]); parent.IsExpanded = true; resultTreeOid2GoalDict.Add(goal.GoalEntryDC.ParentOid.Value, parent); AddParentToTree(tree, parent, totalOid2GoalDict, resultTreeOid2GoalDict); } parent.Items.Add(goal); parent.Children.Add(goal); } } else { tree.Add(goal); } } private Dictionary CreateGoalDictionary(ObservableCollection combinedGoalTree) { var dict = new Dictionary(); AddGoalsToDict(combinedGoalTree, dict); return dict; } private void AddGoalsToDict(IList goals, Dictionary dict) { if (goals != null) { foreach (var item in goals) { if (!dict.ContainsKey(item.GoalEntryDC.ValueListEntryOid.Value)) { dict.Add(item.GoalEntryDC.ValueListEntryOid.Value, item); } AddGoalsToDict(item.Items, dict); } } } private SupportConceptGoalTreeItem CreateCopy(SupportConceptGoalTreeItem item) { var copy = new SupportConceptGoalTreeItem(); var entryDc = new ValueListEntryDC(); entryDc.Abbreviation = item.GoalEntryDC.Abbreviation; entryDc.Notice = item.GoalEntryDC.Notice; entryDc.TypeDescription = item.GoalEntryDC.TypeDescription; entryDc.ValueListEntryOid = item.GoalEntryDC.ValueListEntryOid; entryDc.ValueListEntryVersion = item.GoalEntryDC.ValueListEntryVersion; entryDc.Type = item.GoalEntryDC.Type; entryDc.ParentOid = item.GoalEntryDC.ParentOid; copy.GoalEntryDC = entryDc; copy.GoalEntryVM = new ValueListEntryVM(entryDc); return copy; } private ObservableCollection GetSelectedGoals(SupportConceptVM pViewModel) { var list = new ObservableCollection(); foreach (var item in pViewModel.CombinedGoalTree) { IterateThroughSelectedGoalsAndMassnahmen(item, list); } return list; } private void IterateThroughSelectedGoalsAndMassnahmen(SupportConceptGoalTreeItem item, ObservableCollection list) { if (item.IsChecked || (item.Items.Count == 0 && (item.GoalType == ValueListEntryType.SupportConceptIndividualGoalCategoryType || item.GoalType == ValueListEntryType.SupportConceptIndividualGoalType))) { list.Add(item); } foreach (var child in item.Items) { IterateThroughSelectedGoalsAndMassnahmen(child, list); } } public override bool IsDirty { get { return this.ViewModel != null ? this.ViewModel.IsDirty : false; } } public SupportConceptVM ViewModel { get { return this._ViewModel; } set { this._ViewModel = value; this.RootGrid.DataContext = value; } } private void button_AddGoals_Click(object sender, RoutedEventArgs e) { var control = new GoalImportControl(CreateGoalTreeCopy(ViewModel.CombinedGoalTree, SelectedGoals)); var beWoWindow = new BeWoWindow { rootGroupBox = { Header = "Ziele hinzufügen" }, Height = this.Height, Width = this.Width, MinWidth = 550, WindowStartupLocation = WindowStartupLocation.CenterOwner, GroupBoxContent = control, Owner = BeWoApp.CurrentBeWo.MainWindow }; control.ButtonImportClicked += () => { ImportGoals(control.GetSelectedGoals()); beWoWindow.Close(); }; control.ButtonCancelClicked += () => { beWoWindow.Close(); }; beWoWindow.ShowDialog(); } private ObservableCollection CreateGoalTreeCopy(ObservableCollection totalGoalTree, ObservableCollection selectedGoals) { var selectedDict = CreateGoalDictionary(selectedGoals); var listWithoutSelected = GetGoalsWithoutSelected(totalGoalTree, selectedDict); var copyList = new List(); foreach (var item in listWithoutSelected.Where(i => i.Children == null || i.Children.Count == 0)) { if (item.GoalType == ValueListEntryType.SupportConceptGoalCategoryType || item.GoalType == ValueListEntryType.SupportConceptGoalType) { SupportConceptGoalTreeItem copy = CreateCopy(item); copy.IsExpanded = false; copyList.Add(copy); } } ObservableCollection tree = CreateTree(copyList, totalGoalTree); return tree; } private ObservableCollection GetGoalsWithoutSelected(ObservableCollection totalGoalTree, Dictionary selectedDict) { var list = new ObservableCollection(); foreach (var item in totalGoalTree) { AddGoalsWithoutSelected(item, list, selectedDict); } return list; } private void AddGoalsWithoutSelected(SupportConceptGoalTreeItem item, ObservableCollection list, Dictionary selectedDict) { if (!selectedDict.ContainsKey(item.GoalEntryDC.ValueListEntryOid.Value)) { list.Add(item); } foreach (var child in item.Items) { AddGoalsWithoutSelected(child, list, selectedDict); } } private void ImportGoals(IList goals) { foreach (var goal in goals) { AddGoalToTree(goal, null); } } private void AddGoalToTree(SupportConceptGoalTreeItem goal, SupportConceptGoalTreeItem child) { var copy = CreateCopy(goal); copy.IsExpanded = true; if (child != null) { copy.Items.Add(child); } var parentElement = GetExistingParent(copy); if (parentElement == null) { parentElement = GetParentFromTotalTree(copy); if (parentElement != null) { AddGoalToTree(parentElement, copy); } else { SelectedGoals.Add(copy); } } else { int idx = 0; foreach (var ei in parentElement.Items) { if (goal.DetailDescription.CompareTo(ei.DetailDescription) > 0) { idx++; } } parentElement.Items.Insert(idx, goal); //parentElement.Items.Sort((a, b) => a.DetailDescription.CompareTo(b.DetailDescription)); } ViewModel.ChangedGoals.Add(copy); } private SupportConceptGoalTreeItem GetExistingParent(SupportConceptGoalTreeItem copy) { return GetParentForGoal(SelectedGoals, copy); } private SupportConceptGoalTreeItem GetParentFromTotalTree(SupportConceptGoalTreeItem copy) { return GetParentForGoal(ViewModel.CombinedGoalTree, copy); } private SupportConceptGoalTreeItem GetParentForGoal(IList goals, SupportConceptGoalTreeItem copy) { SupportConceptGoalTreeItem found = null; if (copy.GoalEntryDC.ParentOid.HasValue) { foreach (var goal in goals) { if (goal.GoalEntryDC.ValueListEntryOid == copy.GoalEntryDC.ParentOid) { return goal; } found = GetParentForGoal(goal.Items, copy); if (found != null) { return found; } } } return found; } private void button_aufklappenAllGoals_Click(object sender, RoutedEventArgs e) { SwitchAllGoalTreeItems(true); } private void button_zuklappenAllGoals_Click(object sender, RoutedEventArgs e) { SwitchAllGoalTreeItems(false); } private void SwitchAllGoalTreeItems(bool check) { Style stylee = new Style { TargetType = typeof(TreeViewItem) }; stylee.Setters.Add(new Setter(TreeViewItem.IsExpandedProperty, check)); treeview_Goals.ItemContainerStyle = stylee; } private void GoalTreeViewItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) { var source = e.OriginalSource as DependencyObject; while (source != null && !(source is TreeViewItem)) { source = VisualTreeHelper.GetParent(source); } if (source != null) { ((TreeViewItem)source).IsSelected = true; _SelectedGoalTreeNode = (TreeViewItem)source; } else { _SelectedGoalTreeNode = null; } } private void TextBox_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Return) { UpdateSelectedTreeItemText((TextBox)sender); } } private void TextBox_LostFocus(object sender, RoutedEventArgs e) { UpdateSelectedTreeItemText((TextBox)sender); } private static void UpdateSelectedTreeItemText(TextBoxBase textBox) { if (!textBox.IsReadOnly) { textBox.Background = Brushes.Transparent; textBox.IsReadOnly = true; textBox.BorderThickness = new Thickness(0); textBox.Padding = new Thickness(1, 3, 1, 3); } } private void ContextMenuGoals_Click(object sender, RoutedEventArgs e) { if (!(e.OriginalSource is MenuItem twi)) return; var selectedItem = treeview_Goals.SelectedItem as SupportConceptGoalTreeItem; switch (twi.Header) { case "Neues individuelles Ziel": if (_SelectedGoalTreeNode != null) { ViewModel.AddTreeItemWithControl(selectedItem, ValueListEntryType.SupportConceptIndividualGoalCategoryType, "Ziel hinzufügen"); } break; case "Neue individuelle Maßnahme": if (_SelectedGoalTreeNode != null) { ViewModel.AddTreeItemWithControl(selectedItem, ValueListEntryType.SupportConceptIndividualGoalType, "Maßnahme hinzufügen"); } break; case "Löschen": if (selectedItem != null) { var zielOderMassnahme = selectedItem.GoalEntryVM.Type == ValueListEntryType.SupportConceptGoalCategoryType || selectedItem.GoalEntryVM.Type == ValueListEntryType.SupportConceptIndividualGoalCategoryType ? "das Ziel" : "die Maßnahme"; var pluralendung = zielOderMassnahme.Equals("das Ziel") ? "en" : "n"; if (MessageBox.Show("ACHTUNG: Das Löschen von " + zielOderMassnahme.Substring(4) + pluralendung + " kann nicht rückgängig gemacht werden.\n" + "Alle verknüpften Eintragungen in der Zeiterfassung bleiben erhalten.\n\n" + "Wollen Sie " + zielOderMassnahme + " '" + selectedItem.GoalEntryVM?.DisplayName + "' wirklich löschen?", "Löschen bestätigen", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { ViewModel.DeleteIndividualGoal(selectedItem); } } break; case "Bearbeiten": if (selectedItem != null) ViewModel.EditTreeItem(selectedItem.GoalEntryVM); break; } } private void ContextMenuGoals_Opened(object sender, RoutedEventArgs e) { if (e.OriginalSource is ContextMenu) { var menu = e.OriginalSource as ContextMenu; var goal = treeview_Goals.SelectedItem as SupportConceptGoalTreeItem; foreach (var menuitem in menu.Items.OfType().Select(item => item)) { if (menuitem.Header.Equals("Bearbeiten")) { menuitem.IsEnabled = goal != null && (goal.GoalType.Equals(ValueListEntryType.SupportConceptIndividualGoalCategoryType) || goal.GoalType.Equals(ValueListEntryType.SupportConceptIndividualGoalType)); } else if (menuitem.Header.Equals("Neues individuelles Ziel") || menuitem.Header.Equals("Neue individuelle Maßnahme")) { menuitem.IsEnabled = goal != null && (goal.GoalType.Equals(ValueListEntryType.SupportConceptIndividualGoalCategoryType) || goal.GoalType.Equals(ValueListEntryType.SupportConceptGoalCategoryType)); } else if (menuitem.Header.Equals("Löschen")) { menuitem.IsEnabled = goal != null && (goal.GoalType.Equals(ValueListEntryType.SupportConceptIndividualGoalCategoryType) || goal.GoalType.Equals(ValueListEntryType.SupportConceptIndividualGoalType)); } } } } private void button_AddRating_Click(object sender, RoutedEventArgs e) { ViewModel.Ratings.VMList.AddNew(); } private void btn_deleteRating_Click(object sender, RoutedEventArgs e) { var vm = ((Button)sender).Tag as RatingVM; ViewModel.Ratings.VMList.Remove(vm); } } }