Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.Console/3.2/HiveServerManagementConsole.cs @ 3220

Last change on this file since 3220 was 3220, checked in by kgrading, 14 years ago

improved the DAL further, changed minor details for the presentation (#830)

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