예제 #1
0
        internal void RefreshTreeView(Dictionary <AddinManagerAssembly, List <IWordExCommand> > nodesInfo)
        {
            if (nodesInfo != null)
            {
                if (nodesInfo.Comparer.GetType() != typeof(AssemblyComparer))
                {
                    throw new ArgumentException("The dictionary used to synchronize the treeview must have an \"AssemblyComparer\".");
                }

                //
                treeView1.Nodes.Clear();
                foreach (var ndInfo in nodesInfo)
                {
                    AddinManagerAssembly  asm     = ndInfo.Key;
                    List <IWordExCommand> methods = ndInfo.Value;
                    // 添加新的程序集
                    TreeNode tnAss = new TreeNode(asm.Assembly.ManifestModule.ScopeName);
                    tnAss.Tag = asm;
                    treeView1.Nodes.Add(tnAss);
                    // 添加此程序集中所有的外部命令
                    foreach (IWordExCommand m in methods)
                    {
                        TreeNode tnMethod = new TreeNode(m.GetType().FullName);
                        tnMethod.Tag = m;
                        tnAss.Nodes.Add(tnMethod);
                    }
                }

                // 刷新
                _nodesInfo = nodesInfo;
                //
                treeView1.ExpandAll();
                treeView1.Refresh();
            }
        }
예제 #2
0
        private static Dictionary <AddinManagerAssembly, List <IWordExCommand> > DeserializeAssemblies(
            AssemblyInfos amInfos)
        {
            Dictionary <AddinManagerAssembly, List <IWordExCommand> > nodesInfo;

            nodesInfo = new Dictionary <AddinManagerAssembly, List <IWordExCommand> >(new AssemblyComparer());
            //
            if (amInfos != null)
            {
                foreach (string assemblyPath in amInfos.AssemblyPaths)
                {
                    if (File.Exists(assemblyPath))
                    {
                        // 将每一个程序集中的外部命令提取出来
                        List <IWordExCommand> m = ExCommandFinder.RetriveExternalCommandsFromAssembly(assemblyPath);
                        if (m.Any())
                        {
                            Assembly             ass        = m[0].GetType().Assembly;
                            AddinManagerAssembly amAssembly = new AddinManagerAssembly(assemblyPath, ass);
                            if (nodesInfo.ContainsKey(amAssembly))
                            {
                                nodesInfo[amAssembly] = m;
                            }
                            else
                            {
                                nodesInfo.Add(amAssembly, m);
                            }
                        }
                    }
                }
            }
            return(nodesInfo);
        }
예제 #3
0
        private void RunExternalCommand(TreeNode ndCommand)
        {
            var exCommand            = ndCommand.Tag as IWordExCommand;
            AddinManagerAssembly asm = ndCommand.Parent.Tag as AddinManagerAssembly;
            //
            string assemblyPath = asm.Path;

            ExCommandExecutor.InvokeExternalCommand(assemblyPath, exCommand, _WordApplication);
        }
예제 #4
0
        private void RemoveAssembly(TreeNode ndAss)
        {
            if (ndAss.Level != 0)
            {
                throw new ArgumentException("this is not a node representing an assembly.");
            }
            //
            AddinManagerAssembly asm = ndAss.Tag as AddinManagerAssembly;

            _nodesInfo.Remove(asm);
        }
예제 #5
0
        private void RemoveMethod(TreeNode ndAss)
        {
            if (ndAss.Level != 1)
            {
                throw new ArgumentException("this is not a node representing an method.");
            }
            //
            AddinManagerAssembly asm = ndAss.Parent.Tag as AddinManagerAssembly;

            IWordExCommand mtd = ndAss.Tag as IWordExCommand;

            //
            _nodesInfo[asm].Remove(mtd);
        }
예제 #6
0
        private void button_Reload_Click(object sender, EventArgs e)
        {
            TreeNode nd    = treeView1.SelectedNode;
            TreeNode ndAss = null;

            if (nd == null)
            {
                return;
            }
            //
            if (nd.Level == 0) // 移除程序集
            {
                ndAss = nd;
            }
            else if (nd.Level == 1)// 移除某个方法所对应的程序集
            {
                ndAss = nd.Parent;
            }
            AddinManagerAssembly mtd = ndAss.Tag as AddinManagerAssembly;
            string assFullPath       = mtd.Path;

            // 重新加载此程序集
            if (!string.IsNullOrEmpty(assFullPath))
            {
                bool hasNewMethodAdded = false;
                //
                var methods = ExCommandFinder.RetriveExternalCommandsFromAssembly(assFullPath);
                if (methods.Any())
                {
                    // 更新 Dictionary
                    AddMethodsInOneAssembly(assFullPath, methods);
                    hasNewMethodAdded = true;
                }

                if (hasNewMethodAdded)
                {
                    // 刷新界面
                    RefreshTreeView(_nodesInfo);
                }
            }
        }
예제 #7
0
        /// <summary> 将从一个 Assembly 中加载进来的所有有效的外部命令同步到 _nodesInfo 中 </summary>
        /// <param name="methods"></param>
        private void AddMethodsInOneAssembly(string assemblyPath, List <IWordExCommand> methods)
        {
            AddinManagerAssembly asm;

            if (methods.Any())
            {
                asm = new AddinManagerAssembly(assemblyPath, methods.First().GetType().Assembly);
                //
                List <IWordExCommand> mds = new List <IWordExCommand>();
                foreach (var m in methods)
                {
                    mds.Add(m);
                }
                //
                if (_nodesInfo.ContainsKey(asm))  // 覆盖式刷新
                {
                    _nodesInfo[asm] = mds;
                }
                else  // 直接进行添加就可以了
                {
                    _nodesInfo.Add(asm, mds);
                }
            }
        }