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

Last change on this file since 1714 was 1714, checked in by kopal, 12 years ago
  • a pluginmodel now cannot be connected to itself
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        [NonSerialized]
33        private WorkspaceManager workspaceManagerEditor;
34
35        /// <summary>
36        /// The surrounding WorkspaceManagerEditor
37        /// </summary>
38        public WorkspaceManager WorkspaceManagerEditor { 
39            get{
40                return workspaceManagerEditor;
41            }
42            set{ 
43                workspaceManagerEditor = value;
44            }
45        }
46
47        /// <summary>
48        /// All PluginModels of our Workspace Model
49        /// </summary>
50        public List<PluginModel> AllPluginModels;
51
52        /// <summary>
53        /// All Connector Models of our Workspace Model
54        /// </summary>
55        public List<ConnectorModel> AllConnectorModels;
56
57        /// <summary>
58        /// All ConnectionModels of our Workspace Model
59        /// </summary>
60        public List<ConnectionModel> AllConnectionModels;
61
62        /// <summary>
63        /// Creates a new Workspace Model
64        /// </summary>
65        public WorkspaceModel()
66        { 
67            this.AllPluginModels = new List<PluginModel>();
68            this.AllConnectionModels = new List<ConnectionModel>();
69            this.AllConnectorModels = new List<ConnectorModel>();           
70        }
71
72        /// <summary>
73        /// Creates a new PluginModel belonging to this WorkspaceModel
74        /// </summary>
75        /// <param name="x"></param>
76        /// <param name="y"></param>
77        /// <param name="width"></param>
78        /// <param name="height"></param>
79        /// <param name="pluginType"></param>
80        /// <returns></returns>
81        public PluginModel newPluginModel(Point position, double width, double height, Type pluginType)
82        {
83            PluginModel pluginModel = new PluginModel();
84            pluginModel.WorkspaceModel = this;           
85            pluginModel.Startable = pluginType.GetPluginInfoAttribute().Startable;
86            pluginModel.Position = position;
87            pluginModel.Width = width;
88            pluginModel.Height = height;
89            pluginModel.PluginType = pluginType;           
90            pluginModel.Name = pluginType.Name;
91            pluginModel.generateConnectors();
92            pluginModel.Plugin.OnGuiLogNotificationOccured += this.WorkspaceManagerEditor.GuiLogNotificationOccured;
93            pluginModel.Plugin.OnPluginProgressChanged += pluginModel.PluginProgressChanged;
94            pluginModel.Plugin.OnPluginStatusChanged += pluginModel.PluginStatusChanged;
95            this.AllPluginModels.Add(pluginModel);
96            this.WorkspaceManagerEditor.HasChanges = true;
97            return pluginModel;
98        }
99
100        /// <summary>
101        /// Creates a new PluginModel belonging to this WorkspaceModel
102        /// Position and Dimension are (x,y,width,height) = (0,0,0,0)
103        /// </summary>
104        /// <param name="pluginType"></param>
105        /// <returns></returns>
106        public PluginModel newPluginModel(Type pluginType)
107        {
108            return newPluginModel(new Point(0, 0), 0, 0, pluginType);
109        }       
110
111        /// <summary>
112        /// Creates a new Connection starting at "from"-Connector going to "to"-Connector with
113        /// the given connectionType
114        /// </summary>
115        /// <param name="from"></param>
116        /// <param name="to"></param>
117        /// <param name="connectionType"></param>
118        /// <returns></returns>
119        public ConnectionModel newConnectionModel(ConnectorModel from, ConnectorModel to, Type connectionType)
120        {
121            ConnectionModel connectionModel = new ConnectionModel();
122            connectionModel.WorkspaceModel = this;
123            connectionModel.From = from;
124            connectionModel.To = to;
125            from.OutputConnections.Add(connectionModel);
126            to.InputConnections.Add(connectionModel);
127            connectionModel.ConnectionType = connectionType;
128            this.AllConnectionModels.Add(connectionModel);
129            this.WorkspaceManagerEditor.HasChanges = true;
130            return connectionModel;
131        }
132
133        /// <summary>
134        /// Deletes the pluginModel and all of its Connectors and the connected Connections
135        /// from our WorkspaceModel
136        /// </summary>
137        /// <param name="pluginModel"></param>
138        /// <returns></returns>
139        public bool deletePluginModel(PluginModel pluginModel)
140        {
141            //we can only delete PluginModels which are part of our WorkspaceModel
142            if (this.AllPluginModels.Contains(pluginModel))
143            {
144                // remove all InputConnectors belonging to this pluginModel from our WorkspaceModel
145                foreach (ConnectorModel inputConnector in new List<ConnectorModel>(pluginModel.InputConnectors))
146                {
147                    deleteConnectorModel(inputConnector);
148                }
149
150                // remove all OutputConnectors belonging to this pluginModel from our WorkspaceModel
151                foreach (ConnectorModel outputConnector in new List<ConnectorModel>(pluginModel.OutputConnectors))
152                {
153                    deleteConnectorModel(outputConnector);
154                }
155                pluginModel.Plugin.Dispose();
156                pluginModel.onDelete();
157                this.WorkspaceManagerEditor.HasChanges = true;
158                return this.AllPluginModels.Remove(pluginModel);
159            }           
160            return false;
161        }
162
163        /// <summary>
164        /// Deletes the connectorModel and the connected Connections
165        /// from our WorkspaceModel
166        /// </summary>
167        /// <param name="connectorModel"></param>
168        /// <returns></returns>
169        private bool deleteConnectorModel(ConnectorModel connectorModel)
170        {
171            //we can only delete ConnectorModels which are part of our WorkspaceModel
172            if(this.AllConnectorModels.Contains(connectorModel)){
173
174                //remove all input ConnectionModels belonging to this Connector from our WorkspaceModel
175                foreach (ConnectionModel connectionModel in new List<ConnectionModel>(connectorModel.InputConnections))
176                {
177                    deleteConnectionModel(connectionModel);
178                }
179
180                //remove all output ConnectionModels belonging to this Connector from our WorkspaceModel
181                foreach (ConnectionModel outputConnection in new List<ConnectionModel>(connectorModel.OutputConnections))
182                {
183                    deleteConnectionModel(outputConnection);
184                }
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.To.InputConnections.Remove(connectionModel);
204            connectionModel.From.OutputConnections.Remove(connectionModel);           
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 (PluginModel pluginModel in this.AllPluginModels)
216            {
217                pluginModel.State = PluginModelState.Normal;
218            }
219            foreach (ConnectionModel connection in this.AllConnectionModels)
220            {
221                connection.Active = false;
222            }
223            foreach (ConnectorModel connector in this.AllConnectorModels)
224            {
225                connector.HasData = false;
226                connector.Data = null;
227            }
228        }
229
230        /// <summary>
231        /// Reconnects a Connection with an other Connector
232        /// </summary>
233        /// <param name="connectionModel"></param>
234        /// <param name="connectorModel"></param>
235        /// <returns></returns>
236        public bool reconnectConnection(ConnectionModel connectionModel, ConnectorModel connectorModel)
237        {
238            if (connectionModel.To != null)
239            {
240                connectionModel.To.InputConnections.Remove(connectionModel);
241            }
242            connectionModel.To = connectorModel;
243            connectorModel.InputConnections.Add(connectionModel);
244            this.WorkspaceManagerEditor.HasChanges = true;
245            return true;
246        }
247
248        /// <summary>
249        /// Checks wether a Connector and a Connector are compatible to be connected
250        /// They are compatible if their types are equal or the base type of the Connector
251        /// is equal to the type of the other Connector
252        /// </summary>
253        /// <param name="connectorModelA"></param>
254        /// <param name="connectorModelB"></param>
255        /// <returns></returns>
256        public static bool compatibleConnectors(ConnectorModel connectorModelA, ConnectorModel connectorModelB)
257        {
258            if (!connectorModelA.Outgoing || connectorModelB.Outgoing || connectorModelA.PluginModel == connectorModelB.PluginModel)
259            {
260                return false;
261            }
262               
263            if (connectorModelA.ConnectorType.Equals(connectorModelB.ConnectorType)
264                || connectorModelA.ConnectorType.FullName == "System.Object"
265                || connectorModelB.ConnectorType.FullName == "System.Object"
266                || connectorModelA.ConnectorType.IsSubclassOf(connectorModelB.ConnectorType)
267                || connectorModelA.ConnectorType.GetInterfaces().Contains(connectorModelB.ConnectorType))             
268            {
269                return true;
270            }
271            else
272            {
273                return false;
274            }
275        }
276    }
277}
Note: See TracBrowser for help on using the repository browser.