aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModelLocator.cs
blob: e0338a319e9977fa998be56fd21684871af8cc25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
using System.Linq;
using System.Windows;
using Tango.BL.ActionLogs;
using Tango.Core.DI;
using Tango.Integration.ExternalBridge;
using Tango.Logging;
using Tango.MachineStudio.Common.Authentication;
using Tango.MachineStudio.Common.Diagnostics;
using Tango.MachineStudio.Common.EventLogging;
using Tango.MachineStudio.Common.FirmwareUpgrade;
using Tango.MachineStudio.Common.Modules;
using Tango.MachineStudio.Common.Navigation;
using Tango.MachineStudio.Common.Notifications;
using Tango.MachineStudio.Common.Speech;
using Tango.MachineStudio.Common.StudioApplication;
using Tango.MachineStudio.Common.Threading;
using Tango.MachineStudio.Common.Video;
using Tango.MachineStudio.Common.Web;
using Tango.MachineStudio.UI.Authentication;
using Tango.MachineStudio.UI.Console;
using Tango.MachineStudio.UI.FirmwareUpgrade;
using Tango.MachineStudio.UI.Modules;
using Tango.MachineStudio.UI.Navigation;
using Tango.MachineStudio.UI.Notifications;
using Tango.MachineStudio.UI.StudioApplication;
using Tango.MachineStudio.UI.SupervisingController;
using Tango.MachineStudio.UI.TFS;
using Tango.MachineStudio.UI.Threading;
using Tango.MachineStudio.UI.ViewModels;
using Tango.MachineStudio.UI.Views;
using Tango.Settings;
using Tango.TFS;

namespace Tango.MachineStudio.UI
{
    /// <summary>
    /// This class contains static references to all the view models in the
    /// application and provides an entry point for the bindings.
    /// </summary>
    public class ViewModelLocator
    {
        private LogManager LogManager = LogManager.Default;

        /// <summary>
        /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
        public ViewModelLocator()
        {
            ////if (ViewModelBase.IsInDesignModeStatic)
            ////{
            ////    // Create design time view services and models
            ////    TangoIOC.Default.Register<IDataService, DesignDataService>();
            ////}
            ////else
            ////{
            ////    // Create run time view services and models
            ////    TangoIOC.Default.Register<IDataService, DataService>();
            ////}

            TangoIOC.Default.Unregister<INotificationProvider>();
            TangoIOC.Default.Unregister<IAuthenticationProvider>();
            TangoIOC.Default.Unregister<INavigationManager>();
            TangoIOC.Default.Unregister<IStudioModuleLoader>();
            TangoIOC.Default.Unregister<IStudioApplicationManager>();
            TangoIOC.Default.Unregister<IVideoCaptureProvider>();
            TangoIOC.Default.Unregister<IDiagnosticsFrameProvider>();
            TangoIOC.Default.Unregister<IEventLogger>();
            TangoIOC.Default.Unregister<ISpeechProvider>();
            TangoIOC.Default.Unregister<ITeamFoundationServiceClient>();
            TangoIOC.Default.Unregister<IDispatcherProvider>();
            TangoIOC.Default.Unregister<IFirmwareUpgrader>();
            TangoIOC.Default.Unregister<MachineStudioWebClient>();
            TangoIOC.Default.Unregister<IActionLogManager>();



            if (App.StartupArgs.Contains("-webDebug"))
            {
                TangoIOC.Default.Register<MachineStudioWebClient, MachineStudioWebClient>(new MachineStudioWebClient("http://localhost:1111", null));
            }
            else
            {
                TangoIOC.Default.Register<MachineStudioWebClient, MachineStudioWebClient>(new MachineStudioWebClient());
            }

            TangoIOC.Default.Register<IDispatcherProvider, DefaultDispatcherProvider>(new DefaultDispatcherProvider(Application.Current.Dispatcher));
            TangoIOC.Default.Register<INotificationProvider, DefaultNotificationProvider>();
            TangoIOC.Default.Register<IAuthenticationProvider, DefaultAuthenticationProvider>();
            TangoIOC.Default.Register<INavigationManager, DefaultNavigationManager>();
            TangoIOC.Default.Register<IStudioModuleLoader, DefaultStudioModuleLoader>();
            TangoIOC.Default.Register<IStudioApplicationManager, DefaultStudioApplicationManager>();
            TangoIOC.Default.Register<IVideoCaptureProvider, DefaultVideoCaptureProvider>();
            TangoIOC.Default.Register<IDiagnosticsFrameProvider, DefaultDiagnosticsFrameProvider>();
            TangoIOC.Default.Register<IEventLogger, DefaultEventLogger>();
            TangoIOC.Default.Register<ISpeechProvider, DefaultSpeechProvider>();
            TangoIOC.Default.Register<IFirmwareUpgrader, DefaultFirmwareUpgrader>();
            TangoIOC.Default.Register<IActionLogManager, DefaultActionLogManager>(new DefaultActionLogManager() { IsAsync = true });
            TangoIOC.Default.Register<TeamFoundationServiceExtendedClient>(new TeamFoundationServiceExtendedClient("https://twinetfs.visualstudio.com", String.Empty, "pyulwgs7m3v7pizz3oxusypdkdfw43txggo5mjwu2ouyv2qwprhq"));


            TangoIOC.Default.Register<MainViewVM>();
            TangoIOC.Default.Register<LoadingViewVM>();
            TangoIOC.Default.Register<ShutdownViewVM>();
            TangoIOC.Default.Register<LoginViewVM>();
            TangoIOC.Default.Register<MachineConnectionViewVM>();
            TangoIOC.Default.Register<ConnectedMachineViewVM>();
            TangoIOC.Default.Register<MachineLoginViewVM>();
            TangoIOC.Default.Register<UpdateViewVM>();
            TangoIOC.Default.Register<ConsoleWindowVM>();
            TangoIOC.Default.Register<AboutViewVM>();

            //Register View (Supervising Controller Pattern).
            //if (!ViewModelBase.IsInDesignModeStatic)
            //{
            //    LogManager.Log(String.Format("Registering Supervising Controller {0}...", nameof(IMainView)));
            //    TangoIOC.Default.Register(() => (IMainView)MainView.Self);
            //}
        }

        public MainViewVM MainViewVM
        {
            get
            {
                return TangoIOC.Default.GetInstance<MainViewVM>();
            }
        }

        public LoadingViewVM LoadingViewVM
        {
            get
            {
                return TangoIOC.Default.GetInstance<LoadingViewVM>();
            }
        }

        public ShutdownViewVM ShutdownViewVM
        {
            get
            {
                return TangoIOC.Default.GetInstance<ShutdownViewVM>();
            }
        }

        public LoginViewVM LoginViewVM
        {
            get
            {
                return TangoIOC.Default.GetInstance<LoginViewVM>();
            }
        }

        public MachineConnectionViewVM MachineConnectionViewVM
        {
            get
            {
                return TangoIOC.Default.GetInstance<MachineConnectionViewVM>();
            }
        }

        public ConnectedMachineViewVM ConnectedMachineViewVM
        {
            get
            {
                return TangoIOC.Default.GetInstance<ConnectedMachineViewVM>();
            }
        }

        public MachineLoginViewVM MachineLoginViewVM
        {
            get
            {
                return TangoIOC.Default.GetInstance<MachineLoginViewVM>();
            }
        }

        public UpdateViewVM UpdateViewVM
        {
            get
            {
                return TangoIOC.Default.GetInstance<UpdateViewVM>();
            }
        }

        public ConsoleWindowVM ConsoleWindowVM
        {
            get
            {
                return TangoIOC.Default.GetInstance<ConsoleWindowVM>();
            }
        }

        public AboutViewVM AboutViewVM
        {
            get
            {
                return TangoIOC.Default.GetInstance<AboutViewVM>();
            }
        }
    }
}
tArea, TextViewPosition pos) { DocumentLine documentLine = textArea.Document.GetLineByNumber(pos.Line); VisualLine visualLine = textArea.TextView.GetOrConstructVisualLine(documentLine); int vc = visualLine.ValidateVisualColumn(pos, true); TextLine textLine = visualLine.GetTextLine(vc); return visualLine.GetTextLineVisualXPosition(textLine, vc); } void CalculateSegments() { DocumentLine nextLine = document.GetLineByNumber(Math.Min(startLine, endLine)); do { VisualLine vl = textArea.TextView.GetOrConstructVisualLine(nextLine); int startVC = vl.GetVisualColumn(new Point(startXPos, 0), true); int endVC = vl.GetVisualColumn(new Point(endXPos, 0), true); int baseOffset = vl.FirstDocumentLine.Offset; int startOffset = baseOffset + vl.GetRelativeOffset(startVC); int endOffset = baseOffset + vl.GetRelativeOffset(endVC); segments.Add(new SelectionSegment(startOffset, startVC, endOffset, endVC)); nextLine = vl.LastDocumentLine.NextLine; } while (nextLine != null && nextLine.LineNumber <= Math.Max(startLine, endLine)); } TextViewPosition GetStart() { SelectionSegment segment = (startLine < endLine ? segments.First() : segments.Last()); if (startXPos < endXPos) { return new TextViewPosition(document.GetLocation(segment.StartOffset), segment.StartVisualColumn); } else { return new TextViewPosition(document.GetLocation(segment.EndOffset), segment.EndVisualColumn); } } TextViewPosition GetEnd() { SelectionSegment segment = (startLine < endLine ? segments.Last() : segments.First()); if (startXPos < endXPos) { return new TextViewPosition(document.GetLocation(segment.EndOffset), segment.EndVisualColumn); } else { return new TextViewPosition(document.GetLocation(segment.StartOffset), segment.StartVisualColumn); } } #endregion /// <inheritdoc/> public override string GetText() { StringBuilder b = new StringBuilder(); foreach (ISegment s in this.Segments) { if (b.Length > 0) b.AppendLine(); b.Append(document.GetText(s)); } return b.ToString(); } /// <inheritdoc/> public override Selection StartSelectionOrSetEndpoint(TextViewPosition startPosition, TextViewPosition endPosition) { return SetEndpoint(endPosition); } /// <inheritdoc/> public override int Length { get { return this.Segments.Sum(s => s.Length); } } /// <inheritdoc/> public override bool EnableVirtualSpace { get { return true; } } /// <inheritdoc/> public override ISegment SurroundingSegment { get { return new SimpleSegment(topLeftOffset, bottomRightOffset - topLeftOffset); } } /// <inheritdoc/> public override IEnumerable<SelectionSegment> Segments { get { return segments; } } /// <inheritdoc/> public override TextViewPosition StartPosition { get { return start; } } /// <inheritdoc/> public override TextViewPosition EndPosition { get { return end; } } /// <inheritdoc/> public override bool Equals(object obj) { RectangleSelection r = obj as RectangleSelection; return r != null && r.textArea == this.textArea && r.topLeftOffset == this.topLeftOffset && r.bottomRightOffset == this.bottomRightOffset && r.startLine == this.startLine && r.endLine == this.endLine && r.startXPos == this.startXPos && r.endXPos == this.endXPos; } /// <inheritdoc/> public override int GetHashCode() { return topLeftOffset ^ bottomRightOffset; } /// <inheritdoc/> public override Selection SetEndpoint(TextViewPosition endPosition) { return new RectangleSelection(textArea, startLine, startXPos, endPosition); } int GetVisualColumnFromXPos(int line, double xPos) { var vl = textArea.TextView.GetOrConstructVisualLine(textArea.Document.GetLineByNumber(line)); return vl.GetVisualColumn(new Point(xPos, 0), true); } /// <inheritdoc/> public override Selection UpdateOnDocumentChange(DocumentChangeEventArgs e) { TextLocation newStartLocation = textArea.Document.GetLocation(e.GetNewOffset(topLeftOffset, AnchorMovementType.AfterInsertion)); TextLocation newEndLocation = textArea.Document.GetLocation(e.GetNewOffset(bottomRightOffset, AnchorMovementType.BeforeInsertion)); return new RectangleSelection(textArea, new TextViewPosition(newStartLocation, GetVisualColumnFromXPos(newStartLocation.Line, startXPos)), new TextViewPosition(newEndLocation, GetVisualColumnFromXPos(newEndLocation.Line, endXPos))); } /// <inheritdoc/> public override void ReplaceSelectionWithText(string newText) { if (newText == null) throw new ArgumentNullException("newText"); using (textArea.Document.RunUpdate()) { TextViewPosition start = new TextViewPosition(document.GetLocation(topLeftOffset), GetVisualColumnFromXPos(startLine, startXPos)); TextViewPosition end = new TextViewPosition(document.GetLocation(bottomRightOffset), GetVisualColumnFromXPos(endLine, endXPos)); int insertionLength; int totalInsertionLength = 0; int firstInsertionLength = 0; int editOffset = Math.Min(topLeftOffset, bottomRightOffset); TextViewPosition pos; if (NewLineFinder.NextNewLine(newText, 0) == SimpleSegment.Invalid) { // insert same text into every line foreach (SelectionSegment lineSegment in this.Segments.Reverse()) { ReplaceSingleLineText(textArea, lineSegment, newText, out insertionLength); totalInsertionLength += insertionLength; firstInsertionLength = insertionLength; } int newEndOffset = editOffset + totalInsertionLength; pos = new TextViewPosition(document.GetLocation(editOffset + firstInsertionLength)); textArea.Selection = new RectangleSelection(textArea, pos, Math.Max(startLine, endLine), GetXPos(textArea, pos)); } else { string[] lines = newText.Split(NewLineFinder.NewlineStrings, segments.Count, StringSplitOptions.None); int line = Math.Min(startLine, endLine); for (int i = lines.Length - 1; i >= 0; i--) { ReplaceSingleLineText(textArea, segments[i], lines[i], out insertionLength); firstInsertionLength = insertionLength; } pos = new TextViewPosition(document.GetLocation(editOffset + firstInsertionLength)); textArea.ClearSelection(); } textArea.Caret.Position = textArea.TextView.GetPosition(new Point(GetXPos(textArea, pos), textArea.TextView.GetVisualTopByDocumentLine(Math.Max(startLine, endLine)))).GetValueOrDefault(); } } void ReplaceSingleLineText(TextArea textArea, SelectionSegment lineSegment, string newText, out int insertionLength) { if (lineSegment.Length == 0) { if (newText.Length > 0 && textArea.ReadOnlySectionProvider.CanInsert(lineSegment.StartOffset)) { newText = AddSpacesIfRequired(newText, new TextViewPosition(document.GetLocation(lineSegment.StartOffset), lineSegment.StartVisualColumn), new TextViewPosition(document.GetLocation(lineSegment.EndOffset), lineSegment.EndVisualColumn)); textArea.Document.Insert(lineSegment.StartOffset, newText); } } else { ISegment[] segmentsToDelete = textArea.GetDeletableSegments(lineSegment); for (int i = segmentsToDelete.Length - 1; i >= 0; i--) { if (i == segmentsToDelete.Length - 1) { if (segmentsToDelete[i].Offset == SurroundingSegment.Offset && segmentsToDelete[i].Length == SurroundingSegment.Length) { newText = AddSpacesIfRequired(newText, new TextViewPosition(document.GetLocation(lineSegment.StartOffset), lineSegment.StartVisualColumn), new TextViewPosition(document.GetLocation(lineSegment.EndOffset), lineSegment.EndVisualColumn)); } textArea.Document.Replace(segmentsToDelete[i], newText); } else { textArea.Document.Remove(segmentsToDelete[i]); } } } insertionLength = newText.Length; } /// <summary> /// Performs a rectangular paste operation. /// </summary> public static bool PerformRectangularPaste(TextArea textArea, TextViewPosition startPosition, string text, bool selectInsertedText) { if (textArea == null) throw new ArgumentNullException("textArea"); if (text == null) throw new ArgumentNullException("text"); int newLineCount = text.Count(c => c == '\n'); // TODO might not work in all cases, but single \r line endings are really rare today. TextLocation endLocation = new TextLocation(startPosition.Line + newLineCount, startPosition.Column); if (endLocation.Line <= textArea.Document.LineCount) { int endOffset = textArea.Document.GetOffset(endLocation); if (textArea.Selection.EnableVirtualSpace || textArea.Document.GetLocation(endOffset) == endLocation) { RectangleSelection rsel = new RectangleSelection(textArea, startPosition, endLocation.Line, GetXPos(textArea, startPosition)); rsel.ReplaceSelectionWithText(text); if (selectInsertedText && textArea.Selection is RectangleSelection) { RectangleSelection sel = (RectangleSelection)textArea.Selection; textArea.Selection = new RectangleSelection(textArea, startPosition, sel.endLine, sel.endXPos); } return true; } } return false; } /// <summary> /// Gets the name of the entry in the DataObject that signals rectangle selections. /// </summary> public const string RectangularSelectionDataType = "AvalonEditRectangularSelection"; /// <inheritdoc/> public override System.Windows.DataObject CreateDataObject(TextArea textArea) { var data = base.CreateDataObject(textArea); MemoryStream isRectangle = new MemoryStream(1); isRectangle.WriteByte(1); data.SetData(RectangularSelectionDataType, isRectangle, false); return data; } /// <inheritdoc/> public override string ToString() { // It's possible that ToString() gets called on old (invalid) selections, e.g. for "change from... to..." debug message // make sure we don't crash even when the desired locations don't exist anymore. return string.Format("[RectangleSelection {0} {1} {2} to {3} {4} {5}]", startLine, topLeftOffset, startXPos, endLine, bottomRightOffset, endXPos); } } }