|
| 1 | +/* |
| 2 | +* Copyright (c) Microsoft Corporation. All rights reserved. This code released |
| 3 | +* under the terms of the Microsoft Limited Public License (MS-LPL). |
| 4 | +*/ |
| 5 | +using System; |
| 6 | +using System.ComponentModel; |
| 7 | +using System.Diagnostics; |
| 8 | +using Microsoft.TeamFoundation.Client; |
| 9 | +using Microsoft.TeamFoundation.Controls; |
| 10 | + |
| 11 | +namespace GitHub.VisualStudio |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Team Explorer plugin common base class. |
| 15 | + /// </summary> |
| 16 | + public class TeamExplorerBase : IDisposable, INotifyPropertyChanged |
| 17 | + { |
| 18 | + #region Members |
| 19 | + |
| 20 | + private bool m_contextSubscribed = false; |
| 21 | + |
| 22 | + #endregion |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Get/set the service provider. |
| 26 | + /// </summary> |
| 27 | + public IServiceProvider ServiceProvider |
| 28 | + { |
| 29 | + get { return m_serviceProvider; } |
| 30 | + set |
| 31 | + { |
| 32 | + // Unsubscribe from Team Foundation context changes |
| 33 | + if (m_serviceProvider != null) |
| 34 | + { |
| 35 | + UnsubscribeContextChanges(); |
| 36 | + } |
| 37 | + |
| 38 | + m_serviceProvider = value; |
| 39 | + |
| 40 | + // Subscribe to Team Foundation context changes |
| 41 | + if (m_serviceProvider != null) |
| 42 | + { |
| 43 | + SubscribeContextChanges(); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + private IServiceProvider m_serviceProvider = null; |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Get the requested service from the service provider. |
| 51 | + /// </summary> |
| 52 | + public T GetService<T>() |
| 53 | + { |
| 54 | + Debug.Assert(this.ServiceProvider != null, "GetService<T> called before service provider is set"); |
| 55 | + if (this.ServiceProvider != null) |
| 56 | + { |
| 57 | + return (T)this.ServiceProvider.GetService(typeof(T)); |
| 58 | + } |
| 59 | + |
| 60 | + return default(T); |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Show a notification in the Team Explorer window. |
| 65 | + /// </summary> |
| 66 | + protected Guid ShowNotification(string message, NotificationType type) |
| 67 | + { |
| 68 | + ITeamExplorer teamExplorer = GetService<ITeamExplorer>(); |
| 69 | + if (teamExplorer != null) |
| 70 | + { |
| 71 | + Guid guid = Guid.NewGuid(); |
| 72 | + teamExplorer.ShowNotification(message, type, NotificationFlags.None, null, guid); |
| 73 | + return guid; |
| 74 | + } |
| 75 | + |
| 76 | + return Guid.Empty; |
| 77 | + } |
| 78 | + |
| 79 | + #region IDisposable |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Dispose. |
| 83 | + /// </summary> |
| 84 | + public virtual void Dispose() |
| 85 | + { |
| 86 | + UnsubscribeContextChanges(); |
| 87 | + } |
| 88 | + |
| 89 | + #endregion |
| 90 | + |
| 91 | + #region INotifyPropertyChanged |
| 92 | + |
| 93 | + public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged; |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Raise the PropertyChanged event for the specified property. |
| 97 | + /// </summary> |
| 98 | + /// <param name="propertyName">Property name</param> |
| 99 | + protected void RaisePropertyChanged(string propertyName) |
| 100 | + { |
| 101 | + if (this.PropertyChanged != null) |
| 102 | + { |
| 103 | + this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + #endregion |
| 108 | + |
| 109 | + #region Team Foundation Context |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Subscribe to context changes. |
| 113 | + /// </summary> |
| 114 | + protected void SubscribeContextChanges() |
| 115 | + { |
| 116 | + Debug.Assert(this.ServiceProvider != null, "ServiceProvider must be set before subscribing to context changes"); |
| 117 | + if (this.ServiceProvider == null || m_contextSubscribed) |
| 118 | + { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + ITeamFoundationContextManager tfContextManager = GetService<ITeamFoundationContextManager>(); |
| 123 | + if (tfContextManager != null) |
| 124 | + { |
| 125 | + tfContextManager.ContextChanged += ContextChanged; |
| 126 | + m_contextSubscribed = true; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Unsubscribe from context changes. |
| 132 | + /// </summary> |
| 133 | + protected void UnsubscribeContextChanges() |
| 134 | + { |
| 135 | + if (this.ServiceProvider == null || !m_contextSubscribed) |
| 136 | + { |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + ITeamFoundationContextManager tfContextManager = GetService<ITeamFoundationContextManager>(); |
| 141 | + if (tfContextManager != null) |
| 142 | + { |
| 143 | + tfContextManager.ContextChanged -= ContextChanged; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /// <summary> |
| 148 | + /// ContextChanged event handler. |
| 149 | + /// </summary> |
| 150 | + protected virtual void ContextChanged(object sender, ContextChangedEventArgs e) |
| 151 | + { |
| 152 | + } |
| 153 | + |
| 154 | + /// <summary> |
| 155 | + /// Get the current Team Foundation context. |
| 156 | + /// </summary> |
| 157 | + protected ITeamFoundationContext CurrentContext |
| 158 | + { |
| 159 | + get |
| 160 | + { |
| 161 | + ITeamFoundationContextManager tfContextManager = GetService<ITeamFoundationContextManager>(); |
| 162 | + if (tfContextManager != null) |
| 163 | + { |
| 164 | + return tfContextManager.CurrentContext; |
| 165 | + } |
| 166 | + |
| 167 | + return null; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + #endregion |
| 172 | + } |
| 173 | +} |
0 commit comments