Close a Document: XCloseable


As of OOo 1.1.0, each document type supports the com.sun.star.util.XCloseable interface. To close these objects, call the object method close(bForce). If bForce is True, the object must close. In other words, it may not refuse to close. If, however, bForce is False, the object may refuse to close.

Typically it isn't the document that refuses to close, but an event listener that vetoes the request. Before a document is closed, a message is sent to all registered listeners, giving them an opportunity to prevent the closure. If the close request is not vetoed by any listener, a message is sent to each registered listener, advising them that the document will close. The interface com.sun.star.util.XCloseBroadcaster provides this capability (see Table 5 ).

Table 5: Object methods defined by the interface XCloseBroadcaster.

Object Method

Description

addCloseListener(XCloseListener)

Add listener to receive or have a veto for "close" events.

removeCloseListener(XCloseListener)

Remove an object registered as a close listener using addCloseListener().

The macro in Listing 9 demonstrates the safe way to close a document using the object's close() method. For versions of OOo prior to 1.1.0, the close() method is not available, so you must use the dispose() method instead. The dispose() method is unconditional and is not the preferred method to close a document because it doesn't allow a registered user who might be using the document to veto the close and finish what he or she is doing.

Listing 9: The safe way to close a document.
start example
 If HasUnoInterfaces(oDoc, "com.sun.star.util.XCloseable") Then   oDoc.close(true) Else   oDoc.dispose() End If 
end example
 
Warning  

The dispose() method exists for legacy purposes only. Do not use it. Assume, for example, that you start printing a document and then immediately dispose() the document. The document that is being used for printing is suddenly gone and your program crashes.




OpenOffice.org Macros Explained
OpenOffice.org Macros Explained
ISBN: 1930919514
EAN: 2147483647
Year: 2004
Pages: 203

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net