Ejemplo n.º 1
0
        public void AddToLibrary(ICustomLibrary customLibrary)
        {
            try
            {
                string           objectName = CommonDefine.Untitled;
                ISerializeWriter writer     = GetSerializeWriter(ref objectName);
                if (writer == null)
                {
                    return;
                }

                IDocumentService DocService = ServiceLocator.Current.GetInstance <IDocumentService>();
                ILibrary         library    = DocService.LibraryManager.GetLibrary(customLibrary.LibraryGID, customLibrary.FileName);
                if (library != null)
                {
                    ICustomObject newObject = library.AddCustomObject(writer, objectName, CreateIconImage(false), null);
                    if (newObject != null)
                    {
                        customLibrary.AddCustomObject(newObject.Guid);
                    }
                }
                else
                {
                    // If we cannot get the specific library, there is something wrong about this custom library.
                    // Refresh this custom library
                    customLibrary.Refresh();
                }
            }
            catch (Exception ex)
            {
                NLogger.Warn("Add to library failed. ex:{0}", ex.ToString());

                MessageBox.Show(GlobalData.FindResource("Error_Create_Library") + ex.Message);
            }
        }
        protected override void RunInternal()
        {
            // Load a library file
            string   libraryFileName = Path.Combine(Program.WORKING_DIRECTORY, "Test_CreateLibraryDocument.libpn");
            ILibrary libraryLoad     = Program.Service.LibraryManager.LoadLibrary(libraryFileName);

            // Cache the library guid.
            Guid libraryGuid = libraryLoad.Guid;

            // Create a new document.
            Program.Service.NewDocument(DocumentType.Standard);
            IDocument document = Program.Service.Document;

            // Create a page.
            IDocumentPage page = document.CreatePage("Home");

            // Create the page node in page tree.
            ITreeNode node = document.DocumentSettings.LayoutSetting.PageTree.AddChild(TreeNodeType.Page);

            node.AttachedObject = page;

            // Must open the page before you read and modify it.
            page.Open();

            // Get the page view for base adaptive view.
            IPageView baseView = page.PageViews[document.AdaptiveViewSet.Base.Guid];

            // Create widgets on the base view in this page.
            IButton button = baseView.CreateWidget(WidgetType.Button) as IButton;

            button.WidgetStyle.Height = 30;
            button.WidgetStyle.Width  = 100;
            button.Name    = "Button 1";
            button.Text    = "Button";
            button.Tooltip = "Html button.";

            // Get the loaded library with the cached guid.
            ILibrary libraryGet = Program.Service.LibraryManager.GetLibrary(libraryGuid);

            // Put the button in a writer.
            ISerializeWriter writer = document.CreateSerializeWriter(document.AdaptiveViewSet.Base.Guid);

            writer.AddWidget(button);

            // Create a new custom object which contains the button and add to the loaded library.
            libraryGet.AddCustomObject(writer, "New_Custom_Object_Which_Contains_Button", null, null);

            // Save changes to file
            libraryGet.Save(libraryGet.Name);

            page.Close();

            // Save the document to a pn file.
            string fileName = Path.Combine(Program.WORKING_DIRECTORY, _caseName + ".pn");

            Program.Service.Save(fileName);

            // Close this document when you don't work on it anymore.
            Program.Service.Close();

            // Delete loaded libraries
            Program.Service.LibraryManager.DeleteLibrary(libraryGuid);
        }