private void DeleteHosts(classHost host = null, List <classHost> hosts = null) { if (host != null) { var result = windowMessage.Show("Remove the host ''" + host.IP + "''?", this, MessageBoxButton.OKCancel); if (result == MessageBoxResult.OK) { classContainer container = tvContainers.SelectedItem as classContainer; if (container != null) { container.Hosts.Remove(host); IsChanged = true; } } } if (hosts != null) { var result = windowMessage.Show("Do you really wont to remove " + hosts.Count + " hosts?", this, MessageBoxButton.OKCancel); if (result == MessageBoxResult.OK) { classContainer container = tvContainers.SelectedItem as classContainer; if (container != null) { foreach (classHost item in hosts) { container.Hosts.Remove(item); } IsChanged = true; } } } }
//Move Host private void lvHosts_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.SystemKey == Key.Up && Keyboard.Modifiers == ModifierKeys.Alt) { classContainer container = tvContainers.SelectedItem as classContainer; if (container == null) { return; } var i = container.Hosts.IndexOf(lvHosts.SelectedItem as classHost); if (i > 0) { container.Hosts.Move(i, i - 1); IsChanged = true; } } else if (e.SystemKey == Key.Down && Keyboard.Modifiers == ModifierKeys.Alt) { classContainer container = tvContainers.SelectedItem as classContainer; if (container == null) { return; } var i = container.Hosts.IndexOf(lvHosts.SelectedItem as classHost); if (i < container.Hosts.Count - 1 && i >= 0) { container.Hosts.Move(i, i + 1); IsChanged = true; } } }
private void DeleteContainer(classContainer container) { if (container != null) { var result = windowMessage.Show("Remove the container ''" + container.Name + "''?", this, MessageBoxButton.OKCancel); if (result == MessageBoxResult.OK) { var parentcollection = FindParentCollectionForContainer(Containers, container); if (parentcollection != null) { parentcollection.Remove(container); IsChanged = true; } } } }
public windowContainer(Window owner, classContainer container = null) { this.Owner = owner; this.Height = this.Owner.ActualHeight * 0.8; this.Width = this.Owner.ActualWidth * 0.3; InitializeComponent(); this.WindowStyle = WindowStyle.SingleBorderWindow; if (container != null) { Name.Text = container.Name; Name.SelectAll(); } }
private void tvContainers_DragEnterOver(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(typeof(classContainer))) { TreeViewItem tvidestination = (TreeViewItem)FindVisualParent(e.OriginalSource as DependencyObject); classContainer objdestination = (tvidestination != null && tvidestination.DataContext.GetType() == typeof(classContainer)) ? tvidestination.DataContext as classContainer : null; classContainer objsource = (classContainer)e.Data.GetData(typeof(classContainer)); e.Effects = DragDropEffects.Move; if (objsource == objdestination) { e.Effects = DragDropEffects.None; } else if (objsource != null & objdestination != null) { ObservableCollection <classContainer> selfparentcollection = FindParentCollectionForContainer(objsource.Children, objdestination); if (selfparentcollection != null) { e.Effects = DragDropEffects.None; } ObservableCollection <classContainer> sourceparentcollection = FindParentCollectionForContainer(objsource.Children, objdestination); if (sourceparentcollection == objdestination.Children) { e.Effects = DragDropEffects.None; } } else if (objsource != null & objdestination == null) { ObservableCollection <classContainer> sourceparentcollection = FindParentCollectionForContainer(Containers, objsource); if (sourceparentcollection == Containers) { e.Effects = DragDropEffects.None; } } } else { e.Effects = DragDropEffects.None; } e.Handled = true; }
//MenuItem Events private void MenuItem_Click(object sender, RoutedEventArgs e) { var mi = sender as MenuItem; if (mi.Name == "AddContainer") { windowContainer wc = new windowContainer(this); wc.ShowDialog(); if (wc.DialogResult == true) { classContainer container = new classContainer(wc.Name.Text, true); Containers.Add(container); IsChanged = true; TreeViewItem tvi = FindTreeViewItemFromObject(tvContainers.ItemContainerGenerator, container); tvi.IsSelected = true; } } else if (mi.Name == "AddSubContainer") { windowContainer wc = new windowContainer(this); wc.ShowDialog(); if (wc.DialogResult == true) { classContainer container = tvContainers.SelectedItem as classContainer; classContainer subcontainer = new classContainer(wc.Name.Text, true); container.Children.Add(subcontainer); container.IsExpanded = true; IsChanged = true; TreeViewItem tvi = FindTreeViewItemFromObject(tvContainers.ItemContainerGenerator, subcontainer); tvi.IsSelected = true; } } else if (mi.Name == "EditContainer") { classContainer container = tvContainers.SelectedItem as classContainer; windowContainer wcEdit = new windowContainer(this, container); wcEdit.ShowDialog(); if (wcEdit.DialogResult == true) { container.Name = wcEdit.Name.Text; IsChanged = true; } } else if (mi.Name == "RemoveContainer") { DeleteContainer(tvContainers.SelectedItem as classContainer); } else if (mi.Name == "CreateHost") { if (tvContainers.SelectedItem != null) { windowHost wh = new windowHost(this); wh.ShowDialog(); if (wh.DialogResult == true) { classHost host = new classHost(wh.IP_address.Text, wh.DNS_Name.Text, wh.Description.Text); classContainer container = (classContainer)tvContainers.SelectedItem; container.Hosts.Add(host); IsChanged = true; } } } else if (mi.Name == "EditHost") { classHost host = lvHosts.SelectedItem as classHost; windowHost whEdit = new windowHost(this, host); whEdit.ShowDialog(); if (whEdit.DialogResult == true) { host.IP = whEdit.IP_address.Text; host.DNS_Name = whEdit.DNS_Name.Text; host.Description = whEdit.Description.Text; IsChanged = true; } } else if (mi.Name == "RemoveHost") { if (lvHosts.SelectedItems.Count == 1) { DeleteHosts(lvHosts.SelectedItem as classHost); } else if (lvHosts.SelectedItems.Count > 1) { List <classHost> hosts = new List <classHost>(); foreach (classHost item in lvHosts.SelectedItems) { hosts.Add(item); } DeleteHosts(null, hosts); } } }
private void tvContainers_Drop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(typeof(classContainer))) { TreeViewItem tvidestination = (TreeViewItem)FindVisualParent(e.OriginalSource as DependencyObject); classContainer objdestination = (tvidestination != null && tvidestination.DataContext.GetType() == typeof(classContainer)) ? tvidestination.DataContext as classContainer : null; classContainer objsource = (classContainer)e.Data.GetData(typeof(classContainer)); TreeViewItem tvisource = (TreeViewItem)((e.Source as TreeView).ItemContainerGenerator.ContainerFromItem(objsource)); if (objsource == objdestination) { return; } else if (objsource != null & objdestination != null) { ObservableCollection <classContainer> selfparentcollection = FindParentCollectionForContainer(objsource.Children, objdestination); if (selfparentcollection != null) { return; } ObservableCollection <classContainer> sourceparentcollection = FindParentCollectionForContainer(Containers, objsource); if (sourceparentcollection == objdestination.Children) { return; } objdestination.Children.Add(objsource); IsChanged = true; if (sourceparentcollection != null) { sourceparentcollection.Remove(objsource); } else { return; } tvidestination.IsExpanded = true; } else if (objsource != null & objdestination == null) { ObservableCollection <classContainer> sourceparentcollection = FindParentCollectionForContainer(Containers, objsource); if (sourceparentcollection == Containers) { return; } Containers.Add(objsource); IsChanged = true; if (sourceparentcollection != null) { sourceparentcollection.Remove(objsource); } else { return; } } objsource.IsSelected = true; tvContainers.Focus(); } }
//Find Collection<classContainer> From classContainer public ObservableCollection <classContainer> FindParentCollectionForContainer(ObservableCollection <classContainer> containers, classContainer container) { ObservableCollection <classContainer> p = null; foreach (var c in containers) { if (c == container) { return(containers); } else { p = FindParentCollectionForContainer(c.Children, container); if (p != null) { break; } } } return(p); }