OO-Snippets: Get the Application Name of a document

Commons

Keywordsapplication name, XComponent, com.sun.star.text.TextDocument
LanguageJava
ApplicationOffice
AuthorsTobias Krais (www.design-to-use.de) (initial)
Supported Versions2.0.x  1.1.x  
Supported OSAll  
QuestionHow to get the Application Name of a document?
Answer

Code-Snippet-Listing (snippet-source)

import com.sun.star.frame.XModuleManager;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public class Snippets {
    /**
     * Component context to be passed to a component via
     * com.sun.star.lang.XSingleComponentFactory . Arbitrary values
     * (e.g. deployment values) can be retrieved from the context.
     * 
     * How to get this Object, see ../Office/Office.BootstrapOpenOffice.snip
     * or ../Office/Office.ConnectToListeningOpenOffice.snip
     */
    private static XComponentContext xComponentContext;
    
    /**
     * Factory interface for creating component instances giving a context from
     * which to retrieve deployment values.
     * 
     * How to get this Object, see ../Office/Office.BootstrapOpenOffice.snip
     * or ../Office/Office.ConnectToListeningOpenOffice.snip
     */
    private static XMultiComponentFactory xMCF; 

	
	/**
     * Get the application name of a document. E.g. for a writer document
     * "com.sun.star.text.TextDocument"
     * 
     * @param myXComponent UNO Representativ of the opened document.
     * @return The OpenOffice application name. It looks like
     * "com.sun.star.text.TextDocument".
     */
    public static String getApplicationName(XComponent myXComponent) {
        XModuleManager xMM = null;
        try {
            xMM = (XModuleManager)UnoRuntime.queryInterface(
                    XModuleManager.class,
                    xMCF.createInstanceWithContext(
                            "com.sun.star.frame.ModuleManager",
                            xComponentContext));
        }
        catch (com.sun.star.uno.Exception e) {
            return null;
        }
        
        String sOOoApp = null;
        try{
            // Getting the application name of the document,
            // e.g. "com.sun.star.text.TextDocument" for writer
            sOOoApp = xMM.identify(myXComponent);
        }
        catch(com.sun.star.uno.Exception e)
        {
            return null;
        }
        return sOOoApp;
    }
}

Changelog

DateUserModification
2006-12-11tobiaskraisInitial version

and