/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /************************************************************************* * * The Contents of this file are made available subject to the terms of * the BSD license. * * Copyright 2000, 2010 Oracle and/or its affiliates. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of Sun Microsystems, Inc. nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * *************************************************************************/ // Import everything we use import com.sun.star.beans.XPropertySet; import com.sun.star.beans.XMultiPropertySet; import com.sun.star.beans.XHierarchicalPropertySet; import com.sun.star.beans.XMultiHierarchicalPropertySet; import com.sun.star.beans.XPropertyState; import com.sun.star.beans.XMultiPropertyStates; import com.sun.star.configuration.XTemplateInstance; import com.sun.star.container.XNameAccess; import com.sun.star.container.XNameReplace; import com.sun.star.container.XNameContainer; import com.sun.star.container.XNamed; import com.sun.star.container.XChild; import com.sun.star.container.XHierarchicalNameAccess; import com.sun.star.container.XHierarchicalName; import com.sun.star.lang.XComponent; import com.sun.star.lang.XMultiComponentFactory; import com.sun.star.lang.XSingleServiceFactory; import com.sun.star.lang.XMultiServiceFactory; import com.sun.star.lang.XServiceInfo; import com.sun.star.lang.EventObject; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XComponentContext; import com.sun.star.uno.XInterface; import com.sun.star.uno.AnyConverter; import com.sun.star.util.XChangesBatch; import com.sun.star.util.XChangesNotifier; import com.sun.star.util.XChangesListener; import com.sun.star.util.ChangesEvent; // Config examples /* These examples show how to use the following features of the Config API: o Accessing data o Updating data o Updating properties in groups o Adding and removing items in sets o Resetting data to their defaults Each example is in a separate method call. */ public class ConfigExamples { // The ComponentContext interface of the remote component context private final XComponentContext mxContext; // The MultiComponentFactory interface of the ServiceManager private final XMultiComponentFactory mxServiceManager; // The MultiServiceFactory interface of the ConfigurationProvider private XMultiServiceFactory mxProvider = null; public static void main( String args[] ) { try { // get the remote office component context com.sun.star.uno.XComponentContext xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); if( xContext != null ) System.out.println("Connected to a running office ..."); else System.out.println( "ERROR: Cannot connect - no remote component context available." ); // Create an instance of the class and call its run method ConfigExamples aExample = new ConfigExamples(xContext); aExample.run( ); // if you own the service manager dispose it here // to ensure that the default provider is properly disposed and flushed System.exit(0); } catch( Exception e ) { e.printStackTrace(); System.exit(-1); } } /** Create a ConfigExamples instance supplying a service factory */ public ConfigExamples(XComponentContext xContext) { mxContext = xContext; mxServiceManager = xContext.getServiceManager(); } /** Run the examples with a default ConfigurationProvider */ public void run() throws com.sun.star.uno.Exception { mxProvider = createProvider(); runExamples( ); // we are using the default ConfigurationProvider, so we must not dispose it mxProvider = null; } /** Run the examples with a given ConfigurationProvider */ public void runExamples( ) { if (checkProvider(mxProvider)) { System.out.println("\nStarting examples."); readDataExample(); browseDataExample(); updateGroupExample(); resetGroupExample(); updateSetExample(); System.out.println("\nAll Examples completed."); } else System.out.println("ERROR: Cannot run examples without ConfigurationProvider."); } /** Do some simple checks, if there is a valid ConfigurationProvider */ public static boolean checkProvider(XMultiServiceFactory xProvider) { // check the provider we have if (xProvider == null) { System.out.println("No provider available. Cannot access configuration data."); return false; } try { // check the provider implementation XServiceInfo xProviderServices = UnoRuntime.queryInterface( XServiceInfo.class, xProvider ); if (xProviderServices == null || !xProviderServices.supportsService("com.sun.star.configuration.ConfigurationProvider")) { System.out.println("WARNING: The provider is not a com.sun.star.configuration.ConfigurationProvider"); } if (xProviderServices != null) { System.out.println("Using provider implementation: " + xProviderServices.getImplementationName()); } return true; } catch (com.sun.star.uno.RuntimeException e) { System.err.println("ERROR: Failure while checking the provider services."); e.printStackTrace(); return false; } } /** Get the provider we have */ public XMultiServiceFactory getProvider( ) { return mxProvider; } /** Create a default configuration provider */ public XMultiServiceFactory createProvider( ) throws com.sun.star.uno.Exception { final String sProviderService = "com.sun.star.configuration.ConfigurationProvider"; // create the provider and return it as a XMultiServiceFactory XMultiServiceFactory xProvider = UnoRuntime.queryInterface(XMultiServiceFactory.class, mxServiceManager.createInstanceWithContext(sProviderService, mxContext)); return xProvider; } /** Create a specified read-only configuration view */ public Object createConfigurationView( String sPath ) throws com.sun.star.uno.Exception { XMultiServiceFactory xProvider = getProvider(); // The service name: Need only read access: final String sReadOnlyView = "com.sun.star.configuration.ConfigurationAccess"; // creation arguments: nodepath com.sun.star.beans.PropertyValue aPathArgument = new com.sun.star.beans.PropertyValue(); aPathArgument.Name = "nodepath"; aPathArgument.Value = sPath; Object[] aArguments = new Object[1]; aArguments[0] = aPathArgument; // create the view Object xViewRoot = xProvider.createInstanceWithArguments(sReadOnlyView, aArguments); return xViewRoot; } /** Create a specified updatable configuration view */ Object createUpdatableView( String sPath ) throws com.sun.star.uno.Exception { XMultiServiceFactory xProvider = getProvider(); // The service name: Need update access: final String cUpdatableView = "com.sun.star.configuration.ConfigurationUpdateAccess"; // creation arguments: nodepath com.sun.star.beans.PropertyValue aPathArgument = new com.sun.star.beans.PropertyValue(); aPathArgument.Name = "nodepath"; aPathArgument.Value = sPath; Object[] aArguments = new Object[1]; aArguments[0] = aPathArgument; // create the view Object xViewRoot = xProvider.createInstanceWithArguments(cUpdatableView, aArguments); return xViewRoot; } /** This method demonstrates read access to data */ protected void readDataExample () { try { System.out.println("\n--- starting example: read grid option settings --------------------"); Object aData = readGridConfiguration( ); System.out.println("Read grid options: " + aData); } catch ( Exception e ) { e.printStackTrace(); } } /** This method demonstrates browsing access to data */ protected void browseDataExample () { try { System.out.println("\n--- starting example: browse filter configuration ------------------"); printRegisteredFilters( ); } catch ( Exception e ) { e.printStackTrace(); } } /** This method demonstrates update access to group data */ protected void updateGroupExample () { try { System.out.println("\n--- starting example: update group data --------------"); editGridOptions( ); } catch ( Exception e ) { e.printStackTrace(); } } /** This method demonstrates resetting data to its default state */ protected void resetGroupExample () { try { System.out.println("\n--- starting example: reset group data -----------------------------"); Object aOldData = readGridConfiguration( ); resetGridConfiguration( ); Object aNewData = readGridConfiguration( ); System.out.println("Before reset: user grid options: " + aOldData); System.out.println("After reset: default grid options: " + aNewData); } catch ( Exception e ) { e.printStackTrace(); } } /** This method demonstrates update access to set data */ protected void updateSetExample () { try { System.out.println("\n--- starting example: update set data ---------------"); storeSampleDataSource( ); } catch ( Exception e ) { e.printStackTrace(); } } // READ example /// class to hold information about grid settings private static class GridOptions { private boolean visible; private int resolution_x; private int resolution_y; private int subdivision_x; private int subdivision_y; @Override public String toString() { StringBuffer aBuffer = new StringBuffer(); aBuffer.append("[ Grid is "); aBuffer.append(visible ? "VISIBLE" : "HIDDEN"); aBuffer.append("; resolution = (" + resolution_x + "," + resolution_y + ")"); aBuffer.append("; subdivision = (" + subdivision_x + "," + subdivision_y + ")"); aBuffer.append(" ]"); return aBuffer.toString(); } } /// This method reads information about grid settings protected GridOptions readGridConfiguration() throws com.sun.star.uno.Exception { // The path to the root element final String cGridOptionsPath = "/org.openoffice.Office.Calc/Grid"; // create the view Object xViewRoot = createConfigurationView(cGridOptionsPath); // the result structure GridOptions options = new GridOptions(); // accessing a single nested value XHierarchicalPropertySet xProperties = UnoRuntime.queryInterface(XHierarchicalPropertySet.class, xViewRoot); Object aVisible = xProperties.getHierarchicalPropertyValue("Option/VisibleGrid"); options.visible = ((Boolean) aVisible).booleanValue(); // accessing a nested object and its subproperties Object xSubdivision = xProperties.getHierarchicalPropertyValue("Subdivision"); XMultiPropertySet xSubdivProperties = UnoRuntime.queryInterface(XMultiPropertySet.class, xSubdivision); // variables for multi-element access String[] aElementNames = new String[] { "XAxis", "YAxis" }; Object[] aElementValues = xSubdivProperties.getPropertyValues(aElementNames); options.subdivision_x = ((Integer) aElementValues[0]).intValue(); options.subdivision_y = ((Integer) aElementValues[1]).intValue(); // accessing deeply nested subproperties Object xResolution = xProperties.getHierarchicalPropertyValue("Resolution"); XMultiHierarchicalPropertySet xResolutionProperties = UnoRuntime.queryInterface(XMultiHierarchicalPropertySet.class, xResolution); aElementNames[0] = "XAxis/Metric"; aElementNames[1] = "YAxis/Metric"; aElementValues = xResolutionProperties.getHierarchicalPropertyValues(aElementNames); options.resolution_x = ((Integer) aElementValues[0]).intValue(); options.resolution_y = ((Integer) aElementValues[1]).intValue(); // all options have been retrieved - clean up and return // we are done with the view - dispose it UnoRuntime.queryInterface(XComponent.class, xViewRoot).dispose(); return options; } // BROWSE example /// Interface to process information when browsing the configuration tree public interface IConfigurationProcessor { /// process a value item void processValueElement( String sPath_, Object aValue_ ); /// process a structural item void processStructuralElement( String sPath_, XInterface xElement_); } /// Internal method to recursively browse a structural element in preorder public void browseElementRecursively( XInterface xElement, IConfigurationProcessor aProcessor ) throws com.sun.star.uno.Exception { // First process this as an element (preorder traversal) XHierarchicalName xElementPath = UnoRuntime.queryInterface(XHierarchicalName.class, xElement); String sPath = xElementPath.getHierarchicalName(); aProcessor.processStructuralElement( sPath, xElement); // now process this as a container XNameAccess xChildAccess = UnoRuntime.queryInterface(XNameAccess.class, xElement); // get a list of child elements String[] aElementNames = xChildAccess.getElementNames(); // and process them one by one for(int i=0; i< aElementNames.length; ++i) { Object aChild = xChildAccess.getByName( aElementNames[i] ); // is it a structural element (object) ... if ( AnyConverter.isObject(aChild) && !AnyConverter.isArray(aChild) ) { // then get an interface XInterface xChildElement = UnoRuntime.queryInterface(XInterface.class, aChild); // and continue processing child elements recursively browseElementRecursively( xChildElement, aProcessor ); } // ... or is it a simple value else { // Build the path to it from the path of // the element and the name of the child String sChildPath; sChildPath = xElementPath.composeHierarchicalName(aElementNames[i]); // and process the value aProcessor.processValueElement( sChildPath, aChild ); } } } /** Method to browse the part rooted at sRootPath of the configuration that the Provider provides. All nodes will be processed by the IConfigurationProcessor passed. */ public void browseConfiguration( String sRootPath, IConfigurationProcessor aProcessor ) throws com.sun.star.uno.Exception { // create the root element XInterface xViewRoot = (XInterface)createConfigurationView( sRootPath ); // now do the processing browseElementRecursively( xViewRoot, aProcessor ); // we are done with the view - dispose it // This assumes that the processor // does not keep a reference to the elements in processStructuralElement UnoRuntime.queryInterface(XComponent.class,xViewRoot).dispose(); xViewRoot = null; } /** Method to browse the filter configuration. Information about installed filters will be printed. */ public void printRegisteredFilters() throws com.sun.star.uno.Exception { final String sFilterKey = "/org.openoffice.TypeDetection.Filter/Filters"; // browse the configuration, dumping filter information browseConfiguration( sFilterKey, new IConfigurationProcessor () { /// prints Path and Value of properties public void processValueElement( String sPath_, Object aValue_ ) { if (AnyConverter.isArray(aValue_)) { final Object [] aArray = (Object [])aValue_; System.out.print("\tValue: " + sPath_ + " = { "); for (int i=0; i