summaryrefslogtreecommitdiff
path: root/odk/examples/DevelopersGuide/ScriptingFramework/SayHello/SayHello/SayHello.java
blob: b120c4cfa9f9829df5fed1f7444795a65cf72667 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import com.sun.star.script.provider.XScriptContext;

import com.sun.star.uno.UnoRuntime;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.lang.XComponent;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextRange;
import com.sun.star.text.XText;
import com.sun.star.beans.PropertyValue;
import com.sun.star.awt.ActionEvent;

public class SayHello
{
    public static void start(XScriptContext ctxt, ActionEvent e)
        throws Exception
    {
        SayHello.start(ctxt);
    }

    public static void start(XScriptContext ctxt)
        throws Exception
    {
        // getting the text document object
        XTextDocument xTextDocument = createDocument(ctxt);

        XText xText = xTextDocument.getText();
        XTextRange xTextRange = xText.getEnd();
        xTextRange.setString("Hello");
    }

    private static XTextDocument createDocument(XScriptContext ctxt)
        throws Exception
    {
        XComponentLoader loader = (XComponentLoader)
            UnoRuntime.queryInterface(
                XComponentLoader.class, ctxt.getDesktop());

        XComponent comp = loader.loadComponentFromURL(
            "private:factory/swriter", "_blank", 4, new PropertyValue[0]);

        XTextDocument doc = (XTextDocument)
            UnoRuntime.queryInterface( XTextDocument.class, comp);

        return doc;
    }
}