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

/Overthere/Overthere/OverthereShell.cs

https://bitbucket.org/floAr/personal
C# | 248 lines | 206 code | 40 blank | 2 comment | 17 complexity | 8d53917091dd5cc1fd791a41b8cf8961 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. using System.Windows;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using Overthere.Interfaces;
  10. using System.ServiceModel;
  11. using System.Windows.Controls;
  12. using System.Windows.Documents;
  13. using System.Collections.ObjectModel;
  14. namespace Overthere
  15. {
  16. public class OverthereShell : ContentPresenter, IDisposable
  17. {
  18. TabControl _tabs;
  19. Dictionary<string, IConsoleCommand> _commands = new Dictionary<string, IConsoleCommand>();
  20. static readonly Type[] _builtInCommandTypes = new Type[]
  21. {
  22. typeof(SwitchCommand),
  23. typeof(SessionsCommand),
  24. typeof(NewCmdCommand),
  25. typeof(NewRemoteCommand),
  26. typeof(ExitCommand),
  27. };
  28. private Window _window;
  29. private List<ConsoleSession> _sessions;
  30. public ReadOnlyCollection<ConsoleSession> Sessions { get { return _sessions.AsReadOnly(); } }
  31. public ConsoleViewer CurrentViewer
  32. {
  33. get
  34. {
  35. return (ConsoleViewer)((TabItem)_tabs.SelectedItem).Content;
  36. }
  37. }
  38. public ConsoleSession CurrentSession
  39. {
  40. get
  41. {
  42. return CurrentViewer.Session;
  43. }
  44. set
  45. {
  46. foreach (TabItem item in _tabs.Items.Cast<TabItem>())
  47. {
  48. if (((ConsoleViewer)item.Content).Session == value)
  49. {
  50. _tabs.SelectedItem = item;
  51. }
  52. }
  53. }
  54. }
  55. public IEnumerable<ConsoleViewer> Viewers
  56. {
  57. get
  58. {
  59. return _tabs.Items.Cast<TabItem>().Select(i => i.Content).Cast<ConsoleViewer>();
  60. }
  61. }
  62. public OverthereShell(Window window)
  63. {
  64. _sessions = new List<ConsoleSession>();
  65. _window = window;
  66. _tabs = new TabControl()
  67. {
  68. Padding = new Thickness(0.0),
  69. };
  70. this.Content = _tabs;
  71. ConsoleSession localSession = new ConsoleSession("Cmd", new CmdConsole());
  72. AddSession(localSession);
  73. //ConsoleSession remoteSession = new ConsoleSession("Durham-Laptop", new RemoteConsole("Durham-Laptop"));
  74. //_sessions.Add(remoteSession);
  75. foreach (Type commandType in _builtInCommandTypes)
  76. {
  77. IConsoleCommand command = (IConsoleCommand)Activator.CreateInstance(commandType);
  78. _commands.Add(command.Command, command);
  79. }
  80. }
  81. public void AddSession(ConsoleSession session)
  82. {
  83. _sessions.Add(session);
  84. TabItem item = OverthereShell.CreateNewTab(session.Name);
  85. _tabs.Items.Add(item);
  86. ((ConsoleViewer)item.Content).Bind(this, session);
  87. if (_tabs.Items.Count == 1)
  88. {
  89. item.Visibility = Visibility.Collapsed;
  90. }
  91. else
  92. {
  93. foreach (TabItem tabItem in _tabs.Items)
  94. {
  95. tabItem.Visibility = Visibility.Visible;
  96. }
  97. }
  98. }
  99. public void RemoveSession(ConsoleSession session)
  100. {
  101. if (this.Sessions.Contains(session))
  102. {
  103. _sessions.Remove(session);
  104. foreach (TabItem item in _tabs.Items.Cast<TabItem>())
  105. {
  106. ConsoleViewer viewer = item.Content as ConsoleViewer;
  107. if (viewer.Session == session)
  108. {
  109. _tabs.Items.Remove(item);
  110. viewer.Unbind();
  111. viewer.Dispose();
  112. if (_tabs.Items.Count == 0)
  113. _window.Close();
  114. return;
  115. }
  116. }
  117. }
  118. }
  119. public void KeyPressed(KeyEventArgs e)
  120. {
  121. }
  122. public void KeyPreview(KeyEventArgs e)
  123. {
  124. if (KeyboardEx.IsControlDown && e.Key == Key.Tab)
  125. {
  126. _tabs.Focus();
  127. }
  128. }
  129. public bool HandleCommand(string commandString)
  130. {
  131. List<Token> tokens = Token.Split(commandString);
  132. if (tokens.Count > 0)
  133. {
  134. string commandKey = tokens[0].Value;
  135. IConsoleCommand command;
  136. if (_commands.TryGetValue(commandKey, out command))
  137. {
  138. try
  139. {
  140. CurrentViewer.WriteOutput(commandString + Environment.NewLine);
  141. object[] args = ArgumentProcessor.Process(tokens, command.ArgTypes);
  142. return command.Execute(this, args);
  143. }
  144. catch (InvalidOperationException)
  145. {
  146. CurrentViewer.WriteError("Use correct syntax: " + command.CorrectSyntax + Environment.NewLine);
  147. return false;
  148. }
  149. }
  150. }
  151. return true;
  152. }
  153. public void Dispose()
  154. {
  155. foreach (ConsoleSession session in this.Sessions)
  156. {
  157. session.Dispose();
  158. }
  159. _sessions.Clear();
  160. foreach (ConsoleViewer viewer in this.Viewers)
  161. {
  162. viewer.Dispose();
  163. }
  164. GC.SuppressFinalize(this);
  165. }
  166. public static ConsoleViewer CreateViewer()
  167. {
  168. ConsoleViewer viewer = new ConsoleViewer()
  169. {
  170. Background = Brushes.Black,
  171. Foreground = Brushes.White,
  172. VerticalScrollBarVisibility = ScrollBarVisibility.Visible,
  173. HorizontalAlignment = HorizontalAlignment.Stretch,
  174. FontFamily = new FontFamily("Consolas"),
  175. Padding = new Thickness(0.0),
  176. Margin = new Thickness(0.0),
  177. };
  178. viewer.Document = new FlowDocument()
  179. {
  180. PagePadding = new Thickness(0.0),
  181. TextAlignment = TextAlignment.Left,
  182. };
  183. viewer.BeginInit();
  184. viewer.EndInit();
  185. viewer.Document.BeginInit();
  186. viewer.Document.EndInit();
  187. return viewer;
  188. }
  189. public static TabItem CreateNewTab(string name)
  190. {
  191. ConsoleViewer viewer = OverthereShell.CreateViewer();
  192. TabItem item = new TabItem()
  193. {
  194. Header = name,
  195. BorderThickness = new Thickness(0.0),
  196. Content = viewer,
  197. };
  198. item.PreviewKeyDown += (s, e) =>
  199. {
  200. if (!e.Key.IsModifier() && e.Key != Key.Tab)
  201. {
  202. viewer.Focus();
  203. }
  204. };
  205. return item;
  206. }
  207. }
  208. }