// Load the extension nodes at the specified path. If the path // contains extension nodes implemented in an add-in which is // not loaded, the add-in will be automatically loaded internal void LoadExtensions(string requestedExtensionPath) { TreeNode node = GetNode(requestedExtensionPath); if (node == null) { throw new InvalidOperationException("Extension point not defined: " + requestedExtensionPath); } ExtensionPoint ep = node.ExtensionPoint; if (ep != null) { // Collect extensions to be loaded from add-ins. Before loading the extensions, // they must be sorted, that's why loading is split in two steps (collecting + loading). ArrayList loadData = new ArrayList(); foreach (string addin in GetAddinsForPath(ep.Path, ep.Addins)) { ExtensionLoadData ed = GetAddinExtensions(addin, ep); if (ed != null) { // Insert the addin data taking into account dependencies. // An add-in must be processed after all its dependencies. bool added = false; for (int n = 0; n < loadData.Count; n++) { ExtensionLoadData other = (ExtensionLoadData)loadData [n]; if (AddinManager.Registry.AddinDependsOn(other.AddinId, ed.AddinId)) { loadData.Insert(n, ed); added = true; break; } } if (!added) { loadData.Add(ed); } } } // Now load the extensions ArrayList loadedNodes = new ArrayList(); foreach (ExtensionLoadData data in loadData) { foreach (Extension ext in data.Extensions) { TreeNode cnode = GetNode(ext.Path); if (cnode != null && cnode.ExtensionNodeSet != null) { LoadModuleExtensionNodes(ext, data.AddinId, cnode.ExtensionNodeSet, loadedNodes); } else { AddinManager.ReportError("Extension node not found or not extensible: " + ext.Path, data.AddinId, null, false); } } } // Call the OnAddinLoaded method on nodes, if the add-in is already loaded foreach (TreeNode nod in loadedNodes) { nod.ExtensionNode.OnAddinLoaded(); } NotifyExtensionsChanged(new ExtensionEventArgs(requestedExtensionPath)); } }
internal void ActivateAddinExtensions(string id) { // Looks for loaded extension points which are extended by the provided // add-in, and adds the new nodes try { fireEvents = true; Addin addin = AddinManager.Registry.GetAddin(id); if (addin == null) { AddinManager.ReportError("Required add-in not found", id, null, false); return; } // Take note that his add-in has been enabled at run-time // Needed because loaded add-in descriptions may not include this add-in. RegisterRuntimeEnabledAddin(id); // Look for loaded extension points Hashtable eps = new Hashtable(); foreach (ModuleDescription mod in addin.Description.AllModules) { foreach (Extension ext in mod.Extensions) { ExtensionPoint ep = tree.FindLoadedExtensionPoint(ext.Path); if (ep != null && !eps.Contains(ep)) { eps.Add(ep, ep); } } } // Add the new nodes ArrayList loadedNodes = new ArrayList(); foreach (ExtensionPoint ep in eps.Keys) { ExtensionLoadData data = GetAddinExtensions(id, ep); if (data != null) { foreach (Extension ext in data.Extensions) { TreeNode node = GetNode(ext.Path); if (node != null && node.ExtensionNodeSet != null) { if (node.ChildrenLoaded) { LoadModuleExtensionNodes(ext, data.AddinId, node.ExtensionNodeSet, loadedNodes); } } else { AddinManager.ReportError("Extension node not found or not extensible: " + ext.Path, id, null, false); } } // Global extension change event. Other events are fired by LoadModuleExtensionNodes. NotifyExtensionsChanged(new ExtensionEventArgs(ep.Path)); } } // Call the OnAddinLoaded method on nodes, if the add-in is already loaded foreach (TreeNode nod in loadedNodes) { nod.ExtensionNode.OnAddinLoaded(); } } finally { fireEvents = false; } // Do the same in child contexts lock (conditionTypes) { if (childContexts != null) { foreach (WeakReference wref in childContexts) { ExtensionContext ctx = wref.Target as ExtensionContext; if (ctx != null) { ctx.ActivateAddinExtensions(id); } } } } }
internal bool LoadAddin(IProgressStatus statusMonitor, string id, bool throwExceptions) { try { if (IsAddinLoaded(id)) { return(true); } if (!AddinManager.Registry.IsAddinEnabled(id)) { string msg = GettextCatalog.GetString("Disabled add-ins can't be loaded."); AddinManager.ReportError(msg, id, null, false); if (throwExceptions) { throw new InvalidOperationException(msg); } return(false); } ArrayList addins = new ArrayList(); Stack depCheck = new Stack(); ResolveLoadDependencies(addins, depCheck, id, false); addins.Reverse(); if (statusMonitor != null) { statusMonitor.SetMessage("Loading Addins"); } for (int n = 0; n < addins.Count; n++) { if (statusMonitor != null) { statusMonitor.SetProgress((double)n / (double)addins.Count); } Addin iad = (Addin)addins [n]; if (IsAddinLoaded(iad.Id)) { continue; } if (statusMonitor != null) { statusMonitor.SetMessage(string.Format(GettextCatalog.GetString("Loading {0} add-in"), iad.Id)); } if (!InsertAddin(statusMonitor, iad)) { return(false); } } return(true); } catch (Exception ex) { AddinManager.ReportError("Add-in could not be loaded: " + ex.Message, id, ex, false); if (statusMonitor != null) { statusMonitor.ReportError("Add-in '" + id + "' could not be loaded.", ex); } if (throwExceptions) { throw; } return(false); } }
void LoadExtensionElement(TreeNode tnode, string addin, ExtensionNodeDescriptionCollection extension, ref int curPos, BaseCondition parentCondition, bool inComplextCondition, ArrayList addedNodes) { foreach (ExtensionNodeDescription elem in extension) { if (inComplextCondition) { parentCondition = ReadComplexCondition(elem, parentCondition); inComplextCondition = false; continue; } if (elem.NodeName == "ComplexCondition") { LoadExtensionElement(tnode, addin, elem.ChildNodes, ref curPos, parentCondition, true, addedNodes); continue; } if (elem.NodeName == "Condition") { Condition cond = new Condition(elem, parentCondition); LoadExtensionElement(tnode, addin, elem.ChildNodes, ref curPos, cond, false, addedNodes); continue; } string after = elem.GetAttribute("insertafter"); if (after.Length > 0) { int i = tnode.Children.IndexOfNode(after); if (i != -1) { curPos = i + 1; } } string before = elem.GetAttribute("insertbefore"); if (before.Length > 0) { int i = tnode.Children.IndexOfNode(before); if (i != -1) { curPos = i; } } // Find the type of the node in this extension ExtensionNodeType ntype = AddinManager.SessionService.FindType(tnode.ExtensionNodeSet, elem.NodeName, addin); if (ntype == null) { AddinManager.ReportError("Node '" + elem.NodeName + "' not allowed in extension: " + tnode.GetPath(), addin, null, false); continue; } string id = elem.GetAttribute("id"); if (id.Length == 0) { id = AutoIdPrefix + (++internalId); } TreeNode cnode = new TreeNode(id); ExtensionNode enode = ReadNode(cnode, addin, ntype, elem); if (enode == null) { continue; } cnode.Condition = parentCondition; cnode.ExtensionNodeSet = ntype; tnode.InsertChildNode(curPos, cnode); addedNodes.Add(cnode); if (cnode.Condition != null) { Context.RegisterNodeCondition(cnode, cnode.Condition); } // Load children if (elem.ChildNodes.Count > 0) { int cp = 0; LoadExtensionElement(cnode, addin, elem.ChildNodes, ref cp, parentCondition, false, addedNodes); } curPos++; } if (Context.FireEvents) { tnode.NotifyChildrenChanged(); } }
bool InitializeNodeType(ExtensionNodeType ntype) { RuntimeAddin p = AddinManager.SessionService.GetAddin(ntype.AddinId); if (p == null) { if (!AddinManager.SessionService.IsAddinLoaded(ntype.AddinId)) { if (!AddinManager.SessionService.LoadAddin(null, ntype.AddinId, false)) { return(false); } p = AddinManager.SessionService.GetAddin(ntype.AddinId); if (p == null) { AddinManager.ReportError("Add-in not found", ntype.AddinId, null, false); return(false); } } } // If no type name is provided, use TypeExtensionNode by default if (ntype.TypeName == null || ntype.TypeName.Length == 0) { ntype.Type = typeof(TypeExtensionNode); return(true); } ntype.Type = p.GetType(ntype.TypeName, false); if (ntype.Type == null) { AddinManager.ReportError("Extension node type '" + ntype.TypeName + "' not found.", ntype.AddinId, null, false); return(false); } Hashtable fields = new Hashtable(); // Check if the type has NodeAttribute attributes applied to fields. Type type = ntype.Type; while (type != typeof(object) && type != null) { foreach (FieldInfo field in type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)) { NodeAttributeAttribute at = (NodeAttributeAttribute)Attribute.GetCustomAttribute(field, typeof(NodeAttributeAttribute), true); if (at != null) { ExtensionNodeType.FieldData fdata = new ExtensionNodeType.FieldData(); fdata.Field = field; fdata.Required = at.Required; fdata.Localizable = at.Localizable; string name; if (at.Name != null && at.Name.Length > 0) { name = at.Name; } else { name = field.Name; } fields [name] = fdata; } } type = type.BaseType; } if (fields.Count > 0) { ntype.Fields = fields; } return(true); }
bool InitializeNodeType(ExtensionNodeType ntype) { RuntimeAddin p = AddinManager.SessionService.GetAddin(ntype.AddinId); if (p == null) { if (!AddinManager.SessionService.IsAddinLoaded(ntype.AddinId)) { if (!AddinManager.SessionService.LoadAddin(null, ntype.AddinId, false)) { return(false); } p = AddinManager.SessionService.GetAddin(ntype.AddinId); if (p == null) { AddinManager.ReportError("Add-in not found", ntype.AddinId, null, false); return(false); } } } // If no type name is provided, use TypeExtensionNode by default if (ntype.TypeName == null || ntype.TypeName.Length == 0) { ntype.Type = typeof(TypeExtensionNode); return(true); } ntype.Type = p.GetType(ntype.TypeName, false); if (ntype.Type == null) { AddinManager.ReportError("Extension node type '" + ntype.TypeName + "' not found.", ntype.AddinId, null, false); return(false); } Hashtable fields = new Hashtable(); ArrayList reqFields = new ArrayList(); // Check if the type has NodeAttribute attributes applied to fields. foreach (FieldInfo field in ntype.Type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) { NodeAttributeAttribute at = (NodeAttributeAttribute)Attribute.GetCustomAttribute(field, typeof(NodeAttributeAttribute), true); if (at != null) { string name; if (at.Name != null && at.Name.Length > 0) { name = at.Name; } else { name = field.Name; } if (at.Required) { reqFields.Add(name); } fields [name] = field; } } if (fields.Count > 0) { ntype.Fields = fields; if (reqFields.Count > 0) { ntype.RequiredFields = (string[])reqFields.ToArray(typeof(string)); } } return(true); }