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

Last change on this file since 1617 was 1617, checked in by kopal, 12 years ago
  • a delete of a plugin now leads to the delete of all left connections
  • added a first version of a Settings to the WorkspaceManager
  • some small bug fixes
File size: 11.2 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;
23
24namespace WorkspaceManager.Model
25{
26    /// <summary>
27    /// Class to represent our Workspace
28    /// </summary>
29    [Serializable]
30    public class WorkspaceModel 
31    {
32        /// <summary>
33        /// The surrounding WorkspaceManagerEditor
34        /// </summary>
35        public WorkspaceManager WorkspaceManagerEditor { get; set; }
36
37        /// <summary>
38        /// All PluginModels of our Workspace Model
39        /// </summary>
40        public List<PluginModel> AllPluginModels;
41
42        /// <summary>
43        /// All Connector Models of our Workspace Model
44        /// </summary>
45        public List<ConnectorModel> AllConnectorModels;
46
47        /// <summary>
48        /// All ConnectionModels of our Workspace Model
49        /// </summary>
50        public List<ConnectionModel> AllConnectionModels;
51
52        /// <summary>
53        /// Creates a new Workspace Model
54        /// </summary>
55        public WorkspaceModel()
56        { 
57            this.AllPluginModels = new List<PluginModel>();
58            this.AllConnectionModels = new List<ConnectionModel>();
59            this.AllConnectorModels = new List<ConnectorModel>();           
60        }
61
62        /// <summary>
63        /// Creates a new PluginModel belonging to this WorkspaceModel
64        /// </summary>
65        /// <param name="x"></param>
66        /// <param name="y"></param>
67        /// <param name="width"></param>
68        /// <param name="height"></param>
69        /// <param name="pluginType"></param>
70        /// <returns></returns>
71        public PluginModel newPluginModel(Point position, double width, double height, Type pluginType)
72        {
73            PluginModel pluginModel = new PluginModel();
74            pluginModel.WorkspaceModel = this;
75            pluginModel.Position = position;
76            pluginModel.Width = width;
77            pluginModel.Height = height;
78            pluginModel.PluginType = pluginType;
79            pluginModel.generateConnectors();
80            pluginModel.Name = pluginType.Name;
81            pluginModel.Plugin.OnGuiLogNotificationOccured += this.WorkspaceManagerEditor.GuiLogNotificationOccured;
82            pluginModel.Plugin.OnPluginProgressChanged += pluginModel.PluginProgressChanged;
83            pluginModel.Plugin.OnPluginStatusChanged += pluginModel.PluginStatusChanged;
84            this.AllPluginModels.Add(pluginModel);
85            return pluginModel;
86        }
87
88        /// <summary>
89        /// Creates a new PluginModel belonging to this WorkspaceModel
90        /// Position and Dimension are (x,y,width,height) = (0,0,0,0)
91        /// </summary>
92        /// <param name="pluginType"></param>
93        /// <returns></returns>
94        public PluginModel newPluginModel(Type pluginType)
95        {
96            return newPluginModel(new Point(0, 0), 0, 0, pluginType);
97        }       
98
99        /// <summary>
100        /// Creates a new Connection starting at "from"-Connector going to "to"-Connector with
101        /// the given connectionType
102        /// </summary>
103        /// <param name="from"></param>
104        /// <param name="to"></param>
105        /// <param name="connectionType"></param>
106        /// <returns></returns>
107        public ConnectionModel newConnectionModel(ConnectorModel from, ConnectorModel to, Type connectionType)
108        {
109            ConnectionModel connectionModel = new ConnectionModel();
110            connectionModel.WorkspaceModel = this;
111            connectionModel.From = from;
112            connectionModel.To = to;
113            from.OutputConnections.Add(connectionModel);
114            to.InputConnection = connectionModel;
115            connectionModel.ConnectionType = connectionType;
116            this.AllConnectionModels.Add(connectionModel);
117            return connectionModel;
118        }
119
120        /// <summary>
121        /// Deletes the pluginModel and all of its Connectors and the connected Connections
122        /// from our WorkspaceModel
123        /// </summary>
124        /// <param name="pluginModel"></param>
125        /// <returns></returns>
126        public bool deletePluginModel(PluginModel pluginModel)
127        {
128            //we can only delete PluginModels which are part of our WorkspaceModel
129            if (this.AllPluginModels.Contains(pluginModel))
130            {
131                // remove all InputConnectors belonging to this pluginModel from our WorkspaceModel
132                foreach (ConnectorModel inputConnector in new List<ConnectorModel>(pluginModel.InputConnectors))
133                {
134                    deleteConnectorModel(inputConnector);
135                }
136
137                // remove all OutputConnectors belonging to this pluginModel from our WorkspaceModel
138                foreach (ConnectorModel outputConnector in new List<ConnectorModel>(pluginModel.OutputConnectors))
139                {
140                    deleteConnectorModel(outputConnector);
141                }
142                pluginModel.Plugin.Dispose();
143                pluginModel.onDelete();
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 new List<ConnectionModel>(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                connectorModel.onDelete();
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            connectionModel.onDelete();
190            return this.AllConnectionModels.Remove(connectionModel);
191        }
192
193        /// <summary>
194        /// Sets all Connections and Connectors to state nonActive/noData
195        /// </summary>
196        public void resetStates()
197        {
198            foreach (ConnectionModel connection in this.AllConnectionModels)
199            {
200                connection.Active = false;
201            }
202            foreach (ConnectorModel connector in this.AllConnectorModels)
203            {
204                connector.HasData = false;
205                connector.Data = null;
206            }
207        }
208
209        /// <summary>
210        /// Reconnects a Connection with an other Connector
211        /// </summary>
212        /// <param name="connectionModel"></param>
213        /// <param name="connectorModel"></param>
214        /// <returns></returns>
215        public bool reconnectConnection(ConnectionModel connectionModel, ConnectorModel connectorModel)
216        {
217            if (connectionModel.To != null)
218            {
219                connectionModel.To.InputConnection = null;
220            }
221            connectionModel.To = connectorModel;
222            connectorModel.InputConnection = connectionModel;
223            return true;
224        }
225
226        /// <summary>
227        /// Checks wether a Connector and a Connector are compatible to be connected
228        /// They are compatible if their types are equal or the base type of the Connector
229        /// is equal to the type of the other Connector
230        /// </summary>
231        /// <param name="connectorModelA"></param>
232        /// <param name="connectorModelB"></param>
233        /// <returns></returns>
234        public static bool compatibleConnectors(ConnectorModel connectorModelA, ConnectorModel connectorModelB)
235        {
236            if (connectorModelA.ConnectorType.Equals(connectorModelB.ConnectorType)
237                || connectorModelA.ConnectorType.BaseType.Equals(connectorModelB.ConnectorType))
238            {
239                return true;
240            }
241            else
242            {
243                return false;
244            }
245        }
246
247        /// <summary>
248        /// Checks wether a Connection and a Connector are compatible to be connected
249        /// They are compatible if their types are equal or the base type of the connection
250        /// is equal to the type of the connector
251        /// </summary>
252        /// <param name="connectionModel"></param>
253        /// <param name="connectorModel"></param>
254        /// <returns></returns>
255        public static bool compatibleConnectors(ConnectionModel connectionModel, ConnectorModel connectorModel)
256        {
257            if (connectionModel.ConnectionType.Equals(connectorModel.ConnectorType)
258                || connectionModel.ConnectionType.BaseType.Equals(connectorModel.ConnectorType))
259            {
260                return true;
261            }
262            else 
263            { 
264                return false; 
265            }
266        }
267
268        /// <summary>
269        /// Checks wether a Connection and a Connector are compatible to be connected
270        /// They are compatible if their types are equal or the base type of the connection
271        /// is equal to the type of the connector
272        /// </summary>
273        /// <param name="connectorModel"></param>
274        /// <param name="connectionModel"></param>
275        /// <returns></returns>
276        public static bool compatibleConnectors(ConnectorModel connectorModel, ConnectionModel connectionModel)
277        {
278            return compatibleConnectors(connectionModel, connectorModel);
279        }
280    }
281}
Note: See TracBrowser for help on using the repository browser.