source: trunk/CrypPlugins/WorkspaceManager/Model/WorkspaceModel.cs @ 1790

Last change on this file since 1790 was 1790, checked in by kopal, 11 years ago
  • now the initializes() method of a plugin will be called when the workspace is deserialized
  • bug fix of the minimized view of the plugins
File size: 13.3 KB
Line 
1/*                             
2   Copyright 2010 Nils Kopal, Viktor M.
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15*/
16
17using System;
18using System.Collections.Generic;
19using System.Linq;
20using System.Text;
21using System.Windows;
22using Cryptool.PluginBase;
23using System.Reflection;
24
25namespace WorkspaceManager.Model
26{
27    /// <summary>
28    /// Class to represent our Workspace
29    /// </summary>
30    [Serializable]
31    public class WorkspaceModel 
32    {
33        [NonSerialized]
34        private WorkspaceManager workspaceManagerEditor;
35
36        /// <summary>
37        /// The surrounding WorkspaceManagerEditor
38        /// </summary>
39        public WorkspaceManager WorkspaceManagerEditor { 
40            get{
41                return workspaceManagerEditor;
42            }
43            set{ 
44                workspaceManagerEditor = value;
45            }
46        }
47
48        /// <summary>
49        /// All PluginModels of our Workspace Model
50        /// </summary>
51        public List<PluginModel> AllPluginModels;
52
53        /// <summary>
54        /// All Connector Models of our Workspace Model
55        /// </summary>
56        public List<ConnectorModel> AllConnectorModels;
57
58        /// <summary>
59        /// All ConnectionModels of our Workspace Model
60        /// </summary>
61        public List<ConnectionModel> AllConnectionModels;
62
63        /// <summary>
64        /// Creates a new Workspace Model
65        /// </summary>
66        public WorkspaceModel()
67        { 
68            this.AllPluginModels = new List<PluginModel>();
69            this.AllConnectionModels = new List<ConnectionModel>();
70            this.AllConnectorModels = new List<ConnectorModel>();           
71        }
72
73        /// <summary>
74        /// Creates a new PluginModel belonging to this WorkspaceModel
75        /// </summary>
76        /// <param name="x"></param>
77        /// <param name="y"></param>
78        /// <param name="width"></param>
79        /// <param name="height"></param>
80        /// <param name="pluginType"></param>
81        /// <returns></returns>
82        public PluginModel newPluginModel(Point position, double width, double height, Type pluginType)
83        {
84            PluginModel pluginModel = new PluginModel();
85            pluginModel.WorkspaceModel = this;           
86            pluginModel.Startable = pluginType.GetPluginInfoAttribute().Startable;
87            pluginModel.Position = position;
88            pluginModel.Width = width;
89            pluginModel.Height = height;
90            pluginModel.PluginType = pluginType;           
91            pluginModel.Name = pluginType.Name;
92            pluginModel.RepeatStart = false;
93            pluginModel.generateConnectors();
94            pluginModel.Plugin.Initialize();
95            pluginModel.Plugin.OnGuiLogNotificationOccured += this.WorkspaceManagerEditor.GuiLogNotificationOccured;
96            pluginModel.Plugin.OnGuiLogNotificationOccured += pluginModel.GuiLogNotificationOccured;
97            pluginModel.Plugin.OnPluginProgressChanged += pluginModel.PluginProgressChanged;
98            pluginModel.Plugin.OnPluginStatusChanged += pluginModel.PluginStatusChanged;           
99            this.AllPluginModels.Add(pluginModel);
100            this.WorkspaceManagerEditor.HasChanges = true;
101            return pluginModel;
102        }
103
104        /// <summary>
105        /// Creates a new PluginModel belonging to this WorkspaceModel
106        /// Position and Dimension are (x,y,width,height) = (0,0,0,0)
107        /// </summary>
108        /// <param name="pluginType"></param>
109        /// <returns></returns>
110        public PluginModel newPluginModel(Type pluginType)
111        {
112            return newPluginModel(new Point(0, 0), 0, 0, pluginType);
113        }       
114
115        /// <summary>
116        /// Creates a new Connection starting at "from"-Connector going to "to"-Connector with
117        /// the given connectionType
118        /// </summary>
119        /// <param name="from"></param>
120        /// <param name="to"></param>
121        /// <param name="connectionType"></param>
122        /// <returns></returns>
123        public ConnectionModel newConnectionModel(ConnectorModel from, ConnectorModel to, Type connectionType)
124        {
125            ConnectionModel connectionModel = new ConnectionModel();
126            connectionModel.WorkspaceModel = this;
127            connectionModel.From = from;
128            connectionModel.To = to;
129            from.OutputConnections.Add(connectionModel);
130            to.InputConnections.Add(connectionModel);
131            connectionModel.ConnectionType = connectionType;
132
133            //If we connect two IControls we have to set data directly:
134            if (from.IControl && to.IControl)
135            {
136                object data = null;
137                //Get IControl data from "to"
138                if (to.IsDynamic)
139                {
140                    data = to.PluginModel.Plugin.GetType().GetMethod(to.DynamicGetterName).Invoke(to.PluginModel.Plugin, new object[] { to.PropertyName });
141                }
142                else
143                {
144                    data = to.PluginModel.Plugin.GetType().GetProperty(to.PropertyName).GetValue(to.PluginModel.Plugin, null);
145                }
146
147                //Set IControl data
148                if (from.IsDynamic)
149                {
150                    MethodInfo propertyInfo = from.PluginModel.Plugin.GetType().GetMethod(from.DynamicSetterName);
151                    propertyInfo.Invoke(from.PluginModel.Plugin, new object[] { from.PropertyName, data });
152                }
153                else
154                {
155                    PropertyInfo propertyInfo = from.PluginModel.Plugin.GetType().GetProperty(from.PropertyName);
156                    propertyInfo.SetValue(from.PluginModel.Plugin, data, null);
157                }
158            }
159
160            this.AllConnectionModels.Add(connectionModel);
161            this.WorkspaceManagerEditor.HasChanges = true;
162            return connectionModel;
163        }
164
165        /// <summary>
166        /// Deletes the pluginModel and all of its Connectors and the connected Connections
167        /// from our WorkspaceModel
168        /// </summary>
169        /// <param name="pluginModel"></param>
170        /// <returns></returns>
171        public bool deletePluginModel(PluginModel pluginModel)
172        {
173            //we can only delete PluginModels which are part of our WorkspaceModel
174            if (this.AllPluginModels.Contains(pluginModel))
175            {
176                // remove all InputConnectors belonging to this pluginModel from our WorkspaceModel
177                foreach (ConnectorModel inputConnector in new List<ConnectorModel>(pluginModel.InputConnectors))
178                {
179                    deleteConnectorModel(inputConnector);
180                }
181
182                // remove all OutputConnectors belonging to this pluginModel from our WorkspaceModel
183                foreach (ConnectorModel outputConnector in new List<ConnectorModel>(pluginModel.OutputConnectors))
184                {
185                    deleteConnectorModel(outputConnector);
186                }
187                pluginModel.Plugin.Dispose();
188                pluginModel.onDelete();
189                this.WorkspaceManagerEditor.HasChanges = true;
190                return this.AllPluginModels.Remove(pluginModel);
191            }           
192            return false;
193        }
194
195        /// <summary>
196        /// Deletes the connectorModel and the connected Connections
197        /// from our WorkspaceModel
198        /// </summary>
199        /// <param name="connectorModel"></param>
200        /// <returns></returns>
201        private bool deleteConnectorModel(ConnectorModel connectorModel)
202        {
203            //we can only delete ConnectorModels which are part of our WorkspaceModel
204            if(this.AllConnectorModels.Contains(connectorModel)){
205
206                //remove all input ConnectionModels belonging to this Connector from our WorkspaceModel
207                foreach (ConnectionModel connectionModel in new List<ConnectionModel>(connectorModel.InputConnections))
208                {
209                    deleteConnectionModel(connectionModel);
210                }
211
212                //remove all output ConnectionModels belonging to this Connector from our WorkspaceModel
213                foreach (ConnectionModel outputConnection in new List<ConnectionModel>(connectorModel.OutputConnections))
214                {
215                    deleteConnectionModel(outputConnection);
216                }
217                               
218                connectorModel.onDelete();
219                this.WorkspaceManagerEditor.HasChanges = true;
220                return this.AllConnectorModels.Remove(connectorModel);
221            }
222            return false;
223        }
224
225        /// <summary>
226        /// Removes the connectionModel from our Workspace Model and removes it from all Connectors
227        /// </summary>
228        /// <param name="connectionModel"></param>
229        /// <returns></returns>
230        public bool deleteConnectionModel(ConnectionModel connectionModel)
231        {
232            if (connectionModel == null)
233                return false;
234
235            connectionModel.To.InputConnections.Remove(connectionModel);
236            connectionModel.From.OutputConnections.Remove(connectionModel);           
237            connectionModel.onDelete();
238            this.WorkspaceManagerEditor.HasChanges = true;
239            return this.AllConnectionModels.Remove(connectionModel);
240        }
241
242        /// <summary>
243        /// Sets all Connections and Connectors to state nonActive/noData
244        ///      all plugins to state Normal
245        ///      deletes all stored log events
246        /// </summary>
247        public void resetStates()
248        {
249            foreach (PluginModel pluginModel in this.AllPluginModels)
250            {
251                pluginModel.State = PluginModelState.Normal;
252                pluginModel.GuiLogEvents.Clear();
253            }
254            foreach (ConnectionModel connection in this.AllConnectionModels)
255            {
256                connection.Active = false;
257            }
258            foreach (ConnectorModel connector in this.AllConnectorModels)
259            {
260                connector.HasData = false;
261                connector.Data = null;
262            }
263        }
264
265        /// <summary>
266        /// Reconnects a Connection with an other Connector
267        /// </summary>
268        /// <param name="connectionModel"></param>
269        /// <param name="connectorModel"></param>
270        /// <returns></returns>
271        public bool reconnectConnection(ConnectionModel connectionModel, ConnectorModel connectorModel)
272        {
273            if (connectionModel.To != null)
274            {
275                connectionModel.To.InputConnections.Remove(connectionModel);
276            }
277            connectionModel.To = connectorModel;
278            connectorModel.InputConnections.Add(connectionModel);
279            this.WorkspaceManagerEditor.HasChanges = true;
280            return true;
281        }
282
283        /// <summary>
284        /// Checks wether a Connector and a Connector are compatible to be connected
285        /// They are compatible if their types are equal or the base type of the Connector
286        /// is equal to the type of the other Connector
287        /// It is false if already exists a ConnectionModel between both given ConnectorModels
288        /// </summary>
289        /// <param name="connectorModelA"></param>
290        /// <param name="connectorModelB"></param>
291        /// <returns></returns>
292        public static bool compatibleConnectors(ConnectorModel connectorModelA, ConnectorModel connectorModelB)
293        {
294            if (!connectorModelA.Outgoing || connectorModelB.Outgoing || connectorModelA.PluginModel == connectorModelB.PluginModel)
295            {
296                return false;
297            }
298
299            foreach(ConnectionModel connectionModel in connectorModelA.WorkspaceModel.AllConnectionModels)
300            {
301                if ((connectionModel.From == connectorModelA && connectionModel.To == connectorModelB) ||
302                   (connectionModel.From == connectorModelB && connectionModel.To == connectorModelA))
303                {
304                    return false;
305                }
306            }
307               
308            if (connectorModelA.ConnectorType.Equals(connectorModelB.ConnectorType)
309                || connectorModelA.ConnectorType.FullName == "System.Object"
310                || connectorModelB.ConnectorType.FullName == "System.Object"
311                || connectorModelA.ConnectorType.IsSubclassOf(connectorModelB.ConnectorType)
312                || connectorModelA.ConnectorType.GetInterfaces().Contains(connectorModelB.ConnectorType))             
313            {
314                return true;
315            }
316            else
317            {
318                return false;
319            }
320        }
321    }
322}
Note: See TracBrowser for help on using the repository browser.