~ubuntu-branches/ubuntu/trusty/mono-addins/trusty-proposed

« back to all changes in this revision

Viewing changes to Mono.Addins/Mono.Addins/ConditionType.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-04-25 11:11:33 UTC
  • mfrom: (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110425111133-t05u5p7o5fxx70fu
Tags: 0.6-2
Upload to Unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
 
35
35
namespace Mono.Addins
36
36
{
 
37
        /// <summary>
 
38
        /// A condition evaluator.
 
39
        /// </summary>
 
40
        /// <remarks>
 
41
        /// Add-ins may use conditions to register nodes in an extension point which
 
42
        /// are only visible under some contexts. For example, an add-in registering
 
43
        /// a custom menu option to the main menu of a sample text editor might want
 
44
        /// to make that option visible only for some kind of files. To allow add-ins
 
45
        /// to do this kind of check, the host application needs to define a new condition.
 
46
        /// </remarks>
37
47
        public abstract class ConditionType
38
48
        {
39
49
                internal event EventHandler Changed;
40
50
                string id;
41
51
                
 
52
                /// <summary>
 
53
                /// Evaluates the condition.
 
54
                /// </summary>
 
55
                /// <param name="conditionNode">
 
56
                /// Condition node information.
 
57
                /// </param>
 
58
                /// <returns>
 
59
                /// 'true' if the condition is satisfied.
 
60
                /// </returns>
42
61
                public abstract bool Evaluate (NodeElement conditionNode);
43
62
                
 
63
                /// <summary>
 
64
                /// Notifies that the condition has changed, and that it has to be re-evaluated.
 
65
                /// </summary>
 
66
                /// This method must be called when there is a change in the state that determines
 
67
                /// the result of the evaluation. When this method is called, all node conditions
 
68
                /// depending on it are reevaluated and the corresponding events for adding or
 
69
                /// removing extension nodes are fired.
 
70
                /// <remarks>
 
71
                /// </remarks>
44
72
                public void NotifyChanged ()
45
73
                {
46
74
                        if (Changed != null)
135
163
                                cond.GetConditionTypes (listToFill);
136
164
                }
137
165
        }
 
166
        
 
167
        class NotCondition: BaseCondition
 
168
        {
 
169
                BaseCondition baseCond;
 
170
                
 
171
                public NotCondition (BaseCondition baseCond, BaseCondition parent): base (parent)
 
172
                {
 
173
                        this.baseCond = baseCond;
 
174
                }
 
175
                
 
176
                public override bool Evaluate (ExtensionContext ctx)
 
177
                {
 
178
                        return !base.Evaluate (ctx);
 
179
                }
 
180
                
 
181
                internal override void GetConditionTypes (System.Collections.ArrayList listToFill)
 
182
                {
 
183
                        baseCond.GetConditionTypes (listToFill);
 
184
                }
 
185
        }
138
186
 
139
187
        
140
188
        internal sealed class Condition: BaseCondition
141
189
        {
142
190
                ExtensionNodeDescription node;
143
191
                string typeId;
 
192
                AddinEngine addinEngine;
144
193
                
145
 
                internal Condition (ExtensionNodeDescription element, BaseCondition parent): base (parent)
 
194
                internal Condition (AddinEngine addinEngine, ExtensionNodeDescription element, BaseCondition parent): base (parent)
146
195
                {
 
196
                        this.addinEngine = addinEngine;
147
197
                        typeId = element.GetAttribute ("id");
148
198
                        node = element;
149
199
                }
155
205
                        
156
206
                        ConditionType type = ctx.GetCondition (typeId);
157
207
                        if (type == null) {
158
 
                                AddinManager.ReportError ("Condition '" + typeId + "' not found in current extension context.", null, null, false);
 
208
                                addinEngine.ReportError ("Condition '" + typeId + "' not found in current extension context.", null, null, false);
159
209
                                return false;
160
210
                        }
161
211
                        
163
213
                                return type.Evaluate (node);
164
214
                        }
165
215
                        catch (Exception ex) {
166
 
                                AddinManager.ReportError ("Error while evaluating condition '" + typeId + "'", null, ex, false);
 
216
                                addinEngine.ReportError ("Error while evaluating condition '" + typeId + "'", null, ex, false);
167
217
                                return false;
168
218
                        }
169
219
                }