Free cookie consent management tool by TermsFeed Policy Generator

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

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

Removed References to HiveLogging and updated the default logging mechanism (#991)

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