Page 1 of 1

[Solved] How to access general settings from Java

Posted: Wed Jul 08, 2020 11:19 pm
by johnrdorazio
Can an add-on written in Java access Writer general settings?
In particular I need to access the current measurement unit set for the ruler, to know if for example it is in CM or Inches or MM...
This option can be set in the menu Tools -> Options -> OpenOffice Writer -> General -> Settings -> Measurement unit .
It can also be set by right clicking on the ruler itself. In this case, I have noticed that any change in the measurement unit by right-clicking on the ruler does not change the unit set in the above menu option, however any change in the menu option will be reflected on the ruler and it's context menu that pops up when right-clicking the ruler.
I wonder if these two paths should be better synchronized so that any change in one will be reflected in the other.
But in any case, how would this setting be accessed from a Java add-on?

Re: How to access general settings from Java

Posted: Fri Oct 23, 2020 10:44 pm
by johnrdorazio
After doing a bit of research in the OpenOffice codebase on github, I have made some progress and I am able to retrieve the current Measurement Unit when opening Writer, using this code:

Code: Select all

PropertyValue[] lArgs    = new PropertyValue[1]; 
lArgs[0] = new PropertyValue();
lArgs[0].Name = "nodepath";
lArgs[0].Value = "/org.openoffice.Office.Writer/Layout/Other";
Object configAccess =  xConfigurationServiceFactory.createInstanceWithArguments( 
    "com.sun.star.configuration.ConfigurationAccess",lArgs); 
XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface(XNameAccess.class, configAccess);
                    
//String[] elNames = xNameAccess.getElementNames();
//System.out.println("elNames = " + Arrays.toString(elNames));
//System.out.println(xNameAccess1.getByName("MeasureUnit").toString());
                    /*
                        MM = 1
                        CM = 2
                        INCHES = 8
                        PICA = 7
                        POINT = 6
                    */
int mUnit = Integer.parseInt(xNameAccess.getByName("MeasureUnit").toString());
I've also made my own enum in order to help interpret the number returned as MM, CM, INCHES etc.

Now I would like to take the next step, and listen on changes to the unit measurement in the ruler, when a user right-clicks the ruler to change the unit of measurement. Does anyone have a heads up on how to accomplish this?

Re: How to access general settings from Java

Posted: Sat Oct 24, 2020 12:36 am
by JeJe
In Basic as I don't know Java, this gives that information.

Code: Select all

thiscomponent.currentcontroller.getviewsettings.VerticalRulerMetric or .HorizontalRulerMetric 

You may be able to use

Code: Select all

thiscomponent.currentcontroller.getviewsettings.addPropertyChangeListener
to get notification of changes - I don't know.

Edit: correction to second line of code, added getviewsettings

Re: How to access general settings from Java

Posted: Mon Oct 26, 2020 8:46 am
by johnrdorazio
@Jeje would you be able to tell me what type that property is returning? I'm guessing it's a boolean? Or does it return the same integers as in my code above?

Code: Select all

  MM = 1
  CM = 2
  INCHES = 8
  PICA = 7
  POINT = 6
UPDATE: I was able to access the property and determine the type in Java. It's determined to be a long value, I'm using AnyConverter to cast to int without any trouble. The values correspond with the above table.

Code: Select all

                try {
                    XViewSettingsSupplier xViewSettings = (XViewSettingsSupplier) UnoRuntime.queryInterface(XViewSettingsSupplier.class,instance.m_xController);
                    XPropertySet viewSettings=xViewSettings.getViewSettings();
                    System.out.println("I got the HorizontalRulerMetric property, here is the type :" + AnyConverter.getType(viewSettings.getPropertyValue("HorizontalRulerMetric")));
                    System.out.println("And here is the value: " + AnyConverter.toInt(viewSettings.getPropertyValue("HorizontalRulerMetric")));
                } catch (UnknownPropertyException ex) {
                    System.out.println(ex);
                } catch (IllegalArgumentException | Exception ex) {
                    System.out.println(ex);
                }

Re: How to access general settings from Java

Posted: Mon Oct 26, 2020 11:59 am
by JeJe
The API constants are here:

http://www.openoffice.org/api/docs/comm ... dUnit.html

I usually just put Openoffice and the search term, HorizontalRulerMetric into a search engine.

Only some of the constants can be set.