Welcome to mirror list, hosted at ThFree Co, Russian Federation.

WizardDialogController.cs « MonoDevelop.Ide.Gui.Wizard « MonoDevelop.Ide « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7981d7c6eb90802934ebd880e02dee98dc9b8e9c (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
202
203
204
205
//
// WizardDialogController.cs
//
// Author:
//       Vsevolod Kukol <sevoku@microsoft.com>
//
// Copyright (c) 2016 Microsoft Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MonoDevelop.Components;
using MonoDevelop.Core;
using Xwt.Drawing;
namespace MonoDevelop.Ide.Gui.Wizard
{
	public abstract class WizardDialogControllerBase : IWizardDialogController
	{
		string title;
		Image image;
		Control rightSideWidget;
		Xwt.Size defaultPageSize;
		IWizardDialogPage currentPage;

		public string Title {
			get { return title; }
			set {
				title = value;
				OnPropertyChanged (nameof (Title));
			}
		}

		public Image Icon {
			get { return image; }
			set {
				image = value;
				OnPropertyChanged (nameof (Icon));
			}
		}

		public Control RightSideWidget {
			get { return rightSideWidget; }
			set {
				rightSideWidget = value;
				OnPropertyChanged (nameof (RightSideWidget));
			}
		}

		public Xwt.Size DefaultPageSize {
			get { return defaultPageSize; }
			set {
				defaultPageSize = value;
				OnPropertyChanged (nameof (DefaultPageSize));
			}
		}

		public IWizardDialogPage CurrentPage {
			get {
				return currentPage;
			}
			private set {
				currentPage = value;
				OnPropertyChanged (nameof (CurrentPage));
			}
		}

		public abstract bool CurrentPageIsLast { get; }

		public abstract bool CanGoBack { get; }

		public WizardDialogControllerBase (string title, Image icon, Control rightSideWidget, IWizardDialogPage firstPage)
		{
			Title = title;
			Icon = icon;
			CurrentPage = firstPage;
			RightSideWidget = rightSideWidget;
		}

		public async Task GoNext (CancellationToken token)
		{
			if (currentPage.CanGoNext) {
				if (CurrentPageIsLast) {
					await OnCompleted (token).ConfigureAwait (false);
					if (!token.IsCancellationRequested)
						Completed?.Invoke (this, EventArgs.Empty);
				} else {
					var page = await OnGoNext (token).ConfigureAwait (false);
					if (!token.IsCancellationRequested)
						CurrentPage = page;
				}
			}
		}

		protected abstract Task<IWizardDialogPage> OnGoNext (CancellationToken token);

		public async Task GoBack (CancellationToken token)
		{
			if (CanGoBack && currentPage.CanGoBack) {
				CurrentPage = await OnGoBack (token).ConfigureAwait (true);
			}
		}

		protected abstract Task<IWizardDialogPage> OnGoBack (CancellationToken token);

		public bool RunWizard ()
		{
			using (var dialog = new WizardDialog (this)) {
				return dialog.Run (Xwt.MessageDialog.RootWindow);
			}
		}

		public bool RunWizard (Xwt.WindowFrame parentWindow)
		{
			using (var dialog = new WizardDialog (this)) {
				return dialog.Run (parentWindow);
			}
		}

		public event EventHandler Completed;

		public event PropertyChangedEventHandler PropertyChanged;

		protected virtual Task OnCompleted (CancellationToken token)
		{
			return Task.FromResult (true);
		}

		protected virtual void OnPropertyChanged (string propertyName)
		{
			Runtime.RunInMainThread (() => {
				PropertyChanged?.Invoke (this, new System.ComponentModel.PropertyChangedEventArgs (propertyName));
			});
		}

		public void Dispose ()
		{
		}
	}

	public class WizardDialogController : WizardDialogControllerBase
	{
		ReadOnlyCollection<IWizardDialogPage> pages;

		public IReadOnlyCollection<IWizardDialogPage> Pages { get { return pages; } }

		public override bool CanGoBack {
			get {
				return pages.Count > 1 && pages.IndexOf (CurrentPage) > 0;
			}
		}

		public override bool CurrentPageIsLast {
			get { return pages.IndexOf (CurrentPage) == Pages.Count - 1; }
		}

		public WizardDialogController (string title, Image icon, Control rightSideWidget, IWizardDialogPage page)
			: this (title, icon, rightSideWidget, new IWizardDialogPage [] { page })
		{
		}

		public WizardDialogController (string title, Image icon, Control rightSideWidget, IEnumerable<IWizardDialogPage> pages)
			: base (title, icon, rightSideWidget, pages.FirstOrDefault ())
		{
			this.pages = new ReadOnlyCollection<IWizardDialogPage> (pages.ToList ());
			if (this.pages.Count == 0)
				throw new ArgumentException ("pages must contain at least one page.", nameof (pages));
		}

		protected override Task<IWizardDialogPage> OnGoNext (CancellationToken token)
		{
			var currentIndex = pages.IndexOf (CurrentPage);
			if (currentIndex == pages.Count - 1)
				return Task.FromException<IWizardDialogPage>(new InvalidOperationException ());
			else
				return Task.FromResult (pages [currentIndex + 1]);
		}

		protected override Task<IWizardDialogPage> OnGoBack (CancellationToken token)
		{
			var currentIndex = pages.IndexOf (CurrentPage);
			return Task.FromResult (pages [currentIndex - 1]);
		}
	}
}