DSPHTMLCommentUtility d365fo x++

DSPHTMLCommentUtility d365fo x++

Code:

/// <summary>
/// This is a framework class. Customizing this class may cause problems with future upgrades to the software.
/// </summary>
abstract class DSPHTMLCommentUtility
{
    FormGroupControl commentHeaderGroupControl;
    Timezone userPreferredTimeZone;
    HcmIHTMLCommentDialog htmlCommentDialog;
    const str commentGroupControlName = 'commentGroupControl';
    const str commentHTMLViewerControlName = 'commentHTMLViewerControl';
    Map commentControlsMap;
    FormControlId selectedCommentControlId;
    
    /// <summary>
    /// The secondary instruction for the comment dialog.
    /// </summary>
    /// <returns>A string to use as the dialog's secondary instruction</returns>
    /// <remarks>
    /// Override this method if you want to provide a secondary instruction on the comment dialog.
    /// </remarks>
    public str getSecondaryInstruction()
    {
        return '';
    }

    protected void new()
    {
        this.setUserPreferredTimeZone();
    }

    /// <summary>
    /// Sets the commentHeaderGroupControl property to the supplied value.
    /// </summary>
    /// <param name = "_commentHeaderGroupControl">The supplied value</param>
    public void setCommentHeaderGroupControl(FormGroupControl _commentHeaderGroupControl)
    {
        commentHeaderGroupControl = _commentHeaderGroupControl;
    }

    private void addButtons()
    {
        if(commentHeaderGroupControl)
        {
            FormButtonControl btnExpandAll = commentHeaderGroupControl.addControl(FormControlType::Button, 'CommentBtnExpandAll');
            FormButtonControl btnCollaspAll = commentHeaderGroupControl.addControl(FormControlType::Button, 'CommentBtnCollaspAll');

            btnExpandAll.text('Expand all');
            btnCollaspAll.text('Collasp all');

            btnExpandAll.registerOverrideMethod(methodStr(FormButtonControl, clicked), methodStr(DSPHTMLCommentUtility, expandAllCommentControls), this);
            btnCollaspAll.registerOverrideMethod(methodStr(FormButtonControl, clicked), methodStr(DSPHTMLCommentUtility, collaspAllCommentControls), this);
        }
    }

    /// <summary>
    /// Sets the userPreferredTimeZone property to the supplied value.
    /// </summary>
    /// <param name = "_userPreferredTimeZone">The supplied value</param>
    public void setUserPreferredTimeZone(Timezone _userPreferredTimeZone = DateTimeUtil::getUserPreferredTimeZone())
    {
        userPreferredTimeZone = _userPreferredTimeZone;
    }

    abstract public boolean isValidToView()
    {
    }

    public void viewComments(boolean _displayFirstComment = false)
    {
        if (commentHeaderGroupControl)
        {
            // Delete any possible previous comments to prepare the control group for the new comments.
            this.deleteCommentControls();

            if(this.isValidToView())
            {
                this.addButtons();
                Counter numberOfCommentControls = this.addCommentControls();

                if (numberOfCommentControls > 0)
                {
                    if (_displayFirstComment)
                    {
                        // A new comment was added, automatically expand the first comment group.
                        this.commentGroupControl_clicked();
                    }
                }
            }
        }
    }

    abstract DSPTmpHTMLComment getCommentsToDisplay()
    {
    }

    private Counter addCommentControls()
    {
        DSPTmpHTMLComment commentsToDisplay;
        FormGroupControl newCommentGroupControl;
        HtmlViewerControl newCommentHtmlViewerControl;
        utcdatetime localCommentDateTime;
        Counter  controlCounter = 0;
        commentControlsMap = new Map(Types::Integer, Types::Int64);

        commentsToDisplay = this.getCommentsToDisplay();

        while select * from commentsToDisplay
            order by commentsToDisplay.CommentCreatedDateTime desc
        {
            // Convert comment date and time to user local time zone.
            localCommentDateTime = DateTimeUtil::applyTimeZoneOffset(commentsToDisplay.CommentCreatedDateTime, userPreferredTimeZone);

            newCommentGroupControl = this.addCommentGroupControl(commentHeaderGroupControl, controlCounter);

            commentControlsMap.insert(newCommentGroupControl.id(), commentsToDisplay.CommentReference);

            // Display the comment header using the default date time (operating system) defaults.
            newCommentGroupControl.caption(strFmt('%1, %2', commentsToDisplay.CommentCreatedBy, DateTimeUtil::toFormattedStr(localCommentDateTime, -1, -1, -1, -1, -1, -1, -1, -1)));

            newCommentGroupControl.optionValue(0);

            newCommentHtmlViewerControl = this.addCommentHTMLViewerControl(newCommentGroupControl, controlCounter);
            newCommentHtmlViewerControl.parmHTML(commentsToDisplay.Comment);

            controlCounter++;
        }

        return controlCounter;
    }

    /// <summary>
    /// Add a collapsable group control for a hosting a commment.
    /// </summary>
    /// <param name = "_parentFormControl">The parent control for the group control</param>
    /// <param name = "_controlCounter">The unique instance of the group control</param>
    /// <returns>A FormGroupControl</returns>
    private FormGroupControl addCommentGroupControl(FormGroupControl _parentFormControl = commentHeaderGroupControl, Counter _controlCounter = 0)
    {
        // The control must have this name for the <c>CommentGroupControl_clicked</c> method override to work.
        FormGroupControl commentGroupControl;

        // Create the Group control for hosting the child controls required for the comment.
        commentGroupControl = _parentFormControl.addControl(FormControlType::Group, strfmt('%1_%2', commentGroupControlName, _controlCounter));
        commentGroupControl.autoDeclaration(true);
        commentGroupControl.heightMode(FormHeight::SizeToAvailable);
        commentGroupControl.widthMode(FormWidth::SizeToAvailable);
        commentGroupControl.frameOptionButton(FormFrameOptionButton::Hide);
        commentGroupControl.hideIfEmpty(true);
        commentGroupControl.expand(false);

        // Set the group control option button to expandable.
        commentGroupControl.optionValue(0);

        // Set the register override to allow for the "Clicked" events on the comment group control(s).
        commentGroupControl.registerOverrideMethod(methodStr(FormGroupControl, clicked),
                identifierStr('commentGroupControl_clicked'),
                this);

        return commentGroupControl;
    }

    /// <summary>
    /// Add the HtmlViewer control for viewing a comment.
    /// </summary>
    /// <param name = "_parentFormControl">The parent control for the Html viewer control</param>
    /// <param name = "_controlCounter">The unique instance of the viewer control</param>
    /// <returns>An HTMLViewerControl</returns>
    private HTMLViewerControl addCommentHTMLViewerControl(FormGroupControl _parentFormControl, Counter _controlCounter = 0)
    {
        HTMLViewerControl commentHTMLViewerControl;

        // Create the HTML viewer control for displaying the comment.
        commentHTMLViewerControl = _parentFormControl.addControlEx(classstr(HTMLViewerControl), strfmt('%1_%2', commentHTMLViewerControlName, _controlCounter));
        commentHTMLViewerControl.widthMode(FormWidth::SizeToAvailable);
        commentHTMLViewerControl.heightMode(FormHeight::SizeToAvailable);

        return commentHTMLViewerControl;
    }

    private void expandAllCommentControls(FormButtonControl _btnCtrl)
    {
        FormBuildControl parentFormBuildControl, parentFormBuildChildControl;
        FormControl formChildControl;
        FormDesign parentFormDesign = commentHeaderGroupControl.formRun().design();

        FormBuildDesign parentFormBuildDesign = commentHeaderGroupControl.formRun().form().design();
        parentFormBuildControl = parentFormBuildDesign.control(commentHeaderGroupControl.id());

        int childControlCount = parentFormBuildControl.controlCount()-2;

        for (int i = 1; i <= childControlCount; i++)
        {
            parentFormBuildChildControl = parentFormBuildControl.controlNum(i+2);
            formChildControl = parentFormDesign.control(parentFormBuildChildControl.id());
            
            if(formChildControl is FormGroupControl)
            {
                FormGroupControl formGroupControl = formChildControl;
                formGroupControl.expand(1);
                formGroupControl.optionValue(1);
            }
        }
    }

    private void collaspAllCommentControls(FormButtonControl _btnCtrl)
    {
        FormBuildControl parentFormBuildControl, parentFormBuildChildControl;
        FormControl formChildControl;
        FormDesign parentFormDesign = commentHeaderGroupControl.formRun().design();

        FormBuildDesign parentFormBuildDesign = commentHeaderGroupControl.formRun().form().design();
        parentFormBuildControl = parentFormBuildDesign.control(commentHeaderGroupControl.id());

        int childControlCount = parentFormBuildControl.controlCount()-2;

        for (int i = 1; i <= childControlCount; i++)
        {
            // Delete all of the comment group controls for the supplied parent control.
            parentFormBuildChildControl = parentFormBuildControl.controlNum(i+2);
            formChildControl = parentFormDesign.control(parentFormBuildChildControl.id());
            
            if(formChildControl is FormGroupControl)
            {
                FormGroupControl formGroupControl = formChildControl;
                formGroupControl.expand(0);
                formGroupControl.optionValue(0);
            }
        }
    }

    /// <summary>
    /// Removes any comment groups from the <c>commentHeaderGroupControl</c> form control.
    /// </summary>
    private void deleteCommentControls()
    {
        FormBuildControl parentFormBuildControl, parentFormBuildChildControl;
        FormControl formChildControl;
        FormDesign parentFormDesign = commentHeaderGroupControl.formRun().design();

        FormBuildDesign parentFormBuildDesign = commentHeaderGroupControl.formRun().form().design();
        parentFormBuildControl = parentFormBuildDesign.control(commentHeaderGroupControl.id());

        int childControlCount = parentFormBuildControl.controlCount();

        for (int i = 1; i <= childControlCount; i++)
        {
            // Delete all of the comment group controls for the supplied parent control.
            parentFormBuildChildControl = parentFormBuildControl.controlNum(1);

            if(parentFormBuildChildControl)
            {
                formChildControl = parentFormDesign.control(parentFormBuildChildControl.id());

                parentFormDesign.removeControl(formChildControl.id());
            }
        }
    }

    /// <summary>
    /// The <c>Clicked</c> event for all of the comment group controls.
    /// </summary>
    /// <param name="_commentGroupControl">
    /// The <c>FormGroupControl</c> that was clicked.
    /// </param>
    /// <remarks>
    /// Expands and displays the comment associated to the group control; otherwise contracts the group control.
    /// </remarks>
    public void commentGroupControl_clicked(FormGroupControl _commentGroupControl = commentHeaderGroupControl.controlNum(1))
    {
        FormGroupControl commentGroupControl = _commentGroupControl;

        selectedCommentControlId = commentGroupControl.id();

        // Expand or contract the Group control.
        commentGroupControl.expand(!commentGroupControl.expand());

        if (commentGroupControl.optionValue() != 0)
        {
            // Set the group control option button to expandable.
            commentGroupControl.optionValue(0);
        }
        else
        {
            // Set the group control option button to expanded.
            commentGroupControl.optionValue(1);
        }
    }

}

Comments

Popular posts from this blog

D365FO – AX – X++ –Refresh, Reread, Research, and ExecuteQuery

Create Inventory Journal through Code in D365FO X++

SalesLine Reservation in D365fo x++