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

Last change on this file since 1600 was 1600, checked in by matkovic, 12 years ago

-added Animations
-added Arrow Image
-Some style changes

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            pluginModel.Plugin.OnPluginStatusChanged += pluginModel.PluginStatusChanged;
86            this.AllPluginModels.Add(pluginModel);
87            return pluginModel;
88        }
89
90        /// <summary>
91        /// Creates a new PluginModel belonging to this WorkspaceModel
92        /// Position and Dimension are (x,y,width,height) = (0,0,0,0)
93        /// </summary>
94        /// <param name="pluginType"></param>
95        /// <returns></returns>
96        public PluginModel newPluginModel(Type pluginType)
97        {
98            return newPluginModel(new Point(0, 0), 0, 0, pluginType);
99        }       
100
101        /// <summary>
102        /// Creates a new Connection starting at "from"-Connector going to "to"-Connector with
103        /// the given connectionType
104        /// </summary>
105        /// <param name="from"></param>
106        /// <param name="to"></param>
107        /// <param name="connectionType"></param>
108        /// <returns></returns>
109        public ConnectionModel newConnectionModel(ConnectorModel from, ConnectorModel to, Type connectionType)
110        {
111            ConnectionModel connectionModel = new ConnectionModel();
112            connectionModel.WorkspaceModel = this;
113            connectionModel.From = from;
114            connectionModel.To = to;
115            from.OutputConnections.Add(connectionModel);
116            to.InputConnection = connectionModel;
117            connectionModel.ConnectionType = connectionType;
118            this.AllConnectionModels.Add(connectionModel);
119            return connectionModel;
120        }
121
122        /// <summary>
123        /// Deletes the pluginModel and all of its Connectors and the connected Connections
124        /// from our WorkspaceModel
125        /// </summary>
126        /// <param name="pluginModel"></param>
127        /// <returns></returns>
128        public bool deletePluginModel(PluginModel pluginModel)
129        {
130            //we can only delete PluginModels which are part of our WorkspaceModel
131            if (this.AllPluginModels.Contains(pluginModel))
132            {
133                // remove all InputConnectors belonging to this pluginModel from our WorkspaceModel
134                foreach (ConnectorModel inputConnector in pluginModel.InputConnectors)
135                {
136                    deleteConnectorModel(inputConnector);
137                }
138
139                // remove all OutputConnectors belonging to this pluginModel from our WorkspaceModel
140                foreach (ConnectorModel outputConnector in pluginModel.OutputConnectors)
141                {
142                    deleteConnectorModel(outputConnector);
143                }
144                pluginModel.Plugin.Dispose();
145                return this.AllPluginModels.Remove(pluginModel);
146            }           
147            return false;
148        }
149
150        /// <summary>
151        /// Deletes the connectorModel and the connected Connections
152        /// from our WorkspaceModel
153        /// </summary>
154        /// <param name="connectorModel"></param>
155        /// <returns></returns>
156        private bool deleteConnectorModel(ConnectorModel connectorModel)
157        {
158            //we can only delete ConnectorModels which are part of our WorkspaceModel
159            if(this.AllConnectorModels.Contains(connectorModel)){
160
161                //remove all output ConnectionModels belonging to this Connector from our WorkspaceModel
162                foreach (ConnectionModel outputConnection in connectorModel.OutputConnections)
163                {
164                    deleteConnectionModel(outputConnection);
165                }
166
167                //remove the input ConnectionModel belonging to this Connector from our WorkspaceModel
168                if (connectorModel.InputConnection != null)
169                {
170                    deleteConnectionModel(connectorModel.InputConnection);
171                }
172                return this.AllConnectorModels.Remove(connectorModel);
173            }
174            return false;
175        }
176
177        /// <summary>
178        /// Removes the connectionModel from our Workspace Model and removes it from all Connectors
179        /// </summary>
180        /// <param name="connectionModel"></param>
181        /// <returns></returns>
182        public bool deleteConnectionModel(ConnectionModel connectionModel)
183        {
184            if (connectionModel == null)
185                return false;
186
187            connectionModel.From.OutputConnections.Remove(connectionModel);
188            connectionModel.To.InputConnection = null;
189            return this.AllConnectionModels.Remove(connectionModel);
190        }
191
192        /// <summary>
193        /// Sets all Connections and Connectors to state nonActive/noData
194        /// </summary>
195        public void resetStates()
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.