OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

How to change default fonts programatically?

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
Amit Batra
Guest





PostPosted: Sun Dec 14, 2003 8:54 pm    Post subject: How to change default fonts programatically? Reply with quote

Hi,

I want to change the default fonts of the currently opened document in OpenOffice programatically in Java. The same options are available through the menu option "Tools -> Options -> Text Documents -> Basic Fonts -> Defaults" in OpenOffice 1.1.0.

Can you point me to the right API for the same?

-Amit
Back to top
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Mon Dec 15, 2003 10:45 am    Post subject: Reply with quote

There are two possible answers, depending on which direction you want to go.

One possible answer, depending on how one would interpret your question, is that you want to change the Font of the Default style for a currently open document. The gist of the answer would be: get the style families of the doc, then get the style family "paragraphs" (or something like that), and then get the Default style, then change its font. I'm sure one of our Writer guru's could give a more detailed answer.

Another possible answer is that you actually want to change the setting of something that is controlled by Tools --> Options. For this, you need to make a change to the Configuration Manager. I don't have a canned answer for you, but I can point you to places to do some research.....
* The Configuration Manager chapter of the Developer's Guide.
* My post titled: ENLARGE THE SIZE OF YOUR recent files list
http://www.oooforum.org/forum/viewtopic.php?t=3559
* My post titled: FYI...new macro to make OOo listen for UNO connections
http://www.oooforum.org/forum/viewtopic.php?t=3754
* My post titled: Development of a Basic Macro Installer
http://www.oooforum.org/forum/viewtopic.php?t=4255
* My post titled: Musings about the Configuration Manager
http://www.oooforum.org/forum/viewtopic.php?t=4028

By studying the source code of these macros, and the XCU and XCS files in OOo (mentioned in above postings), you can figure out how to change a configuration setting that is kept under Tools --> Options. The "Recent Files List" and "Uno Connection Listener" posts are examples of code that make config. manager changes which the user can NOT make from Tools --> Options.

If I had time, I would go off and produce complete answers to both of the above outlined approaches, as I have done in the past.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
Amit Batra
Guest





PostPosted: Tue Dec 16, 2003 7:36 am    Post subject: Code executes, but nothing happens. Reply with quote

Hi Danny,

Thanks for your advice. I wrote some code to change the configuration settings programatically and verified that it executes in the debugger as desired. Also, if I open the corresponding option in the GUI (Tools->Options), I am able to see the changed settings. But still, it does NOT produce the desired effect in the currently opened document (resetting the fonts to their default values). Here is the code. Can you suggest what could be wrong?

Code:

/**
 * Sets the Default Fonts for this Document
 */
private void setDefaultFonts(Object loadedDocument, XMultiComponentFactory xMultiComponentFactory) throws Exception
{
    XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(
        XTextDocument.class,
        loadedDocument
    );
    if (xTextDocument == null)
        return;

    XMultiServiceFactory xServiceManager = (XMultiServiceFactory) UnoRuntime.queryInterface(
        XMultiServiceFactory.class,
        xMultiComponentFactory
    );

    final String sProviderService = "com.sun.star.configuration.ConfigurationProvider";
    XMultiServiceFactory xProvider = (XMultiServiceFactory) UnoRuntime.queryInterface(
        XMultiServiceFactory.class,
        xServiceManager.createInstance(sProviderService)
    );

    final String cFontSettingsPath = "/org.openoffice.Office.Writer/DefaultFont";
    PropertyValue aPathArgument = new PropertyValue();
    aPathArgument.Name = "nodepath";
    aPathArgument.Value = cFontSettingsPath;

    final String cUpdatableView = "com.sun.star.configuration.ConfigurationUpdateAccess";
    Object xViewRoot = xProvider.createInstanceWithArguments(
        cUpdatableView,
        new Object[] {
            aPathArgument
        }
    );
   
    XNameAccess xChildValues = (XNameAccess) UnoRuntime.queryInterface(
        XNameAccess.class,
        xViewRoot
    );
    String[] elementNames = xChildValues.getElementNames();
    for (int count = 0; count < elementNames.length; ++count)
        System.out.println(elementNames[count] + ": " + xChildValues.getByName(elementNames[count]));

    XMultiPropertySet xChildProperties = (XMultiPropertySet) UnoRuntime.queryInterface(
        XMultiPropertySet.class,
        xViewRoot
    );

    String[] propertyNames = {
        "Standard",
        "Heading",
        "List",
        "Caption",
        "Index",
        "Document"
    };

    Object[] propertyValues = {
        "Times New Roman",
        "Arial",
        "Times New Roman",
        "Times New Roman",
        "Times New Roman",
        Boolean.TRUE
    };

    xChildProperties.setPropertyValues(propertyNames, propertyValues);

    XChangesBatch xChangesBatch = (XChangesBatch) UnoRuntime.queryInterface(
        XChangesBatch.class,
        xViewRoot
    );
    xChangesBatch.commitChanges();
   
    XComponent xComponent = (XComponent) UnoRuntime.queryInterface(
        XComponent.class,
        xViewRoot
    );
    xComponent.dispose();
}
Back to top
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Tue Dec 16, 2003 4:11 pm    Post subject: Reply with quote

Wow!

First of all, it is obvious that you understand the Configuration Manager API. And in Java no less!

I adapted your code to Basic. And tried it. I also can confirm that you are editing the right configuration node.

As you said, you can go to Tools --> Options and see that it takes effect.

In my experience, with the documents I posted, such as the one to increase the size of the recent files list, the instructions I give the user for running it are to Quit out of OOo. The reason for this is because, in my experience, even when calling commitChanges, the changes aren't seen by OOo until you launch the program again. I have even tried the lazyWrite property to False.

I did notice that you are disposing of the xViewRoot at the end. I cannot say whether you should or should not do this. (I mostly dabble with OOo in Basic and Python.)

Based on my experience with updating the configuration manager, I would not expect for OOo to see the changes until after the next time that you launch it. Somehow when configuration changes are made interactively via. the UI, such as when picking Tools --> Options, something else internally must happen that instantly propagates the changes not only into the configuration manager, but somehow gets OOo to "see" that these changes have been made.

In short, if you find a solution to your problem, it would also be a solution to my problem. If I find a solution, I will no doubt post it, and I will try to remember to post a followup to This thread. In any event, it would probably appear on one of my threads that I linked to above, such as the Development of a Basic Macro Installer, or Musings on the Configuration Manager.

I am currently using the Configuration Manager to install AddOn menus, Top Level menus, and Toolbar icons into OOo. It would be great if these could show up immediately without having to re-launch OOo.

Sorry I don't have better news.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group