00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 using System;
00013 using System.Collections.Generic;
00014 using System.Diagnostics;
00015 using System.IO;
00016 using System.Reflection;
00017 using System.Windows.Forms;
00018 using Mcs.Epm.MicrosoftProject.mpFx.Client.Plugins.PluginPlublisher.Properties;
00019 using Mcs.Epm.MicrosoftProject.mpFx.Client.Shared;
00020
00021 namespace Mcs.Epm.MicrosoftProject.mpFx.Client.Plugins.PluginPlublisher.Forms
00022 {
00023
00024
00025
00026 public partial class AddPluginForm : Form
00027 {
00028 #region Instance Data
00029
00030 private readonly PluginHost _PluginHost;
00031 private PluginDescriptor _PuginDescrptor;
00032
00033 #endregion
00034
00035 #region Constructors
00036
00037 public AddPluginForm(PluginHost pluginHost)
00038 {
00039 InitializeComponent();
00040 _PluginHost = pluginHost;
00041
00042 filesPanel.Visible = true;
00043 filesPanel.BringToFront();
00044 }
00045
00046 #endregion
00047
00048 #region Private Methods
00049
00050 private void LoadAssembly()
00051 {
00052 filesDataGridView.Rows.Clear();
00053
00054 _PuginDescrptor = _PluginHost.Gallery.GetPluginInformation(assemblyLocationTextBox.Text);
00055
00056 if (_PuginDescrptor == null)
00057 {
00058 MessageBox.Show(this,
00059 "The selected assembly is not a valid plugin.",
00060 Resources.PluginName,
00061 MessageBoxButtons.OK,
00062 MessageBoxIcon.Error);
00063 return;
00064 }
00065
00066 if (_PluginHost.Gallery.Plugins.ContainsKey(_PuginDescrptor.Guid))
00067 {
00068 if (MessageBox.Show(this,
00069 "The plugin already exists in the Gallery. Update it?",
00070 Resources.PluginName,
00071 MessageBoxButtons.YesNo,
00072 MessageBoxIcon.Question) == DialogResult.No)
00073 {
00074 _PuginDescrptor = null;
00075 okButton.Enabled = false;
00076 return;
00077 }
00078 }
00079
00080 nameTextBox.Text = _PuginDescrptor.Name;
00081 versionTextBox.Text = _PuginDescrptor.Version.ToString();
00082 descriptionTextBox.Text = _PuginDescrptor.Description;
00083 authorTextBox.Text = _PuginDescrptor.Author;
00084 tagTextBox.Text = _PuginDescrptor.Tag;
00085 previewLinkLabel.Tag = _PuginDescrptor.Preview;
00086
00087 Assembly assembly = Assembly.LoadFile(assemblyLocationTextBox.Text);
00088
00089 List<string> referencedAssemblies = _PluginHost.GetDependencies(assembly);
00090
00091 foreach (string referencedAssembly in referencedAssemblies)
00092 {
00093 if (!Tools.DoesExistInGridView(filesDataGridView, 0, referencedAssembly))
00094 {
00095 filesDataGridView.Rows.Add(referencedAssembly);
00096 }
00097 }
00098
00099 filesDataGridView.Rows.Add(assemblyLocationTextBox.Text);
00100 }
00101
00102 private void AddPlugin()
00103 {
00104 List<string> files = new List<string>();
00105 foreach (DataGridViewRow row in filesDataGridView.Rows)
00106 {
00107 Application.DoEvents();
00108 if (row.IsNewRow)
00109 {
00110 continue;
00111 }
00112 string file = row.Cells[0].Value.ToString();
00113 if (File.Exists(file))
00114 {
00115 files.Add(file);
00116 }
00117 }
00118
00119 _PluginHost.Gallery.AddPlugin(_PuginDescrptor, files, _PluginHost.Gallery.Plugins.ContainsKey(_PuginDescrptor.Guid));
00120 }
00121
00122 private void SetRemoveFileEnabled()
00123 {
00124 fileRemoveToolStripButton.Enabled = filesDataGridView.CurrentRow != null &&
00125 !filesDataGridView.CurrentRow.Cells[0].Value.ToString().Equals(assemblyLocationTextBox.Text, StringComparison.CurrentCultureIgnoreCase);
00126 }
00127
00128 #endregion
00129
00130 #region Event Handlers
00131
00132 private void filesDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
00133 {
00134 SetRemoveFileEnabled();
00135 }
00136
00137 private void tagTextBox_TextChanged(object sender, EventArgs e)
00138 {
00139 okButton.Enabled = !string.IsNullOrEmpty(tagTextBox.Text) && _PuginDescrptor != null;
00140 }
00141
00142 private void okButton_Click(object sender, EventArgs e)
00143 {
00144 Cursor = Cursors.WaitCursor;
00145
00146 try
00147 {
00148 AddPlugin();
00149 Close();
00150 }
00151 catch (Exception exception)
00152 {
00153 MessageBox.Show(this,
00154 exception.Message,
00155 Resources.PluginName,
00156 MessageBoxButtons.OK,
00157 MessageBoxIcon.Error);
00158
00159 DialogResult = DialogResult.None;
00160 }
00161 finally
00162 {
00163 Cursor = Cursors.Default;
00164 filesPanel.Visible = true;
00165 filesPanel.BringToFront();
00166 Cursor = Cursors.Default;
00167 }
00168 }
00169
00170 private void previewLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
00171 {
00172 try
00173 {
00174 Process.Start(previewLinkLabel.Tag.ToString());
00175 }
00176 catch (Exception exception)
00177 {
00178 MessageBox.Show(this,
00179 exception.Message,
00180 Resources.PluginName,
00181 MessageBoxButtons.OK,
00182 MessageBoxIcon.Error);
00183
00184 DialogResult = DialogResult.None;
00185 }
00186 finally
00187 {
00188 Cursor = Cursors.Default;
00189 }
00190 }
00191
00192 private void addFileToolStripButton_Click(object sender, EventArgs e)
00193 {
00194 string file = Tools.OpenDll(this, Resources.PluginName);
00195
00196 if (!Tools.DoesExistInGridView(filesDataGridView, 0, file))
00197 {
00198 filesDataGridView.Rows.Add(file);
00199 SetRemoveFileEnabled();
00200 }
00201 }
00202
00203 private void fileRemoveToolStripButton_Click(object sender, EventArgs e)
00204 {
00205 if (filesDataGridView.CurrentRow != null)
00206 {
00207 filesDataGridView.Rows.Remove(filesDataGridView.CurrentRow);
00208 SetRemoveFileEnabled();
00209 }
00210 }
00211
00212 private void filesDataGridView_RowDividerHeightChanged(object sender, DataGridViewRowEventArgs e)
00213 {
00214 SetRemoveFileEnabled();
00215 }
00216
00217 private void filesDataGridView_Leave(object sender, EventArgs e)
00218 {
00219 SetRemoveFileEnabled();
00220 }
00221
00222 private void browseButton_Click(object sender, EventArgs e)
00223 {
00224 assemblyLocationTextBox.Text = Tools.OpenDll(this, Resources.PluginName);
00225
00226 if (!string.IsNullOrEmpty(assemblyLocationTextBox.Text))
00227 {
00228 try
00229 {
00230 LoadAssembly();
00231 }
00232 catch (Exception exception)
00233 {
00234 MessageBox.Show(this,
00235 exception.Message,
00236 Resources.PluginName,
00237 MessageBoxButtons.OK,
00238 MessageBoxIcon.Error);
00239
00240 }
00241 }
00242 }
00243
00244 #endregion
00245 }
00246 }