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

Last change on this file since 1879 was 1879, checked in by kopal, 11 years ago

ConnectorModel, ConnectionModel, ImageModel and TextModel now also have a Parent Model Element

File size: 15.5 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 : VisualElementModel
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        /// All ImageModels of our Workspace Model
65        /// </summary>
66        public List<ImageModel> AllImageModels;
67
68        /// <summary>
69        /// All TextModels of our Workspace Model
70        /// </summary>
71        public List<TextModel> AllTextModels;
72
73        /// <summary>
74        /// Creates a new Workspace Model
75        /// </summary>
76        public WorkspaceModel()
77        { 
78            this.AllPluginModels = new List<PluginModel>();
79            this.AllConnectionModels = new List<ConnectionModel>();
80            this.AllConnectorModels = new List<ConnectorModel>();
81            this.AllImageModels = new List<ImageModel>();
82            this.AllTextModels = new List<TextModel>(); 
83        }
84
85        /// <summary>
86        /// Creates a new PluginModel belonging to this WorkspaceModel
87        /// </summary>
88        /// <param name="x"></param>
89        /// <param name="y"></param>
90        /// <param name="width"></param>
91        /// <param name="height"></param>
92        /// <param name="pluginType"></param>
93        /// <returns></returns>
94        public PluginModel newPluginModel(Point position, double width, double height, Type pluginType)
95        {
96            PluginModel pluginModel = new PluginModel();
97            pluginModel.WorkspaceModel = this;           
98            pluginModel.Startable = pluginType.GetPluginInfoAttribute().Startable;
99            pluginModel.Position = position;
100            pluginModel.Width = width;
101            pluginModel.Height = height;
102            pluginModel.PluginType = pluginType;           
103            pluginModel.Name = pluginType.Name;
104            pluginModel.RepeatStart = false;
105            pluginModel.generateConnectors();
106            pluginModel.Plugin.Initialize();
107            pluginModel.Plugin.OnGuiLogNotificationOccured += this.WorkspaceManagerEditor.GuiLogNotificationOccured;
108            pluginModel.Plugin.OnGuiLogNotificationOccured += pluginModel.GuiLogNotificationOccured;
109            pluginModel.Plugin.OnPluginProgressChanged += pluginModel.PluginProgressChanged;
110            pluginModel.Plugin.OnPluginStatusChanged += pluginModel.PluginStatusChanged;
111            pluginModel.Parent = this;
112            if (pluginModel.Plugin.Settings != null)
113            {
114                pluginModel.Plugin.Settings.PropertyChanged += pluginModel.SettingsPropertyChanged;
115            }           
116            this.AllPluginModels.Add(pluginModel);
117            this.WorkspaceManagerEditor.HasChanges = true;
118            return pluginModel;
119        }
120
121        /// <summary>
122        /// Creates a new PluginModel belonging to this WorkspaceModel
123        /// Position and Dimension are (x,y,width,height) = (0,0,0,0)
124        /// </summary>
125        /// <param name="pluginType"></param>
126        /// <returns></returns>
127        public PluginModel newPluginModel(Type pluginType)
128        {
129            return newPluginModel(new Point(0, 0), 0, 0, pluginType);
130        }       
131
132        /// <summary>
133        /// Creates a new Connection starting at "from"-Connector going to "to"-Connector with
134        /// the given connectionType
135        /// </summary>
136        /// <param name="from"></param>
137        /// <param name="to"></param>
138        /// <param name="connectionType"></param>
139        /// <returns></returns>
140        public ConnectionModel newConnectionModel(ConnectorModel from, ConnectorModel to, Type connectionType)
141        {
142            ConnectionModel connectionModel = new ConnectionModel();
143            connectionModel.WorkspaceModel = this;
144            connectionModel.From = from;
145            connectionModel.To = to;
146            from.OutputConnections.Add(connectionModel);
147            to.InputConnections.Add(connectionModel);
148            connectionModel.ConnectionType = connectionType;
149            connectionModel.Parent = this;
150
151            //If we connect two IControls we have to set data directly:
152            if (from.IControl && to.IControl)
153            {
154                object data = null;
155                //Get IControl data from "to"
156                if (to.IsDynamic)
157                {
158                    data = to.PluginModel.Plugin.GetType().GetMethod(to.DynamicGetterName).Invoke(to.PluginModel.Plugin, new object[] { to.PropertyName });
159                }
160                else
161                {
162                    data = to.PluginModel.Plugin.GetType().GetProperty(to.PropertyName).GetValue(to.PluginModel.Plugin, null);
163                }
164
165                //Set IControl data
166                if (from.IsDynamic)
167                {
168                    MethodInfo propertyInfo = from.PluginModel.Plugin.GetType().GetMethod(from.DynamicSetterName);
169                    propertyInfo.Invoke(from.PluginModel.Plugin, new object[] { from.PropertyName, data });
170                }
171                else
172                {
173                    PropertyInfo propertyInfo = from.PluginModel.Plugin.GetType().GetProperty(from.PropertyName);
174                    propertyInfo.SetValue(from.PluginModel.Plugin, data, null);
175                }
176            }
177
178            this.AllConnectionModels.Add(connectionModel);
179            this.WorkspaceManagerEditor.HasChanges = true;
180            return connectionModel;
181        }
182
183        /// <summary>
184        /// Creates a new ImageModel containing the under imgUri stored Image
185        /// </summary>
186        /// <param name="imgUri"></param>
187        /// <returns></returns>
188        public ImageModel newImageModel(Uri imgUri)
189        {
190            ImageModel imageModel = new ImageModel(imgUri);
191            this.AllImageModels.Add(imageModel);
192            this.WorkspaceManagerEditor.HasChanges = true;
193            imageModel.Parent = this;
194            return imageModel;
195        }
196
197        /// <summary>
198        /// Creates a new TextModel
199        /// </summary>
200        /// <param name="imgUri"></param>
201        /// <returns></returns>
202        public TextModel newTextModel(string text = null )
203        {
204            TextModel textModel = new TextModel(text);
205            this.AllTextModels.Add(textModel);
206            this.WorkspaceManagerEditor.HasChanges = true;
207            textModel.Parent = this;
208            return textModel;
209        }
210
211        /// <summary>
212        /// Deletes the given ImageModel
213        /// </summary>
214        /// <param name="imgUri"></param>
215        /// <returns></returns>
216        public bool deleteImageModel(ImageModel imageModel)
217        {
218            return this.AllImageModels.Remove(imageModel);
219        }
220
221        /// <summary>
222        /// Deletes the given TextModel
223        /// </summary>
224        /// <param name="imgUri"></param>
225        /// <returns></returns>
226        public bool deleteTextModel(TextModel textModel)
227        {
228            return this.AllTextModels.Remove(textModel);
229        }
230
231        /// <summary>
232        /// Deletes the pluginModel and all of its Connectors and the connected Connections
233        /// from our WorkspaceModel
234        /// </summary>
235        /// <param name="pluginModel"></param>
236        /// <returns></returns>
237        public bool deletePluginModel(PluginModel pluginModel)
238        {
239            //we can only delete PluginModels which are part of our WorkspaceModel
240            if (this.AllPluginModels.Contains(pluginModel))
241            {
242                // remove all InputConnectors belonging to this pluginModel from our WorkspaceModel
243                foreach (ConnectorModel inputConnector in new List<ConnectorModel>(pluginModel.InputConnectors))
244                {
245                    deleteConnectorModel(inputConnector);
246                }
247
248                // remove all OutputConnectors belonging to this pluginModel from our WorkspaceModel
249                foreach (ConnectorModel outputConnector in new List<ConnectorModel>(pluginModel.OutputConnectors))
250                {
251                    deleteConnectorModel(outputConnector);
252                }
253                pluginModel.Plugin.Dispose();
254                pluginModel.onDelete();
255                this.WorkspaceManagerEditor.HasChanges = true;
256                return this.AllPluginModels.Remove(pluginModel);
257            }           
258            return false;
259        }
260
261        /// <summary>
262        /// Deletes the connectorModel and the connected Connections
263        /// from our WorkspaceModel
264        /// </summary>
265        /// <param name="connectorModel"></param>
266        /// <returns></returns>
267        private bool deleteConnectorModel(ConnectorModel connectorModel)
268        {
269            //we can only delete ConnectorModels which are part of our WorkspaceModel
270            if(this.AllConnectorModels.Contains(connectorModel)){
271
272                //remove all input ConnectionModels belonging to this Connector from our WorkspaceModel
273                foreach (ConnectionModel connectionModel in new List<ConnectionModel>(connectorModel.InputConnections))
274                {
275                    deleteConnectionModel(connectionModel);
276                }
277
278                //remove all output ConnectionModels belonging to this Connector from our WorkspaceModel
279                foreach (ConnectionModel outputConnection in new List<ConnectionModel>(connectorModel.OutputConnections))
280                {
281                    deleteConnectionModel(outputConnection);
282                }
283                               
284                connectorModel.onDelete();
285                this.WorkspaceManagerEditor.HasChanges = true;
286                return this.AllConnectorModels.Remove(connectorModel);
287            }
288            return false;
289        }
290
291        /// <summary>
292        /// Removes the connectionModel from our Workspace Model and removes it from all Connectors
293        /// </summary>
294        /// <param name="connectionModel"></param>
295        /// <returns></returns>
296        public bool deleteConnectionModel(ConnectionModel connectionModel)
297        {
298            if (connectionModel == null)
299                return false;
300
301            connectionModel.To.InputConnections.Remove(connectionModel);
302            connectionModel.From.OutputConnections.Remove(connectionModel);           
303            connectionModel.onDelete();
304            this.WorkspaceManagerEditor.HasChanges = true;
305            return this.AllConnectionModels.Remove(connectionModel);
306        }
307
308        /// <summary>
309        /// Sets all Connections and Connectors to state nonActive/noData
310        ///      all plugins to state Normal
311        ///      deletes all stored log events
312        /// </summary>
313        public void resetStates()
314        {
315            foreach (PluginModel pluginModel in this.AllPluginModels)
316            {
317                pluginModel.State = PluginModelState.Normal;
318                pluginModel.GuiLogEvents.Clear();
319            }
320            foreach (ConnectionModel connection in this.AllConnectionModels)
321            {
322                connection.Active = false;
323            }
324            foreach (ConnectorModel connector in this.AllConnectorModels)
325            {
326                connector.HasData = false;
327                connector.Data = null;
328            }
329        }
330
331        /// <summary>
332        /// Reconnects a Connection with an other Connector
333        /// </summary>
334        /// <param name="connectionModel"></param>
335        /// <param name="connectorModel"></param>
336        /// <returns></returns>
337        public bool reconnectConnection(ConnectionModel connectionModel, ConnectorModel connectorModel)
338        {
339            if (connectionModel.To != null)
340            {
341                connectionModel.To.InputConnections.Remove(connectionModel);
342            }
343            connectionModel.To = connectorModel;
344            connectorModel.InputConnections.Add(connectionModel);
345            this.WorkspaceManagerEditor.HasChanges = true;
346            return true;
347        }
348
349        /// <summary>
350        /// Checks wether a Connector and a Connector are compatible to be connected
351        /// They are compatible if their types are equal or the base type of the Connector
352        /// is equal to the type of the other Connector
353        /// It is false if already exists a ConnectionModel between both given ConnectorModels
354        /// </summary>
355        /// <param name="connectorModelA"></param>
356        /// <param name="connectorModelB"></param>
357        /// <returns></returns>
358        public static bool compatibleConnectors(ConnectorModel connectorModelA, ConnectorModel connectorModelB)
359        {
360            if (!connectorModelA.Outgoing || connectorModelB.Outgoing || connectorModelA.PluginModel == connectorModelB.PluginModel)
361            {
362                return false;
363            }
364
365            foreach(ConnectionModel connectionModel in connectorModelA.WorkspaceModel.AllConnectionModels)
366            {
367                if ((connectionModel.From == connectorModelA && connectionModel.To == connectorModelB) ||
368                   (connectionModel.From == connectorModelB && connectionModel.To == connectorModelA))
369                {
370                    return false;
371                }
372            }
373               
374            if (connectorModelA.ConnectorType.Equals(connectorModelB.ConnectorType)
375                || connectorModelA.ConnectorType.FullName == "System.Object"
376                || connectorModelB.ConnectorType.FullName == "System.Object"
377                || connectorModelA.ConnectorType.IsSubclassOf(connectorModelB.ConnectorType)
378                || connectorModelA.ConnectorType.GetInterfaces().Contains(connectorModelB.ConnectorType))             
379            {
380                return true;
381            }
382            else
383            {
384                return false;
385            }
386        }
387    }
388}
Note: See TracBrowser for help on using the repository browser.