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

Last change on this file since 1570 was 1570, checked in by kopal, 12 years ago
  • renamed "ConntectorView" to "ConnectorView"
  • bug fix - newConnectionModel now puts the new ConnectionModel into the internal list
  • PluginModels are now executable if
    • all Mandatory Inputs are set + all Outputs are "free"
    • there are no Mandatory Inputs but at least one input is set + all Outputs are "free"
    • there are no Inputs + all Outputs are "free"
  • some small fixes
File size: 11.0 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
17
18using System;
19using System.Collections.Generic;
20using System.Linq;
21using System.Text;
22using System.Windows;
23using Cryptool.PluginBase;
24
25namespace WorkspaceManager.Model
26{
27    /// <summary>
28    /// Class to represent our Workspace
29    /// </summary>
30    [Serializable]
31    public class WorkspaceModel 
32    {
33
34        /// <summary>
35        /// The surrounding WorkspaceManagerEditor
36        /// </summary>
37        public WorkspaceManager WorkspaceManagerEditor { get; set; }
38
39        /// <summary>
40        /// All PluginModels of our Workspace Model
41        /// </summary>
42        public List<PluginModel> AllPluginModels;
43
44        /// <summary>
45        /// All Connector Models of our Workspace Model
46        /// </summary>
47        public List<ConnectorModel> AllConnectorModels;
48
49        /// <summary>
50        /// All ConnectionModels of our Workspace Model
51        /// </summary>
52        public List<ConnectionModel> AllConnectionModels;
53
54        /// <summary>
55        /// Creates a new Workspace Model
56        /// </summary>
57        public WorkspaceModel()
58        { 
59            this.AllPluginModels = new List<PluginModel>();
60            this.AllConnectionModels = new List<ConnectionModel>();
61            this.AllConnectorModels = new List<ConnectorModel>();           
62        }
63
64        /// <summary>
65        /// Creates a new PluginModel belonging to this WorkspaceModel
66        /// </summary>
67        /// <param name="x"></param>
68        /// <param name="y"></param>
69        /// <param name="width"></param>
70        /// <param name="height"></param>
71        /// <param name="pluginType"></param>
72        /// <returns></returns>
73        public PluginModel newPluginModel(Point position, double width, double height, Type pluginType)
74        {
75            PluginModel pluginModel = new PluginModel();
76            pluginModel.WorkspaceModel = this;
77            pluginModel.Position = position;
78            pluginModel.Width = width;
79            pluginModel.Height = height;
80            pluginModel.PluginType = pluginType;
81            pluginModel.generateConnectors();
82            pluginModel.Name = pluginType.Name;
83            pluginModel.Plugin.OnGuiLogNotificationOccured += this.WorkspaceManagerEditor.GuiLogNotificationOccured;
84            pluginModel.Plugin.OnPluginProgressChanged += pluginModel.PluginProgressChanged;
85            this.AllPluginModels.Add(pluginModel);
86            return pluginModel;
87        }
88
89        /// <summary>
90        /// Creates a new PluginModel belonging to this WorkspaceModel
91        /// Position and Dimension are (x,y,width,height) = (0,0,0,0)
92        /// </summary>
93        /// <param name="pluginType"></param>
94        /// <returns></returns>
95        public PluginModel newPluginModel(Type pluginType)
96        {
97            return newPluginModel(new Point(0, 0), 0, 0, pluginType);
98        }       
99
100        /// <summary>
101        /// Creates a new Connection starting at "from"-Connector going to "to"-Connector with
102        /// the given connectionType
103        /// </summary>
104        /// <param name="from"></param>
105        /// <param name="to"></param>
106        /// <param name="connectionType"></param>
107        /// <returns></returns>
108        public ConnectionModel newConnectionModel(ConnectorModel from, ConnectorModel to, Type connectionType)
109        {
110            ConnectionModel connectionModel = new ConnectionModel();
111            connectionModel.WorkspaceModel = this;
112            connectionModel.From = from;
113            connectionModel.To = to;
114            from.OutputConnections.Add(connectionModel);
115            to.InputConnection = connectionModel;
116            connectionModel.ConnectionType = connectionType;
117            this.AllConnectionModels.Add(connectionModel);
118            return connectionModel;
119        }
120
121        /// <summary>
122        /// Deletes the pluginModel and all of its Connectors and the connected Connections
123        /// from our WorkspaceModel
124        /// </summary>
125        /// <param name="pluginModel"></param>
126        /// <returns></returns>
127        public bool deletePluginModel(PluginModel pluginModel)
128        {
129            //we can only delete PluginModels which are part of our WorkspaceModel
130            if (this.AllPluginModels.Contains(pluginModel))
131            {
132                // remove all InputConnectors belonging to this pluginModel from our WorkspaceModel
133                foreach (ConnectorModel inputConnector in pluginModel.InputConnectors)
134                {
135                    deleteConnectorModel(inputConnector);
136                }
137
138                // remove all OutputConnectors belonging to this pluginModel from our WorkspaceModel
139                foreach (ConnectorModel outputConnector in pluginModel.OutputConnectors)
140                {
141                    deleteConnectorModel(outputConnector);
142                }
143                pluginModel.Plugin.Dispose();
144                return this.AllPluginModels.Remove(pluginModel);
145            }           
146            return false;
147        }
148
149        /// <summary>
150        /// Deletes the connectorModel and the connected Connections
151        /// from our WorkspaceModel
152        /// </summary>
153        /// <param name="connectorModel"></param>
154        /// <returns></returns>
155        private bool deleteConnectorModel(ConnectorModel connectorModel)
156        {
157            //we can only delete ConnectorModels which are part of our WorkspaceModel
158            if(this.AllConnectorModels.Contains(connectorModel)){
159
160                //remove all output ConnectionModels belonging to this Connector from our WorkspaceModel
161                foreach (ConnectionModel outputConnection in connectorModel.OutputConnections)
162                {
163                    deleteConnectionModel(outputConnection);
164                }
165
166                //remove the input ConnectionModel belonging to this Connector from our WorkspaceModel
167                if (connectorModel.InputConnection != null)
168                {
169                    deleteConnectionModel(connectorModel.InputConnection);
170                }
171                return this.AllConnectorModels.Remove(connectorModel);
172            }
173            return false;
174        }
175
176        /// <summary>
177        /// Removes the connectionModel from our Workspace Model and removes it from all Connectors
178        /// </summary>
179        /// <param name="connectionModel"></param>
180        /// <returns></returns>
181        public bool deleteConnectionModel(ConnectionModel connectionModel)
182        {           
183            connectionModel.From.OutputConnections.Remove(connectionModel);
184            connectionModel.To.InputConnection = null;
185            return this.AllConnectionModels.Remove(connectionModel);
186        }
187
188        /// <summary>
189        /// Sets all Connections and Connectors to state nonActive/noData
190        /// </summary>
191        public void resetStates()
192        {
193            foreach (PluginModel pluginModel in this.AllPluginModels)
194            {
195                pluginModel.ExecutionState = PluginModelState.Undefined;
196            }
197            foreach (ConnectionModel connection in this.AllConnectionModels)
198            {
199                connection.Active = false;
200            }
201            foreach (ConnectorModel connector in this.AllConnectorModels)
202            {
203                connector.HasData = false;
204                connector.Data = null;
205            }
206        }
207
208        /// <summary>
209        /// Reconnects a Connection with an other Connector
210        /// </summary>
211        /// <param name="connectionModel"></param>
212        /// <param name="connectorModel"></param>
213        /// <returns></returns>
214        public bool reconnectConnection(ConnectionModel connectionModel, ConnectorModel connectorModel)
215        {
216            if (connectionModel.To != null)
217            {
218                connectionModel.To.InputConnection = null;
219            }
220            connectionModel.To = connectorModel;
221            connectorModel.InputConnection = connectionModel;
222            return true;
223        }
224
225        /// <summary>
226        /// Checks wether a Connector and a Connector are compatible to be connected
227        /// They are compatible if their types are equal or the base type of the Connector
228        /// is equal to the type of the other Connector
229        /// </summary>
230        /// <param name="connectorModelA"></param>
231        /// <param name="connectorModelB"></param>
232        /// <returns></returns>
233        public static bool compatibleConnectors(ConnectorModel connectorModelA, ConnectorModel connectorModelB)
234        {
235            if (connectorModelA.ConnectorType.Equals(connectorModelB.ConnectorType)
236                || connectorModelA.ConnectorType.BaseType.Equals(connectorModelB.ConnectorType))
237            {
238                return true;
239            }
240            else
241            {
242                return false;
243            }
244        }
245
246        /// <summary>
247        /// Checks wether a Connection and a Connector are compatible to be connected
248        /// They are compatible if their types are equal or the base type of the connection
249        /// is equal to the type of the connector
250        /// </summary>
251        /// <param name="connectionModel"></param>
252        /// <param name="connectorModel"></param>
253        /// <returns></returns>
254        public static bool compatibleConnectors(ConnectionModel connectionModel, ConnectorModel connectorModel)
255        {
256            if (connectionModel.ConnectionType.Equals(connectorModel.ConnectorType)
257                || connectionModel.ConnectionType.BaseType.Equals(connectorModel.ConnectorType))
258            {
259                return true;
260            }
261            else 
262            { 
263                return false; 
264            }
265        }
266
267        /// <summary>
268        /// Checks wether a Connection and a Connector are compatible to be connected
269        /// They are compatible if their types are equal or the base type of the connection
270        /// is equal to the type of the connector
271        /// </summary>
272        /// <param name="connectorModel"></param>
273        /// <param name="connectionModel"></param>
274        /// <returns></returns>
275        public static bool compatibleConnectors(ConnectorModel connectorModel, ConnectionModel connectionModel)
276        {
277            return compatibleConnectors(connectionModel, connectorModel);
278        }
279    }
280}
Note: See TracBrowser for help on using the repository browser.