#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using HeuristicLab.Core;
using HeuristicLab.Core.Views;
using HeuristicLab.MainForm;
using HeuristicLab.Clients.Hive.Views;
namespace HeuristicLab.Clients.Hive.Administration.Views {
[View("ResourcesView")]
[Content(typeof(IItemList), IsDefaultView = true)]
public partial class ResourcesView : ItemView {
public new IItemList Content {
get { return (IItemList)base.Content; }
set { base.Content = value; }
}
public ResourcesView() {
InitializeComponent();
treeSlaveGroup.ImageList.Images.Add(HiveImageLibrary.Slave);
treeSlaveGroup.ImageList.Images.Add(HiveImageLibrary.SlaveGroup);
updateScheduleControl.UpdateAction = new Action(UpdateSchedule);
updateSlaveGroup.UpdateAction = new Action(UpdateSlaveGroups);
}
#region Register Content Events
protected override void DeregisterContentEvents() {
Content.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler>(Content_ItemsAdded);
Content.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler>(Content_ItemsRemoved);
base.DeregisterContentEvents();
}
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
Content.ItemsAdded += new Collections.CollectionItemsChangedEventHandler>(Content_ItemsAdded);
Content.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler>(Content_ItemsRemoved);
}
#endregion
protected override void OnContentChanged() {
base.OnContentChanged();
if (Content == null) {
slaveView.Content = null;
treeSlaveGroup.Nodes.Clear();
} else {
treeSlaveGroup.Nodes.Clear();
//rebuild
TreeNode ungrp = new TreeNode("UNGROUPED");
ungrp.ImageIndex = treeSlaveGroup.ImageList.Images.Count - 1;
ungrp.SelectedImageIndex = ungrp.ImageIndex;
var newGroup = new SlaveGroup();
newGroup.Name = "UNGROUPED";
newGroup.Id = Guid.NewGuid();
newGroup.Description = "Contains slaves which are in no group";
ungrp.Tag = newGroup;
foreach (Resource g in Content) {
if (g.GetType() == typeof(SlaveGroup)) {
TreeNode tn = new TreeNode();
tn.ImageIndex = treeSlaveGroup.ImageList.Images.Count - 1;
tn.SelectedImageIndex = tn.ImageIndex;
tn.Tag = g;
tn.Text = g.Name;
foreach (Resource r in Content.Where(s => s.ParentResourceId != null && s.ParentResourceId == g.Id)) {
var stn = new TreeNode(r.Name);
stn.ImageIndex = 0;
stn.SelectedImageIndex = stn.ImageIndex;
stn.Tag = r;
tn.Nodes.Add(stn);
}
treeSlaveGroup.Nodes.Add(tn);
} else if (g.GetType() == typeof(Slave)) {
if (g.ParentResourceId == null) {
var stn = new TreeNode(g.Name);
stn.ImageIndex = 0;
stn.SelectedImageIndex = stn.ImageIndex;
stn.Tag = g;
ungrp.Nodes.Add(stn);
}
}
}
treeSlaveGroup.Nodes.Add(ungrp);
}
}
protected override void SetEnabledStateOfControls() {
base.SetEnabledStateOfControls();
// TODO: Put code here to enable or disable controls based on whether the Content is/not null or the view is ReadOnly
}
private void treeSlaveGroup_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
if (slaveView.Content != null && slaveView.Content is SlaveGroup) {
slaveView.Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
}
slaveView.Content = (Resource)e.Node.Tag;
if (e.Node.Tag.GetType() == typeof(Slave)) {
scheduleView.ResourceId = ((Slave)e.Node.Tag).Id;
} else if (e.Node.Tag is SlaveGroup) {
slaveView.Content.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
}
}
void SlaveViewContent_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
OnContentChanged();
}
private void btnAddGroup_Click(object sender, EventArgs e) {
SlaveGroup newGroup = new SlaveGroup();
newGroup.Name = "New Group";
Content.Add(newGroup);
}
void Content_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs> e) {
OnContentChanged();
}
void Content_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs> e) {
OnContentChanged();
}
private void btnRemoveGroup_Click(object sender, EventArgs e) {
//TODO: display some warning?
if (treeSlaveGroup.SelectedNode != null && treeSlaveGroup.SelectedNode.Tag != null) {
Resource res = (Resource)treeSlaveGroup.SelectedNode.Tag;
Content.Remove(res);
if (res is Slave) {
ServiceLocator.Instance.CallHiveService(service => service.DeleteSlave(res.Id));
} else if (res is SlaveGroup) {
//only delete empty groups
if (Content.Where(s => s.ParentResourceId == res.Id).Count() < 0) {
ServiceLocator.Instance.CallHiveService(service => service.DeleteSlaveGroup(res.Id));
} else {
MessageBox.Show("Only empty groups can be deleted.");
}
}
}
}
private void btnSave_Click(object sender, EventArgs e) {
foreach (Resource res in Content) {
if (res is SlaveGroup && res.Id == Guid.Empty) {
SlaveGroup slaveGroup = (SlaveGroup)res;
ServiceLocator.Instance.CallHiveService(service => service.AddSlaveGroup(slaveGroup));
}
if (res.Id != Guid.Empty && res.Modified) {
if (res is SlaveGroup) {
ServiceLocator.Instance.CallHiveService(service => service.UpdateSlaveGroup((SlaveGroup)res));
} else if (res is Slave) {
ServiceLocator.Instance.CallHiveService(service => service.UpdateSlave((Slave)res));
}
}
}
}
private void treeSlaveGroup_DragDrop(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false)) {
Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
TreeNode destNode = ((TreeView)sender).GetNodeAt(pt);
TreeNode newNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
if (destNode.TreeView == newNode.TreeView) {
SlaveGroup sgrp = null;
if (destNode.Tag != null && destNode.Tag is SlaveGroup) {
sgrp = (SlaveGroup)destNode.Tag;
}
if (destNode.Parent != null && destNode.Parent.Tag is SlaveGroup) {
sgrp = (SlaveGroup)destNode.Parent.Tag;
}
if (sgrp != null) {
if (newNode.Tag != null && newNode.Tag is Slave) {
Slave slave = (Slave)newNode.Tag;
if (slave.ParentResourceId != sgrp.Id) {
slave.ParentResourceId = sgrp.Id;
OnContentChanged();
}
}
}
}
}
}
private void treeSlaveGroup_ItemDrag(object sender, ItemDragEventArgs e) {
TreeNode sourceNode = (TreeNode)e.Item;
DoDragDrop(sourceNode, DragDropEffects.All);
}
private void treeSlaveGroup_DragEnter(object sender, DragEventArgs e) {
e.Effect = DragDropEffects.Move;
}
private void treeSlaveGroup_DragOver(object sender, DragEventArgs e) {
e.Effect = DragDropEffects.Move;
}
private void treeSlaveGroup_QueryContinueDrag(object sender, QueryContinueDragEventArgs e) {
e.Action = DragAction.Continue;
}
void ResetView() {
treeSlaveGroup.Nodes.Clear();
if (slaveView.Content != null && slaveView.Content is SlaveGroup) {
slaveView.Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
}
slaveView.Content = null;
scheduleView.ResourceId = Guid.Empty;
}
private void UpdateSlaveGroups() {
this.Invoke(new Action(ResetView));
Content.Clear();
IItemList resources = new ItemList();
ServiceLocator.Instance.CallHiveService(service => {
service.GetSlaveGroups().ForEach(g => resources.Add(g));
service.GetSlaves().ForEach(s => resources.Add(s));
});
Content = resources;
}
private void UpdateSchedule() {
Guid resourceId = scheduleView.ResourceId;
if (resourceId != null) {
ServiceLocator.Instance.CallHiveService(service => {
var appointments = service.GetScheduleForResource(resourceId);
ItemList ias = new ItemList();
appointments.ForEach(a => ias.Add(a));
scheduleView.Invoke(new Action(() => scheduleView.Content = ias));
});
}
}
}
}