7
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
9
// Permission is hereby granted, free of charge, to any person obtaining
10
// a copy of this software and associated documentation files (the
11
// "Software"), to deal in the Software without restriction, including
12
// without limitation the rights to use, copy, modify, merge, publish,
13
// distribute, sublicense, and/or sell copies of the Software, and to
14
// permit persons to whom the Software is furnished to do so, subject to
15
// the following conditions:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
using System.Collections;
33
using System.Reflection;
34
using Mono.Addins.Description;
38
public class ExtensionNode
42
ExtensionNodeList childNodes;
45
ExtensionNodeType nodeType;
46
event ExtensionNodeEventHandler extensionNodeChanged;
49
get { return treeNode != null ? treeNode.Id : string.Empty; }
53
get { return treeNode != null ? treeNode.GetPath () : string.Empty; }
57
get { return !Id.StartsWith (ExtensionTree.AutoIdPrefix); }
60
internal void SetTreeNode (TreeNode node)
65
internal void SetData (string plugid, ExtensionNodeType nodeType)
67
this.addinId = plugid;
68
this.nodeType = nodeType;
71
internal string AddinId {
72
get { return addinId; }
75
internal TreeNode TreeNode {
76
get { return treeNode; }
79
public RuntimeAddin Addin {
81
if (addin == null && addinId != null) {
82
if (!AddinManager.SessionService.IsAddinLoaded (addinId))
83
AddinManager.SessionService.LoadAddin (null, addinId, true);
84
addin = AddinManager.SessionService.GetAddin (addinId);
87
throw new InvalidOperationException ("Add-in '" + addinId + "' could not be loaded.");
92
public event ExtensionNodeEventHandler ExtensionNodeChanged {
94
extensionNodeChanged += value;
95
foreach (ExtensionNode node in ChildNodes)
96
extensionNodeChanged (this, new ExtensionNodeEventArgs (ExtensionChange.Add, node));
99
extensionNodeChanged -= value;
103
public ExtensionNodeList ChildNodes {
108
childrenLoaded = true;
111
if (treeNode.Children.Count == 0) {
112
childNodes = ExtensionNodeList.Empty;
116
catch (Exception ex) {
117
AddinManager.ReportError (null, null, ex, false);
118
childNodes = ExtensionNodeList.Empty;
122
ArrayList list = new ArrayList ();
123
foreach (TreeNode cn in treeNode.Children) {
125
// For each node check if it is visible for the current context.
126
// If something fails while evaluating the condition, just ignore the node.
129
if (cn.ExtensionNode != null && cn.IsEnabled)
130
list.Add (cn.ExtensionNode);
131
} catch (Exception ex) {
132
AddinManager.ReportError (null, null, ex, false);
136
childNodes = new ExtensionNodeList (list);
138
childNodes = ExtensionNodeList.Empty;
144
public object[] GetChildObjects ()
146
return GetChildObjects (typeof(object), true);
149
public object[] GetChildObjects (bool reuseCachedInstance)
151
return GetChildObjects (typeof(object), reuseCachedInstance);
154
public object[] GetChildObjects (Type arrayElementType)
156
return GetChildObjects (arrayElementType, true);
159
public object[] GetChildObjects (Type arrayElementType, bool reuseCachedInstance)
161
ArrayList list = new ArrayList (ChildNodes.Count);
163
for (int n=0; n<ChildNodes.Count; n++) {
164
InstanceExtensionNode node = ChildNodes [n] as InstanceExtensionNode;
166
AddinManager.ReportError ("Error while getting object for node in path '" + Path + "'. Extension node is not a subclass of InstanceExtensionNode.", null, null, false);
171
if (reuseCachedInstance)
172
list.Add (node.GetInstance (arrayElementType));
174
list.Add (node.CreateInstance (arrayElementType));
176
catch (Exception ex) {
177
AddinManager.ReportError ("Error while getting object for node in path '" + Path + "'.", null, ex, false);
180
return (object[]) list.ToArray (arrayElementType);
183
internal protected virtual void Read (NodeElement elem)
185
if (nodeType == null || nodeType.Fields == null)
188
NodeAttribute[] attributes = elem.Attributes;
189
string[] required = nodeType.RequiredFields != null ? (string[]) nodeType.RequiredFields.Clone () : null;
190
int nreq = required != null ? required.Length : 0;
192
foreach (NodeAttribute at in attributes) {
194
FieldInfo f = (FieldInfo) nodeType.Fields [at.name];
198
if (required != null) {
199
int i = Array.IndexOf (required, at.name);
208
if (f.FieldType == typeof(string)) {
211
else if (f.FieldType == typeof(string[])) {
212
string[] ss = at.value.Split (',');
213
if (ss.Length == 0 && ss[0].Length == 0)
214
val = new string [0];
216
for (int n=0; n<ss.Length; n++)
217
ss [n] = ss[n].Trim ();
221
else if (f.FieldType.IsEnum) {
222
val = Enum.Parse (f.FieldType, at.value);
226
val = Convert.ChangeType (at.Value, f.FieldType);
227
} catch (InvalidCastException) {
228
throw new InvalidOperationException ("Property type not supported by [NodeAttribute]: " + f.DeclaringType + "." + f.Name);
232
f.SetValue (this, val);
235
foreach (string s in required)
237
throw new InvalidOperationException ("Required attribute '" + s + "' not found.");
241
internal bool NotifyChildChanged ()
246
ExtensionNodeList oldList = childNodes;
247
childrenLoaded = false;
249
bool changed = false;
251
foreach (ExtensionNode nod in oldList) {
252
if (ChildNodes [nod.Id] == null) {
254
OnChildNodeRemoved (nod);
257
foreach (ExtensionNode nod in ChildNodes) {
258
if (oldList [nod.Id] == null) {
260
OnChildNodeAdded (nod);
264
OnChildrenChanged ();
268
// Called when the add-in that defined this extension node is actually
270
internal protected virtual void OnAddinLoaded ()
274
// Called when the add-in that defined this extension node is being
275
// unloaded from memory.
276
internal protected virtual void OnAddinUnloaded ()
280
// Called when the children list of this node has changed. It may be due to add-ins
281
// being loaded/unloaded, or to conditions being changed.
282
protected virtual void OnChildrenChanged ()
286
protected virtual void OnChildNodeAdded (ExtensionNode node)
288
if (extensionNodeChanged != null)
289
extensionNodeChanged (this, new ExtensionNodeEventArgs (ExtensionChange.Add, node));
292
protected virtual void OnChildNodeRemoved (ExtensionNode node)
294
if (extensionNodeChanged != null)
295
extensionNodeChanged (this, new ExtensionNodeEventArgs (ExtensionChange.Remove, node));