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

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

-fixed "Fit to Screen"
-fixed Scrollbars
-fixed Dragging
-added custom names
-New: IControl- Presentation
--Loading adjusted
-New: Multi selection
-New: Copy /Past (not complete)
-New: Multi Dragging
-New: PluginContainerView Highlighting added

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