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

Last change on this file since 1681 was 1681, checked in by kopal, 12 years ago

some bug fixes

File size: 10.9 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.Runtime.Serialization.Formatters.Soap;
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.generateConnectors();
93            pluginModel.Plugin.OnGuiLogNotificationOccured += this.WorkspaceManagerEditor.GuiLogNotificationOccured;
94            pluginModel.Plugin.OnPluginProgressChanged += pluginModel.PluginProgressChanged;
95            pluginModel.Plugin.OnPluginStatusChanged += pluginModel.PluginStatusChanged;
96            this.AllPluginModels.Add(pluginModel);
97            this.WorkspaceManagerEditor.HasChanges = true;
98            return pluginModel;
99        }
100
101        /// <summary>
102        /// Creates a new PluginModel belonging to this WorkspaceModel
103        /// Position and Dimension are (x,y,width,height) = (0,0,0,0)
104        /// </summary>
105        /// <param name="pluginType"></param>
106        /// <returns></returns>
107        public PluginModel newPluginModel(Type pluginType)
108        {
109            return newPluginModel(new Point(0, 0), 0, 0, pluginType);
110        }       
111
112        /// <summary>
113        /// Creates a new Connection starting at "from"-Connector going to "to"-Connector with
114        /// the given connectionType
115        /// </summary>
116        /// <param name="from"></param>
117        /// <param name="to"></param>
118        /// <param name="connectionType"></param>
119        /// <returns></returns>
120        public ConnectionModel newConnectionModel(ConnectorModel from, ConnectorModel to, Type connectionType)
121        {
122            ConnectionModel connectionModel = new ConnectionModel();
123            connectionModel.WorkspaceModel = this;
124            connectionModel.From = from;
125            connectionModel.To = to;
126            from.OutputConnections.Add(connectionModel);
127            to.InputConnection = connectionModel;
128            connectionModel.ConnectionType = connectionType;
129            this.AllConnectionModels.Add(connectionModel);
130            this.WorkspaceManagerEditor.HasChanges = true;
131            return connectionModel;
132        }
133
134        /// <summary>
135        /// Deletes the pluginModel and all of its Connectors and the connected Connections
136        /// from our WorkspaceModel
137        /// </summary>
138        /// <param name="pluginModel"></param>
139        /// <returns></returns>
140        public bool deletePluginModel(PluginModel pluginModel)
141        {
142            //we can only delete PluginModels which are part of our WorkspaceModel
143            if (this.AllPluginModels.Contains(pluginModel))
144            {
145                // remove all InputConnectors belonging to this pluginModel from our WorkspaceModel
146                foreach (ConnectorModel inputConnector in new List<ConnectorModel>(pluginModel.InputConnectors))
147                {
148                    deleteConnectorModel(inputConnector);
149                }
150
151                // remove all OutputConnectors belonging to this pluginModel from our WorkspaceModel
152                foreach (ConnectorModel outputConnector in new List<ConnectorModel>(pluginModel.OutputConnectors))
153                {
154                    deleteConnectorModel(outputConnector);
155                }
156                pluginModel.Plugin.Dispose();
157                pluginModel.onDelete();
158                this.WorkspaceManagerEditor.HasChanges = true;
159                return this.AllPluginModels.Remove(pluginModel);
160            }           
161            return false;
162        }
163
164        /// <summary>
165        /// Deletes the connectorModel and the connected Connections
166        /// from our WorkspaceModel
167        /// </summary>
168        /// <param name="connectorModel"></param>
169        /// <returns></returns>
170        private bool deleteConnectorModel(ConnectorModel connectorModel)
171        {
172            //we can only delete ConnectorModels which are part of our WorkspaceModel
173            if(this.AllConnectorModels.Contains(connectorModel)){
174
175                //remove all output ConnectionModels belonging to this Connector from our WorkspaceModel
176                foreach (ConnectionModel outputConnection in new List<ConnectionModel>(connectorModel.OutputConnections))
177                {
178                    deleteConnectionModel(outputConnection);
179                }
180
181                //remove the input ConnectionModel belonging to this Connector from our WorkspaceModel
182                if (connectorModel.InputConnection != null)
183                {
184                    deleteConnectionModel(connectorModel.InputConnection);
185                }
186                connectorModel.onDelete();
187                this.WorkspaceManagerEditor.HasChanges = true;
188                return this.AllConnectorModels.Remove(connectorModel);
189            }
190            return false;
191        }
192
193        /// <summary>
194        /// Removes the connectionModel from our Workspace Model and removes it from all Connectors
195        /// </summary>
196        /// <param name="connectionModel"></param>
197        /// <returns></returns>
198        public bool deleteConnectionModel(ConnectionModel connectionModel)
199        {
200            if (connectionModel == null)
201                return false;
202
203            connectionModel.From.OutputConnections.Remove(connectionModel);
204            connectionModel.To.InputConnection = null;
205            connectionModel.onDelete();
206            this.WorkspaceManagerEditor.HasChanges = true;
207            return this.AllConnectionModels.Remove(connectionModel);
208        }
209
210        /// <summary>
211        /// Sets all Connections and Connectors to state nonActive/noData
212        /// </summary>
213        public void resetStates()
214        {
215            foreach (ConnectionModel connection in this.AllConnectionModels)
216            {
217                connection.Active = false;
218            }
219            foreach (ConnectorModel connector in this.AllConnectorModels)
220            {
221                connector.HasData = false;
222                connector.Data = null;
223            }
224        }
225
226        /// <summary>
227        /// Reconnects a Connection with an other Connector
228        /// </summary>
229        /// <param name="connectionModel"></param>
230        /// <param name="connectorModel"></param>
231        /// <returns></returns>
232        public bool reconnectConnection(ConnectionModel connectionModel, ConnectorModel connectorModel)
233        {
234            if (connectionModel.To != null)
235            {
236                connectionModel.To.InputConnection = null;
237            }
238            connectionModel.To = connectorModel;
239            connectorModel.InputConnection = connectionModel;
240            this.WorkspaceManagerEditor.HasChanges = true;
241            return true;
242        }
243
244        /// <summary>
245        /// Checks wether a Connector and a Connector are compatible to be connected
246        /// They are compatible if their types are equal or the base type of the Connector
247        /// is equal to the type of the other Connector
248        /// </summary>
249        /// <param name="connectorModelA"></param>
250        /// <param name="connectorModelB"></param>
251        /// <returns></returns>
252        public static bool compatibleConnectors(ConnectorModel connectorModelA, ConnectorModel connectorModelB)
253        {
254            if (!connectorModelA.Outgoing || connectorModelB.Outgoing || connectorModelB.InputConnection != null)
255            {
256                return false;
257            }
258           
259            if (connectorModelA.ConnectorType.Equals(connectorModelB.ConnectorType)
260                || connectorModelA.ConnectorType.FullName == "System.Object"
261                || connectorModelB.ConnectorType.FullName == "System.Object"
262                || connectorModelA.ConnectorType.IsSubclassOf(connectorModelB.ConnectorType)
263                || connectorModelA.ConnectorType.GetInterfaces().Contains(connectorModelB.ConnectorType))             
264            {
265                return true;
266            }
267            else
268            {
269                return false;
270            }
271        }
272    }
273}
Note: See TracBrowser for help on using the repository browser.