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

Last change on this file since 1617 was 1617, checked in by kopal, 11 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: 10.6 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 Cryptool.PluginBase;
23using System.Threading;
24using System.Windows.Controls;
25using Gears4Net;
26using WorkspaceManager.Execution;
27using System.Windows.Threading;
28
29namespace WorkspaceManager.Model
30{
31    /// <summary>
32    /// Class to represent and wrap a IPlugin in our model graph
33    /// </summary>
34    [Serializable]
35    public class PluginModel : VisualElementModel
36    {       
37        [NonSerialized]
38        private IPlugin plugin;
39       
40        /// <summary>
41        /// This is the current index of the image which should be shown
42        /// on the plugins view
43        /// </summary>
44        private int imageIndex = 0;
45       
46        /// <summary>
47        /// All ingoing connectors of this PluginModel
48        /// </summary>
49        public List<ConnectorModel> InputConnectors = null;
50
51        /// <summary>
52        /// All outgoing connectors of this PluginModel
53        /// </summary>
54        public List<ConnectorModel> OutputConnectors = null;
55
56        /// <summary>
57        /// The wrapped IPlugin of this PluginModel
58        /// if there is currently no plugin instance it
59        /// will automatically create one. Otherwise
60        /// this acts as singleton and returns the created
61        /// instance
62        /// </summary>
63        public IPlugin Plugin{
64            get { 
65                if(plugin==null && PluginType != null){
66                    plugin = PluginType.CreateObject();                   
67                }
68                return plugin;
69            }
70
71            private set
72            {
73                plugin = value;
74            }
75        } 
76
77        /// <summary>
78        /// The Type of the Wrapped IPlugin of this PluginModel
79        /// Depending on this the Plugin of this PluginModel will be instanciated
80        /// </summary>       
81        public Type PluginType = null;
82       
83        /// <summary>
84        /// Is the Plugin actually minimized?
85        /// </summary>
86        public bool Minimized { get; set; }
87
88        /// <summary>
89        /// The execution state of the progress of the wrapped plugin
90        /// </summary>
91        public double PercentageFinished { private get; set; }
92
93        /// <summary>
94        /// Create a new PluginModel
95        /// </summary>
96        public PluginModel()
97        {
98            this.InputConnectors = new List<ConnectorModel>();
99            this.OutputConnectors = new List<ConnectorModel>();
100        }
101
102        /// <summary>
103        /// The WorkspaceModel of this PluginModel
104        /// </summary>
105        public WorkspaceModel WorkspaceModel { get; set; }
106
107        /// <summary>
108        /// Generates all Connectors of this Plugin.
109        /// Warning: Before generation all "old" Connectors will be deleted
110        /// </summary>
111        public void generateConnectors(){
112           
113            if (Plugin != null)
114            {
115                this.InputConnectors.Clear();
116                this.OutputConnectors.Clear();
117
118                foreach (PropertyInfoAttribute propertyInfoAttribute in Plugin.GetProperties())
119                {                   
120                    if (propertyInfoAttribute.Direction.Equals(Direction.InputData))
121                    {
122                        ConnectorModel connectorModel = new ConnectorModel();
123                        connectorModel.ConnectorType = propertyInfoAttribute.PropertyInfo.PropertyType;
124                        connectorModel.WorkspaceModel = WorkspaceModel;
125                        connectorModel.PluginModel = this;
126                        connectorModel.IsMandatory = propertyInfoAttribute.Mandatory;
127                        connectorModel.PropertyName = propertyInfoAttribute.PropertyName;
128                        connectorModel.ToolTip = propertyInfoAttribute.ToolTip;
129                        connectorModel.ConnectorOrientation = ConnectorOrientation.West;
130                        InputConnectors.Add(connectorModel);
131                        WorkspaceModel.AllConnectorModels.Add(connectorModel);
132                    }
133                    else if (propertyInfoAttribute.Direction.Equals(Direction.OutputData))
134                    {
135                        ConnectorModel connectorModel = new ConnectorModel();
136                        connectorModel.ConnectorType = propertyInfoAttribute.PropertyInfo.PropertyType;
137                        connectorModel.WorkspaceModel = WorkspaceModel;
138                        connectorModel.PluginModel = this;
139                        connectorModel.IsMandatory = propertyInfoAttribute.Mandatory;
140                        connectorModel.PropertyName = propertyInfoAttribute.PropertyName;
141                        connectorModel.ToolTip = propertyInfoAttribute.ToolTip;
142                        connectorModel.ConnectorOrientation = ConnectorOrientation.East;
143                        connectorModel.Outgoing = true;
144                        Plugin.PropertyChanged += connectorModel.PropertyChangedOnPlugin;
145                        OutputConnectors.Add(connectorModel);
146                        WorkspaceModel.AllConnectorModels.Add(connectorModel);
147                    }                   
148                }
149            }     
150        }
151
152        /// <summary>
153        /// Get the Image of the Plugin
154        /// </summary>
155        /// <returns></returns>
156        public Image getImage()
157        {
158            return Plugin.GetImage(imageIndex);
159        }
160
161        /// <summary>
162        /// Returns the Presentation of the wrapped IPlugin
163        /// </summary>
164        public UserControl PluginPresentation
165        {
166            get
167            {
168                if(this.Plugin.Presentation != null){
169                    return this.Plugin.Presentation;
170                }else{
171                    return this.Plugin.QuickWatchPresentation;
172                }
173            }
174        }
175             
176        /// <summary>
177        /// Should be called by the UI-Thread to paint changes of the PluginModel
178        /// </summary>
179        public void paint()
180        {
181            //Enter some Code which calls the paint method of the IPlugin
182        }
183
184
185        /// <summary>
186        /// Checks wether this PluginModel is executable or not and sets the isExecutable bool
187        ///
188        /// There are 3 ways in that a plugin can be executable:
189        ///     1. All mandatory inputs are set + all outputs are "free"
190        ///     2. There are no mandatory inputs and at least one non-mandatory input is set + all outputs are "free"
191        ///     3. There are no inputs + all outputs are "free"
192        /// </summary>
193        public void checkExecutable(ProtocolBase protocolBase)
194        {                           
195            bool AtLeastOneInputSet = false;
196            //First test if every mandatory Connector has data
197            //or one non-mandatory input has data
198            foreach (ConnectorModel connectorModel in this.InputConnectors)
199            {
200                if (connectorModel.IsMandatory && !connectorModel.HasData)
201                {
202                    return;
203                }
204                else if (connectorModel.HasData)
205                {
206                    AtLeastOneInputSet = true;
207                }
208
209            }
210
211            //Next test if every connected output Connection is not active
212            foreach (ConnectorModel connectorModel in this.OutputConnectors)
213            {
214                foreach (ConnectionModel connection in connectorModel.OutputConnections)
215                {
216                    if (connection.Active)
217                    {                           
218                       return;
219                    }                       
220                }
221            }
222
223            if (AtLeastOneInputSet || this.InputConnectors.Count == 0)
224            {
225                MessagePreExecution msg = new MessagePreExecution();
226                msg.PluginModel = this;
227               
228                //protocolBase is set at Startup of the ExecutionEngine
229                //but it could be that we have an event before setting
230                //of the protocl base (triggered by user clicking on
231                //a plugins presentation (button or so))
232                if (protocolBase != null)
233                {
234                    protocolBase.BroadcastMessageReliably(msg);
235                }
236            }
237            return;
238        }
239
240        /// <summary>
241        /// Progress of the plugin changed
242        /// </summary>
243        /// <param name="sender"></param>
244        /// <param name="args"></param>
245        public void PluginProgressChanged(IPlugin sender, PluginProgressEventArgs args)
246        {
247            //Calculate % of the plugins process
248            this.PercentageFinished = args.Value / args.Max;
249            //Tell the ExecutionEngine that this plugin needs a gui update
250            this.GuiNeedsUpdate = true;
251        }
252
253        /// <summary>
254        /// Status of the plugin changed
255        /// </summary>
256        /// <param name="sender"></param>
257        /// <param name="args"></param>
258        public void PluginStatusChanged(IPlugin sender, StatusEventArgs args)
259        {
260            if (args.StatusChangedMode == StatusChangedMode.ImageUpdate)
261            {
262                this.imageIndex = args.ImageIndex;
263            }
264               
265            if (this.WorkspaceModel.WorkspaceManagerEditor.isExecuting())
266            {
267                this.GuiNeedsUpdate = true;
268            }
269            else
270            {
271                this.WorkspaceModel.WorkspaceManagerEditor.Presentation.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback)delegate
272                {
273                    this.UpdateableView.update();
274                }, null);
275            }           
276        }
277
278        /// <summary>
279        /// The pluginProtocol of the current ExecutionEngine run to set/get
280        /// </summary>
281        public PluginProtocol PluginProtocol { get; set; }
282    }
283}
Note: See TracBrowser for help on using the repository browser.