Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.Hive.Server.Console/3.2/HiveServerManagementConsole.cs @ 3931

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

added minor speedups and better transaction handling to the server (#828)

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