Free cookie consent management tool by TermsFeed Policy Generator

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

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

check if there are groups or jobs (#600)

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