Free cookie consent management tool by TermsFeed Policy Generator

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

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

changed login, exception handling on login failed (#626)

File size: 30.3 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      #region Job
219      ResponseList<Job> jobsOld = jobs;
220      try {
221        IJobManager jobManager =
222                ServiceLocator.GetJobManager();
223
224        jobs = jobManager.GetAllJobs();
225
226        IDictionary<int, Job> jobsOldHelp;
227        CloneList(jobsOld, out jobsOldHelp);
228
229        GetDelta(jobsOld.List, jobsOldHelp);
230
231      }
232      catch (FaultException fe) {
233        MessageBox.Show(fe.Message);
234      }
235
236      #endregion
237    }
238
239    #endregion
240
241
242    /// <summary>
243    /// Adds Exceptionobs to ListView and TreeView
244    /// </summary>
245    private void AddJobs() {
246      try {
247        List<ListViewItem> jobObjects = new List<ListViewItem>();
248        IJobManager jobManager =
249          ServiceLocator.GetJobManager();
250        jobs = jobManager.GetAllJobs();
251
252        lvJobControl.Items.Clear();
253
254        ListViewGroup lvJobCalculating = new ListViewGroup("calculating", HorizontalAlignment.Left);
255        ListViewGroup lvJobFinished = new ListViewGroup("finished", HorizontalAlignment.Left);
256        ListViewGroup lvJobPending = new ListViewGroup("pending", HorizontalAlignment.Left);
257
258        jobGroup = new List<ListViewGroup>();
259        jobGroup.Add(lvJobCalculating);
260        jobGroup.Add(lvJobFinished);
261        jobGroup.Add(lvJobPending);
262
263        if (jobs != null && jobs.List != null) {
264
265          foreach (Job job in jobs.List) {
266            if (job.State == State.calculating) {
267              ListViewItem lvi = new ListViewItem(job.Id.ToString(), 1, lvJobCalculating);
268              lvi.Tag = job;
269              jobObjects.Add(lvi);
270
271              lvi.ToolTipText = (job.Percentage * 100) + "% of job calculated";
272            } else if (job.State == State.finished) {
273              ListViewItem lvi = new ListViewItem(job.Id.ToString(), 0, lvJobFinished);
274              lvi.Tag = job;
275              jobObjects.Add(lvi);
276              //lvJobControl.Items.Add(lvi);
277            } else if (job.State == State.offline || job.State == State.pending) {
278              ListViewItem lvi = new ListViewItem(job.Id.ToString(), 2, lvJobPending);
279              lvi.Tag = job;
280              jobObjects.Add(lvi);
281            }
282          } // Jobs
283          lvJobControl.BeginUpdate();
284          foreach (ListViewItem lvi in jobObjects) {
285            lvJobControl.Items.Add(lvi);
286          }
287          // actualization
288          lvJobControl.Groups.Add(lvJobCalculating);
289          lvJobControl.Groups.Add(lvJobFinished);
290          lvJobControl.Groups.Add(lvJobPending);
291          lvJobControl.EndUpdate();
292
293          if (currentJob != null) {
294            JobClicked();
295          }
296        }
297      }
298      catch (Exception ex) {
299        closeFormEvent(true, true);
300        this.Close();
301      }
302    }
303
304    List<ListViewGroup> lvClientGroups;
305    private void AddClients() {
306      lvClientGroups = new List<ListViewGroup>();
307      clientList.Clear();
308      tvClientControl.Nodes.Clear();
309      try {
310        ResponseList<ClientGroup> clientGroups = ClientManager.GetAllClientGroups();
311
312        foreach (ClientGroup cg in clientGroups.List) {
313          AddClientOrGroup(cg, null);
314        }
315
316        if (currentGroupNode != null) {
317          lvClientControl.Items.Clear();
318          lvClientControl.Groups.Clear();
319          AddGroupsToListView(currentGroupNode);
320        }
321        tvClientControl.ExpandAll();
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
517    private void RefreshForm() {
518      foreach (Changes change in changes) {
519        if (change.Types == Type.Job) {
520          RefreshJob(change);
521        }
522      }
523    }
524
525    private void RefreshJob(Changes change) {
526      if (change.ChangeType == Change.Update) {
527        for (int i = 0; i < lvJobControl.Items.Count; i++) {
528          if (lvJobControl.Items[i].Text == change.ID.ToString()) {
529            foreach (Job job in jobs.List) {
530              if (job.Id == change.ID) {
531                lvJobControl.Items[i].Tag = job;
532                if (currentJob != null) {
533                  if (currentJob.Id == change.ID) {
534                    currentJob = job;
535                    JobClicked();
536                  }
537                }
538                break;
539              }
540            }
541            State state = jobs.List[change.Position].State;
542            if (state == State.finished) {
543              lvJobControl.Items[i].Group = jobGroup[1];
544              lvJobControl.Items[i].ImageIndex = 0;
545            } else if (state == State.calculating) {
546              lvJobControl.Items[i].Group = jobGroup[0];
547              lvJobControl.Items[i].ImageIndex = 1;
548            } else if (state == State.offline || state == State.pending) {
549              lvJobControl.Items[i].Group = jobGroup[2];
550              lvJobControl.Items[i].ImageIndex = 2;
551            }
552            lvJobControl.Refresh();
553          }
554        }
555      } else if (change.ChangeType == Change.Create) {
556
557        ListViewItem lvi = new ListViewItem(
558          change.ID.ToString(), 2, jobGroup[2]);
559        foreach (Job job in jobs.List) {
560          if (job.Id == change.ID) {
561            lvi.Tag = job;
562            break;
563          }
564        }
565        lvJobControl.Items.Add(lvi);
566
567      } else if (change.ChangeType == Change.Delete) {
568        for (int i = 0; i < lvJobControl.Items.Count; i++) {
569          if (change.ID.ToString() == lvJobControl.Items[i].Text.ToString()) {
570            lvJobControl.Items[i].Remove();
571            break;
572          }
573        }
574      }
575    }
576
577
578    #region Eventhandlers
579    /// <summary>
580    /// Send event to Login-GUI when closing
581    /// </summary>
582    /// <param name="sender"></param>
583    /// <param name="e"></param>
584    private void Close_Click(object sender, EventArgs e) {
585      if (closeFormEvent != null) {
586        closeFormEvent(true, false);
587      }
588      this.Close();
589    }
590
591    /// <summary>
592    /// Send evnt to Login-GUI when closing
593    /// </summary>
594    /// <param name="sender"></param>
595    /// <param name="e"></param>
596    private void HiveServerConsoleInformation_FormClosing(object sender, FormClosingEventArgs e) {
597      if (closeFormEvent != null) {
598        closeFormEvent(true, false);
599      }
600    }
601
602    private void AddJob_Click(object sender, EventArgs e) {
603      AddJobForm newForm = new AddJobForm();
604      newForm.Show();
605      newForm.addJobEvent += new addDelegate(updaterWoker.RunWorkerAsync);
606    }
607
608    private void OnLVJobControlClicked(object sender, EventArgs e) {
609      currentJob = (Job)lvJobControl.SelectedItems[0].Tag;
610      JobClicked();
611    }
612
613    private void lvJobControl_MouseMove(object sender, MouseEventArgs e) {
614      if ((lvJobControl.GetItemAt(e.X, e.Y) != null) &&
615        (lvJobControl.GetItemAt(e.X, e.Y).ToolTipText != null)) {
616        tt.SetToolTip(lvJobControl, lvJobControl.GetItemAt(e.X, e.Y).ToolTipText);
617      }
618    }
619
620    private void lvJobControl_MouseUp(object sender, MouseEventArgs e) {
621      // If the right mouse button was clicked and released,
622      // display the shortcut menu assigned to the ListView.
623      lvJobControl.ContextMenuStrip.Items.Clear();
624      ListViewHitTestInfo hitTestInfo = lvJobControl.HitTest(e.Location);
625      if (e.Button == MouseButtons.Right && hitTestInfo.Item != null && lvJobControl.SelectedItems.Count == 1) {
626        Job selectedJob = (Job)lvJobControl.SelectedItems[0].Tag;
627
628        if (selectedJob != null && selectedJob.State == State.calculating) {
629          lvJobControl.ContextMenuStrip.Items.Add(menuItemAbortJob);
630          lvJobControl.ContextMenuStrip.Items.Add(menuItemGetSnapshot);
631        }
632      }
633      lvJobControl.ContextMenuStrip.Show(new Point(e.X, e.Y));
634    }
635
636    private void tvClientControl_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
637      lvClientControl.Items.Clear();
638      lvClientControl.Groups.Clear();
639      currentGroupNode = e.Node;
640      AddGroupsToListView(e.Node);
641    }
642
643    private void groupToolStripMenuItem_Click(object sender, EventArgs e) {
644      AddGroup addgroup = new AddGroup();
645      parentgroup = Guid.Empty;
646      if ((tvClientControl.SelectedNode != null) && (((ClientGroup)tvClientControl.SelectedNode.Tag).Id != Guid.Empty)) {
647        parentgroup = ((ClientGroup)tvClientControl.SelectedNode.Tag).Id;
648      }
649      addgroup.addGroupEvent += new AddGroupDelegate(addgroup_addGroupEvent);
650      addgroup.Show();
651    }
652
653    private void projectToolStripMenuItem_Click(object sender, EventArgs e) {
654      AddProject addproject = new AddProject();
655      addproject.AddProjectEvent += new AddProjectDelegate(addproject_AddProjectEvent);
656      addproject.Show();
657    }
658
659    private void OnLVClientClicked(object sender, EventArgs e) {
660      currentClient = (ClientInfo)lvClientControl.SelectedItems[0].Tag;
661      ClientClicked();
662    }
663
664    private void tvClientControl_MouseUp(object sender, MouseEventArgs e) {
665      // If the right mouse button was clicked and released,
666      // display the shortcut menu assigned to the ListView.
667      contextMenuGroup.Items.Clear();
668      TreeViewHitTestInfo hitTestInfo = tvClientControl.HitTest(e.Location);
669      tvClientControl.SelectedNode = hitTestInfo.Node;
670      if (e.Button != MouseButtons.Right) return;
671      if (hitTestInfo.Node != null) {
672        Resource selectedGroup = (Resource)tvClientControl.SelectedNode.Tag;
673
674        if (selectedGroup != null) {
675          contextMenuGroup.Items.Add(menuItemAddGroup);
676          contextMenuGroup.Items.Add(menuItemDeleteGroup);
677        }
678      } else {
679        contextMenuGroup.Items.Add(menuItemAddGroup);
680      }
681      tvClientControl.ContextMenuStrip.Show(tvClientControl, new Point(e.X, e.Y));
682    }
683
684    private void addproject_AddProjectEvent(string name) {
685      IJobManager jobManager = ServiceLocator.GetJobManager();
686
687      Project pg = new Project() { Name = name };
688      jobManager.CreateProject(pg);
689
690    }
691
692    private void addgroup_addGroupEvent(string name) {
693      IClientManager clientManager = ServiceLocator.GetClientManager();
694
695      if (parentgroup != Guid.Empty) {
696        ClientGroup cg = new ClientGroup() { Name = name };
697        ResponseObject<ClientGroup> respcg = clientManager.AddClientGroup(cg);
698        Response res = clientManager.AddResourceToGroup(parentgroup, respcg.Obj);
699        if (res != null && !res.Success) {
700          MessageBox.Show(res.StatusMessage, "Error adding Group", MessageBoxButtons.OK, MessageBoxIcon.Error);
701        }
702      } else {
703        ClientGroup cg = new ClientGroup() { Name = name };
704        clientManager.AddClientGroup(cg);
705      }
706      AddClients();
707    }
708
709
710    private void Refresh_Click(object sender, EventArgs e) {
711      Form overlayingForm = new Form();
712
713      overlayingForm.Show();
714      overlayingForm.FormBorderStyle = FormBorderStyle.None;
715      overlayingForm.BackColor = Color.Gray;
716      overlayingForm.Opacity = 0.4;
717      overlayingForm.Size = this.Size;
718      overlayingForm.Location = this.Location;
719
720      //Label lbl = new Label();
721      //overlayingForm.Controls.Add(lbl);
722      //lbl.AutoSize = true;
723      //lbl.Text = "Loading";
724      //lbl.Name = "lblName";
725      //lbl.Font = new System.Drawing.Font("Microsoft Sans Serif", 20F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
726      //lbl.ForeColor = Color.Black;
727      //lbl.BackColor = Color.Transparent;
728      //lbl.Location = new Point(overlayingForm.Width / 2, overlayingForm.Height / 2);
729
730      AddClients();
731
732      overlayingForm.Close();
733    }
734
735    private void largeIconsToolStripMenuItem_Click(object sender, EventArgs e) {
736      lvClientControl.View = View.LargeIcon;
737      lvJobControl.View = View.LargeIcon;
738      largeIconsToolStripMenuItem.CheckState = CheckState.Checked;
739      smallIconsToolStripMenuItem.CheckState = CheckState.Unchecked;
740      listToolStripMenuItem.CheckState = CheckState.Unchecked;
741    }
742
743    private void smallIconsToolStripMenuItem_Click(object sender, EventArgs e) {
744      lvClientControl.View = View.SmallIcon;
745      lvJobControl.View = View.SmallIcon;
746      largeIconsToolStripMenuItem.CheckState = CheckState.Unchecked;
747      smallIconsToolStripMenuItem.CheckState = CheckState.Checked;
748      listToolStripMenuItem.CheckState = CheckState.Unchecked;
749    }
750
751    private void listToolStripMenuItem_Click(object sender, EventArgs e) {
752      lvClientControl.View = View.List;
753      lvJobControl.View = View.List;
754      largeIconsToolStripMenuItem.CheckState = CheckState.Unchecked;
755      smallIconsToolStripMenuItem.CheckState = CheckState.Unchecked;
756      listToolStripMenuItem.CheckState = CheckState.Checked;
757    }
758
759    private void tvClientControl_DragLeave(object sender, EventArgs e) {
760      foreach (TreeNode node in tvClientControl.Nodes) {
761        node.BackColor = Color.White;
762        node.ForeColor = Color.Black;
763      }
764    }
765    #endregion
766
767    #region Helper methods
768
769    private void CloneList(ResponseList<Job> oldList, out IDictionary<int, Job> newList) {
770      newList = new Dictionary<int, Job>();
771      for (int i = 0; i < oldList.List.Count; i++) {
772        newList.Add(i, oldList.List[i]);
773      }
774    }
775
776    private bool IsEqual(ClientInfo ci1, ClientInfo ci2) {
777      if (ci2 == null) {
778        return false;
779      }
780      if (ci1.Id.Equals(ci2.Id)) {
781        return true;
782      } else return false;
783    }
784
785    private int CapacityRam(int noCores, int freeCores) {
786      if (noCores > 0) {
787        int capacity = ((noCores - freeCores) / noCores) * 100;
788        return capacity;
789      }
790      return 100;
791    }
792
793    private void GetDelta(IList<Job> oldJobs, IDictionary<int, Job> helpJobs) {
794      bool found = false;
795      for (int i = 0; i < jobs.List.Count; i++) {
796        Job job = jobs.List[i];
797        for (int j = 0; j < oldJobs.Count; j++) {
798
799          Job jobold = oldJobs[j];
800
801          if (job.Id.Equals(jobold.Id)) {
802
803            found = true;
804            bool change = false;
805            if (job.State != jobold.State) {
806              change = true;
807            }
808            if (job.State != State.offline) {
809              if ((!IsEqual(job.Client, jobold.Client)) || (job.State != jobold.State)
810                   || (job.Percentage != jobold.Percentage)) {
811                change = true;
812              }
813            } else if (job.DateCalculated != jobold.DateCalculated) {
814              change = true;
815            }
816            if (change) {
817              changes.Add(new Changes { Types = Type.Job, ID = job.Id, ChangeType = Change.Update, Position = i });
818            }
819
820            int removeAt = -1;
821            foreach (KeyValuePair<int, Job> kvp in helpJobs) {
822              if (job.Id.Equals(kvp.Value.Id)) {
823                removeAt = kvp.Key;
824                break;
825              }
826            }
827            if (removeAt >= 0) {
828              helpJobs.Remove(removeAt);
829            }
830            break;
831          }
832
833        }
834        if (found == false) {
835          changes.Add(new Changes { Types = Type.Job, ID = job.Id, ChangeType = Change.Create });
836        }
837        found = false;
838      }
839      foreach (KeyValuePair<int, Job> kvp in helpJobs) {
840        changes.Add(new Changes { Types = Type.Job, ID = kvp.Value.Id, ChangeType = Change.Delete, Position = kvp.Key });
841      }
842    }
843
844    private void GetSnapshotList() {
845
846      lvSnapshots.Items.Clear();
847      IJobManager jobManager = ServiceLocator.GetJobManager();
848
849      ResponseList<JobResult> jobRes = jobManager.GetAllJobResults(currentJob.Id);
850
851      if (jobRes.List != null) {
852        foreach (JobResult jobresult in jobRes.List) {
853          ListViewItem curSnapshot = new ListViewItem(jobresult.ClientId.ToString());
854          double percentage = jobresult.Percentage * 100;
855          curSnapshot.SubItems.Add(percentage.ToString() + " %");
856          curSnapshot.SubItems.Add(jobresult.Timestamp.ToString());
857          lvSnapshots.Items.Add(curSnapshot);
858        }
859      }
860
861      if ((jobRes.List == null) && (jobRes.List.Count == 0)) {
862        lvSnapshots.Visible = false;
863      } else {
864        lvSnapshots.Visible = true;
865      }
866
867    }
868
869    #endregion
870
871
872
873  }
874}
Note: See TracBrowser for help on using the repository browser.