Search in sources :

Example 1 with XComponentContext

use of com.sun.star.uno.XComponentContext in project translationstudio8 by heartsome.

the class TerminationOpenoffice method closeOpenoffice.

// public static void main(String[] args) {
/**
	 * Close openoffice.
	 * @param port
	 *            the port
	 */
public static void closeOpenoffice(String port) {
    XComponentContext xRemoteContext = null;
    XMultiComponentFactory xRemoteServiceManager = null;
    XDesktop xDesktop = null;
    try {
        XComponentContext xLocalContext = com.sun.star.comp.helper.Bootstrap.createInitialComponentContext(null);
        XMultiComponentFactory xLocalServiceManager = xLocalContext.getServiceManager();
        Object urlResolver = xLocalServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", //$NON-NLS-1$
        xLocalContext);
        XUnoUrlResolver xUnoUrlResolver = (XUnoUrlResolver) UnoRuntime.queryInterface(XUnoUrlResolver.class, urlResolver);
        Object initialObject = xUnoUrlResolver.resolve(//$NON-NLS-1$ //$NON-NLS-2$
        "uno:socket,host=localhost,port=" + port + ";urp;StarOffice.ServiceManager");
        // Object initialObject =
        // xUnoUrlResolver.resolve("uno:pipe,name=my_app;urp;StarOffice.ServiceManager"
        // );
        XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, initialObject);
        //$NON-NLS-1$
        Object context = xPropertySet.getPropertyValue("DefaultContext");
        xRemoteContext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, context);
        xRemoteServiceManager = xRemoteContext.getServiceManager();
        // get Desktop instance
        Object desktop = xRemoteServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", //$NON-NLS-1$
        xRemoteContext);
        xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktop);
        TerminateListener terminateListener = new TerminateListener();
        xDesktop.addTerminateListener(terminateListener);
        // try to terminate while we are at work
        atWork = true;
        boolean terminated = xDesktop.terminate();
        System.out.println(//$NON-NLS-1$
        "The Office " + //$NON-NLS-1$ //$NON-NLS-2$
        (terminated ? "has been terminated" : "is still running, we are at work"));
        // no longer at work
        atWork = false;
        // once more: try to terminate
        terminated = xDesktop.terminate();
        System.out.println(//$NON-NLS-1$
        "The Office " + (//$NON-NLS-1$
        terminated ? //$NON-NLS-1$
        "has been terminated" : //$NON-NLS-1$
        "is still running. Someone else prevents termination, e.g. the quickstarter"));
    } catch (java.lang.Exception e) {
        if (Converter.DEBUG_MODE) {
            e.printStackTrace();
        }
    }
}
Also used : XPropertySet(com.sun.star.beans.XPropertySet) XComponentContext(com.sun.star.uno.XComponentContext) XDesktop(com.sun.star.frame.XDesktop) XMultiComponentFactory(com.sun.star.lang.XMultiComponentFactory) XUnoUrlResolver(com.sun.star.bridge.XUnoUrlResolver)

Example 2 with XComponentContext

use of com.sun.star.uno.XComponentContext in project translationstudio8 by heartsome.

the class OPConnection method connect.

/**
	 * (non-Javadoc)
	 * @see net.heartsome.cat.converter.ooconnect.OPconnect#connect()
	 * @throws ConnectException
	 */
public void connect() throws ConnectException {
    try {
        XComponentContext localContext;
        localContext = Bootstrap.createInitialComponentContext(null);
        XMultiComponentFactory localServiceManager = localContext.getServiceManager();
        XConnector connector = (XConnector) UnoRuntime.queryInterface(XConnector.class, localServiceManager.createInstanceWithContext("com.sun.star.connection.Connector", //$NON-NLS-1$
        localContext));
        XConnection connection = connector.connect(strConnection);
        XBridgeFactory bridgeFactory = (XBridgeFactory) UnoRuntime.queryInterface(XBridgeFactory.class, //$NON-NLS-1$
        localServiceManager.createInstanceWithContext("com.sun.star.bridge.BridgeFactory", localContext));
        //$NON-NLS-1$ //$NON-NLS-2$
        bridge = bridgeFactory.createBridge("ms2ooBridge", "urp", connection, null);
        bgComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, bridge);
        // bgComponent.addEventListener(this);
        serviceMg = (XMultiComponentFactory) UnoRuntime.queryInterface(XMultiComponentFactory.class, bridge.getInstance(//$NON-NLS-1$
        "StarOffice.ServiceManager"));
        XPropertySet properties = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, serviceMg);
        componentContext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, properties.getPropertyValue(//$NON-NLS-1$
        "DefaultContext"));
        connected = true;
        if (connected) {
            //$NON-NLS-1$
            System.out.println("has already connected");
        } else {
            //$NON-NLS-1$
            System.out.println("connect to Openoffice fail,please check OpenOffice service that have to open");
        }
    } catch (NoConnectException connectException) {
        //$NON-NLS-1$ //$NON-NLS-2$
        throw new ConnectException(MessageFormat.format(Messages.getString("ooconnect.OPConnection.msg"), strConnection + ": " + connectException.getMessage()));
    } catch (Exception exception) {
        //$NON-NLS-1$
        throw new OPException(MessageFormat.format(Messages.getString("ooconnect.OPConnection.msg"), strConnection), exception);
    } catch (java.lang.Exception e) {
        if (Converter.DEBUG_MODE) {
            e.printStackTrace();
        }
    }
}
Also used : XPropertySet(com.sun.star.beans.XPropertySet) XComponentContext(com.sun.star.uno.XComponentContext) NoConnectException(com.sun.star.connection.NoConnectException) XConnection(com.sun.star.connection.XConnection) XBridgeFactory(com.sun.star.bridge.XBridgeFactory) XMultiComponentFactory(com.sun.star.lang.XMultiComponentFactory) Exception(com.sun.star.uno.Exception) ConnectException(java.net.ConnectException) NoConnectException(com.sun.star.connection.NoConnectException) XConnector(com.sun.star.connection.XConnector) ConnectException(java.net.ConnectException) NoConnectException(com.sun.star.connection.NoConnectException)

Example 3 with XComponentContext

use of com.sun.star.uno.XComponentContext in project translationstudio8 by heartsome.

the class TerminationOpenoffice method getCurrentComponent.

/**
	 * Gets the current component.
	 * @return the current component
	 * @throws Exception
	 *             the exception
	 */
public static XComponent getCurrentComponent() throws Exception {
    XComponentContext xRemoteContext = com.sun.star.comp.helper.Bootstrap.createInitialComponentContext(null);
    // XComponentContext xRemoteContext =
    // com.sun.star.comp.helper.Bootstrap.bootstrap();
    XMultiComponentFactory xRemoteServiceManager = xRemoteContext.getServiceManager();
    //$NON-NLS-1$
    Object desktop = xRemoteServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", xRemoteContext);
    XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktop);
    XComponent currentComponent = xDesktop.getCurrentComponent();
    return currentComponent;
}
Also used : XComponentContext(com.sun.star.uno.XComponentContext) XComponent(com.sun.star.lang.XComponent) XDesktop(com.sun.star.frame.XDesktop) XMultiComponentFactory(com.sun.star.lang.XMultiComponentFactory)

Example 4 with XComponentContext

use of com.sun.star.uno.XComponentContext in project jabref by JabRef.

the class OOBibBase method simpleBootstrap.

private XDesktop simpleBootstrap(String pathToExecutable) throws IllegalAccessException, InvocationTargetException, BootstrapException, CreationException, IOException {
    ClassLoader loader = ClassLoader.getSystemClassLoader();
    if (loader instanceof URLClassLoader) {
        URLClassLoader cl = (URLClassLoader) loader;
        Class<URLClassLoader> sysclass = URLClassLoader.class;
        try {
            Method method = sysclass.getDeclaredMethod("addURL", URL.class);
            method.setAccessible(true);
            method.invoke(cl, new File(pathToExecutable).toURI().toURL());
        } catch (SecurityException | NoSuchMethodException | MalformedURLException t) {
            LOGGER.error("Error, could not add URL to system classloader", t);
            cl.close();
            throw new IOException("Error, could not add URL to system classloader", t);
        }
    } else {
        LOGGER.error("Error occured, URLClassLoader expected but " + loader.getClass() + " received. Could not continue.");
    }
    //Get the office component context:
    XComponentContext xContext = Bootstrap.bootstrap();
    //Get the office service manager:
    XMultiComponentFactory xServiceManager = xContext.getServiceManager();
    //Create the desktop, which is the root frame of the
    //hierarchy of frames that contain viewable components:
    Object desktop;
    try {
        desktop = xServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", xContext);
    } catch (Exception e) {
        throw new CreationException(e.getMessage());
    }
    XDesktop resultDesktop = UnoRuntime.queryInterface(XDesktop.class, desktop);
    UnoRuntime.queryInterface(XComponentLoader.class, desktop);
    return resultDesktop;
}
Also used : MalformedURLException(java.net.MalformedURLException) Method(java.lang.reflect.Method) IOException(java.io.IOException) XDesktop(com.sun.star.frame.XDesktop) XMultiComponentFactory(com.sun.star.lang.XMultiComponentFactory) UnknownPropertyException(com.sun.star.beans.UnknownPropertyException) PropertyVetoException(com.sun.star.beans.PropertyVetoException) IllegalArgumentException(com.sun.star.lang.IllegalArgumentException) IllegalTypeException(com.sun.star.beans.IllegalTypeException) InvocationTargetException(java.lang.reflect.InvocationTargetException) WrappedTargetException(com.sun.star.lang.WrappedTargetException) BootstrapException(com.sun.star.comp.helper.BootstrapException) DisposedException(com.sun.star.lang.DisposedException) NoSuchElementException(com.sun.star.container.NoSuchElementException) MalformedURLException(java.net.MalformedURLException) UndefinedParagraphFormatException(org.jabref.logic.openoffice.UndefinedParagraphFormatException) IOException(java.io.IOException) NotRemoveableException(com.sun.star.beans.NotRemoveableException) PropertyExistException(com.sun.star.beans.PropertyExistException) XComponentContext(com.sun.star.uno.XComponentContext) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) File(java.io.File)

Aggregations

XMultiComponentFactory (com.sun.star.lang.XMultiComponentFactory)4 XComponentContext (com.sun.star.uno.XComponentContext)4 XDesktop (com.sun.star.frame.XDesktop)3 XPropertySet (com.sun.star.beans.XPropertySet)2 IllegalTypeException (com.sun.star.beans.IllegalTypeException)1 NotRemoveableException (com.sun.star.beans.NotRemoveableException)1 PropertyExistException (com.sun.star.beans.PropertyExistException)1 PropertyVetoException (com.sun.star.beans.PropertyVetoException)1 UnknownPropertyException (com.sun.star.beans.UnknownPropertyException)1 XBridgeFactory (com.sun.star.bridge.XBridgeFactory)1 XUnoUrlResolver (com.sun.star.bridge.XUnoUrlResolver)1 BootstrapException (com.sun.star.comp.helper.BootstrapException)1 NoConnectException (com.sun.star.connection.NoConnectException)1 XConnection (com.sun.star.connection.XConnection)1 XConnector (com.sun.star.connection.XConnector)1 NoSuchElementException (com.sun.star.container.NoSuchElementException)1 DisposedException (com.sun.star.lang.DisposedException)1 IllegalArgumentException (com.sun.star.lang.IllegalArgumentException)1 WrappedTargetException (com.sun.star.lang.WrappedTargetException)1 XComponent (com.sun.star.lang.XComponent)1