PageRenderTime 1726ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Ec2BootstrapperGUI/Ec2BootstrapperGUI/Dashbord.xaml.cs

#
C# | 319 lines | 277 code | 35 blank | 7 comment | 49 complexity | b2b1158f226626dced50919fbffa7e96 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Shapes;
  13. using System.Configuration;
  14. using Ec2Bootstrapperlib;
  15. using System.Windows.Media.Animation;
  16. using System.Threading;
  17. using System.Collections.ObjectModel;
  18. namespace Ec2BootstrapperGUI
  19. {
  20. /// <summary>
  21. /// Interaction logic for Window1.xaml
  22. /// </summary>
  23. public partial class Dashboard : Window
  24. {
  25. Thread _monitor = null;
  26. public Dashboard()
  27. {
  28. this.InitializeComponent();
  29. configurationNotReady();
  30. checkConfig();
  31. }
  32. ObservableCollection<CEc2Instance> instances = null;
  33. private delegate void InstanceMonitor();
  34. void instanceMonitor()
  35. {
  36. try
  37. {
  38. do
  39. {
  40. if (instances == null)
  41. return;
  42. //every half minutes
  43. for (int index = 0; index < instances.Count; ++index)
  44. {
  45. CEc2Instance item = instances[index];
  46. if (item != null)
  47. {
  48. if (item.updateWebStatus() == true)
  49. {
  50. instances.RemoveAt(index);
  51. instances.Insert(index, item);
  52. }
  53. }
  54. }
  55. Thread.Sleep(30 * 1000);
  56. } while (true);
  57. }
  58. catch (ThreadAbortException)
  59. {
  60. }
  61. catch (Exception ex)
  62. {
  63. MessageBox.Show("Monitor thread caught exception: " + ex.Message);
  64. }
  65. _monitor = null;
  66. }
  67. public void checkConfig()
  68. {
  69. bool configured = false;
  70. bool configCrashed = false;
  71. try
  72. {
  73. configured = CAwsConfig.Instance.isConfigured();
  74. }
  75. catch (Exception ex)
  76. {
  77. System.Windows.MessageBox.Show(ex.Message, "EC2 Bootstrapper", MessageBoxButton.OK,
  78. MessageBoxImage.Error);
  79. configCrashed = true;
  80. }
  81. if(configured == true)
  82. {
  83. configurationReady();
  84. }
  85. else if (configCrashed == true)
  86. {
  87. configMenu.IsEnabled = false;
  88. }
  89. else
  90. {
  91. ProgBar.Visibility = Visibility.Hidden;
  92. AwsConfiguration config = new AwsConfiguration();
  93. config.dashboard = this;
  94. config.ShowDialog();
  95. }
  96. }
  97. public void configurationNotReady()
  98. {
  99. launchMenu.IsEnabled = false;
  100. refreshMenu.IsEnabled = false;
  101. }
  102. public void configurationReady()
  103. {
  104. launchMenu.IsEnabled = true;
  105. refreshMenu.IsEnabled = true;
  106. showInstances();
  107. }
  108. private void showInstances()
  109. {
  110. stopStatusUpdate();
  111. this.clientR.Children.Clear();
  112. InstanceList instList = new InstanceList();
  113. UserControl userControl = instList as UserControl;
  114. if (userControl != null)
  115. {
  116. userControl.VerticalAlignment = VerticalAlignment.Stretch;
  117. userControl.HorizontalAlignment = HorizontalAlignment.Stretch;
  118. instList.dashboard = this;
  119. this.clientR.Children.Insert(this.clientR.Children.Count, userControl);
  120. }
  121. }
  122. //file menu
  123. private void launchInstance_Click(object sender, RoutedEventArgs e)
  124. {
  125. AmiPicker newInstance = new AmiPicker(this);
  126. newInstance.Show();
  127. }
  128. public void stopStatusUpdate()
  129. {
  130. if (_monitor != null)
  131. {
  132. _monitor.Abort();
  133. _monitor.Join();
  134. _monitor = null;
  135. }
  136. }
  137. public void startStatusUpdate()
  138. {
  139. if (_monitor == null)
  140. {
  141. InstanceList ins = this.clientR.Children[0] as InstanceList;
  142. if (ins != null)
  143. {
  144. instances = ins.instances;
  145. if (instances != null)
  146. {
  147. _monitor = new Thread(instanceMonitor);
  148. _monitor.Start();
  149. }
  150. }
  151. }
  152. }
  153. private void deploy_Click(object sender, RoutedEventArgs e)
  154. {
  155. try
  156. {
  157. InstanceList inslist = this.clientR.Children[0] as InstanceList;
  158. if (inslist != null)
  159. {
  160. if (inslist.instancesLV.SelectedItem != null)
  161. {
  162. CEc2Instance inst = inslist.instancesLV.SelectedItem as CEc2Instance;
  163. if (inst != null)
  164. {
  165. AppDeployment pw = new AppDeployment(this);
  166. pw.instance = inst;
  167. pw.Show();
  168. }
  169. }
  170. }
  171. }
  172. catch (Exception ex)
  173. {
  174. MessageBox.Show(ex.Message);
  175. }
  176. }
  177. private void exit_Click(object sender, RoutedEventArgs e)
  178. {
  179. stopStatusUpdate();
  180. this.Close();
  181. }
  182. //view menu
  183. private void configureMenu_Click(object sender, RoutedEventArgs e)
  184. {
  185. AwsConfiguration config = new AwsConfiguration();
  186. config.dashboard = this;
  187. config.ShowDialog();
  188. }
  189. private void remoteConnect_Click(object sender, RoutedEventArgs e)
  190. {
  191. string argument = null;
  192. InstanceList inslist = this.clientR.Children[0] as InstanceList;
  193. if (inslist != null)
  194. {
  195. if (inslist.instancesLV.SelectedItem != null)
  196. {
  197. CEc2Instance inst = inslist.instancesLV.SelectedItem as CEc2Instance;
  198. if (inst != null)
  199. {
  200. if (string.IsNullOrEmpty(inst.publicDns) == false)
  201. {
  202. argument = "/v:" + inst.publicDns;
  203. }
  204. }
  205. }
  206. }
  207. System.Diagnostics.ProcessStartInfo procStartInfo;
  208. if(string.IsNullOrEmpty(argument) == true)
  209. procStartInfo = new System.Diagnostics.ProcessStartInfo("mstsc.exe ");
  210. else
  211. procStartInfo = new System.Diagnostics.ProcessStartInfo("mstsc.exe ", argument);
  212. System.Diagnostics.Process process = new System.Diagnostics.Process();
  213. process.StartInfo = procStartInfo;
  214. process.Start();
  215. }
  216. //help menu
  217. private void aboutMenu_Click(object sender, RoutedEventArgs e)
  218. {
  219. About about = new About();
  220. about.Show();
  221. }
  222. private void refreshMenu_Click(object sender, RoutedEventArgs e)
  223. {
  224. showInstances();
  225. }
  226. public void enableProgressBar()
  227. {
  228. ProgBar.Visibility = Visibility.Visible;
  229. ProgBar.IsIndeterminate = true;
  230. Duration duration = new Duration(TimeSpan.FromSeconds(10));
  231. DoubleAnimation doubleanimation = new DoubleAnimation(200.0, duration);
  232. ProgBar.BeginAnimation(System.Windows.Controls.ProgressBar.ValueProperty, doubleanimation);
  233. }
  234. public void disableProgressBar()
  235. {
  236. ProgBar.IsIndeterminate = false;
  237. ProgBar.BeginAnimation(System.Windows.Controls.ProgressBar.ValueProperty, null);
  238. ProgBar.Visibility = Visibility.Hidden;
  239. startStatusUpdate();
  240. }
  241. public void showStatus(string status)
  242. {
  243. StatusDesc.Text = status;
  244. if (string.Compare(status, ConstantString.NoInstance) == 0)
  245. {
  246. MessageBox.Show(
  247. "No machines were enumerated. If you suspect an error, check the configuration dialog via Tools | AWS Configuration.",
  248. "Display Instances",
  249. MessageBoxButton.OK,
  250. MessageBoxImage.Information);
  251. }
  252. }
  253. private void MinimizeButton_Click(object sender, RoutedEventArgs e)
  254. {
  255. this.WindowState = WindowState.Minimized;
  256. }
  257. private void ResizingButton_Click(object sender, RoutedEventArgs e)
  258. {
  259. if (this.WindowState == WindowState.Normal)
  260. this.WindowState = WindowState.Maximized;
  261. else
  262. this.WindowState = WindowState.Normal;
  263. }
  264. private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  265. {
  266. if (_monitor != null)
  267. {
  268. _monitor.Abort();
  269. _monitor.Join();
  270. }
  271. }
  272. private void LogoGloss_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  273. {
  274. this.DragMove();
  275. if (e.ClickCount == 2)
  276. {
  277. if (this.WindowState == WindowState.Normal)
  278. this.WindowState = WindowState.Maximized;
  279. else
  280. this.WindowState = WindowState.Normal;
  281. }
  282. }
  283. }
  284. }