Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 84 additions & 65 deletions ModelDesigner.Configuration/ServerSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ public ServerWrapper SelectedAssembly
/// </summary>
public ServerSelector(IGraphicalUserInterface graphicalUserInterface, ISolutionDirectoryPathManagement solutionPath, string codebase, string configuration)
{
if (string.IsNullOrEmpty(codebase))
return;
GraphicalUserInterface = graphicalUserInterface ?? throw new ArgumentNullException(nameof(graphicalUserInterface));
SolutionPath = solutionPath ?? throw new ArgumentNullException(nameof(solutionPath));
OpenPlugIn(solutionPath, codebase, configuration);
LicenseProtection.CheckConstrain();
}
Expand Down Expand Up @@ -259,14 +258,26 @@ public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext contex
/// <returns>The new value of the object. If the value of the object has not changed, this should return the same object it was passed.</returns>
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
return OpenPlugInAssembly(value as ServerWrapper);
ServerWrapper _currentPlugin = value as ServerWrapper;
if (_currentPlugin == null)
return null;
return PluginHelper.OpenPlugInAssembly(_currentPlugin, x => SetupGUI(x, _currentPlugin), _currentPlugin.SolutionPath, new GraphicalUserInterface());
}

#endregion UITypeEditor override

private void SetupGUI(IFileDialog openFileDialog, ServerWrapper currentPlugin)
{
openFileDialog.InitialDirectory = Path.GetFullPath(currentPlugin.PluginDescription.FullName);
openFileDialog.FileName = Path.GetFileName(currentPlugin.PluginDescription.FullName);
openFileDialog.Title = Properties.Resources.OpenPluginTitle;
openFileDialog.Filter = Properties.Resources.OpenPluginFilter;
}
}

private ServerWrapper m_Server = null;
private static IGraphicalUserInterface GraphicalUserInterface;
private IGraphicalUserInterface GraphicalUserInterface;
private ISolutionDirectoryPathManagement SolutionPath { get; }

//methods
private void RaiseOnConfigurationChanged(bool serverChanged)
Expand All @@ -282,6 +293,8 @@ private void RaiseOnConfigurationChanged(bool serverChanged)
/// <param name="configuration">The configuration.</param>
private void OpenPlugIn(ISolutionDirectoryPathManagement solutionPath, string codebase, string configuration)
{
if (string.IsNullOrEmpty(codebase))
return;
string _pluginFullName = IO.RelativeFilePathsCalculator.CalculateAbsoluteFileName(solutionPath.DefaultDirectory, codebase);
if (!File.Exists(_pluginFullName))
_pluginFullName = Path.Combine(solutionPath.DefaultDirectory, codebase);
Expand All @@ -297,7 +310,7 @@ private void OpenPlugIn(ISolutionDirectoryPathManagement solutionPath, string co
IConfiguration _svrInterface;
try
{
GetIServerConfiguration(_fileInfo, out _assembly, out _svrInterface);
PluginHelper.GetIServerConfiguration(_fileInfo, out _assembly, out _svrInterface);
}
catch (Exception ex)
{
Expand All @@ -316,65 +329,6 @@ private void OpenPlugIn(ISolutionDirectoryPathManagement solutionPath, string co
SelectedAssembly = newSelectedAssembly;
}

private static ServerWrapper OpenPlugInAssembly(ServerWrapper server)
{
FileInfo info;
do
{
using (IFileDialog _ofg = GraphicalUserInterface.OpenFileDialogFunc())
{
string _baseDirectory = server.SolutionPath.DefaultDirectory;
if (!string.IsNullOrEmpty(_baseDirectory))
_ofg.InitialDirectory = _baseDirectory;
if (server != null && server.PluginDescription != null)
_ofg.FileName = server.PluginDescription.CodeBase;
_ofg.Title = Properties.Resources.OpenPluginTitle;
_ofg.Filter = Properties.Resources.OpenPluginFilter;
if (_ofg.ShowDialog())
return server;
info = new FileInfo(_ofg.FileName);
}
try
{
if (!info.Exists)
{
GraphicalUserInterface.MessageBoxShowWarning(Resources.OpenPluginWarningNotExist, Resources.OpenPluginTitle);
continue;
}
GetIServerConfiguration(info, out Assembly _pluginAssembly, out IConfiguration _serverConfiguration);
if (_serverConfiguration == null)
{
GraphicalUserInterface.MessageBoxShowWarning(Resources.InterfaceNotImplemented, Resources.OpenPluginTitle);
continue;
}
server = new ServerWrapper(_serverConfiguration, new DataProviderDescription(_pluginAssembly), GraphicalUserInterface, server.SolutionPath);
}
catch (Exception ex)
{
GraphicalUserInterface.MessageBoxShowWarning(ex.Message, Resources.OpenPluginTitle);
}
} while (false);
return server;
}

private static void GetIServerConfiguration(FileInfo info, out Assembly pluginAssembly, out IConfiguration serverConfiguration)
{
string iName = typeof(IConfiguration).ToString();
pluginAssembly = Assembly.LoadFrom(info.FullName);
serverConfiguration = null;
foreach (Type pluginType in pluginAssembly.GetExportedTypes())
//Only look at public types
if (pluginType.IsPublic && !pluginType.IsAbstract && pluginType.GetInterface(iName) != null)
try
{
serverConfiguration = (IConfiguration)Activator.CreateInstance(pluginType);
}
catch (TargetInvocationException _ex)
{
throw new ApplicationException(string.Format("The server configuration plug-in {0}/{1} cannot be loaded. Contact the vendor to get current version of this component", pluginType.FullName, info.Name), _ex);
}
}

#region event handlers

private void m_Server_OnConfigurationChanged(object sender, UAServerConfigurationEventArgs e)
Expand All @@ -394,11 +348,76 @@ private void PluginMenuItemsClear_Click(object sender, EventArgs e)

private void PluginMenuItemsOpen_Click(object sender, EventArgs e)
{
SelectedAssembly = OpenPlugInAssembly(SelectedAssembly);
SelectedAssembly = PluginHelper.OpenPlugInAssembly(this.SelectedAssembly, SetupGUI, this.SolutionPath, GraphicalUserInterface);
}

private void SetupGUI(IFileDialog openFileDialog)
{
openFileDialog.InitialDirectory = this.SelectedAssembly == null ? SolutionPath.DefaultDirectory : SelectedAssembly.SolutionPath.DefaultDirectory;
openFileDialog.FileName = this.SelectedAssembly == null ? String.Empty : SelectedAssembly.PluginDescription.CodeBase;
openFileDialog.Title = Properties.Resources.OpenPluginTitle;
openFileDialog.Filter = Properties.Resources.OpenPluginFilter;
}

#endregion event handlers

#endregion private

private static class PluginHelper
{
internal static ServerWrapper OpenPlugInAssembly(ServerWrapper serverWrapper, Action<IFileDialog> setupGUI, ISolutionDirectoryPathManagement solutionPath, IGraphicalUserInterface gui)
{
ServerWrapper _ret = serverWrapper;
FileInfo info;
do
{
using (IFileDialog _ofg = gui.OpenFileDialogFunc())
{
setupGUI(_ofg);
if (!_ofg.ShowDialog())
return _ret;
info = new FileInfo(_ofg.FileName);
}
try
{
if (!info.Exists)
{
gui.MessageBoxShowWarning(Resources.OpenPluginWarningNotExist, Resources.OpenPluginTitle);
continue;
}
GetIServerConfiguration(info, out Assembly _pluginAssembly, out IConfiguration _serverConfiguration);
if (_serverConfiguration == null)
{
gui.MessageBoxShowWarning(Resources.InterfaceNotImplemented, Resources.OpenPluginTitle);
continue;
}
_ret = new ServerWrapper(_serverConfiguration, new DataProviderDescription(_pluginAssembly), gui, solutionPath);
}
catch (Exception ex)
{
gui.MessageBoxShowWarning(ex.Message, Resources.OpenPluginTitle);
}
} while (false);
return _ret;
}

internal static void GetIServerConfiguration(FileInfo info, out Assembly pluginAssembly, out IConfiguration serverConfiguration)
{
string iName = typeof(IConfiguration).ToString();
pluginAssembly = Assembly.LoadFrom(info.FullName);
serverConfiguration = null;
foreach (Type pluginType in pluginAssembly.GetExportedTypes())
//Only look at public types
if (pluginType.IsPublic && !pluginType.IsAbstract && pluginType.GetInterface(iName) != null)
try
{
serverConfiguration = (IConfiguration)Activator.CreateInstance(pluginType);
}
catch (TargetInvocationException _ex)
{
throw new ApplicationException(string.Format("The server configuration plug-in {0}/{1} cannot be loaded. Contact the vendor to get current version of this component", pluginType.FullName, info.Name), _ex);
}
}
}
}
}
10 changes: 5 additions & 5 deletions ModelDesigner.Configuration/ServerWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ public ServerWrapper(IConfiguration plugin, IDataProviderDescription assembly, I
/// </summary>
/// <param name="plugin">The interface to get access to the plugin.</param>
/// <param name="assembly">TAn assembly containing the plug-in.</param>
/// <param name="userInterface">The user interaction interface that provides basic functionality to implement user interactivity.</param>
/// <param name="gui">The user interaction interface that provides basic functionality to implement user interactivity.</param>
/// <param name="solutionPath">The solution path.</param>
/// <param name="configuration">The file path containing the configuration.</param>
public ServerWrapper(IConfiguration plugin, IDataProviderDescription assembly, IGraphicalUserInterface userInterface, ISolutionDirectoryPathManagement solutionPath, string configuration)
public ServerWrapper(IConfiguration plugin, IDataProviderDescription assembly, IGraphicalUserInterface gui, ISolutionDirectoryPathManagement solutionPath, string configuration)
{
this.SolutionPath = solutionPath ?? throw new ArgumentNullException(nameof(solutionPath));
m_Server = plugin;
SolutionPath = solutionPath ?? throw new ArgumentNullException(nameof(solutionPath));
m_Server = plugin ?? throw new ArgumentNullException(nameof(plugin)); ;
m_Server.OnModified += new EventHandler<UAServerConfigurationEventArgs>(OnConfigurationDataChangeHandler);
PluginDescription = assembly;
FileInfo _file = null;
Expand All @@ -89,7 +89,7 @@ public ServerWrapper(IConfiguration plugin, IDataProviderDescription assembly, I
string _path = IO.RelativeFilePathsCalculator.CalculateAbsoluteFileName(solutionPath.DefaultDirectory, configuration);
_file = new FileInfo(_path);
}
Configuration = new ConfigurationWrapper(_file, m_Server, userInterface);
Configuration = new ConfigurationWrapper(_file, m_Server, gui);
}

#endregion Constructors
Expand Down