source: trunk/sources/HeuristicLab.Clients.Hive.Slave.Views/3.3/SlaveMainViewBase.cs @ 7135

Last change on this file since 7135 was 7135, checked in by ascheibe, 11 years ago

#1672 some fixes for the slave tray ui:

  • removed some more magic numbers
  • fixed reconnecting to windows service when it was stopped
  • added more time for stopping/starting windows service so that no exception is thrown because of a timeout
File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Threading;
24using System.Threading.Tasks;
25using System.Windows.Forms;
26using HeuristicLab.Clients.Hive.SlaveCore.Views.Properties;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29
30namespace HeuristicLab.Clients.Hive.SlaveCore.Views {
31
32  [View("HeuristicLab Slave Main View Base")]
33  [Content(typeof(SlaveItem), IsDefaultView = false)]
34  public partial class SlaveMainViewBase : ItemView {
35
36    public event EventHandler VisibilitySwitched;
37    public void OnVisibilitySwitched() {
38      var handler = VisibilitySwitched;
39      if (handler != null) handler(this, new EventArgs());
40    }
41
42    public new SlaveItem Content {
43      get { return (SlaveItem)base.Content; }
44      set {
45        if (base.Content != value) {
46          base.Content = value;
47        }
48      }
49    }
50
51    void Content_UserVisibleMessageFired(object sender, Common.EventArgs<string> e) {
52      if (Settings.Default.ShowBalloonTips) {
53        notifyIcon.ShowBalloonTip(2000, "HeuristicLab Hive", e.Value, ToolTipIcon.Info);
54      }
55    }
56
57    public SlaveMainViewBase() {
58      InitializeComponent();
59    }
60
61    #region Register Content Events
62    protected override void DeregisterContentEvents() {
63      Content.CoreConnectionChanged -= new EventHandler<Common.EventArgs<CoreConnection>>(Content_CoreConnectionChanged);
64      Content.SlaveDisplayStateChanged -= new EventHandler<Common.EventArgs<SlaveDisplayStat>>(Content_SlaveDisplayStateChanged);
65      base.DeregisterContentEvents();
66    }
67
68    protected override void RegisterContentEvents() {
69      base.RegisterContentEvents();
70      Content.CoreConnectionChanged += new EventHandler<Common.EventArgs<CoreConnection>>(Content_CoreConnectionChanged);
71      Content.SlaveDisplayStateChanged += new EventHandler<Common.EventArgs<SlaveDisplayStat>>(Content_SlaveDisplayStateChanged);
72    }
73
74    void Content_SlaveDisplayStateChanged(object sender, Common.EventArgs<SlaveDisplayStat> e) {
75      if (e.Value == SlaveDisplayStat.NoService) {
76        Task.Factory.StartNew(Connector);
77      }
78    }
79
80    void Content_CoreConnectionChanged(object sender, Common.EventArgs<CoreConnection> e) {
81      if (e.Value == CoreConnection.Offline) {
82        Task.Factory.StartNew(Connector);
83      }
84    }
85    #endregion
86
87    protected override void OnContentChanged() {
88      base.OnContentChanged();
89
90      logView.Content = Content;
91      slaveView.Content = Content;
92      if (Content != null) {
93        Content.UserVisibleMessageFired += new System.EventHandler<Common.EventArgs<string>>(Content_UserVisibleMessageFired);
94        Task.Factory.StartNew(Connector);
95      } else {
96        Content.UserVisibleMessageFired -= new System.EventHandler<Common.EventArgs<string>>(Content_UserVisibleMessageFired);
97      }
98    }
99
100    protected override void SetEnabledStateOfControls() {
101      base.SetEnabledStateOfControls();
102    }
103
104    private void notifyIcon_Click(object sender, MouseEventArgs e) {
105      if (e.Button == MouseButtons.Left) {
106        OnVisibilitySwitched();
107      }
108    }
109
110    private void closeToolStripMenuItem_Click(object sender, EventArgs e) {
111      Application.Exit();
112    }
113
114    private void homepageToolStripMenuItem_Click(object sender, EventArgs e) {
115      System.Diagnostics.Process.Start("http://dev.heuristiclab.com");
116    }
117
118    private void notifyIcon_BalloonTipClicked(object sender, EventArgs e) {
119      OnVisibilitySwitched();
120    }
121
122    private void Connector() {
123      ((SlaveItem)base.Content).Open();
124      bool connected = false;
125      while (!connected) {
126        connected = ((SlaveItem)base.Content).ReconnectToSlaveCore();
127
128        if (!connected) {
129          Thread.Sleep(Settings.Default.ServiceReconnectTimeout);
130        }
131      }
132      this.Invoke(new Action(SetEnabledStateOfControls));
133    }
134
135    private void btnAbout_Click(object sender, EventArgs e) {
136      AboutDialog dialog = new AboutDialog();
137      dialog.ShowDialog();
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.