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

Last change on this file since 1991 was 1991, checked in by matkovic, 11 years ago

-fixed pathfinding
-fixed line drawing
-added Colorpicker
-fixed lock/unlock at TextInputWrapper
-feature: Background and TextForeground in TextInputWrapper added
-fixed a issue With loading and such
-WorkspaceModel doesn't throw exception anymore (Samples functional again)
-fixed some Styles
-Startable implemented and loaded right
-Startable Logo moved

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