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

Last change on this file since 1620 was 1620, checked in by kopal, 12 years ago
  • connectors now may only connect from output to InputConnection
  • view is disabled when editor is executing now
  • connector now has "lastData" which stores last used data
  • minwidth, minheight of an VisualElementModel can only be >=50 now
File size: 10.5 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;
23using System.Runtime.Serialization.Formatters.Soap;
24
25namespace WorkspaceManager.Model
26{
27    /// <summary>
28    /// Class to represent our Workspace
29    /// </summary>
30    [Serializable]
31    public class WorkspaceModel 
32    {
33        [NonSerialized]
34        private WorkspaceManager workspaceManagerEditor;
35
36        /// <summary>
37        /// The surrounding WorkspaceManagerEditor
38        /// </summary>       
39        public WorkspaceManager WorkspaceManagerEditor { 
40            get{
41                return workspaceManagerEditor;
42            }
43            set{ 
44                this.workspaceManagerEditor = value;
45            }
46        }
47
48        /// <summary>
49        /// All PluginModels of our Workspace Model
50        /// </summary>
51        public List<PluginModel> AllPluginModels;
52
53        /// <summary>
54        /// All Connector Models of our Workspace Model
55        /// </summary>
56        public List<ConnectorModel> AllConnectorModels;
57
58        /// <summary>
59        /// All ConnectionModels of our Workspace Model
60        /// </summary>
61        public List<ConnectionModel> AllConnectionModels;
62
63        /// <summary>
64        /// Creates a new Workspace Model
65        /// </summary>
66        public WorkspaceModel()
67        { 
68            this.AllPluginModels = new List<PluginModel>();
69            this.AllConnectionModels = new List<ConnectionModel>();
70            this.AllConnectorModels = new List<ConnectorModel>();           
71        }
72
73        /// <summary>
74        /// Creates a new PluginModel belonging to this WorkspaceModel
75        /// </summary>
76        /// <param name="x"></param>
77        /// <param name="y"></param>
78        /// <param name="width"></param>
79        /// <param name="height"></param>
80        /// <param name="pluginType"></param>
81        /// <returns></returns>
82        public PluginModel newPluginModel(Point position, double width, double height, Type pluginType)
83        {
84            PluginModel pluginModel = new PluginModel();
85            pluginModel.WorkspaceModel = this;
86            pluginModel.Position = position;
87            pluginModel.Width = width;
88            pluginModel.Height = height;
89            pluginModel.PluginType = pluginType;
90            pluginModel.generateConnectors();
91            pluginModel.Name = pluginType.Name;
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.InputConnection = 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 output ConnectionModels belonging to this Connector from our WorkspaceModel
175                foreach (ConnectionModel outputConnection in new List<ConnectionModel>(connectorModel.OutputConnections))
176                {
177                    deleteConnectionModel(outputConnection);
178                }
179
180                //remove the input ConnectionModel belonging to this Connector from our WorkspaceModel
181                if (connectorModel.InputConnection != null)
182                {
183                    deleteConnectionModel(connectorModel.InputConnection);
184                }
185                connectorModel.onDelete();
186                this.WorkspaceManagerEditor.HasChanges = true;
187                return this.AllConnectorModels.Remove(connectorModel);
188            }
189            return false;
190        }
191
192        /// <summary>
193        /// Removes the connectionModel from our Workspace Model and removes it from all Connectors
194        /// </summary>
195        /// <param name="connectionModel"></param>
196        /// <returns></returns>
197        public bool deleteConnectionModel(ConnectionModel connectionModel)
198        {
199            if (connectionModel == null)
200                return false;
201
202            connectionModel.From.OutputConnections.Remove(connectionModel);
203            connectionModel.To.InputConnection = null;
204            connectionModel.onDelete();
205            this.WorkspaceManagerEditor.HasChanges = true;
206            return this.AllConnectionModels.Remove(connectionModel);
207        }
208
209        /// <summary>
210        /// Sets all Connections and Connectors to state nonActive/noData
211        /// </summary>
212        public void resetStates()
213        {
214            foreach (ConnectionModel connection in this.AllConnectionModels)
215            {
216                connection.Active = false;
217            }
218            foreach (ConnectorModel connector in this.AllConnectorModels)
219            {
220                connector.HasData = false;
221                connector.Data = null;
222            }
223        }
224
225        /// <summary>
226        /// Reconnects a Connection with an other Connector
227        /// </summary>
228        /// <param name="connectionModel"></param>
229        /// <param name="connectorModel"></param>
230        /// <returns></returns>
231        public bool reconnectConnection(ConnectionModel connectionModel, ConnectorModel connectorModel)
232        {
233            if (connectionModel.To != null)
234            {
235                connectionModel.To.InputConnection = null;
236            }
237            connectionModel.To = connectorModel;
238            connectorModel.InputConnection = connectionModel;
239            this.WorkspaceManagerEditor.HasChanges = true;
240            return true;
241        }
242
243        /// <summary>
244        /// Checks wether a Connector and a Connector are compatible to be connected
245        /// They are compatible if their types are equal or the base type of the Connector
246        /// is equal to the type of the other Connector
247        /// </summary>
248        /// <param name="connectorModelA"></param>
249        /// <param name="connectorModelB"></param>
250        /// <returns></returns>
251        public static bool compatibleConnectors(ConnectorModel connectorModelA, ConnectorModel connectorModelB)
252        {
253            if (!connectorModelA.Outgoing || connectorModelB.Outgoing || connectorModelB.InputConnection != null)
254            {
255                return false;
256            }
257
258            if (connectorModelA.ConnectorType.Equals(connectorModelB.ConnectorType)
259                || connectorModelA.ConnectorType.BaseType.Equals(connectorModelB.ConnectorType))
260            {
261                return true;
262            }
263            else
264            {
265                return false;
266            }
267        }       
268    }
269}
Note: See TracBrowser for help on using the repository browser.