00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 using System;
00013 using System.Reflection;
00014 using System.Windows.Forms;
00015 using Mcs.Epm.MicrosoftProject.mpFx.Client.Plugins.EventHandlersPlugin.Properties;
00016 using Mcs.Epm.MicrosoftProject.mpFx.Client.Shared;
00017 using Mcs.Epm.MicrosoftProject.mpFx.EventsWebService;
00018
00019 namespace Mcs.Epm.MicrosoftProject.mpFx.Client.Plugins.EventHandlersPlugin.Controls
00020 {
00021 public partial class AddEventHandlerControl : UserControl
00022 {
00023 private readonly EventHandlerPlugIn _EventHandlerPlugIn;
00024
00025 public event EventHandler OnCancelClick;
00026 public event EventHandler OnOkClick;
00027
00028 public AddEventHandlerControl(EventHandlerPlugIn eventHandlerPlugIn)
00029 {
00030 InitializeComponent();
00031
00032 _EventHandlerPlugIn = eventHandlerPlugIn;
00033 PositionControlsPanel();
00034 }
00035
00036 private void PositionControlsPanel()
00037 {
00038 controlsPanel.Top = Height / 2 - controlsPanel.Height / 2;
00039 controlsPanel.Left = Width / 2 - controlsPanel.Width / 2;
00040 }
00041
00042 private void cancelButton_Click(object sender, EventArgs e)
00043 {
00044 if (OnCancelClick != null)
00045 {
00046 OnCancelClick(sender, e);
00047 }
00048 }
00049
00050 private void okButton_Click(object sender, EventArgs e)
00051 {
00052 Assembly eventHandlerAssembly = Assembly.LoadFile(assemblyPathTextBox.Text);
00053 foreach (DataGridViewRow row in eventHandlersdataGridView.Rows)
00054 {
00055 if (Convert.ToBoolean(row.Cells[0].Value))
00056 {
00057 PSEventID? eventID = (PSEventID) row.Tag;
00058
00059 if (_EventHandlerPlugIn.ProjectServer.Events.IsEventHandlerRegisteredForClass((PSEventID) eventID,
00060 row.Cells[2].Value.ToString()))
00061 {
00062 if (MessageBox.Show(this,
00063 Tools.Format(Resources.EventHandlerExists, ((PSEventID)eventID).ToString(), row.Cells[2].Value.ToString()),
00064 _EventHandlerPlugIn.Name,
00065 MessageBoxButtons.OKCancel,
00066 MessageBoxIcon.Information) == DialogResult.Cancel)
00067
00068 {
00069 break;
00070 }
00071 continue;
00072 }
00073
00074 try
00075 {
00076 _EventHandlerPlugIn.ProjectServer.Events.Install((PSEventID) eventID,
00077 eventHandlerAssembly.FullName,
00078 row.Cells[1].Value.ToString(),
00079 row.Cells[3].Value.ToString(),
00080 row.Cells[4].Value.ToString(),
00081 row.Cells[2].Value.ToString(),
00082 string.Empty);
00083 }
00084 catch (Exception exception)
00085 {
00086 MessageBox.Show(this,
00087 exception.Message,
00088 _EventHandlerPlugIn.Name,
00089 MessageBoxButtons.OKCancel,
00090 MessageBoxIcon.Information);
00091 return;
00092 }
00093 }
00094 }
00095 if (OnOkClick != null)
00096 {
00097 OnOkClick(sender, e);
00098 }
00099 }
00100
00101 private void AddEventHandlerControl_Load(object sender, EventArgs e)
00102 {
00103
00104 }
00105
00106 private void AddEventHandlerControl_Resize(object sender, EventArgs e)
00107 {
00108 PositionControlsPanel();
00109 }
00110
00111 private void openFileButton_Click(object sender, EventArgs e)
00112 {
00113 using (OpenFileDialog openFileDialog = new OpenFileDialog())
00114 {
00115 openFileDialog.CheckFileExists = true;
00116 openFileDialog.CheckPathExists = true;
00117 openFileDialog.DefaultExt = "dll";
00118 openFileDialog.Filter = ".NET Assemblies (*.dll) | *.dll";
00119 openFileDialog.Multiselect = false;
00120 openFileDialog.Title = Resources.PluginTitle;
00121
00122
00123 if (openFileDialog.ShowDialog() == DialogResult.Cancel)
00124 {
00125 return;
00126 }
00127
00128 assemblyPathTextBox.Text = openFileDialog.FileName;
00129
00130 LoadAssemblyEventHandlers();
00131 }
00132 }
00133
00134 private void LoadAssemblyEventHandlers()
00135 {
00136 try
00137 {
00138 eventHandlersdataGridView.Rows.Clear();
00139
00140 Assembly eventHandlerAssembly = Assembly.LoadFile(assemblyPathTextBox.Text);
00141 foreach (Type type in eventHandlerAssembly.GetTypes())
00142 {
00143 foreach (MethodInfo method in type.GetMethods())
00144 {
00145
00146 if (method.DeclaringType.FullName == type.FullName)
00147 {
00148 int index = type.BaseType.Name.LastIndexOf("EventReceiver");
00149 if (index == -1)
00150 {
00151 continue;
00152 }
00153 string sourceName = type.BaseType.Name.Substring(0, index);
00154 string eventName = method.Name.Substring(2);
00155 string eventMethod = method.Name;
00156 string className = type.FullName;
00157
00158 PSEventID? eventId = _EventHandlerPlugIn.ProjectServer.Events.GetEventID(sourceName, eventName);
00159
00160 int newRowIndex = eventHandlersdataGridView.Rows.Add(false, sourceName, className, eventName, eventMethod, 0);
00161
00162 eventHandlersdataGridView.Rows[newRowIndex].Tag = eventId;
00163 }
00164 }
00165 }
00166 }
00167 catch (Exception e)
00168 {
00169 MessageBox.Show(null, e.Message, Resources.PluginTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
00170 }
00171 }
00172
00173 private void eventHandlersdataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
00174 {
00175 foreach (DataGridViewRow row in eventHandlersdataGridView.Rows)
00176 {
00177 if (Convert.ToBoolean(row.Cells[0].Value))
00178 {
00179 okButton.Enabled = true;
00180 return;
00181 }
00182 }
00183
00184 okButton.Enabled = false;
00185 }
00186 }
00187 }