This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
/
AbstractViewContent.cs
566 lines (486 loc) · 14.5 KB
/
AbstractViewContent.cs
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.ComponentModel.Design;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows.Forms;
using ICSharpCode.Core;
using ICSharpCode.Core.Presentation;
namespace ICSharpCode.SharpDevelop.Gui
{
/// <summary>
/// Provides a default implementation for the IViewContent interface.
/// It provides a files collection that, by default, automatically registers the view with the
/// files added to it.
/// Several properties have default implementation that depend on the contents of the files collection,
/// e.g. IsDirty is true when at least one of the files in the collection is dirty.
/// To support the changed event, this class registers event handlers with the members of the files collection.
///
/// When used with an empty Files collection, IsViewOnly will return true and this class can be used as a base class
/// for view contents not using files.
/// </summary>
public abstract class AbstractViewContent : IViewContent
{
/// <summary>
/// Create a new AbstractViewContent instance.
/// </summary>
protected AbstractViewContent()
{
secondaryViewContentCollection = new SecondaryViewContentCollection(this);
InitFiles();
}
/// <summary>
/// Create a new AbstractViewContent instance with the specified primary file.
/// </summary>
protected AbstractViewContent(OpenedFile file) : this()
{
if (file == null)
throw new ArgumentNullException("file");
this.Files.Add(file);
}
public abstract object Control {
get;
}
public virtual object InitiallyFocusedControl {
get { return null; }
}
IWorkbenchWindow workbenchWindow;
IWorkbenchWindow IViewContent.WorkbenchWindow {
get { return workbenchWindow; }
set {
if (workbenchWindow != value) {
workbenchWindow = value;
OnWorkbenchWindowChanged();
}
}
}
public IWorkbenchWindow WorkbenchWindow {
get { return workbenchWindow; }
}
protected virtual void OnWorkbenchWindowChanged()
{
}
string tabPageText = "TabPageText";
public event EventHandler TabPageTextChanged;
/// <summary>
/// Gets/Sets the title of the current tab page.
/// This value will be passed through the string parser before being displayed.
/// </summary>
public string TabPageText {
get { return tabPageText; }
set {
if (tabPageText != value) {
tabPageText = value;
if (TabPageTextChanged != null) {
TabPageTextChanged(this, EventArgs.Empty);
}
}
}
}
#region Secondary view content support
sealed class SecondaryViewContentCollection : ICollection<IViewContent>
{
readonly AbstractViewContent parent;
readonly List<IViewContent> list = new List<IViewContent>();
public SecondaryViewContentCollection(AbstractViewContent parent)
{
this.parent = parent;
}
public int Count {
get { return list.Count; }
}
public bool IsReadOnly {
get { return false; }
}
public void Add(IViewContent item)
{
if (item == null) {
throw new ArgumentNullException("item");
}
if (item.WorkbenchWindow != null && item.WorkbenchWindow != parent.WorkbenchWindow) {
throw new ArgumentException("The view content already is displayed in another workbench window.");
}
list.Add(item);
if (parent.workbenchWindow != null) {
parent.workbenchWindow.ViewContents.Add(item);
}
}
public void Clear()
{
if (parent.workbenchWindow != null) {
foreach (IViewContent vc in list) {
parent.workbenchWindow.ViewContents.Remove(vc);
}
}
list.Clear();
}
public bool Contains(IViewContent item)
{
return list.Contains(item);
}
public void CopyTo(IViewContent[] array, int arrayIndex)
{
list.CopyTo(array, arrayIndex);
}
public bool Remove(IViewContent item)
{
if (list.Remove(item)) {
if (parent.workbenchWindow != null) {
parent.workbenchWindow.ViewContents.Remove(item);
}
return true;
} else {
return false;
}
}
public IEnumerator<IViewContent> GetEnumerator()
{
return list.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return list.GetEnumerator();
}
}
readonly SecondaryViewContentCollection secondaryViewContentCollection;
/// <summary>
/// Gets the collection that stores the secondary view contents.
/// </summary>
public virtual ICollection<IViewContent> SecondaryViewContents {
get {
return secondaryViewContentCollection;
}
}
/// <summary>
/// Gets switching without a Save/Load cycle for <paramref name="file"/> is supported
/// when switching from this view content to <paramref name="newView"/>.
/// </summary>
public virtual bool SupportsSwitchFromThisWithoutSaveLoad(OpenedFile file, IViewContent newView)
{
return newView == this;
}
/// <summary>
/// Gets switching without a Save/Load cycle for <paramref name="file"/> is supported
/// when switching from <paramref name="oldView"/> to this view content.
/// </summary>
public virtual bool SupportsSwitchToThisWithoutSaveLoad(OpenedFile file, IViewContent oldView)
{
return oldView == this;
}
/// <summary>
/// Executes an action before switching from this view content to the new view content.
/// </summary>
public virtual void SwitchFromThisWithoutSaveLoad(OpenedFile file, IViewContent newView)
{
}
/// <summary>
/// Executes an action before switching from the old view content to this view content.
/// </summary>
public virtual void SwitchToThisWithoutSaveLoad(OpenedFile file, IViewContent oldView)
{
}
#endregion
#region Files
FilesCollection files;
ReadOnlyCollection<OpenedFile> filesReadonly;
void InitFiles()
{
files = new FilesCollection(this);
filesReadonly = new ReadOnlyCollection<OpenedFile>(files);
}
protected Collection<OpenedFile> Files {
get { return files; }
}
IList<OpenedFile> IViewContent.Files {
get { return filesReadonly; }
}
/// <summary>
/// Gets the primary file being edited. Might return null if no file is edited.
/// </summary>
public virtual OpenedFile PrimaryFile {
get {
WorkbenchSingleton.AssertMainThread();
if (files.Count != 0)
return files[0];
else
return null;
}
}
/// <summary>
/// Gets the name of the primary file being edited. Might return null if no file is edited.
/// </summary>
public virtual FileName PrimaryFileName {
get {
OpenedFile file = PrimaryFile;
if (file != null)
return file.FileName;
else
return null;
}
}
protected bool AutomaticallyRegisterViewOnFiles = true;
void RegisterFileEventHandlers(OpenedFile newItem)
{
newItem.FileNameChanged += OnFileNameChanged;
newItem.IsDirtyChanged += OnIsDirtyChanged;
if (AutomaticallyRegisterViewOnFiles) {
newItem.RegisterView(this);
}
OnIsDirtyChanged(null, EventArgs.Empty); // re-evaluate this.IsDirty after changing the file collection
}
void UnregisterFileEventHandlers(OpenedFile oldItem)
{
oldItem.FileNameChanged -= OnFileNameChanged;
oldItem.IsDirtyChanged -= OnIsDirtyChanged;
if (AutomaticallyRegisterViewOnFiles) {
oldItem.UnregisterView(this);
}
OnIsDirtyChanged(null, EventArgs.Empty); // re-evaluate this.IsDirty after changing the file collection
}
void OnFileNameChanged(object sender, EventArgs e)
{
OnFileNameChanged((OpenedFile)sender);
if (titleName == null && files.Count > 0 && sender == files[0]) {
OnTitleNameChanged(EventArgs.Empty);
}
}
/// <summary>
/// Is called when the file name of a file opened in this view content changes.
/// </summary>
protected virtual void OnFileNameChanged(OpenedFile file)
{
}
private sealed class FilesCollection : Collection<OpenedFile>
{
AbstractViewContent parent;
public FilesCollection(AbstractViewContent parent)
{
this.parent = parent;
}
protected override void InsertItem(int index, OpenedFile item)
{
base.InsertItem(index, item);
parent.RegisterFileEventHandlers(item);
}
protected override void SetItem(int index, OpenedFile item)
{
parent.UnregisterFileEventHandlers(this[index]);
base.SetItem(index, item);
parent.RegisterFileEventHandlers(item);
}
protected override void RemoveItem(int index)
{
parent.UnregisterFileEventHandlers(this[index]);
base.RemoveItem(index);
}
protected override void ClearItems()
{
foreach (OpenedFile item in this) {
parent.UnregisterFileEventHandlers(item);
}
base.ClearItems();
}
}
#endregion
#region TitleName
public event EventHandler TitleNameChanged;
void OnTitleNameChanged(EventArgs e)
{
if (TitleNameChanged != null) {
TitleNameChanged(this, e);
}
}
string titleName;
LanguageDependentExtension titleNameLocalizeExtension;
string IViewContent.TitleName {
get {
if (titleName != null)
return titleName;
else if (files.Count > 0)
return Path.GetFileName(files[0].FileName);
else
return "[Default Title]";
}
}
public string TitleName {
get { return titleName; }
protected set {
if (titleNameLocalizeExtension != null) {
titleNameLocalizeExtension.PropertyChanged -= OnTitleNameLocalizationChanged;
titleNameLocalizeExtension = null;
}
if (titleName != value) {
titleName = value;
OnTitleNameChanged(EventArgs.Empty);
}
}
}
/// <summary>
/// Sets a localized title that will update automatically when the language changes.
/// </summary>
/// <param name="text">The input to the string parser which will localize title.</param>
protected void SetLocalizedTitle(string text)
{
titleNameLocalizeExtension = new StringParseExtension(text) { UsesAccessors = false };
titleNameLocalizeExtension.PropertyChanged += OnTitleNameLocalizationChanged;
OnTitleNameLocalizationChanged(null, null);
}
void OnTitleNameLocalizationChanged(object sender, EventArgs e)
{
string value = titleNameLocalizeExtension.Value;
if (titleName != value) {
titleName = value;
OnTitleNameChanged(EventArgs.Empty);
}
}
#endregion
#region IDisposable
public event EventHandler Disposed;
bool isDisposed;
public bool IsDisposed {
get { return isDisposed; }
}
public virtual void Dispose()
{
workbenchWindow = null;
UnregisterOnActiveViewContentChanged();
if (AutomaticallyRegisterViewOnFiles) {
this.Files.Clear();
}
isDisposed = true;
if (Disposed != null) {
Disposed(this, EventArgs.Empty);
}
}
#endregion
#region IsDirty
bool IsDirtyInternal {
get {
foreach (OpenedFile file in this.Files) {
if (file.IsDirty)
return true;
}
return false;
}
}
bool isDirty;
public virtual bool IsDirty {
get { return isDirty; }
}
void OnIsDirtyChanged(object sender, EventArgs e)
{
bool newIsDirty = IsDirtyInternal;
if (newIsDirty != isDirty) {
isDirty = newIsDirty;
RaiseIsDirtyChanged();
}
}
/// <summary>
/// Raise the IsDirtyChanged event. Call this method only if you have overridden the IsDirty property
/// to implement your own handling of IsDirty.
/// </summary>
protected void RaiseIsDirtyChanged()
{
if (IsDirtyChanged != null)
IsDirtyChanged(this, EventArgs.Empty);
}
public event EventHandler IsDirtyChanged;
#endregion
#region IsActiveViewContent
EventHandler isActiveViewContentChanged;
bool registeredOnViewContentChange;
bool wasActiveViewContent;
/// <summary>
/// Gets if this view content is the active view content.
/// </summary>
protected bool IsActiveViewContent {
get { return WorkbenchSingleton.Workbench != null && WorkbenchSingleton.Workbench.ActiveViewContent == this; }
}
/// <summary>
/// Is raised when the value of the IsActiveViewContent property changes.
/// </summary>
protected event EventHandler IsActiveViewContentChanged {
add {
if (!registeredOnViewContentChange) {
// register WorkbenchSingleton.Workbench.ActiveViewContentChanged only on demand
wasActiveViewContent = IsActiveViewContent;
// null check is required to support running in unit test mode
if (WorkbenchSingleton.Workbench != null) {
WorkbenchSingleton.Workbench.ActiveViewContentChanged += OnActiveViewContentChanged;
}
registeredOnViewContentChange = true;
}
isActiveViewContentChanged += value;
}
remove {
isActiveViewContentChanged -= value;
}
}
void UnregisterOnActiveViewContentChanged()
{
if (registeredOnViewContentChange) {
// null check is required to support running in unit test mode
if (WorkbenchSingleton.Workbench != null) {
WorkbenchSingleton.Workbench.ActiveViewContentChanged -= OnActiveViewContentChanged;
}
registeredOnViewContentChange = false;
}
}
void OnActiveViewContentChanged(object sender, EventArgs e)
{
bool isActiveViewContent = IsActiveViewContent;
if (isActiveViewContent != wasActiveViewContent) {
wasActiveViewContent = isActiveViewContent;
if (isActiveViewContentChanged != null)
isActiveViewContentChanged(this, e);
}
}
#endregion
#region IServiceProvider
ServiceContainer services = new ServiceContainer();
public object GetService(Type serviceType)
{
object obj = services.GetService(serviceType);
if (obj == null) {
if (serviceType.IsInstanceOfType(this))
return this;
}
return obj;
}
public ServiceContainer Services {
get { return services; }
}
#endregion
public virtual void Save(OpenedFile file, Stream stream)
{
}
public virtual void Load(OpenedFile file, Stream stream)
{
}
public virtual INavigationPoint BuildNavPoint()
{
return null;
}
/// <summary>
/// Gets if the view content is read-only (can be saved only when choosing another file name).
/// </summary>
public virtual bool IsReadOnly {
get { return false; }
}
/// <summary>
/// Gets if the view content is view-only (cannot be saved at all).
/// </summary>
public virtual bool IsViewOnly {
get { return Files.Count == 0; }
}
public virtual bool CloseWithSolution {
get {
var fileName = this.PrimaryFileName;
return fileName == null
|| Project.ProjectService.OpenSolution.FindProjectContainingFile(fileName) != null;
}
}
}
}