/Overthere/Overthere/OverthereShell.cs
C# | 248 lines | 206 code | 40 blank | 2 comment | 17 complexity | 8d53917091dd5cc1fd791a41b8cf8961 MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.Windows;
- using System.Windows.Input;
- using System.Windows.Media;
- using Overthere.Interfaces;
- using System.ServiceModel;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Collections.ObjectModel;
-
- namespace Overthere
- {
- public class OverthereShell : ContentPresenter, IDisposable
- {
- TabControl _tabs;
-
- Dictionary<string, IConsoleCommand> _commands = new Dictionary<string, IConsoleCommand>();
- static readonly Type[] _builtInCommandTypes = new Type[]
- {
- typeof(SwitchCommand),
- typeof(SessionsCommand),
- typeof(NewCmdCommand),
- typeof(NewRemoteCommand),
- typeof(ExitCommand),
- };
-
- private Window _window;
-
- private List<ConsoleSession> _sessions;
- public ReadOnlyCollection<ConsoleSession> Sessions { get { return _sessions.AsReadOnly(); } }
-
- public ConsoleViewer CurrentViewer
- {
- get
- {
- return (ConsoleViewer)((TabItem)_tabs.SelectedItem).Content;
- }
- }
-
- public ConsoleSession CurrentSession
- {
- get
- {
- return CurrentViewer.Session;
- }
- set
- {
- foreach (TabItem item in _tabs.Items.Cast<TabItem>())
- {
- if (((ConsoleViewer)item.Content).Session == value)
- {
- _tabs.SelectedItem = item;
- }
- }
- }
- }
-
- public IEnumerable<ConsoleViewer> Viewers
- {
- get
- {
- return _tabs.Items.Cast<TabItem>().Select(i => i.Content).Cast<ConsoleViewer>();
- }
- }
-
- public OverthereShell(Window window)
- {
- _sessions = new List<ConsoleSession>();
-
- _window = window;
- _tabs = new TabControl()
- {
- Padding = new Thickness(0.0),
- };
- this.Content = _tabs;
-
- ConsoleSession localSession = new ConsoleSession("Cmd", new CmdConsole());
- AddSession(localSession);
-
- //ConsoleSession remoteSession = new ConsoleSession("Durham-Laptop", new RemoteConsole("Durham-Laptop"));
- //_sessions.Add(remoteSession);
-
- foreach (Type commandType in _builtInCommandTypes)
- {
- IConsoleCommand command = (IConsoleCommand)Activator.CreateInstance(commandType);
- _commands.Add(command.Command, command);
- }
- }
-
- public void AddSession(ConsoleSession session)
- {
- _sessions.Add(session);
-
- TabItem item = OverthereShell.CreateNewTab(session.Name);
- _tabs.Items.Add(item);
-
- ((ConsoleViewer)item.Content).Bind(this, session);
-
- if (_tabs.Items.Count == 1)
- {
- item.Visibility = Visibility.Collapsed;
- }
- else
- {
- foreach (TabItem tabItem in _tabs.Items)
- {
- tabItem.Visibility = Visibility.Visible;
- }
- }
- }
-
- public void RemoveSession(ConsoleSession session)
- {
- if (this.Sessions.Contains(session))
- {
- _sessions.Remove(session);
-
- foreach (TabItem item in _tabs.Items.Cast<TabItem>())
- {
- ConsoleViewer viewer = item.Content as ConsoleViewer;
- if (viewer.Session == session)
- {
- _tabs.Items.Remove(item);
- viewer.Unbind();
- viewer.Dispose();
-
- if (_tabs.Items.Count == 0)
- _window.Close();
-
- return;
- }
- }
- }
- }
-
- public void KeyPressed(KeyEventArgs e)
- {
-
- }
-
- public void KeyPreview(KeyEventArgs e)
- {
- if (KeyboardEx.IsControlDown && e.Key == Key.Tab)
- {
- _tabs.Focus();
- }
- }
-
- public bool HandleCommand(string commandString)
- {
- List<Token> tokens = Token.Split(commandString);
-
- if (tokens.Count > 0)
- {
- string commandKey = tokens[0].Value;
-
- IConsoleCommand command;
- if (_commands.TryGetValue(commandKey, out command))
- {
- try
- {
- CurrentViewer.WriteOutput(commandString + Environment.NewLine);
- object[] args = ArgumentProcessor.Process(tokens, command.ArgTypes);
-
- return command.Execute(this, args);
- }
- catch (InvalidOperationException)
- {
- CurrentViewer.WriteError("Use correct syntax: " + command.CorrectSyntax + Environment.NewLine);
- return false;
- }
- }
- }
-
- return true;
- }
-
- public void Dispose()
- {
- foreach (ConsoleSession session in this.Sessions)
- {
- session.Dispose();
- }
-
- _sessions.Clear();
-
- foreach (ConsoleViewer viewer in this.Viewers)
- {
- viewer.Dispose();
- }
-
- GC.SuppressFinalize(this);
- }
-
- public static ConsoleViewer CreateViewer()
- {
- ConsoleViewer viewer = new ConsoleViewer()
- {
- Background = Brushes.Black,
- Foreground = Brushes.White,
- VerticalScrollBarVisibility = ScrollBarVisibility.Visible,
- HorizontalAlignment = HorizontalAlignment.Stretch,
- FontFamily = new FontFamily("Consolas"),
- Padding = new Thickness(0.0),
- Margin = new Thickness(0.0),
- };
-
- viewer.Document = new FlowDocument()
- {
- PagePadding = new Thickness(0.0),
- TextAlignment = TextAlignment.Left,
- };
-
- viewer.BeginInit();
- viewer.EndInit();
-
- viewer.Document.BeginInit();
- viewer.Document.EndInit();
-
- return viewer;
- }
-
- public static TabItem CreateNewTab(string name)
- {
- ConsoleViewer viewer = OverthereShell.CreateViewer();
- TabItem item = new TabItem()
- {
- Header = name,
- BorderThickness = new Thickness(0.0),
- Content = viewer,
- };
-
- item.PreviewKeyDown += (s, e) =>
- {
- if (!e.Key.IsModifier() && e.Key != Key.Tab)
- {
- viewer.Focus();
- }
- };
-
- return item;
- }
- }
- }