ProcessPanel.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.ComponentModel;
  30. using System.Data;
  31. using System.Diagnostics;
  32. using System.Drawing;
  33. using System.Text;
  34. using System.Windows.Forms;
  35. namespace OpenSim.GridLaunch.GUI.WinForm
  36. {
  37. public partial class ProcessPanel : Form, IGUI
  38. {
  39. public ProcessPanel()
  40. {
  41. Application.EnableVisualStyles();
  42. //Application.SetCompatibleTextRenderingDefault(false);
  43. InitializeComponent();
  44. Program.AppCreated += Program_AppCreated;
  45. Program.AppRemoved += Program_AppRemoved;
  46. Program.AppConsoleOutput += Program_AppConsoleOutput;
  47. Program.AppConsoleError += Program_AppConsoleError;
  48. log4netAppender.LogLine += log4netAppender_LogLine;
  49. }
  50. #region Module Start / Stop
  51. public void StartGUI()
  52. {
  53. Application.Run(this);
  54. }
  55. public void StopGUI()
  56. {
  57. this.Close();
  58. }
  59. #endregion
  60. #region Main log tab
  61. void log4netAppender_LogLine(Color color, string LogText)
  62. {
  63. ucLogWindow1.Write(color, LogText);
  64. }
  65. #endregion
  66. #region Form events
  67. private void btnShutdown_Click(object sender, EventArgs e)
  68. {
  69. Program.Shutdown();
  70. }
  71. #endregion
  72. #region GridLaunch Events
  73. public delegate void Program_AppCreatedDelegate(string App);
  74. public void Program_AppCreated(string App)
  75. {
  76. if (this.InvokeRequired) {
  77. this.Invoke(new Program_AppCreatedDelegate(Program_AppCreated), App);
  78. return;
  79. }
  80. Trace.WriteLine("Start: " + App);
  81. // Do we already have app window for that app?
  82. if (AppWindow_Get(App) != null)
  83. return;
  84. // New log window
  85. ucAppWindow aw = new ucAppWindow();
  86. // New tab page
  87. TabPage tp = new TabPage(App);
  88. // Add log window into tab page
  89. tp.Controls.Add(aw);
  90. // Add tab page into tab control
  91. tabControl1.TabPages.Add(tp);
  92. // Add it all to our internal list
  93. AppWindow_Add(App, aw);
  94. // Hook up events
  95. aw.LineEntered += AppWindow_LineEntered;
  96. // Fill log window fully inside tab page
  97. aw.Dock = DockStyle.Fill;
  98. }
  99. public delegate void Program_AppRemovedDelegate(string App);
  100. public void Program_AppRemoved(string App)
  101. {
  102. if (this.InvokeRequired) {
  103. this.Invoke(new Program_AppRemovedDelegate(Program_AppRemoved), App);
  104. return;
  105. }
  106. Trace.WriteLine("Stop: " + App);
  107. // Get app window
  108. ucAppWindow aw = AppWindow_Get(App);
  109. if (aw == null)
  110. return;
  111. // Get its tab page
  112. TabPage tp = aw.Parent as TabPage;
  113. if (tp != null)
  114. {
  115. // Remove tab page from tab control
  116. tabControl1.TabPages.Remove(tp);
  117. // Remove app window from tab
  118. tp.Controls.Remove(aw);
  119. }
  120. // Dispose of app window
  121. aw.Dispose();
  122. // Dispose of tab page
  123. if (tp != null)
  124. tp.Dispose();
  125. // Remove from our internal list
  126. AppWindow_Remove(App);
  127. }
  128. public delegate void Program_AppConsoleOutputDelegate(string App, string LogText);
  129. void Program_AppConsoleOutput(string App, string LogText)
  130. {
  131. if (this.InvokeRequired)
  132. {
  133. this.Invoke(new Program_AppConsoleOutputDelegate(Program_AppConsoleOutput), App, LogText);
  134. return;
  135. }
  136. // Get app window
  137. ucAppWindow aw = AppWindow_Get(App);
  138. // Write text to it
  139. if (aw != null)
  140. aw.Write(System.Drawing.Color.Black, LogText);
  141. }
  142. public delegate void Program_AppConsoleErrorDelegate(string App, string LogText);
  143. void Program_AppConsoleError(string App, string LogText)
  144. {
  145. if (this.InvokeRequired) {
  146. this.Invoke(new Program_AppConsoleErrorDelegate(Program_AppConsoleError), App, LogText);
  147. return;
  148. }
  149. // Get app window
  150. ucAppWindow aw = AppWindow_Get(App);
  151. // Write text to it
  152. if (aw != null)
  153. aw.Write(System.Drawing.Color.Red, LogText);
  154. }
  155. #endregion
  156. #region App Window events
  157. private void AppWindow_LineEntered(ucAppWindow AppWindow, string LogText)
  158. {
  159. Program.WriteLine(AppWindow_Get(AppWindow), LogText);
  160. }
  161. #endregion
  162. private void ProcessPanel_Load(object sender, EventArgs e)
  163. {
  164. string[] arr = new string[Program.Settings.Components.Keys.Count];
  165. Program.Settings.Components.Keys.CopyTo(arr, 0);
  166. cblStartupComponents.Items.AddRange(arr);
  167. // Now correct all check states
  168. for (int i = 0; i < cblStartupComponents.Items.Count; i++)
  169. {
  170. string _name = cblStartupComponents.Items[i] as string;
  171. bool _checked = Program.Settings.Components[_name];
  172. cblStartupComponents.SetItemChecked(i, _checked);
  173. }
  174. }
  175. #region Internal App Window list and functions
  176. private Dictionary<string, ucAppWindow> _appWindows = new Dictionary<string, ucAppWindow>();
  177. private Dictionary<ucAppWindow, string> _appWindows_rev = new Dictionary<ucAppWindow, string>();
  178. private void AppWindow_Add(string AppWindowName, ucAppWindow AppWindow)
  179. {
  180. lock (_appWindows)
  181. {
  182. _appWindows.Add(AppWindowName, AppWindow);
  183. _appWindows_rev.Add(AppWindow, AppWindowName);
  184. // Hook events
  185. AppWindow.LineEntered += AppWindow_LineEntered;
  186. }
  187. }
  188. private void AppWindow_Remove(ucAppWindow AppWindow)
  189. {
  190. lock (_appWindows)
  191. {
  192. if (_appWindows_rev.ContainsKey(AppWindow))
  193. {
  194. // Unhook events
  195. AppWindow.LineEntered -= AppWindow_LineEntered;
  196. // Delete from list
  197. string name = _appWindows_rev[AppWindow];
  198. _appWindows.Remove(name);
  199. _appWindows_rev.Remove(AppWindow);
  200. }
  201. }
  202. }
  203. private void AppWindow_Remove(string AppWindowName)
  204. {
  205. lock (_appWindows)
  206. {
  207. if (_appWindows.ContainsKey(AppWindowName))
  208. {
  209. ucAppWindow AppWindow = _appWindows[AppWindowName];
  210. // Unhook events
  211. AppWindow.LineEntered -= AppWindow_LineEntered;
  212. // Delete from list
  213. _appWindows.Remove(AppWindowName);
  214. _appWindows_rev.Remove(AppWindow);
  215. }
  216. }
  217. }
  218. private string AppWindow_Get(ucAppWindow AppWindow)
  219. {
  220. lock (_appWindows)
  221. {
  222. if (_appWindows_rev.ContainsKey(AppWindow))
  223. return _appWindows_rev[AppWindow];
  224. }
  225. return null;
  226. }
  227. private ucAppWindow AppWindow_Get(string AppWindowName)
  228. {
  229. lock (_appWindows)
  230. {
  231. if (_appWindows.ContainsKey(AppWindowName))
  232. return _appWindows[AppWindowName];
  233. }
  234. return null;
  235. }
  236. #endregion
  237. private void btnSave_Click(object sender, EventArgs e)
  238. {
  239. Program.Settings.Components.Clear();
  240. for (int i = 0; i < cblStartupComponents.Items.Count; i++)
  241. {
  242. string _name = cblStartupComponents.Items[i] as string;
  243. bool _checked = cblStartupComponents.GetItemChecked(i);
  244. Program.Settings.Components.Add(_name, _checked);
  245. Program.Settings.SaveConfig();
  246. }
  247. }
  248. }
  249. }