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

Last change on this file since 1581 was 1581, checked in by kopal, 12 years ago
  • added ToolTip to ConnectorModel
  • added Icon to PluginModel
File size: 11.1 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            connectionModel.From.OutputConnections.Remove(connectionModel);
185            connectionModel.To.InputConnection = null;
186            return this.AllConnectionModels.Remove(connectionModel);
187        }
188
189        /// <summary>
190        /// Sets all Connections and Connectors to state nonActive/noData
191        /// </summary>
192        public void resetStates()
193        {
194            foreach (PluginModel pluginModel in this.AllPluginModels)
195            {
196                pluginModel.ExecutionState = PluginModelState.Undefined;
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.