Source code for ooodev.adapter.form.submission.submission_veto_listener

from __future__ import annotations
from typing import TYPE_CHECKING

import uno
from import XSubmissionVetoListener

from ooo.dyn.util.veto_exception import VetoException

from import CancelEventArgs
from import GenericArgs
from ooodev.adapter.adapter_base import AdapterBase

    from import EventObject
    from import XSubmission

[docs]class SubmissionVetoListener(AdapterBase, XSubmissionVetoListener): """ Is implement by components which want to observe (and probably veto) the submission of data. See Also: `API XSubmissionVetoListener <>`_ """
[docs] def __init__(self, trigger_args: GenericArgs | None = None, subscriber: XSubmission | None = None) -> None: """ Constructor Args: trigger_args (GenericArgs, Optional): Args that are passed to events when they are triggered. subscriber (XSubmission, optional): An UNO object that implements the ```` interface. If passed in then this listener instance is automatically added to it. """ super().__init__(trigger_args=trigger_args) if subscriber: subscriber.addSubmissionVetoListener(self)
[docs] def submitting(self, event: EventObject) -> None: """ Is invoked when a component, at which the listener has been registered, is about to submit its data. If event is canceled and the cancel args are not handled then a ``VetoException`` will be raised. Args: event (EventObject): Event data for the event. Raises: ``VetoException`` Returns: None: Note: When ``submitting`` event is invoked it will contain a :py:class:`` instance as the trigger event. When the event is triggered the ``CancelEventArgs.cancel`` can be set to ``True`` to cancel the submission. Also if canceled the ``CancelEventArgs.handled`` can be set to ``True`` to indicate that the submission should be performed. The ``CancelEventArgs.event_data`` will contain the original ```` that triggered the update. Also the ``CancelEventArgs`` can set a ``message`` value that will be used as the message for the ``VetoException``. If the ``event.set("skip_veto_exception", True)`` is set then the ``VetoException`` will not be raised. This is probably not a good idea but it is there if you need it. The following example shows how to use the ``CancelEventArgs`` to cancel the submission of data. .. code-block:: python def on_submitting(src: Any, event: CancelEventArgs) -> None: if not validate_data(): event.cancel = True event.set("message", "Canceling due to data validation fail.") """ # raise VetoException("VetoException", self, 0) cancel_args = CancelEventArgs(self.__class__.__qualname__) cancel_args.event_data = event cancel_args.set("message", "VetoException Raise due to CancelEventArgs") self._trigger_direct_event("submitting", cancel_args) if cancel_args.cancel: if not cancel_args.handled: # just in case raise a VetoException here is not the correct thing to do # then give an out. skip_veto_exception = cancel_args.get("skip_veto_exception", False) if skip_veto_exception: return # if the cancel event was handled then we return True to indicate that the submit should be performed msg = cancel_args.get("message", "VetoException Raise due to CancelEventArgs") raise VetoException(msg, self)
[docs] def disposing(self, event: EventObject) -> None: """ Gets called when the broadcaster is about to be disposed. All listeners and all other objects, which reference the broadcaster should release the reference to the source. No method should be invoked anymore on this object ( including ``XComponent.removeEventListener()`` ). This method is called for every listener registration of derived listener interfaced, not only for registrations at ``XComponent``. Args: event (EventObject): Event data for the event. Returns: None: """ # from self._trigger_event("disposing", event)