[Solved] How to access general settings from Java

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
johnrdorazio
Posts: 35
Joined: Sun Aug 17, 2014 2:04 pm
Location: Italy

[Solved] How to access general settings from Java

Post 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?
Last edited by johnrdorazio on Fri Nov 06, 2020 12:39 am, edited 1 time in total.
OpenOffice 4.1.6, NetBeans 8.1, Windows 10 x64 Home
johnrdorazio
Posts: 35
Joined: Sun Aug 17, 2014 2:04 pm
Location: Italy

Re: How to access general settings from Java

Post 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?
OpenOffice 4.1.6, NetBeans 8.1, Windows 10 x64 Home
JeJe
Volunteer
Posts: 2784
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to access general settings from Java

Post 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
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
johnrdorazio
Posts: 35
Joined: Sun Aug 17, 2014 2:04 pm
Location: Italy

Re: How to access general settings from Java

Post 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);
                }
OpenOffice 4.1.6, NetBeans 8.1, Windows 10 x64 Home
JeJe
Volunteer
Posts: 2784
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to access general settings from Java

Post 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.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply