/// <summary> /// Deletes the connectorModel and the connected Connections /// from our WorkspaceModel /// </summary> /// <param name="connectorModel"></param> /// <returns></returns> internal bool deleteConnectorModel(ConnectorModel connectorModel) { //we can only delete ConnectorModels which are part of our WorkspaceModel if (AllConnectorModels.Contains(connectorModel)) { //remove all input ConnectionModels belonging to this Connector from our WorkspaceModel foreach (ConnectionModel connectionModel in new List <ConnectionModel>(connectorModel.InputConnections)) { //deleteConnectionModel(connectionModel); ModifyModel(new DeleteConnectionModelOperation(connectionModel)); } //remove all output ConnectionModels belonging to this Connector from our WorkspaceModel foreach (ConnectionModel outputConnection in new List <ConnectionModel>(connectorModel.OutputConnections)) { //deleteConnectionModel(outputConnection); ModifyModel(new DeleteConnectionModelOperation(outputConnection)); } //remove the connector model from the outputconnectors of this plugin if (connectorModel.PluginModel.OutputConnectors.Contains(connectorModel)) { connectorModel.PluginModel.OutputConnectors.Remove(connectorModel); } //remove the connector model from the inputconnectors of this plugin if (connectorModel.PluginModel.InputConnectors.Contains(connectorModel)) { connectorModel.PluginModel.InputConnectors.Remove(connectorModel); } OnDeletedChildElement(connectorModel); return(AllConnectorModels.Remove(connectorModel)); } return(false); }
/// <summary> /// Checks wether a Connector and a Connector are compatible to be connected /// They are compatible if their types are equal or the base type of the Connector /// is equal to the type of the other Connector. There exists also the following connection /// possibilities: /// - int and BigInteger /// - string and byte array /// - string and cstream /// It is false if already exists a ConnectionModel between both given ConnectorModels /// </summary> /// <param name="connectorModelA"></param> /// <param name="connectorModelB"></param> /// <returns></returns> public static ConversionLevel compatibleConnectors(ConnectorModel connectorModelA, ConnectorModel connectorModelB) { if (connectorModelA == null) { return(ConversionLevel.Red); } if (connectorModelB == null) { return(ConversionLevel.Red); } if (connectorModelA.PluginModel == connectorModelB.PluginModel) { return(ConversionLevel.NA); } if (connectorModelA.Outgoing && connectorModelB.Outgoing) { return(ConversionLevel.Red); } if (!connectorModelA.Outgoing && !connectorModelB.Outgoing) { return(ConversionLevel.Red); } foreach (ConnectionModel connectionModel in connectorModelA.WorkspaceModel.AllConnectionModels) { if ((connectionModel.From == connectorModelA && connectionModel.To == connectorModelB) || (connectionModel.From == connectorModelB && connectionModel.To == connectorModelA)) { return(ConversionLevel.Red); } } // wander 2011-07-06: workaround for #280. May be removed safely in future. if (connectorModelA.ConnectorType == null) { connectorModelA.ConnectorType = typeof(System.Object); } if (connectorModelB.ConnectorType == null) { connectorModelB.ConnectorType = typeof(System.Object); } if (connectorModelA.ConnectorType.Equals(connectorModelB.ConnectorType) || connectorModelA.ConnectorType.FullName == "System.Object" || connectorModelB.ConnectorType.FullName == "System.Object" || connectorModelA.ConnectorType.IsSubclassOf(connectorModelB.ConnectorType) || connectorModelA.ConnectorType.GetInterfaces().Contains(connectorModelB.ConnectorType)) { return(ConversionLevel.Green); } if (((connectorModelA.ConnectorType.FullName == "System.Int32" || connectorModelA.ConnectorType.FullName == "System.Int64") && connectorModelB.ConnectorType.FullName == "System.Numerics.BigInteger") || ((connectorModelB.ConnectorType.FullName == "System.Int32" || connectorModelB.ConnectorType.FullName == "System.Int64") && connectorModelA.ConnectorType.FullName == "System.Numerics.BigInteger") || (connectorModelB.ConnectorType.FullName == "System.String" && connectorModelA.ConnectorType.FullName == "System.Byte[]") || (connectorModelB.ConnectorType.FullName == "System.Byte[]" && connectorModelA.ConnectorType.FullName == "System.String") || (connectorModelB.ConnectorType.FullName == "System.String" && connectorModelA.ConnectorType.FullName == "System.Int32") || (connectorModelB.ConnectorType.FullName == "System.String" && connectorModelA.ConnectorType.FullName == "System.Int64") || (connectorModelB.ConnectorType.FullName == "System.String" && connectorModelA.ConnectorType.FullName == "System.Numerics.BigInteger") || (connectorModelB.ConnectorType.FullName == "System.String" && connectorModelA.ConnectorType.FullName == "System.Boolean") || (connectorModelB.ConnectorType.FullName == "System.String" && connectorModelA.ConnectorType.FullName == "Cryptool.PluginBase.IO.ICryptoolStream") || (connectorModelB.ConnectorType.FullName == "Cryptool.PluginBase.IO.ICryptoolStream" && connectorModelA.ConnectorType.FullName == "System.String")) { return(ConversionLevel.Yellow); } return(ConversionLevel.Red); }
/// <summary> /// Generate a single Connector of this Plugin. /// </summary> /// <param name="propertyInfoAttribute"></param> internal void generateConnector(PropertyInfoAttribute propertyInfoAttribute) { if (propertyInfoAttribute.Direction.Equals(Direction.InputData)) { ConnectorModel connectorModel = new ConnectorModel(); connectorModel.Caption = propertyInfoAttribute.Caption; connectorModel.ConnectorType = propertyInfoAttribute.PropertyInfo.PropertyType; connectorModel.WorkspaceModel = WorkspaceModel; connectorModel.PluginModel = this; connectorModel.IsMandatory = propertyInfoAttribute.Mandatory; connectorModel.PropertyName = propertyInfoAttribute.PropertyName; connectorModel.Name = propertyInfoAttribute.PropertyName; connectorModel.ToolTip = propertyInfoAttribute.ToolTip; connectorModel.IControl = false; connectorModel.PluginModel.Plugin.PropertyChanged += connectorModel.PropertyChangedOnPlugin; InputConnectors.Add(connectorModel); WorkspaceModel.AllConnectorModels.Add(connectorModel); } else if (propertyInfoAttribute.Direction.Equals(Direction.ControlSlave)) { ConnectorModel connectorModel = new ConnectorModel(); connectorModel.Caption = propertyInfoAttribute.Caption; connectorModel.ConnectorType = propertyInfoAttribute.PropertyInfo.PropertyType; connectorModel.WorkspaceModel = WorkspaceModel; connectorModel.PluginModel = this; connectorModel.IsMandatory = propertyInfoAttribute.Mandatory; connectorModel.PropertyName = propertyInfoAttribute.PropertyName; connectorModel.Name = propertyInfoAttribute.PropertyName; connectorModel.ToolTip = propertyInfoAttribute.ToolTip; connectorModel.IControl = true; connectorModel.PluginModel.Plugin.PropertyChanged += connectorModel.PropertyChangedOnPlugin; InputConnectors.Add(connectorModel); WorkspaceModel.AllConnectorModels.Add(connectorModel); } else if (propertyInfoAttribute.Direction.Equals(Direction.OutputData)) { ConnectorModel connectorModel = new ConnectorModel(); connectorModel.Caption = propertyInfoAttribute.Caption; connectorModel.ConnectorType = propertyInfoAttribute.PropertyInfo.PropertyType; connectorModel.WorkspaceModel = WorkspaceModel; connectorModel.PluginModel = this; connectorModel.IsMandatory = propertyInfoAttribute.Mandatory; connectorModel.PropertyName = propertyInfoAttribute.PropertyName; connectorModel.Name = propertyInfoAttribute.PropertyName; connectorModel.ToolTip = propertyInfoAttribute.ToolTip; connectorModel.Outgoing = true; connectorModel.IControl = false; connectorModel.PluginModel.Plugin.PropertyChanged += connectorModel.PropertyChangedOnPlugin; OutputConnectors.Add(connectorModel); WorkspaceModel.AllConnectorModels.Add(connectorModel); } else if (propertyInfoAttribute.Direction.Equals(Direction.ControlMaster)) { ConnectorModel connectorModel = new ConnectorModel(); connectorModel.Caption = propertyInfoAttribute.Caption; connectorModel.ConnectorType = propertyInfoAttribute.PropertyInfo.PropertyType; connectorModel.WorkspaceModel = WorkspaceModel; connectorModel.PluginModel = this; connectorModel.IsMandatory = propertyInfoAttribute.Mandatory; connectorModel.PropertyName = propertyInfoAttribute.PropertyName; connectorModel.Name = propertyInfoAttribute.PropertyName; connectorModel.ToolTip = propertyInfoAttribute.ToolTip; connectorModel.Outgoing = true; connectorModel.IControl = true; connectorModel.PluginModel.Plugin.PropertyChanged += connectorModel.PropertyChangedOnPlugin; OutputConnectors.Add(connectorModel); WorkspaceModel.AllConnectorModels.Add(connectorModel); } }
private void restoreSettings(PersistantModel persistantModel, WorkspaceModel workspacemodel) { //restore all settings of each plugin foreach (PersistantPlugin persistantPlugin in persistantModel.PersistantPluginList) { if (persistantPlugin.PluginModel.Plugin.Settings == null) { continue; // do not attempt deserialization if plugin type has no settings } foreach (PersistantSetting persistantSetting in persistantPlugin.PersistantSettingsList) { PropertyInfo[] arrpInfo = persistantPlugin.PluginModel.Plugin.Settings.GetType().GetProperties(); foreach (PropertyInfo pInfo in arrpInfo) { try { DontSaveAttribute[] dontSave = (DontSaveAttribute[])pInfo.GetCustomAttributes(typeof(DontSaveAttribute), false); if (dontSave.Length == 0) { if (pInfo.Name.Equals(persistantSetting.Name)) { if (persistantSetting.Type.Equals("System.String")) { pInfo.SetValue(persistantPlugin.PluginModel.Plugin.Settings, (String)persistantSetting.Value, null); } else if (persistantSetting.Type.Equals("System.Int16")) { pInfo.SetValue(persistantPlugin.PluginModel.Plugin.Settings, System.Int16.Parse((String)persistantSetting.Value), null); } else if (persistantSetting.Type.Equals("System.Int32")) { pInfo.SetValue(persistantPlugin.PluginModel.Plugin.Settings, System.Int32.Parse((String)persistantSetting.Value), null); } else if (persistantSetting.Type.Equals("System.Int64")) { pInfo.SetValue(persistantPlugin.PluginModel.Plugin.Settings, System.Int64.Parse((String)persistantSetting.Value), null); } else if (persistantSetting.Type.Equals("System.Double")) { pInfo.SetValue(persistantPlugin.PluginModel.Plugin.Settings, System.Double.Parse((String)persistantSetting.Value), null); } else if (persistantSetting.Type.Equals("System.Boolean")) { pInfo.SetValue(persistantPlugin.PluginModel.Plugin.Settings, System.Boolean.Parse((String)persistantSetting.Value), null); } else if (pInfo.PropertyType.IsEnum) { Int32 result = 0; System.Int32.TryParse((String)persistantSetting.Value, out result); object newEnumValue = Enum.ToObject(pInfo.PropertyType, result); pInfo.SetValue(persistantPlugin.PluginModel.Plugin.Settings, newEnumValue, null); } } } } catch (Exception ex) { throw new Exception(String.Format(Resources.ModelPersistance_restoreSettings_Could_not_restore_the_setting___0___of_plugin___1__, persistantSetting.Name, persistantPlugin.PluginModel.Name), ex); } } } } //check if all properties belonging to its ConnectorModels really exist and if each property has a ConnectorModel //if not generate new ConnectorModels foreach (PluginModel pluginModel in workspacemodel.AllPluginModels) { var connectorModels = new List <ConnectorModel>(); connectorModels.AddRange(pluginModel.InputConnectors); connectorModels.AddRange(pluginModel.OutputConnectors); //Check if a property of a ConnectorModel was deleted or its type changed => delete the ConnectorModel);); foreach (ConnectorModel connectorModel in new List <ConnectorModel>(connectorModels)) { var propertyInfo = connectorModel.PluginModel.Plugin.GetType().GetProperty(connectorModel.PropertyName); if (propertyInfo == null || !connectorModel.ConnectorType.Equals(propertyInfo.PropertyType)) { //the property belonging to this ConnectorModel was not found //or the type of the saved property differs to the real one //so we delete the connector pluginModel.WorkspaceModel.deleteConnectorModel(connectorModel); connectorModels.Remove(connectorModel); GuiLogMessage(string.Format(Resources.ModelPersistance_restoreSettings_A_property_with_name___0___of_type___1___does_not_exist_in___2___3___but_a_ConnectorModel_exists_in_the_PluginModel__Delete_the_ConnectorModel_now_, connectorModel.PropertyName, connectorModel.ConnectorType.Name, pluginModel.PluginType, pluginModel.Name), NotificationLevel.Warning); } } //Check if there are properties which have no own ConnectorModel foreach (PropertyInfoAttribute propertyInfoAttribute in pluginModel.Plugin.GetProperties()) { var query = from c in connectorModels where c.PropertyName.Equals(propertyInfoAttribute.PropertyName) select c; if (query.Count() == 0) { //we found a property which has no ConnectorModel, so we create a new one pluginModel.generateConnector(propertyInfoAttribute); GuiLogMessage(string.Format(Resources.ModelPersistance_restoreSettings_A_ConnectorModel_for_the_plugins_property___0___of_type___1___does_not_exist_in_the_PluginModel_of___2___3____Create_a_ConnectorModel_now_, propertyInfoAttribute.PropertyName, propertyInfoAttribute.PropertyInfo.PropertyType.Name, pluginModel.PluginType, pluginModel.Name), NotificationLevel.Warning); } } } //initialize the plugins //connect all listener for plugins/plugin models foreach (PluginModel pluginModel in workspacemodel.AllPluginModels) { try { pluginModel.Plugin.Initialize(); if (pluginModel.Plugin.Settings != null) { pluginModel.Plugin.Settings.Initialize(); } pluginModel.PercentageFinished = 0; } catch (Exception ex) { throw new Exception(String.Format(Resources.ModelPersistance_restoreSettings_Error_while_initializing____0__, pluginModel.Name), ex); } pluginModel.Plugin.OnGuiLogNotificationOccured += workspacemodel.GuiLogMessage; pluginModel.Plugin.OnPluginProgressChanged += pluginModel.PluginProgressChanged; pluginModel.Plugin.OnPluginStatusChanged += pluginModel.PluginStatusChanged; if (pluginModel.Plugin.Settings != null) { pluginModel.Plugin.Settings.PropertyChanged += pluginModel.SettingsPropertyChanged; } } foreach (ConnectorModel connectorModel in workspacemodel.AllConnectorModels) { //refresh language stuff foreach (var property in connectorModel.PluginModel.Plugin.GetProperties()) { if (property.PropertyName.Equals(connectorModel.PropertyName)) { connectorModel.ToolTip = property.ToolTip; connectorModel.Caption = property.Caption; break; } } connectorModel.PluginModel.Plugin.PropertyChanged += connectorModel.PropertyChangedOnPlugin; } //restore all IControls foreach (ConnectionModel connectionModel in workspacemodel.AllConnectionModels) { ConnectorModel from = connectionModel.From; ConnectorModel to = connectionModel.To; try { if (from.IControl && to.IControl) { object data = null; //Get IControl data from "to" data = to.PluginModel.Plugin.GetType().GetProperty(to.PropertyName).GetValue(to.PluginModel.Plugin, null); PropertyInfo propertyInfo = from.PluginModel.Plugin.GetType().GetProperty(from.PropertyName); propertyInfo.SetValue(from.PluginModel.Plugin, data, null); } } catch (Exception ex) { throw new Exception(String.Format(Resources.ModelPersistance_restoreSettings_Error_while_restoring_IControl_Connection_between___0___to___1____Workspace_surely_will_not_work_well_, from.PluginModel.Name, to.PluginModel.Name), ex); } } //Check if all TextModels and ImageModelsmodels are valid (byte array != null || byte array is empty) //Otherwise delete them from the model and show a warning GuiLogMessage foreach (var textModel in new List <TextModel>(workspacemodel.AllTextModels)) { if (!textModel.HasData()) { GuiLogMessage( string.Format(Resources.ModelPersistance_restoreSettings_TextModel), NotificationLevel.Warning); workspacemodel.AllTextModels.Remove(textModel); } } foreach (var imageModel in new List <ImageModel>(workspacemodel.AllImageModels)) { if (!imageModel.HasData()) { GuiLogMessage(string.Format(Resources.ModelPersistance_restoreSettings_ImageModel), NotificationLevel.Warning); workspacemodel.AllImageModels.Remove(imageModel); } } }