using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Controls; using System.Windows; using HeuristicLab.BackgroundProcessing; using System.IO; using HeuristicLab.Persistence.Default.Xml; using HeuristicLab.Persistence.Core; using HeuristicLab.PluginInfrastructure; namespace HeuristicLab.OKB.Cockpit.Admin { public partial class OperatorGraphEditorHost : UserControl { public enum EditorType { Algorithm, Problem }; private IOperatorGraphEditor activeEditor; private Dictionary availableEditors; public OperatorGraphEditorHost() { InitializeComponent(); availableEditors = ApplicationManager.Manager .GetInstances() .ToDictionary(e => e.PlatformName, e => e); } public void LoadData(byte[] data, OKBAdmin.Platform platform, EditorType type) { availableEditors.TryGetValue(platform.Name, out activeEditor); if (activeEditor == null) { MessageBox.Show( String.Format("Unsupported platform '{0}'", platform.Name), typeof(OperatorGraphEditorHost).Name, MessageBoxButton.OK, MessageBoxImage.Error); return; } try { activeEditor.Load(data); } catch (Exception x) { MessageBox.Show( String.Format("Could not load {0} implementation:\n{1}", platform.Name, x.ToString()), typeof(OperatorGraphEditorHost).Name, MessageBoxButton.OK, MessageBoxImage.Error); } try { Dispatcher.Invoke(new Action(() => { if (type == EditorType.Algorithm) { FormsHost.Child = activeEditor.CreateAlgorithmControl(); } else { FormsHost.Child = activeEditor.CreateProblemControl(); } })); } catch (Exception x) { MessageBox.Show( String.Format("Could not instatiate operator graph editor:\n{1}", x.ToString()), typeof(OperatorGraphEditorHost).Name, MessageBoxButton.OK, MessageBoxImage.Error); } } public byte[] GetAlgorithmData() { return (byte[])Dispatcher.Invoke(new Func(control => { if (activeEditor == null) return null; return activeEditor.GetAlgorithmData(control); }), FormsHost.Child); } public byte[] GetProblemData() { return (byte[])Dispatcher.Invoke(new Func(control => { if (activeEditor == null) return null; return activeEditor.GetProblemData(control); }), FormsHost.Child); } } }