00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 using System;
00013 using System.Collections.Generic;
00014 using System.Windows.Forms;
00015 using Mcs.Epm.MicrosoftProject.mpFx.Client.Shared.Properties;
00016
00017 namespace Mcs.Epm.MicrosoftProject.mpFx.Client.Shared.Forms
00018 {
00019
00020
00021
00022 public partial class PluginManagerForm : Form
00023 {
00024 #region Instance Data
00025
00026 private readonly List<IOptionsControl> _OptionsControlsList = new List<IOptionsControl>();
00027 private PluginGalleryBrowserForm _PluginGalleryBrowserForm;
00028 private readonly PluginHost _PluginHost;
00029
00030 #endregion
00031
00032 #region Constructors
00033
00034 public PluginManagerForm(PluginHost pluginHost)
00035 {
00036 Tools.ValidateObjectParam(pluginHost, Tools.Format(Resources.InvalidArgument, "pluginHost"));
00037
00038 _PluginHost = pluginHost;
00039
00040 InitializeComponent();
00041
00042 optionsPanel.Top = pluginsPanel.Top;
00043 optionsPanel.Left = pluginsPanel.Left;
00044 pluginsPanel.BringToFront();
00045 treeView.SelectedNode = treeView.Nodes[0];
00046
00047 }
00048
00049 #endregion
00050
00051 #region Private Methods
00052
00053 private void LoadPluginInformation()
00054 {
00055 optionsTabControl.TabPages.Clear();
00056 pluginsDataGridView.Rows.Clear();
00057
00058 SaveSettings();
00059 _OptionsControlsList.Clear();
00060
00061 foreach (KeyValuePair<Guid, IMpFxClientPlugin> plugin in _PluginHost.Plugins)
00062 {
00063 if (!plugin.Value.IsMarkedForDeletion)
00064 {
00065 Control control = plugin.Value.OptionsControl;
00066 _OptionsControlsList.Add((IOptionsControl)control);
00067 control.Enabled = plugin.Value.IsLoaded;
00068 control.Dock = DockStyle.Fill;
00069
00070 if (plugin.Value.IsLoaded)
00071 {
00072 TabPage tab = new TabPage();
00073
00074 tab.Text = plugin.Value.Name;
00075 tab.Controls.Add(control);
00076
00077 optionsTabControl.TabPages.Add(tab);
00078
00079 }
00080
00081 pluginsDataGridView.Rows.Add(plugin.Value.IsLoaded ? "Loaded" : "Unloaded",
00082 plugin.Value.Guid,
00083 plugin.Value.Name,
00084 plugin.Value.Description,
00085 plugin.Value.Author,
00086 plugin.Value.Version.ToString());
00087
00088 }
00089 }
00090
00091 if (pluginsDataGridView.Rows.Count > 0)
00092 {
00093 pluginsDataGridView.Rows[0].Selected = true;
00094 SetButtonsEnabledState();
00095 }
00096 }
00097
00098 private void LoadSelectedPlugin(Guid guid)
00099 {
00100 try
00101 {
00102 _PluginHost.LoadPlugin(guid);
00103 LoadPluginInformation();
00104
00105 }
00106 catch (Exception exception)
00107 {
00108 MessageBox.Show(this, exception.Message, Resources.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
00109 }
00110 }
00111
00112 private void UnloadSelectedPlugin(Guid guid)
00113 {
00114 try
00115 {
00116 _PluginHost.UnloadPlugin(guid);
00117
00118 LoadPluginInformation();
00119 SetButtonsEnabledState();
00120
00121 }
00122 catch (Exception exception)
00123 {
00124 MessageBox.Show(this, exception.Message, Resources.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
00125 }
00126 }
00127
00128 private void SaveSettings()
00129 {
00130 try
00131 {
00132 foreach (IOptionsControl optionsControl in _OptionsControlsList)
00133 {
00134 optionsControl.SaveSettings();
00135 }
00136 }
00137 catch (Exception exception)
00138 {
00139 MessageBox.Show(this, exception.Message, Resources.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
00140 }
00141 }
00142
00143 private void SetButtonsEnabledState()
00144 {
00145 removeToolStripButton.Enabled = pluginsDataGridView.CurrentRow != null;
00146 unloadToolStripButton.Enabled = pluginsDataGridView.CurrentRow != null &&
00147 pluginsDataGridView.CurrentRow.Cells[0].Value.ToString().Equals("Loaded",
00148 StringComparison.CurrentCultureIgnoreCase);
00149
00150 loadToolStripButton.Enabled = pluginsDataGridView.CurrentRow != null &&
00151 pluginsDataGridView.CurrentRow.Cells[0].Value.ToString().Equals("Unloaded",
00152 StringComparison.CurrentCultureIgnoreCase);
00153 }
00154
00155 #endregion
00156
00157 #region Event Handlers
00158
00159 private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
00160 {
00161 switch (treeView.SelectedNode.Index)
00162 {
00163 case 0:
00164 pluginsPanel.BringToFront();
00165 break;
00166 case 1:
00167 if (optionsTabControl.TabCount > 0)
00168 {
00169 optionsTabControl.Visible = true;
00170 noPluginsLoadedLabel.Visible = false;
00171 }
00172 else
00173 {
00174 noPluginsLoadedLabel.Visible = true;
00175 optionsTabControl.Visible = false;
00176 }
00177 optionsPanel.BringToFront();
00178 break;
00179 default:
00180 break;
00181 }
00182 }
00183
00184 private void PluginManagerForm_Shown(object sender, EventArgs e)
00185 {
00186 LoadPluginInformation();
00187 }
00188
00189 private void unloadToolStripButton_Click(object sender, EventArgs e)
00190 {
00191 if (pluginsDataGridView.CurrentRow == null)
00192 {
00193 SetButtonsEnabledState();
00194 return;
00195 }
00196 int index = pluginsDataGridView.CurrentRow.Index;
00197
00198 UnloadSelectedPlugin(new Guid(pluginsDataGridView.CurrentRow.Cells[1].Value.ToString()));
00199
00200 pluginsDataGridView.Rows[index].Selected = true;
00201 }
00202
00203 private void loadToolStripButton_Click(object sender, EventArgs e)
00204 {
00205 if (pluginsDataGridView.CurrentRow == null)
00206 {
00207 SetButtonsEnabledState();
00208 return;
00209 }
00210
00211 int index = pluginsDataGridView.CurrentRow.Index;
00212
00213 LoadSelectedPlugin(new Guid(pluginsDataGridView.CurrentRow.Cells[1].Value.ToString()));
00214
00215 pluginsDataGridView.Rows[index].Selected = true;
00216
00217 }
00218
00219 private void okButton_Click(object sender, EventArgs e)
00220 {
00221 SaveSettings();
00222 Close();
00223 }
00224
00225 private void visitMpFxPluginGalleryToolStripMenuItem_Click(object sender, EventArgs e)
00226 {
00227 if (_PluginGalleryBrowserForm == null || _PluginGalleryBrowserForm.IsDisposed)
00228 {
00229 _PluginGalleryBrowserForm = new PluginGalleryBrowserForm(_PluginHost);
00230 _PluginGalleryBrowserForm.WindowState = FormWindowState.Normal;
00231 }
00232
00233 _PluginGalleryBrowserForm.Show(this);
00234 }
00235
00236 private void removeToolStripButton_Click(object sender, EventArgs e)
00237 {
00238 if (pluginsDataGridView.CurrentRow == null)
00239 {
00240 removeToolStripButton.Enabled = false;
00241 return;
00242 }
00243
00244 try
00245 {
00246 Guid guid = new Guid(pluginsDataGridView.CurrentRow.Cells[1].Value.ToString());
00247 if (MessageBox.Show(this,
00248 "Are you sure you want to remove the plugin?",
00249 Resources.AppTitle,
00250 MessageBoxButtons.YesNo,
00251 MessageBoxIcon.Question) == DialogResult.No)
00252 {
00253 return;
00254 }
00255
00256 _PluginHost.Plugins[guid].IsMarkedForDeletion = true;
00257 _PluginHost.RemovePlugin(guid);
00258
00259 UnloadSelectedPlugin(guid);
00260 }
00261 catch (Exception exception)
00262 {
00263 MessageBox.Show(this, exception.Message, Resources.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
00264 }
00265 }
00266
00267 private void pluginsDataGridView_SelectionChanged(object sender, EventArgs e)
00268 {
00269 SetButtonsEnabledState();
00270 }
00271
00272 private void installFromToolStripMenuItem_Click(object sender, EventArgs e)
00273 {
00274 try
00275 {
00276 string pluginFile = Tools.OpenDll(this, Resources.AppTitle);
00277
00278 if (string.IsNullOrEmpty(pluginFile))
00279 {
00280 return;
00281 }
00282
00283 statusToolStripLabel.Visible = true;
00284 toolStripProgressBar.Visible = true;
00285
00286 Application.DoEvents();
00287 PluginDescriptor pluginDescriptor = _PluginHost.Gallery.GetPluginInformation(pluginFile);
00288
00289 if (pluginDescriptor == null)
00290 {
00291 MessageBox.Show(this, "The selected assembly is not a valid plugin.", Resources.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
00292 return;
00293 }
00294
00295 if (_PluginHost.Plugins.ContainsKey(pluginDescriptor.Guid))
00296 {
00297 if (_PluginHost.Plugins[pluginDescriptor.Guid].IsMarkedForDeletion)
00298 {
00299 MessageBox.Show(this, "The plugin is already installed and marked for deletion. Please restart this application to complete the removal of the plugin. ", Resources.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
00300 return;
00301 }
00302
00303 MessageBox.Show(this, "The plugin is already installed.", Resources.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
00304 return;
00305 }
00306
00307 Application.DoEvents();
00308
00309 _PluginHost.AddPluginToLocalGallery(pluginDescriptor, pluginFile);
00310
00311 Application.DoEvents();
00312 AppDomain testDomain = AppDomain.CreateDomain(Resources.AppTitle);
00313
00314 IInterfaceInspector interfaceInspector = InterfaceInspector.Get(testDomain, Resources.Inspector);
00315
00316 IMpFxClientPlugin plugin = _PluginHost.LoadPlugin(interfaceInspector, pluginFile, true);
00317
00318 Application.DoEvents();
00319 if (plugin == null)
00320 {
00321 MessageBox.Show(this, "The plugin is could not be installed.", Resources.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
00322 return;
00323
00324 }
00325 LoadPluginInformation();
00326 }
00327 catch (Exception exception)
00328 {
00329 MessageBox.Show(this, exception.Message, Resources.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
00330 }
00331 finally
00332 {
00333 statusToolStripLabel.Visible = false;
00334 toolStripProgressBar.Visible = false;
00335 pluginsToolStrip.Refresh();
00336 }
00337
00338 }
00339
00340 #endregion
00341 }
00342 }