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

Last change on this file since 1791 was 1791, checked in by kopal, 11 years ago
  • Plugins without Presentation now can not be maximized
  • Enum Settings will be saved/loaded correctly now
  • Change of Settings (which have a PropertyChangedEvent) will cause editor to have "hasChanges"
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            pluginModel.Plugin.Settings.PropertyChanged += pluginModel.SettingsPropertyChanged;
100            this.AllPluginModels.Add(pluginModel);
101            this.WorkspaceManagerEditor.HasChanges = true;
102            return pluginModel;
103        }
104
105        /// <summary>
106        /// Creates a new PluginModel belonging to this WorkspaceModel
107        /// Position and Dimension are (x,y,width,height) = (0,0,0,0)
108        /// </summary>
109        /// <param name="pluginType"></param>
110        /// <returns></returns>
111        public PluginModel newPluginModel(Type pluginType)
112        {
113            return newPluginModel(new Point(0, 0), 0, 0, pluginType);
114        }       
115
116        /// <summary>
117        /// Creates a new Connection starting at "from"-Connector going to "to"-Connector with
118        /// the given connectionType
119        /// </summary>
120        /// <param name="from"></param>
121        /// <param name="to"></param>
122        /// <param name="connectionType"></param>
123        /// <returns></returns>
124        public ConnectionModel newConnectionModel(ConnectorModel from, ConnectorModel to, Type connectionType)
125        {
126            ConnectionModel connectionModel = new ConnectionModel();
127            connectionModel.WorkspaceModel = this;
128            connectionModel.From = from;
129            connectionModel.To = to;
130            from.OutputConnections.Add(connectionModel);
131            to.InputConnections.Add(connectionModel);
132            connectionModel.ConnectionType = connectionType;
133
134            //If we connect two IControls we have to set data directly:
135            if (from.IControl && to.IControl)
136            {
137                object data = null;
138                //Get IControl data from "to"
139                if (to.IsDynamic)
140                {
141                    data = to.PluginModel.Plugin.GetType().GetMethod(to.DynamicGetterName).Invoke(to.PluginModel.Plugin, new object[] { to.PropertyName });
142                }
143                else
144                {
145                    data = to.PluginModel.Plugin.GetType().GetProperty(to.PropertyName).GetValue(to.PluginModel.Plugin, null);
146                }
147
148                //Set IControl data
149                if (from.IsDynamic)
150                {
151                    MethodInfo propertyInfo = from.PluginModel.Plugin.GetType().GetMethod(from.DynamicSetterName);
152                    propertyInfo.Invoke(from.PluginModel.Plugin, new object[] { from.PropertyName, data });
153                }
154                else
155                {
156                    PropertyInfo propertyInfo = from.PluginModel.Plugin.GetType().GetProperty(from.PropertyName);
157                    propertyInfo.SetValue(from.PluginModel.Plugin, data, null);
158                }
159            }
160
161            this.AllConnectionModels.Add(connectionModel);
162            this.WorkspaceManagerEditor.HasChanges = true;
163            return connectionModel;
164        }
165
166        /// <summary>
167        /// Deletes the pluginModel and all of its Connectors and the connected Connections
168        /// from our WorkspaceModel
169        /// </summary>
170        /// <param name="pluginModel"></param>
171        /// <returns></returns>
172        public bool deletePluginModel(PluginModel pluginModel)
173        {
174            //we can only delete PluginModels which are part of our WorkspaceModel
175            if (this.AllPluginModels.Contains(pluginModel))
176            {
177                // remove all InputConnectors belonging to this pluginModel from our WorkspaceModel
178                foreach (ConnectorModel inputConnector in new List<ConnectorModel>(pluginModel.InputConnectors))
179                {
180                    deleteConnectorModel(inputConnector);
181                }
182
183                // remove all OutputConnectors belonging to this pluginModel from our WorkspaceModel
184                foreach (ConnectorModel outputConnector in new List<ConnectorModel>(pluginModel.OutputConnectors))
185                {
186                    deleteConnectorModel(outputConnector);
187                }
188                pluginModel.Plugin.Dispose();
189                pluginModel.onDelete();
190                this.WorkspaceManagerEditor.HasChanges = true;
191                return this.AllPluginModels.Remove(pluginModel);
192            }           
193            return false;
194        }
195
196        /// <summary>
197        /// Deletes the connectorModel and the connected Connections
198        /// from our WorkspaceModel
199        /// </summary>
200        /// <param name="connectorModel"></param>
201        /// <returns></returns>
202        private bool deleteConnectorModel(ConnectorModel connectorModel)
203        {
204            //we can only delete ConnectorModels which are part of our WorkspaceModel
205            if(this.AllConnectorModels.Contains(connectorModel)){
206
207                //remove all input ConnectionModels belonging to this Connector from our WorkspaceModel
208                foreach (ConnectionModel connectionModel in new List<ConnectionModel>(connectorModel.InputConnections))
209                {
210                    deleteConnectionModel(connectionModel);
211                }
212
213                //remove all output ConnectionModels belonging to this Connector from our WorkspaceModel
214                foreach (ConnectionModel outputConnection in new List<ConnectionModel>(connectorModel.OutputConnections))
215                {
216                    deleteConnectionModel(outputConnection);
217                }
218                               
219                connectorModel.onDelete();
220                this.WorkspaceManagerEditor.HasChanges = true;
221                return this.AllConnectorModels.Remove(connectorModel);
222            }
223            return false;
224        }
225
226        /// <summary>
227        /// Removes the connectionModel from our Workspace Model and removes it from all Connectors
228        /// </summary>
229        /// <param name="connectionModel"></param>
230        /// <returns></returns>
231        public bool deleteConnectionModel(ConnectionModel connectionModel)
232        {
233            if (connectionModel == null)
234                return false;
235
236            connectionModel.To.InputConnections.Remove(connectionModel);
237            connectionModel.From.OutputConnections.Remove(connectionModel);           
238            connectionModel.onDelete();
239            this.WorkspaceManagerEditor.HasChanges = true;
240            return this.AllConnectionModels.Remove(connectionModel);
241        }
242
243        /// <summary>
244        /// Sets all Connections and Connectors to state nonActive/noData
245        ///      all plugins to state Normal
246        ///      deletes all stored log events
247        /// </summary>
248        public void resetStates()
249        {
250            foreach (PluginModel pluginModel in this.AllPluginModels)
251            {
252                pluginModel.State = PluginModelState.Normal;
253                pluginModel.GuiLogEvents.Clear();
254            }
255            foreach (ConnectionModel connection in this.AllConnectionModels)
256            {
257                connection.Active = false;
258            }
259            foreach (ConnectorModel connector in this.AllConnectorModels)
260            {
261                connector.HasData = false;
262                connector.Data = null;
263            }
264        }
265
266        /// <summary>
267        /// Reconnects a Connection with an other Connector
268        /// </summary>
269        /// <param name="connectionModel"></param>
270        /// <param name="connectorModel"></param>
271        /// <returns></returns>
272        public bool reconnectConnection(ConnectionModel connectionModel, ConnectorModel connectorModel)
273        {
274            if (connectionModel.To != null)
275            {
276                connectionModel.To.InputConnections.Remove(connectionModel);
277            }
278            connectionModel.To = connectorModel;
279            connectorModel.InputConnections.Add(connectionModel);
280            this.WorkspaceManagerEditor.HasChanges = true;
281            return true;
282        }
283
284        /// <summary>
285        /// Checks wether a Connector and a Connector are compatible to be connected
286        /// They are compatible if their types are equal or the base type of the Connector
287        /// is equal to the type of the other Connector
288        /// It is false if already exists a ConnectionModel between both given ConnectorModels
289        /// </summary>
290        /// <param name="connectorModelA"></param>
291        /// <param name="connectorModelB"></param>
292        /// <returns></returns>
293        public static bool compatibleConnectors(ConnectorModel connectorModelA, ConnectorModel connectorModelB)
294        {
295            if (!connectorModelA.Outgoing || connectorModelB.Outgoing || connectorModelA.PluginModel == connectorModelB.PluginModel)
296            {
297                return false;
298            }
299
300            foreach(ConnectionModel connectionModel in connectorModelA.WorkspaceModel.AllConnectionModels)
301            {
302                if ((connectionModel.From == connectorModelA && connectionModel.To == connectorModelB) ||
303                   (connectionModel.From == connectorModelB && connectionModel.To == connectorModelA))
304                {
305                    return false;
306                }
307            }
308               
309            if (connectorModelA.ConnectorType.Equals(connectorModelB.ConnectorType)
310                || connectorModelA.ConnectorType.FullName == "System.Object"
311                || connectorModelB.ConnectorType.FullName == "System.Object"
312                || connectorModelA.ConnectorType.IsSubclassOf(connectorModelB.ConnectorType)
313                || connectorModelA.ConnectorType.GetInterfaces().Contains(connectorModelB.ConnectorType))             
314            {
315                return true;
316            }
317            else
318            {
319                return false;
320            }
321        }
322    }
323}
Note: See TracBrowser for help on using the repository browser.