Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Exporter-715/sources/HeuristicLab.Hive.Server.Console/3.2/HiveServerManagementConsole.cs @ 2319

Last change on this file since 2319 was 2118, checked in by aleitner, 15 years ago

change comments, delete manual login-data (#569)

File size: 30.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Data;
26using System.Drawing;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.Hive.Contracts.Interfaces;
31using HeuristicLab.Hive.Contracts.BusinessObjects;
32using HeuristicLab.Hive.Contracts;
33using System.Threading;
34using System.ServiceModel;
35
36namespace HeuristicLab.Hive.Server.ServerConsole {
37
38  /// <summary>
39  /// if form is closed the loginform gets an information
40  /// </summary>
41  /// <param name="cf"></param>
42  /// <param name="error"></param>
43  public delegate void closeForm(bool cf, bool error);
44
45  public partial class HiveServerManagementConsole : Form {
46
47    public event closeForm closeFormEvent;
48
49    #region private variables
50    private ResponseList<Job> jobs = null;
51
52    List<ListViewGroup> jobGroup;
53    private Dictionary<Guid, List<ListViewItem>> clientList = new Dictionary<Guid, List<ListViewItem>>();
54    private List<Changes> changes = new List<Changes>();
55
56    private Job currentJob = null;
57    private ClientInfo currentClient = null;
58
59    private TreeNode currentGroupNode = null;
60    Guid parentgroup = Guid.Empty;
61    private ToolTip tt = new ToolTip();
62
63    private const string NOGROUP = "No group";
64    private TreeNode hoverNode;
65    private List<ListViewGroup> lvClientGroups;
66    #endregion
67
68    #region Properties
69    private IClientManager ClientManager {
70      get {
71        try {
72          return ServiceLocator.GetClientManager();
73        }
74        catch (FaultException ex) {
75          MessageBox.Show(ex.Message);
76        }
77        return null;
78      }
79    }
80    #endregion
81
82    public HiveServerManagementConsole() {
83      InitializeComponent();
84      Init();
85      AddClients();
86      AddJobs();
87      timerSyncronize.Start();
88    }
89
90    private void Init() {
91      //adding context menu items for jobs
92      menuItemAbortJob.Click += (s, e) => {
93        IJobManager jobManager = ServiceLocator.GetJobManager();
94        if (lvJobControl.SelectedItems.Count == 1) {
95          jobManager.AbortJob(((Job)(lvJobControl.SelectedItems[0].Tag)).Id);
96        }
97      };
98
99      //adding context menu items for jobs
100      menuItemGetSnapshot.Click += (s, e) => {
101        IJobManager jobManager = ServiceLocator.GetJobManager();
102        if (lvJobControl.SelectedItems.Count == 1) {
103          jobManager.RequestSnapshot(((Job)(lvJobControl.SelectedItems[0].Tag)).Id);
104        }
105      };
106
107      //adding group
108      menuItemAddGroup.Click += (s, e) => {
109        AddGroup addgroup = new AddGroup();
110        parentgroup = Guid.Empty;
111        if ((tvClientControl.SelectedNode != null) && (((ClientGroup)tvClientControl.SelectedNode.Tag).Id != Guid.Empty)) {
112          parentgroup = ((ClientGroup)tvClientControl.SelectedNode.Tag).Id;
113        }
114        addgroup.addGroupEvent += new AddGroupDelegate(addgroup_addGroupEvent);
115        addgroup.Show();
116      };
117
118      //adding group
119      menuItemDeleteGroup.Click += (s, e) => {
120        IClientManager clientManager = ServiceLocator.GetClientManager();
121        if (tvClientControl.SelectedNode != null) {
122          Response resp = clientManager.DeleteClientGroup(((ClientGroup)tvClientControl.SelectedNode.Tag).Id);
123          if (tvClientControl.SelectedNode == currentGroupNode) {
124            currentGroupNode = null;
125          }
126          tvClientControl.Nodes.Remove(tvClientControl.SelectedNode);
127          AddClients();
128        }
129      };
130
131      // drag item to treeview
132      lvClientControl.ItemDrag += delegate(object sender, ItemDragEventArgs e) {
133        List<string> itemIDs = new List<string>((sender as ListView).SelectedItems.Count);
134        foreach (ListViewItem item in (sender as ListView).SelectedItems) {
135          itemIDs.Add(item.Name);
136        }
137        (sender as ListView).DoDragDrop(itemIDs.ToArray(), DragDropEffects.Move);
138      };
139
140      // drag enter on treeview
141      tvClientControl.DragEnter += delegate(object sender, DragEventArgs e) {
142        e.Effect = DragDropEffects.Move;
143      };
144
145      // drag over
146      tvClientControl.DragOver += delegate(object sender, DragEventArgs e) {
147        Point mouseLocation = tvClientControl.PointToClient(new Point(e.X, e.Y));
148        TreeNode node = tvClientControl.GetNodeAt(mouseLocation);
149        if (node != null && ((ClientGroup)node.Tag).Id != Guid.Empty) {
150          e.Effect = DragDropEffects.Move;
151          if (hoverNode == null) {
152            node.BackColor = Color.LightBlue;
153            node.ForeColor = Color.White;
154            hoverNode = node;
155          } else if (hoverNode != node) {
156            hoverNode.BackColor = Color.White;
157            hoverNode.ForeColor = Color.Black;
158            node.BackColor = Color.LightBlue;
159            node.ForeColor = Color.White;
160            hoverNode = node;
161          }
162        } else {
163          e.Effect = DragDropEffects.None;
164        }
165      };
166
167      // drag drop event
168      tvClientControl.DragDrop += delegate(object sender, DragEventArgs e) {
169        if (e.Data.GetDataPresent(typeof(string[]))) {
170          Point dropLocation = (sender as TreeView).PointToClient(new Point(e.X, e.Y));
171          TreeNode dropNode = (sender as TreeView).GetNodeAt(dropLocation);
172          if (((ClientGroup)dropNode.Tag).Id != Guid.Empty) {
173            Dictionary<ClientInfo, Guid> clients = new Dictionary<ClientInfo, Guid>();
174            foreach (ListViewItem lvi in lvClientControl.SelectedItems) {
175              Guid groupId = Guid.Empty;
176              foreach (ListViewGroup lvg in lvClientGroups) {
177                if (lvi.Group.Header == ((ClientGroup)lvg.Tag).Name) {
178                  groupId = ((ClientGroup)lvg.Tag).Id;
179                }
180              }
181              clients.Add((ClientInfo)lvi.Tag, groupId);
182            }
183            ChangeGroup(clients, ((ClientGroup)dropNode.Tag).Id);
184          }
185          tvClientControl_DragLeave(null, EventArgs.Empty);
186          AddClients();
187        }
188      };
189    }
190
191    /// <summary>
192    /// Change client to other group
193    /// </summary>
194    /// <param name="clients">list of clients</param>
195    /// <param name="clientgroupID">group of clients</param>
196    private void ChangeGroup(Dictionary<ClientInfo, Guid> clients, Guid clientgroupID) {
197      IClientManager clientManager = ServiceLocator.GetClientManager();
198      foreach (KeyValuePair<ClientInfo, Guid> client in clients) {
199        if (client.Key.Id != Guid.Empty) {
200          Response resp = clientManager.DeleteResourceFromGroup(client.Value, client.Key.Id);
201        }
202        clientManager.AddResourceToGroup(clientgroupID, client.Key);
203      }
204    }
205
206    #region Backgroundworker
207    /// <summary>
208    /// event on Ticker
209    /// </summary>
210    /// <param name="obj"></param>
211    /// <param name="e"></param>
212    private void TickSync(object obj, EventArgs e) {
213      if (!updaterWoker.IsBusy) {
214        updaterWoker.RunWorkerAsync();
215      }
216    }
217
218    private void updaterWoker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
219      RefreshForm();
220    }
221
222    private void updaterWoker_DoWork(object sender, DoWorkEventArgs e) {
223
224      changes.Clear();
225
226      ResponseList<Job> jobsOld = jobs;
227      try {
228        IJobManager jobManager =
229                ServiceLocator.GetJobManager();
230
231        jobs = jobManager.GetAllJobs();
232
233        IDictionary<int, Job> jobsOldHelp;
234        CloneList(jobsOld, out jobsOldHelp);
235
236        GetDelta(jobsOld.List, jobsOldHelp);
237
238      }
239      catch (FaultException fe) {
240        MessageBox.Show(fe.Message);
241      }
242
243    }
244
245    #endregion
246
247
248    /// <summary>
249    /// Adds Exceptionobs to ListView and TreeView
250    /// </summary>
251    private void AddJobs() {
252      try {
253        List<ListViewItem> jobObjects = new List<ListViewItem>();
254        IJobManager jobManager =
255          ServiceLocator.GetJobManager();
256        jobs = jobManager.GetAllJobs();
257
258        lvJobControl.Items.Clear();
259
260        ListViewGroup lvJobCalculating = new ListViewGroup("calculating", HorizontalAlignment.Left);
261        ListViewGroup lvJobFinished = new ListViewGroup("finished", HorizontalAlignment.Left);
262        ListViewGroup lvJobPending = new ListViewGroup("pending", HorizontalAlignment.Left);
263
264        jobGroup = new List<ListViewGroup>();
265        jobGroup.Add(lvJobCalculating);
266        jobGroup.Add(lvJobFinished);
267        jobGroup.Add(lvJobPending);
268
269        if (jobs != null && jobs.List != null) {
270
271          foreach (Job job in jobs.List) {
272            if (job.State == State.calculating) {
273              ListViewItem lvi = new ListViewItem(job.Id.ToString(), 1, lvJobCalculating);
274              lvi.Tag = job;
275              jobObjects.Add(lvi);
276
277              lvi.ToolTipText = (job.Percentage * 100) + "% of job calculated";
278            } else if (job.State == State.finished) {
279              ListViewItem lvi = new ListViewItem(job.Id.ToString(), 0, lvJobFinished);
280              lvi.Tag = job;
281              jobObjects.Add(lvi);
282            } else if (job.State == State.offline || job.State == State.pending) {
283              ListViewItem lvi = new ListViewItem(job.Id.ToString(), 2, lvJobPending);
284              lvi.Tag = job;
285              jobObjects.Add(lvi);
286            }
287          } // Jobs
288          lvJobControl.BeginUpdate();
289          foreach (ListViewItem lvi in jobObjects) {
290            lvJobControl.Items.Add(lvi);
291          }
292          // actualization
293          lvJobControl.Groups.Add(lvJobCalculating);
294          lvJobControl.Groups.Add(lvJobFinished);
295          lvJobControl.Groups.Add(lvJobPending);
296          lvJobControl.EndUpdate();
297
298          if (currentJob != null) {
299            JobClicked();
300          }
301        }
302      }
303      catch (Exception ex) {
304        closeFormEvent(true, true);
305        this.Close();
306      }
307    }
308
309    private void AddClients() {
310      lvClientGroups = new List<ListViewGroup>();
311      clientList.Clear();
312      tvClientControl.Nodes.Clear();
313      try {
314        ResponseList<ClientGroup> clientGroups = ClientManager.GetAllClientGroups();
315
316        if (clientGroups != null && clientGroups.List != null) {
317          foreach (ClientGroup cg in clientGroups.List) {
318            AddClientOrGroup(cg, null);
319          }
320
321          if (currentGroupNode != null) {
322            lvClientControl.Items.Clear();
323            lvClientControl.Groups.Clear();
324            AddGroupsToListView(currentGroupNode);
325          }
326          tvClientControl.ExpandAll();
327        }
328
329      }
330      catch (FaultException fe) {
331        MessageBox.Show(fe.Message);
332      }
333    }
334
335    private void AddClientOrGroup(ClientGroup clientGroup, TreeNode currentNode) {
336      currentNode = CreateTreeNode(clientGroup, currentNode);
337      List<ListViewItem> clientGroupList = new List<ListViewItem>();
338      ListViewGroup lvg;
339      if (string.IsNullOrEmpty(clientGroup.Name)) {
340        lvg = new ListViewGroup(NOGROUP, HorizontalAlignment.Left);
341      } else {
342        lvg = new ListViewGroup(clientGroup.Name, HorizontalAlignment.Left);
343      }
344      lvClientControl.Groups.Add(lvg);
345      lvg.Tag = clientGroup;
346      lvClientGroups.Add(lvg);
347      foreach (Resource resource in clientGroup.Resources) {
348        if (resource is ClientInfo) {
349          int percentageUsage = CapacityRam(((ClientInfo)resource).NrOfCores, ((ClientInfo)resource).NrOfFreeCores);
350          int usage = 3;
351          if ((((ClientInfo)resource).State != State.offline) &&
352            (((ClientInfo)resource).State != State.nullState)) {
353            if ((percentageUsage >= 0) && (percentageUsage <= 25)) {
354              usage = 0;
355            } else if ((percentageUsage > 25) && (percentageUsage <= 75)) {
356              usage = 1;
357            } else if ((percentageUsage > 75) && (percentageUsage <= 100)) {
358              usage = 2;
359            }
360          }
361          ListViewItem lvi = new ListViewItem(resource.Name, usage, lvg);
362          lvi.Tag = resource as ClientInfo;
363          clientGroupList.Add(lvi);
364        } else if (resource is ClientGroup) {
365          AddClientOrGroup(resource as ClientGroup, currentNode);
366        }
367      }
368      clientList.Add(clientGroup.Id, clientGroupList);
369    }
370
371    private TreeNode CreateTreeNode(ClientGroup clientGroup, TreeNode currentNode) {
372      TreeNode tn;
373      if (string.IsNullOrEmpty(clientGroup.Name)) {
374        tn = new TreeNode(NOGROUP);
375      } else {
376        tn = new TreeNode(clientGroup.Name);
377      }
378      tn.Tag = clientGroup;
379      if (currentNode == null) {
380        tvClientControl.Nodes.Add(tn);
381      } else {
382        currentNode.Nodes.Add(tn);
383      }
384      return tn;
385    }
386
387    private void AddGroupsToListView(TreeNode node) {
388      if (node != null) {
389        ListViewGroup lvg = new ListViewGroup(node.Text, HorizontalAlignment.Left);
390        lvClientControl.Groups.Add(lvg);
391        lvg.Tag = node.Tag;
392        foreach (ListViewItem item in clientList[((ClientGroup)node.Tag).Id]) {
393          item.Group = lvg;
394          lvClientControl.Items.Add(item);
395        }
396
397        if (node.Nodes != null) {
398          foreach (TreeNode curNode in node.Nodes) {
399            AddGroupsToListView(curNode);
400          }
401        }
402      }
403    }
404
405    /// <summary>
406    /// if one job is clicked, the details for the clicked job are shown
407    /// in the second panel
408    /// </summary>
409    private void JobClicked() {
410      plJobDetails.Visible = true;
411      lvJobDetails.Items.Clear();
412
413      lvSnapshots.Enabled = true;
414
415      if (currentJob.State == State.offline) {
416        pbJobControl.Image = ilLargeImgJob.Images[2];
417      } else if (currentJob.State == State.calculating) {
418        pbJobControl.Image = ilLargeImgJob.Images[1];
419      } else if (currentJob.State == State.finished) {
420        pbJobControl.Image = ilLargeImgJob.Images[0];
421      }
422
423      lblJobName.Text = currentJob.Id.ToString();
424      progressJob.Value = (int)(currentJob.Percentage * 100);
425      lblProgress.Text = (int)(currentJob.Percentage * 100) + "% calculated";
426
427      ListViewItem lvi = new ListViewItem();
428      lvi.Text = "User:";
429      lvi.SubItems.Add(currentJob.UserId.ToString());
430      lvJobDetails.Items.Add(lvi);
431
432      lvi = null;
433      lvi = new ListViewItem();
434      lvi.Text = "created at:";
435      lvi.SubItems.Add(currentJob.DateCreated.ToString());
436      lvJobDetails.Items.Add(lvi);
437
438      if (currentJob.ParentJob != null) {
439        lvi = null;
440        lvi = new ListViewItem();
441        lvi.Text = "Parent job:";
442        lvi.SubItems.Add(currentJob.ParentJob.ToString());
443        lvJobDetails.Items.Add(lvi);
444      }
445
446      lvi = null;
447      lvi = new ListViewItem();
448      lvi.Text = "Priority:";
449      lvi.SubItems.Add(currentJob.Priority.ToString());
450      lvJobDetails.Items.Add(lvi);
451
452      if (currentJob.Project != null) {
453        lvi = null;
454        lvi = new ListViewItem();
455        lvi.Text = "Project:";
456        lvi.SubItems.Add(currentJob.Project.Name.ToString());
457        lvJobDetails.Items.Add(lvi);
458      }
459
460      if (currentJob.Client != null) {
461        lvi = null;
462        lvi = new ListViewItem();
463        lvi.Text = "Calculation begin:";
464        lvi.SubItems.Add(currentJob.DateCalculated.ToString());
465        lvJobDetails.Items.Add(lvi);
466
467
468        lvi = null;
469        lvi = new ListViewItem();
470        lvi.Text = "Client calculated:";
471        lvi.SubItems.Add(currentJob.Client.Name.ToString());
472        lvJobDetails.Items.Add(lvi);
473
474        if (currentJob.State == State.finished) {
475          IJobManager jobManager =
476            ServiceLocator.GetJobManager();
477          ResponseObject<JobResult> jobRes = jobManager.GetLastJobResultOf(currentJob.Id);
478
479          if (jobRes != null && jobRes.Obj != null) {
480            lvi = null;
481            lvi = new ListViewItem();
482            lvi.Text = "Calculation ended:";
483            lvi.SubItems.Add(jobRes.Obj.DateFinished.ToString());
484            lvJobDetails.Items.Add(lvi);
485          }
486        }
487      }
488      if (currentJob.State != State.offline) {
489        lvSnapshots.Items.Clear();
490        GetSnapshotList();
491      } else {
492        lvSnapshots.Visible = false;
493      }
494    }
495
496    /// <summary>
497    /// if one client is clicked, the details for the clicked client are shown
498    /// in the second panel
499    /// </summary>
500    private void ClientClicked() {
501      plClientDetails.Visible = true;
502
503      if (currentClient != null) {
504        int percentageUsage = CapacityRam(currentClient.NrOfCores, currentClient.NrOfFreeCores);
505        int usage = 3;
506        if ((currentClient.State != State.offline) && (currentClient.State != State.nullState)) {
507          if ((percentageUsage >= 0) && (percentageUsage <= 25)) {
508            usage = 0;
509          } else if ((percentageUsage > 25) && (percentageUsage <= 75)) {
510            usage = 1;
511          } else if ((percentageUsage > 75) && (percentageUsage <= 100)) {
512            usage = 2;
513          }
514        }
515        pbClientControl.Image = ilLargeImgClient.Images[usage];
516        lblClientName.Text = currentClient.Name;
517        lblLogin.Text = currentClient.Login.ToString();
518        lblState.Text = currentClient.State.ToString();
519      }
520    }
521
522    private void RefreshForm() {
523      foreach (Changes change in changes) {
524        if (change.Types == Type.Job) {
525          RefreshJob(change);
526        }
527      }
528    }
529
530    private void RefreshJob(Changes change) {
531      if (change.ChangeType == Change.Update) {
532        for (int i = 0; i < lvJobControl.Items.Count; i++) {
533          if (lvJobControl.Items[i].Text == change.ID.ToString()) {
534            foreach (Job job in jobs.List) {
535              if (job.Id == change.ID) {
536                lvJobControl.Items[i].Tag = job;
537                if (currentJob != null) {
538                  if (currentJob.Id == change.ID) {
539                    currentJob = job;
540                    JobClicked();
541                  }
542                }
543                break;
544              }
545            }
546            State state = jobs.List[change.Position].State;
547            if (state == State.finished) {
548              lvJobControl.Items[i].Group = jobGroup[1];
549              lvJobControl.Items[i].ImageIndex = 0;
550            } else if (state == State.calculating) {
551              lvJobControl.Items[i].Group = jobGroup[0];
552              lvJobControl.Items[i].ImageIndex = 1;
553            } else if (state == State.offline || state == State.pending) {
554              lvJobControl.Items[i].Group = jobGroup[2];
555              lvJobControl.Items[i].ImageIndex = 2;
556            }
557            lvJobControl.Refresh();
558          }
559        }
560      } else if (change.ChangeType == Change.Create) {
561
562        ListViewItem lvi = new ListViewItem(
563          change.ID.ToString(), 2, jobGroup[2]);
564        foreach (Job job in jobs.List) {
565          if (job.Id == change.ID) {
566            lvi.Tag = job;
567            break;
568          }
569        }
570        lvJobControl.Items.Add(lvi);
571
572      } else if (change.ChangeType == Change.Delete) {
573        for (int i = 0; i < lvJobControl.Items.Count; i++) {
574          if (change.ID.ToString() == lvJobControl.Items[i].Text.ToString()) {
575            lvJobControl.Items[i].Remove();
576            break;
577          }
578        }
579      }
580    }
581
582
583    #region Eventhandlers
584    /// <summary>
585    /// Send event to Login-GUI when closing
586    /// </summary>
587    /// <param name="sender"></param>
588    /// <param name="e"></param>
589    private void Close_Click(object sender, EventArgs e) {
590      if (closeFormEvent != null) {
591        closeFormEvent(true, false);
592      }
593      this.Close();
594    }
595
596    /// <summary>
597    /// Send evnt to Login-GUI when closing
598    /// </summary>
599    /// <param name="sender"></param>
600    /// <param name="e"></param>
601    private void HiveServerConsoleInformation_FormClosing(object sender, FormClosingEventArgs e) {
602      if (closeFormEvent != null) {
603        closeFormEvent(true, false);
604      }
605    }
606
607    private void AddJob_Click(object sender, EventArgs e) {
608      AddJobForm newForm = new AddJobForm();
609      newForm.Show();
610      newForm.addJobEvent += new addDelegate(updaterWoker.RunWorkerAsync);
611    }
612
613    private void OnLVJobControlClicked(object sender, EventArgs e) {
614      currentJob = (Job)lvJobControl.SelectedItems[0].Tag;
615      JobClicked();
616    }
617
618    private void lvJobControl_MouseMove(object sender, MouseEventArgs e) {
619      if ((lvJobControl.GetItemAt(e.X, e.Y) != null) &&
620        (lvJobControl.GetItemAt(e.X, e.Y).ToolTipText != null)) {
621        tt.SetToolTip(lvJobControl, lvJobControl.GetItemAt(e.X, e.Y).ToolTipText);
622      }
623    }
624
625    private void lvJobControl_MouseUp(object sender, MouseEventArgs e) {
626      // If the right mouse button was clicked and released,
627      // display the shortcut menu assigned to the ListView.
628      lvJobControl.ContextMenuStrip.Items.Clear();
629      ListViewHitTestInfo hitTestInfo = lvJobControl.HitTest(e.Location);
630      if (e.Button == MouseButtons.Right && hitTestInfo.Item != null && lvJobControl.SelectedItems.Count == 1) {
631        Job selectedJob = (Job)lvJobControl.SelectedItems[0].Tag;
632
633        if (selectedJob != null && selectedJob.State == State.calculating) {
634          lvJobControl.ContextMenuStrip.Items.Add(menuItemAbortJob);
635          lvJobControl.ContextMenuStrip.Items.Add(menuItemGetSnapshot);
636        }
637      }
638      lvJobControl.ContextMenuStrip.Show(new Point(e.X, e.Y));
639    }
640
641    private void tvClientControl_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
642      lvClientControl.Items.Clear();
643      lvClientControl.Groups.Clear();
644      currentGroupNode = e.Node;
645      AddGroupsToListView(e.Node);
646    }
647
648    private void groupToolStripMenuItem_Click(object sender, EventArgs e) {
649      AddGroup addgroup = new AddGroup();
650      parentgroup = Guid.Empty;
651      if ((tvClientControl.SelectedNode != null) && (((ClientGroup)tvClientControl.SelectedNode.Tag).Id != Guid.Empty)) {
652        parentgroup = ((ClientGroup)tvClientControl.SelectedNode.Tag).Id;
653      }
654      addgroup.addGroupEvent += new AddGroupDelegate(addgroup_addGroupEvent);
655      addgroup.Show();
656    }
657
658    private void projectToolStripMenuItem_Click(object sender, EventArgs e) {
659      AddProject addproject = new AddProject();
660      addproject.AddProjectEvent += new AddProjectDelegate(addproject_AddProjectEvent);
661      addproject.Show();
662    }
663
664    private void OnLVClientClicked(object sender, EventArgs e) {
665      currentClient = (ClientInfo)lvClientControl.SelectedItems[0].Tag;
666      ClientClicked();
667    }
668
669    private void tvClientControl_MouseUp(object sender, MouseEventArgs e) {
670      // If the right mouse button was clicked and released,
671      // display the shortcut menu assigned to the ListView.
672      contextMenuGroup.Items.Clear();
673      TreeViewHitTestInfo hitTestInfo = tvClientControl.HitTest(e.Location);
674      tvClientControl.SelectedNode = hitTestInfo.Node;
675      if (e.Button != MouseButtons.Right) return;
676      if (hitTestInfo.Node != null) {
677        Resource selectedGroup = (Resource)tvClientControl.SelectedNode.Tag;
678
679        if (selectedGroup != null) {
680          contextMenuGroup.Items.Add(menuItemAddGroup);
681          contextMenuGroup.Items.Add(menuItemDeleteGroup);
682        }
683      } else {
684        contextMenuGroup.Items.Add(menuItemAddGroup);
685      }
686      tvClientControl.ContextMenuStrip.Show(tvClientControl, new Point(e.X, e.Y));
687    }
688
689    private void addproject_AddProjectEvent(string name) {
690      IJobManager jobManager = ServiceLocator.GetJobManager();
691
692      Project pg = new Project() { Name = name };
693      jobManager.CreateProject(pg);
694
695    }
696
697    private void addgroup_addGroupEvent(string name) {
698      IClientManager clientManager = ServiceLocator.GetClientManager();
699
700      if (parentgroup != Guid.Empty) {
701        ClientGroup cg = new ClientGroup() { Name = name };
702        ResponseObject<ClientGroup> respcg = clientManager.AddClientGroup(cg);
703        Response res = clientManager.AddResourceToGroup(parentgroup, respcg.Obj);
704        if (res != null && !res.Success) {
705          MessageBox.Show(res.StatusMessage, "Error adding Group", MessageBoxButtons.OK, MessageBoxIcon.Error);
706        }
707      } else {
708        ClientGroup cg = new ClientGroup() { Name = name };
709        clientManager.AddClientGroup(cg);
710      }
711      AddClients();
712    }
713
714
715    private void Refresh_Click(object sender, EventArgs e) {
716      Form overlayingForm = new Form();
717
718      overlayingForm.Show();
719      overlayingForm.FormBorderStyle = FormBorderStyle.None;
720      overlayingForm.BackColor = Color.Gray;
721      overlayingForm.Opacity = 0.4;
722      overlayingForm.Size = this.Size;
723      overlayingForm.Location = this.Location;
724
725      AddClients();
726
727      overlayingForm.Close();
728    }
729
730    private void largeIconsToolStripMenuItem_Click(object sender, EventArgs e) {
731      lvClientControl.View = View.LargeIcon;
732      lvJobControl.View = View.LargeIcon;
733      largeIconsToolStripMenuItem.CheckState = CheckState.Checked;
734      smallIconsToolStripMenuItem.CheckState = CheckState.Unchecked;
735      listToolStripMenuItem.CheckState = CheckState.Unchecked;
736    }
737
738    private void smallIconsToolStripMenuItem_Click(object sender, EventArgs e) {
739      lvClientControl.View = View.SmallIcon;
740      lvJobControl.View = View.SmallIcon;
741      largeIconsToolStripMenuItem.CheckState = CheckState.Unchecked;
742      smallIconsToolStripMenuItem.CheckState = CheckState.Checked;
743      listToolStripMenuItem.CheckState = CheckState.Unchecked;
744    }
745
746    private void listToolStripMenuItem_Click(object sender, EventArgs e) {
747      lvClientControl.View = View.List;
748      lvJobControl.View = View.List;
749      largeIconsToolStripMenuItem.CheckState = CheckState.Unchecked;
750      smallIconsToolStripMenuItem.CheckState = CheckState.Unchecked;
751      listToolStripMenuItem.CheckState = CheckState.Checked;
752    }
753
754    private void tvClientControl_DragLeave(object sender, EventArgs e) {
755      foreach (TreeNode node in tvClientControl.Nodes) {
756        node.BackColor = Color.White;
757        node.ForeColor = Color.Black;
758      }
759    }
760    #endregion
761
762    #region Helper methods
763
764    private void CloneList(ResponseList<Job> oldList, out IDictionary<int, Job> newList) {
765      newList = new Dictionary<int, Job>();
766      for (int i = 0; i < oldList.List.Count; i++) {
767        newList.Add(i, oldList.List[i]);
768      }
769    }
770
771    private bool IsEqual(ClientInfo ci1, ClientInfo ci2) {
772      if (ci2 == null) {
773        return false;
774      }
775      if (ci1.Id.Equals(ci2.Id)) {
776        return true;
777      } else return false;
778    }
779
780    private int CapacityRam(int noCores, int freeCores) {
781      if (noCores > 0) {
782        int capacity = ((noCores - freeCores) / noCores) * 100;
783        return capacity;
784      }
785      return 100;
786    }
787
788    private void GetDelta(IList<Job> oldJobs, IDictionary<int, Job> helpJobs) {
789      bool found = false;
790      for (int i = 0; i < jobs.List.Count; i++) {
791        Job job = jobs.List[i];
792        for (int j = 0; j < oldJobs.Count; j++) {
793
794          Job jobold = oldJobs[j];
795
796          if (job.Id.Equals(jobold.Id)) {
797
798            found = true;
799            bool change = false;
800            if (job.State != jobold.State) {
801              change = true;
802            }
803            if (job.State != State.offline) {
804              if ((!IsEqual(job.Client, jobold.Client)) || (job.State != jobold.State)
805                   || (job.Percentage != jobold.Percentage)) {
806                change = true;
807              }
808            } else if (job.DateCalculated != jobold.DateCalculated) {
809              change = true;
810            }
811            if (change) {
812              changes.Add(new Changes { Types = Type.Job, ID = job.Id, ChangeType = Change.Update, Position = i });
813            }
814
815            int removeAt = -1;
816            foreach (KeyValuePair<int, Job> kvp in helpJobs) {
817              if (job.Id.Equals(kvp.Value.Id)) {
818                removeAt = kvp.Key;
819                break;
820              }
821            }
822            if (removeAt >= 0) {
823              helpJobs.Remove(removeAt);
824            }
825            break;
826          }
827
828        }
829        if (found == false) {
830          changes.Add(new Changes { Types = Type.Job, ID = job.Id, ChangeType = Change.Create });
831        }
832        found = false;
833      }
834      foreach (KeyValuePair<int, Job> kvp in helpJobs) {
835        changes.Add(new Changes { Types = Type.Job, ID = kvp.Value.Id, ChangeType = Change.Delete, Position = kvp.Key });
836      }
837    }
838
839    private void GetSnapshotList() {
840
841      lvSnapshots.Items.Clear();
842      IJobManager jobManager = ServiceLocator.GetJobManager();
843
844      ResponseList<JobResult> jobRes = jobManager.GetAllJobResults(currentJob.Id);
845
846      if (jobRes.List != null) {
847        foreach (JobResult jobresult in jobRes.List) {
848          ListViewItem curSnapshot = new ListViewItem(jobresult.ClientId.ToString());
849          double percentage = jobresult.Percentage * 100;
850          curSnapshot.SubItems.Add(percentage.ToString() + " %");
851          curSnapshot.SubItems.Add(jobresult.Timestamp.ToString());
852          lvSnapshots.Items.Add(curSnapshot);
853        }
854      }
855
856      if ((jobRes.List == null) && (jobRes.List.Count == 0)) {
857        lvSnapshots.Visible = false;
858      } else {
859        lvSnapshots.Visible = true;
860      }
861
862    }
863
864    #endregion
865
866
867
868  }
869}
Note: See TracBrowser for help on using the repository browser.