1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * The Contents of this file are made available subject to the terms of 4*cdf0e10cSrcweir * the BSD license. 5*cdf0e10cSrcweir * 6*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 7*cdf0e10cSrcweir * All rights reserved. 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * Redistribution and use in source and binary forms, with or without 10*cdf0e10cSrcweir * modification, are permitted provided that the following conditions 11*cdf0e10cSrcweir * are met: 12*cdf0e10cSrcweir * 1. Redistributions of source code must retain the above copyright 13*cdf0e10cSrcweir * notice, this list of conditions and the following disclaimer. 14*cdf0e10cSrcweir * 2. Redistributions in binary form must reproduce the above copyright 15*cdf0e10cSrcweir * notice, this list of conditions and the following disclaimer in the 16*cdf0e10cSrcweir * documentation and/or other materials provided with the distribution. 17*cdf0e10cSrcweir * 3. Neither the name of Sun Microsystems, Inc. nor the names of its 18*cdf0e10cSrcweir * contributors may be used to endorse or promote products derived 19*cdf0e10cSrcweir * from this software without specific prior written permission. 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22*cdf0e10cSrcweir * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23*cdf0e10cSrcweir * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24*cdf0e10cSrcweir * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25*cdf0e10cSrcweir * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26*cdf0e10cSrcweir * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27*cdf0e10cSrcweir * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28*cdf0e10cSrcweir * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29*cdf0e10cSrcweir * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 30*cdf0e10cSrcweir * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 31*cdf0e10cSrcweir * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32*cdf0e10cSrcweir * 33*cdf0e10cSrcweir *************************************************************************/ 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir // Import everything we use 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 38*cdf0e10cSrcweir import com.sun.star.beans.XMultiPropertySet; 39*cdf0e10cSrcweir import com.sun.star.beans.XHierarchicalPropertySet; 40*cdf0e10cSrcweir import com.sun.star.beans.XMultiHierarchicalPropertySet; 41*cdf0e10cSrcweir import com.sun.star.beans.XPropertyState; 42*cdf0e10cSrcweir import com.sun.star.beans.XMultiPropertyStates; 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver; 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir import com.sun.star.configuration.XTemplateInstance; 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir import com.sun.star.container.XNameAccess; 49*cdf0e10cSrcweir import com.sun.star.container.XNameReplace; 50*cdf0e10cSrcweir import com.sun.star.container.XNameContainer; 51*cdf0e10cSrcweir import com.sun.star.container.XNamed; 52*cdf0e10cSrcweir import com.sun.star.container.XChild; 53*cdf0e10cSrcweir import com.sun.star.container.XHierarchicalNameAccess; 54*cdf0e10cSrcweir import com.sun.star.container.XHierarchicalName; 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir import com.sun.star.lang.XComponent; 57*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory; 58*cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory; 59*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 60*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory; 61*cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo; 62*cdf0e10cSrcweir import com.sun.star.lang.EventObject; 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 65*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 66*cdf0e10cSrcweir import com.sun.star.uno.XInterface; 67*cdf0e10cSrcweir import com.sun.star.uno.XNamingService; 68*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter; 69*cdf0e10cSrcweir 70*cdf0e10cSrcweir import com.sun.star.util.XChangesBatch; 71*cdf0e10cSrcweir import com.sun.star.util.XChangesNotifier; 72*cdf0e10cSrcweir import com.sun.star.util.XChangesListener; 73*cdf0e10cSrcweir import com.sun.star.util.ChangesEvent; 74*cdf0e10cSrcweir /** Config examples 75*cdf0e10cSrcweir @author Joerg Barfurth 76*cdf0e10cSrcweir */ 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir /* These examples show how to use the following features of the Config API: 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir o Accessing data 81*cdf0e10cSrcweir o Updating data 82*cdf0e10cSrcweir o Updating properties in groups 83*cdf0e10cSrcweir o Adding and removing items in sets 84*cdf0e10cSrcweir o Resetting data to their defaults 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir Each example is in a separate method call. 87*cdf0e10cSrcweir */ 88*cdf0e10cSrcweir public class ConfigExamples 89*cdf0e10cSrcweir { 90*cdf0e10cSrcweir // The ComponentContext interface of the remote component context 91*cdf0e10cSrcweir private XComponentContext mxContext = null; 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir // The MultiComponentFactory interface of the ServiceManager 94*cdf0e10cSrcweir private XMultiComponentFactory mxServiceManager = null; 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir // The MultiServiceFactory interface of the ConfigurationProvider 97*cdf0e10cSrcweir private XMultiServiceFactory mxProvider = null; 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir public static void main( String args[] ) 100*cdf0e10cSrcweir { 101*cdf0e10cSrcweir try { 102*cdf0e10cSrcweir // get the remote office component context 103*cdf0e10cSrcweir com.sun.star.uno.XComponentContext xContext = 104*cdf0e10cSrcweir com.sun.star.comp.helper.Bootstrap.bootstrap(); 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir if( xContext != null ) 107*cdf0e10cSrcweir System.out.println("Connected to a running office ..."); 108*cdf0e10cSrcweir else 109*cdf0e10cSrcweir System.out.println( "ERROR: Cannot connect - no remote component context available." ); 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir // Create an instance of the class and call it's run method 112*cdf0e10cSrcweir ConfigExamples aExample = new ConfigExamples(xContext); 113*cdf0e10cSrcweir aExample.run( ); 114*cdf0e10cSrcweir 115*cdf0e10cSrcweir // if you own the service manager dispose it here 116*cdf0e10cSrcweir // to ensure that the default provider is properly disposed and flushed 117*cdf0e10cSrcweir System.exit(0); 118*cdf0e10cSrcweir } 119*cdf0e10cSrcweir catch( Exception e ) 120*cdf0e10cSrcweir { 121*cdf0e10cSrcweir e.printStackTrace(); 122*cdf0e10cSrcweir System.exit(-1); 123*cdf0e10cSrcweir } 124*cdf0e10cSrcweir } 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir /** Create a ConfigExamples instance supplying a service factory 127*cdf0e10cSrcweir */ 128*cdf0e10cSrcweir public ConfigExamples(XComponentContext xContext) 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir mxContext = xContext; 131*cdf0e10cSrcweir mxServiceManager = xContext.getServiceManager(); 132*cdf0e10cSrcweir } 133*cdf0e10cSrcweir 134*cdf0e10cSrcweir /** Run the examples with a default ConfigurationProvider 135*cdf0e10cSrcweir */ 136*cdf0e10cSrcweir public void run() 137*cdf0e10cSrcweir throws com.sun.star.uno.Exception 138*cdf0e10cSrcweir { 139*cdf0e10cSrcweir mxProvider = createProvider(); 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir runExamples( ); 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir // we are using the default ConfigurationProvider, so we must not dispose it 144*cdf0e10cSrcweir mxProvider = null; 145*cdf0e10cSrcweir } 146*cdf0e10cSrcweir 147*cdf0e10cSrcweir /** Run the examples with a given ConfigurationProvider 148*cdf0e10cSrcweir */ 149*cdf0e10cSrcweir public void runExamples( ) 150*cdf0e10cSrcweir { 151*cdf0e10cSrcweir if (checkProvider(mxProvider)) 152*cdf0e10cSrcweir { 153*cdf0e10cSrcweir System.out.println("\nStarting examples."); 154*cdf0e10cSrcweir 155*cdf0e10cSrcweir readDataExample(); 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir browseDataExample(); 158*cdf0e10cSrcweir 159*cdf0e10cSrcweir updateGroupExample(); 160*cdf0e10cSrcweir 161*cdf0e10cSrcweir resetGroupExample(); 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir updateSetExample(); 164*cdf0e10cSrcweir 165*cdf0e10cSrcweir System.out.println("\nAll Examples completed."); 166*cdf0e10cSrcweir } 167*cdf0e10cSrcweir else 168*cdf0e10cSrcweir System.out.println("ERROR: Cannot run examples without ConfigurationProvider."); 169*cdf0e10cSrcweir 170*cdf0e10cSrcweir } 171*cdf0e10cSrcweir 172*cdf0e10cSrcweir /** Do some simple checks, if tehre is a valid ConfigurationProvider 173*cdf0e10cSrcweir */ 174*cdf0e10cSrcweir public static boolean checkProvider(XMultiServiceFactory xProvider) 175*cdf0e10cSrcweir { 176*cdf0e10cSrcweir // check the provider we have 177*cdf0e10cSrcweir if (xProvider == null) 178*cdf0e10cSrcweir { 179*cdf0e10cSrcweir System.out.println("No provider available. Cannot access configuration data."); 180*cdf0e10cSrcweir return false; 181*cdf0e10cSrcweir 182*cdf0e10cSrcweir } 183*cdf0e10cSrcweir 184*cdf0e10cSrcweir try 185*cdf0e10cSrcweir { 186*cdf0e10cSrcweir // check the provider implementation 187*cdf0e10cSrcweir XServiceInfo xProviderServices = 188*cdf0e10cSrcweir (XServiceInfo) UnoRuntime.queryInterface( XServiceInfo.class, xProvider ); 189*cdf0e10cSrcweir 190*cdf0e10cSrcweir if (xProviderServices == null || 191*cdf0e10cSrcweir !xProviderServices.supportsService("com.sun.star.configuration.ConfigurationProvider")) 192*cdf0e10cSrcweir { 193*cdf0e10cSrcweir System.out.println("WARNING: The provider is not a com.sun.star.configuration.ConfigurationProvider"); 194*cdf0e10cSrcweir } 195*cdf0e10cSrcweir 196*cdf0e10cSrcweir if (xProviderServices != null) 197*cdf0e10cSrcweir { 198*cdf0e10cSrcweir System.out.println("Using provider implementation: " + xProviderServices.getImplementationName()); 199*cdf0e10cSrcweir } 200*cdf0e10cSrcweir 201*cdf0e10cSrcweir return true; 202*cdf0e10cSrcweir } 203*cdf0e10cSrcweir catch (com.sun.star.uno.RuntimeException e) 204*cdf0e10cSrcweir { 205*cdf0e10cSrcweir System.err.println("ERROR: Failure while checking the provider services."); 206*cdf0e10cSrcweir e.printStackTrace(); 207*cdf0e10cSrcweir return false; 208*cdf0e10cSrcweir } 209*cdf0e10cSrcweir } 210*cdf0e10cSrcweir 211*cdf0e10cSrcweir /** Get the provider we have 212*cdf0e10cSrcweir */ 213*cdf0e10cSrcweir public XMultiServiceFactory getProvider( ) 214*cdf0e10cSrcweir { 215*cdf0e10cSrcweir return mxProvider; 216*cdf0e10cSrcweir } 217*cdf0e10cSrcweir 218*cdf0e10cSrcweir /** Create a default configuration provider 219*cdf0e10cSrcweir */ 220*cdf0e10cSrcweir public XMultiServiceFactory createProvider( ) 221*cdf0e10cSrcweir throws com.sun.star.uno.Exception 222*cdf0e10cSrcweir { 223*cdf0e10cSrcweir final String sProviderService = "com.sun.star.configuration.ConfigurationProvider"; 224*cdf0e10cSrcweir 225*cdf0e10cSrcweir // create the provider and return it as a XMultiServiceFactory 226*cdf0e10cSrcweir XMultiServiceFactory xProvider = (XMultiServiceFactory) 227*cdf0e10cSrcweir UnoRuntime.queryInterface(XMultiServiceFactory.class, 228*cdf0e10cSrcweir mxServiceManager.createInstanceWithContext(sProviderService, 229*cdf0e10cSrcweir mxContext)); 230*cdf0e10cSrcweir 231*cdf0e10cSrcweir return xProvider; 232*cdf0e10cSrcweir } 233*cdf0e10cSrcweir 234*cdf0e10cSrcweir /** Create a specified read-only configuration view 235*cdf0e10cSrcweir */ 236*cdf0e10cSrcweir public Object createConfigurationView( String sPath ) 237*cdf0e10cSrcweir throws com.sun.star.uno.Exception 238*cdf0e10cSrcweir { 239*cdf0e10cSrcweir XMultiServiceFactory xProvider = getProvider(); 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir // The service name: Need only read access: 242*cdf0e10cSrcweir final String sReadOnlyView = "com.sun.star.configuration.ConfigurationAccess"; 243*cdf0e10cSrcweir 244*cdf0e10cSrcweir // creation arguments: nodepath 245*cdf0e10cSrcweir com.sun.star.beans.PropertyValue aPathArgument = new com.sun.star.beans.PropertyValue(); 246*cdf0e10cSrcweir aPathArgument.Name = "nodepath"; 247*cdf0e10cSrcweir aPathArgument.Value = sPath; 248*cdf0e10cSrcweir 249*cdf0e10cSrcweir Object[] aArguments = new Object[1]; 250*cdf0e10cSrcweir aArguments[0] = aPathArgument; 251*cdf0e10cSrcweir 252*cdf0e10cSrcweir // create the view 253*cdf0e10cSrcweir Object xViewRoot = xProvider.createInstanceWithArguments(sReadOnlyView, aArguments); 254*cdf0e10cSrcweir 255*cdf0e10cSrcweir return xViewRoot; 256*cdf0e10cSrcweir } 257*cdf0e10cSrcweir 258*cdf0e10cSrcweir /** Create a specified updatable configuration view 259*cdf0e10cSrcweir */ 260*cdf0e10cSrcweir Object createUpdatableView( String sPath ) 261*cdf0e10cSrcweir throws com.sun.star.uno.Exception 262*cdf0e10cSrcweir { 263*cdf0e10cSrcweir XMultiServiceFactory xProvider = getProvider(); 264*cdf0e10cSrcweir 265*cdf0e10cSrcweir // The service name: Need update access: 266*cdf0e10cSrcweir final String cUpdatableView = "com.sun.star.configuration.ConfigurationUpdateAccess"; 267*cdf0e10cSrcweir 268*cdf0e10cSrcweir // creation arguments: nodepath 269*cdf0e10cSrcweir com.sun.star.beans.PropertyValue aPathArgument = new com.sun.star.beans.PropertyValue(); 270*cdf0e10cSrcweir aPathArgument.Name = "nodepath"; 271*cdf0e10cSrcweir aPathArgument.Value = sPath; 272*cdf0e10cSrcweir 273*cdf0e10cSrcweir Object[] aArguments = new Object[1]; 274*cdf0e10cSrcweir aArguments[0] = aPathArgument; 275*cdf0e10cSrcweir 276*cdf0e10cSrcweir // create the view 277*cdf0e10cSrcweir Object xViewRoot = xProvider.createInstanceWithArguments(cUpdatableView, aArguments); 278*cdf0e10cSrcweir 279*cdf0e10cSrcweir return xViewRoot; 280*cdf0e10cSrcweir } 281*cdf0e10cSrcweir 282*cdf0e10cSrcweir /** This method demonstrates read access to data 283*cdf0e10cSrcweir */ 284*cdf0e10cSrcweir protected void readDataExample () 285*cdf0e10cSrcweir { 286*cdf0e10cSrcweir try 287*cdf0e10cSrcweir { 288*cdf0e10cSrcweir System.out.println("\n--- starting example: read grid option settings --------------------"); 289*cdf0e10cSrcweir Object aData = readGridConfiguration( ); 290*cdf0e10cSrcweir System.out.println("Read grid options: " + aData); 291*cdf0e10cSrcweir 292*cdf0e10cSrcweir } 293*cdf0e10cSrcweir catch ( Exception e ) 294*cdf0e10cSrcweir { 295*cdf0e10cSrcweir e.printStackTrace(); 296*cdf0e10cSrcweir } 297*cdf0e10cSrcweir } 298*cdf0e10cSrcweir 299*cdf0e10cSrcweir /** This method demonstrates browsing access to data 300*cdf0e10cSrcweir */ 301*cdf0e10cSrcweir protected void browseDataExample () 302*cdf0e10cSrcweir { 303*cdf0e10cSrcweir try 304*cdf0e10cSrcweir { 305*cdf0e10cSrcweir System.out.println("\n--- starting example: browse filter configuration ------------------"); 306*cdf0e10cSrcweir printRegisteredFilters( ); 307*cdf0e10cSrcweir } 308*cdf0e10cSrcweir catch ( Exception e ) 309*cdf0e10cSrcweir { 310*cdf0e10cSrcweir e.printStackTrace(); 311*cdf0e10cSrcweir } 312*cdf0e10cSrcweir } 313*cdf0e10cSrcweir 314*cdf0e10cSrcweir /** This method demonstrates update access to group data 315*cdf0e10cSrcweir */ 316*cdf0e10cSrcweir protected void updateGroupExample () 317*cdf0e10cSrcweir { 318*cdf0e10cSrcweir try 319*cdf0e10cSrcweir { 320*cdf0e10cSrcweir System.out.println("\n--- starting example: update group data --------------"); 321*cdf0e10cSrcweir editGridOptions( ); 322*cdf0e10cSrcweir } 323*cdf0e10cSrcweir catch ( Exception e ) 324*cdf0e10cSrcweir { 325*cdf0e10cSrcweir e.printStackTrace(); 326*cdf0e10cSrcweir } 327*cdf0e10cSrcweir } 328*cdf0e10cSrcweir 329*cdf0e10cSrcweir /** This method demonstrates resetting data to its default state 330*cdf0e10cSrcweir */ 331*cdf0e10cSrcweir protected void resetGroupExample () 332*cdf0e10cSrcweir { 333*cdf0e10cSrcweir try 334*cdf0e10cSrcweir { 335*cdf0e10cSrcweir System.out.println("\n--- starting example: reset group data -----------------------------"); 336*cdf0e10cSrcweir Object aOldData = readGridConfiguration( ); 337*cdf0e10cSrcweir resetGridConfiguration( ); 338*cdf0e10cSrcweir Object aNewData = readGridConfiguration( ); 339*cdf0e10cSrcweir System.out.println("Before reset: user grid options: " + aOldData); 340*cdf0e10cSrcweir System.out.println("After reset: default grid options: " + aNewData); 341*cdf0e10cSrcweir } 342*cdf0e10cSrcweir catch ( Exception e ) 343*cdf0e10cSrcweir { 344*cdf0e10cSrcweir e.printStackTrace(); 345*cdf0e10cSrcweir } 346*cdf0e10cSrcweir } 347*cdf0e10cSrcweir 348*cdf0e10cSrcweir /** This method demonstrates update access to set data 349*cdf0e10cSrcweir */ 350*cdf0e10cSrcweir protected void updateSetExample () 351*cdf0e10cSrcweir { 352*cdf0e10cSrcweir try 353*cdf0e10cSrcweir { 354*cdf0e10cSrcweir System.out.println("\n--- starting example: update set data ---------------"); 355*cdf0e10cSrcweir storeSampleDataSource( ); 356*cdf0e10cSrcweir } 357*cdf0e10cSrcweir catch ( Exception e ) 358*cdf0e10cSrcweir { 359*cdf0e10cSrcweir e.printStackTrace(); 360*cdf0e10cSrcweir } 361*cdf0e10cSrcweir } 362*cdf0e10cSrcweir 363*cdf0e10cSrcweir // READ example 364*cdf0e10cSrcweir /// class to hold information about grid settings 365*cdf0e10cSrcweir public static class GridOptions 366*cdf0e10cSrcweir { 367*cdf0e10cSrcweir public boolean visible; 368*cdf0e10cSrcweir public int resolution_x; 369*cdf0e10cSrcweir public int resolution_y; 370*cdf0e10cSrcweir public int subdivision_x; 371*cdf0e10cSrcweir public int subdivision_y; 372*cdf0e10cSrcweir 373*cdf0e10cSrcweir public String toString() { 374*cdf0e10cSrcweir StringBuffer aBuffer = new StringBuffer(); 375*cdf0e10cSrcweir aBuffer.append("[ Grid is "); aBuffer.append(visible ? "VISIBLE" : "HIDDEN"); 376*cdf0e10cSrcweir aBuffer.append("; resolution = (" + resolution_x + "," + resolution_y + ")"); 377*cdf0e10cSrcweir aBuffer.append("; subdivision = (" + subdivision_x + "," + subdivision_y + ")"); 378*cdf0e10cSrcweir aBuffer.append(" ]"); 379*cdf0e10cSrcweir return aBuffer.toString(); 380*cdf0e10cSrcweir } 381*cdf0e10cSrcweir }; 382*cdf0e10cSrcweir 383*cdf0e10cSrcweir /// This method reads information about grid settings 384*cdf0e10cSrcweir protected GridOptions readGridConfiguration() 385*cdf0e10cSrcweir throws com.sun.star.uno.Exception 386*cdf0e10cSrcweir { 387*cdf0e10cSrcweir // The path to the root element 388*cdf0e10cSrcweir final String cGridOptionsPath = "/org.openoffice.Office.Calc/Grid"; 389*cdf0e10cSrcweir 390*cdf0e10cSrcweir // create the view 391*cdf0e10cSrcweir Object xViewRoot = createConfigurationView(cGridOptionsPath); 392*cdf0e10cSrcweir 393*cdf0e10cSrcweir // the result structure 394*cdf0e10cSrcweir GridOptions options = new GridOptions(); 395*cdf0e10cSrcweir 396*cdf0e10cSrcweir // accessing a single nested value 397*cdf0e10cSrcweir XHierarchicalPropertySet xProperties = 398*cdf0e10cSrcweir (XHierarchicalPropertySet)UnoRuntime.queryInterface(XHierarchicalPropertySet.class, xViewRoot); 399*cdf0e10cSrcweir 400*cdf0e10cSrcweir Object aVisible = xProperties.getHierarchicalPropertyValue("Option/VisibleGrid"); 401*cdf0e10cSrcweir options.visible = ((Boolean) aVisible).booleanValue(); 402*cdf0e10cSrcweir 403*cdf0e10cSrcweir // accessing a nested object and its subproperties 404*cdf0e10cSrcweir Object xSubdivision = xProperties.getHierarchicalPropertyValue("Subdivision"); 405*cdf0e10cSrcweir 406*cdf0e10cSrcweir XMultiPropertySet xSubdivProperties = 407*cdf0e10cSrcweir (XMultiPropertySet)UnoRuntime.queryInterface(XMultiPropertySet.class, xSubdivision); 408*cdf0e10cSrcweir 409*cdf0e10cSrcweir // variables for multi-element access 410*cdf0e10cSrcweir String[] aElementNames = new String[2]; 411*cdf0e10cSrcweir 412*cdf0e10cSrcweir aElementNames[0] = "XAxis"; 413*cdf0e10cSrcweir aElementNames[1] = "YAxis"; 414*cdf0e10cSrcweir 415*cdf0e10cSrcweir Object[] aElementValues = xSubdivProperties.getPropertyValues(aElementNames); 416*cdf0e10cSrcweir 417*cdf0e10cSrcweir options.subdivision_x = ((Integer) aElementValues[0]).intValue(); 418*cdf0e10cSrcweir options.subdivision_y = ((Integer) aElementValues[1]).intValue(); 419*cdf0e10cSrcweir 420*cdf0e10cSrcweir // accessing deeply nested subproperties 421*cdf0e10cSrcweir Object xResolution = xProperties.getHierarchicalPropertyValue("Resolution"); 422*cdf0e10cSrcweir 423*cdf0e10cSrcweir XMultiHierarchicalPropertySet xResolutionProperties = 424*cdf0e10cSrcweir (XMultiHierarchicalPropertySet) 425*cdf0e10cSrcweir UnoRuntime.queryInterface(XMultiHierarchicalPropertySet.class, xResolution); 426*cdf0e10cSrcweir 427*cdf0e10cSrcweir aElementNames[0] = "XAxis/Metric"; 428*cdf0e10cSrcweir aElementNames[1] = "YAxis/Metric"; 429*cdf0e10cSrcweir 430*cdf0e10cSrcweir aElementValues = xResolutionProperties.getHierarchicalPropertyValues(aElementNames); 431*cdf0e10cSrcweir 432*cdf0e10cSrcweir options.resolution_x = ((Integer) aElementValues[0]).intValue(); 433*cdf0e10cSrcweir options.resolution_y = ((Integer) aElementValues[1]).intValue(); 434*cdf0e10cSrcweir 435*cdf0e10cSrcweir // all options have been retrieved - clean up and return 436*cdf0e10cSrcweir // we are done with the view - dispose it 437*cdf0e10cSrcweir 438*cdf0e10cSrcweir ((XComponent)UnoRuntime.queryInterface(XComponent.class, xViewRoot)).dispose(); 439*cdf0e10cSrcweir 440*cdf0e10cSrcweir return options; 441*cdf0e10cSrcweir } 442*cdf0e10cSrcweir 443*cdf0e10cSrcweir // BROWSE example 444*cdf0e10cSrcweir /// Interface to procees information when browsing the configuration tree 445*cdf0e10cSrcweir public static interface IConfigurationProcessor 446*cdf0e10cSrcweir { 447*cdf0e10cSrcweir /// process a value item 448*cdf0e10cSrcweir public abstract void processValueElement( String sPath_, Object aValue_ ); 449*cdf0e10cSrcweir /// process a structural item 450*cdf0e10cSrcweir public abstract void processStructuralElement( String sPath_, XInterface xElement_); 451*cdf0e10cSrcweir }; 452*cdf0e10cSrcweir 453*cdf0e10cSrcweir /// Internal method to recursively browse a structural element in preorder 454*cdf0e10cSrcweir public void browseElementRecursively( XInterface xElement, IConfigurationProcessor aProcessor ) 455*cdf0e10cSrcweir throws com.sun.star.uno.Exception 456*cdf0e10cSrcweir { 457*cdf0e10cSrcweir // First process this as an element (preorder traversal) 458*cdf0e10cSrcweir XHierarchicalName xElementPath = 459*cdf0e10cSrcweir (XHierarchicalName) UnoRuntime.queryInterface(XHierarchicalName.class, xElement); 460*cdf0e10cSrcweir 461*cdf0e10cSrcweir String sPath = xElementPath.getHierarchicalName(); 462*cdf0e10cSrcweir 463*cdf0e10cSrcweir aProcessor.processStructuralElement( sPath, xElement); 464*cdf0e10cSrcweir 465*cdf0e10cSrcweir // now process this as a container 466*cdf0e10cSrcweir XNameAccess xChildAccess = 467*cdf0e10cSrcweir (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, xElement); 468*cdf0e10cSrcweir 469*cdf0e10cSrcweir // get a list of child elements 470*cdf0e10cSrcweir String[] aElementNames = xChildAccess.getElementNames(); 471*cdf0e10cSrcweir 472*cdf0e10cSrcweir // and process them one by one 473*cdf0e10cSrcweir for(int i=0; i< aElementNames.length; ++i) 474*cdf0e10cSrcweir { 475*cdf0e10cSrcweir Object aChild = xChildAccess.getByName( aElementNames[i] ); 476*cdf0e10cSrcweir AnyConverter aAnyConv = new AnyConverter(); 477*cdf0e10cSrcweir // is it a structural element (object) ... 478*cdf0e10cSrcweir if ( aAnyConv.isObject(aChild) && !aAnyConv.isArray(aChild) ) 479*cdf0e10cSrcweir { 480*cdf0e10cSrcweir // then get an interface 481*cdf0e10cSrcweir XInterface xChildElement = (XInterface)UnoRuntime.queryInterface(XInterface.class, aChild); 482*cdf0e10cSrcweir 483*cdf0e10cSrcweir // and continue processing child elements recursively 484*cdf0e10cSrcweir browseElementRecursively( xChildElement, aProcessor ); 485*cdf0e10cSrcweir } 486*cdf0e10cSrcweir // ... or is it a simple value 487*cdf0e10cSrcweir else 488*cdf0e10cSrcweir { 489*cdf0e10cSrcweir // Build the path to it from the path of 490*cdf0e10cSrcweir // the element and the name of the child 491*cdf0e10cSrcweir String sChildPath; 492*cdf0e10cSrcweir sChildPath = 493*cdf0e10cSrcweir xElementPath.composeHierarchicalName(aElementNames[i]); 494*cdf0e10cSrcweir 495*cdf0e10cSrcweir // and process the value 496*cdf0e10cSrcweir aProcessor.processValueElement( sChildPath, aChild ); 497*cdf0e10cSrcweir } 498*cdf0e10cSrcweir } 499*cdf0e10cSrcweir } 500*cdf0e10cSrcweir 501*cdf0e10cSrcweir /** Method to browse the part rooted at sRootPath 502*cdf0e10cSrcweir of the configuration that the Provider provides. 503*cdf0e10cSrcweir 504*cdf0e10cSrcweir All nodes will be processed by the IConfigurationProcessor passed. 505*cdf0e10cSrcweir */ 506*cdf0e10cSrcweir public void browseConfiguration( String sRootPath, IConfigurationProcessor aProcessor ) 507*cdf0e10cSrcweir throws com.sun.star.uno.Exception 508*cdf0e10cSrcweir { 509*cdf0e10cSrcweir // create the root element 510*cdf0e10cSrcweir XInterface xViewRoot = (XInterface)createConfigurationView( sRootPath ); 511*cdf0e10cSrcweir 512*cdf0e10cSrcweir // now do the processing 513*cdf0e10cSrcweir browseElementRecursively( xViewRoot, aProcessor ); 514*cdf0e10cSrcweir 515*cdf0e10cSrcweir // we are done with the view - dispose it 516*cdf0e10cSrcweir // This assumes that the processor 517*cdf0e10cSrcweir // does not keep a reference to the elements in processStructuralElement 518*cdf0e10cSrcweir 519*cdf0e10cSrcweir ((XComponent) UnoRuntime.queryInterface(XComponent.class,xViewRoot)).dispose(); 520*cdf0e10cSrcweir xViewRoot = null; 521*cdf0e10cSrcweir } 522*cdf0e10cSrcweir 523*cdf0e10cSrcweir /** Method to browse the filter configuration. 524*cdf0e10cSrcweir 525*cdf0e10cSrcweir Information about installed filters will be printed. 526*cdf0e10cSrcweir */ 527*cdf0e10cSrcweir public void printRegisteredFilters() 528*cdf0e10cSrcweir throws com.sun.star.uno.Exception 529*cdf0e10cSrcweir { 530*cdf0e10cSrcweir final String sProviderService = "com.sun.star.configuration.ConfigurationProvider"; 531*cdf0e10cSrcweir final String sFilterKey = "/org.openoffice.TypeDetection.Filter/Filters"; 532*cdf0e10cSrcweir 533*cdf0e10cSrcweir // browse the configuration, dumping filter information 534*cdf0e10cSrcweir browseConfiguration( sFilterKey, 535*cdf0e10cSrcweir new IConfigurationProcessor () { 536*cdf0e10cSrcweir /// prints Path and Value of properties 537*cdf0e10cSrcweir public void processValueElement( String sPath_, Object aValue_ ) { 538*cdf0e10cSrcweir if (new AnyConverter().isArray(aValue_)) 539*cdf0e10cSrcweir { 540*cdf0e10cSrcweir final Object [] aArray = (Object [])aValue_; 541*cdf0e10cSrcweir 542*cdf0e10cSrcweir System.out.print("\tValue: " + sPath_ + " = { "); 543*cdf0e10cSrcweir for (int i=0; i<aArray.length; ++i) 544*cdf0e10cSrcweir { 545*cdf0e10cSrcweir if (i != 0) System.out.print(", "); 546*cdf0e10cSrcweir System.out.print(aArray[i]); 547*cdf0e10cSrcweir } 548*cdf0e10cSrcweir System.out.println(" }"); 549*cdf0e10cSrcweir } 550*cdf0e10cSrcweir else 551*cdf0e10cSrcweir System.out.println("\tValue: " + sPath_ + " = " + aValue_); 552*cdf0e10cSrcweir } 553*cdf0e10cSrcweir 554*cdf0e10cSrcweir /// prints the Filter entries 555*cdf0e10cSrcweir public void processStructuralElement( String sPath_, XInterface xElement_) { 556*cdf0e10cSrcweir // get template information, to detect instances of the 'Filter' template 557*cdf0e10cSrcweir XTemplateInstance xInstance = 558*cdf0e10cSrcweir ( XTemplateInstance )UnoRuntime.queryInterface( XTemplateInstance .class,xElement_); 559*cdf0e10cSrcweir 560*cdf0e10cSrcweir // only select the Filter entries 561*cdf0e10cSrcweir if (xInstance != null && xInstance.getTemplateName().endsWith("Filter")) { 562*cdf0e10cSrcweir XNamed xNamed = (XNamed)UnoRuntime.queryInterface(XNamed.class,xElement_); 563*cdf0e10cSrcweir System.out.println("Filter " + xNamed.getName() + " (" + sPath_ + ")"); 564*cdf0e10cSrcweir } 565*cdf0e10cSrcweir } 566*cdf0e10cSrcweir } ); 567*cdf0e10cSrcweir } 568*cdf0e10cSrcweir 569*cdf0e10cSrcweir // GROUP UPDATE example 570*cdf0e10cSrcweir 571*cdf0e10cSrcweir /** This method simulates editing configuration data using a GridEditor dialog class 572*cdf0e10cSrcweir */ 573*cdf0e10cSrcweir public void editGridOptions( ) 574*cdf0e10cSrcweir throws com.sun.star.uno.Exception 575*cdf0e10cSrcweir { 576*cdf0e10cSrcweir // The path to the root element 577*cdf0e10cSrcweir final String cGridOptionsPath = "/org.openoffice.Office.Calc/Grid"; 578*cdf0e10cSrcweir 579*cdf0e10cSrcweir // create the view 580*cdf0e10cSrcweir Object xViewRoot = createUpdatableView( cGridOptionsPath ); 581*cdf0e10cSrcweir 582*cdf0e10cSrcweir // the 'editor' 583*cdf0e10cSrcweir GridOptionsEditor dialog = new GridOptionsEditor(); 584*cdf0e10cSrcweir 585*cdf0e10cSrcweir // set up the initial values and register listeners 586*cdf0e10cSrcweir // get a data access interface, to supply the view with a model 587*cdf0e10cSrcweir XMultiHierarchicalPropertySet xProperties = 588*cdf0e10cSrcweir (XMultiHierarchicalPropertySet) 589*cdf0e10cSrcweir UnoRuntime.queryInterface(XMultiHierarchicalPropertySet.class, xViewRoot); 590*cdf0e10cSrcweir 591*cdf0e10cSrcweir dialog.setModel( xProperties ); 592*cdf0e10cSrcweir 593*cdf0e10cSrcweir // get a listener object (probably an adapter) that notifies 594*cdf0e10cSrcweir // the dialog of external changes to its model 595*cdf0e10cSrcweir XChangesListener xListener = dialog.createChangesListener( ); 596*cdf0e10cSrcweir 597*cdf0e10cSrcweir XChangesNotifier xNotifier = 598*cdf0e10cSrcweir (XChangesNotifier)UnoRuntime.queryInterface(XChangesNotifier.class, xViewRoot); 599*cdf0e10cSrcweir 600*cdf0e10cSrcweir xNotifier.addChangesListener( xListener ); 601*cdf0e10cSrcweir 602*cdf0e10cSrcweir // trigger the listener 603*cdf0e10cSrcweir changeSomeData( cGridOptionsPath + "/Subdivision" ); 604*cdf0e10cSrcweir 605*cdf0e10cSrcweir if (dialog.execute() == GridOptionsEditor.SAVE_SETTINGS) 606*cdf0e10cSrcweir { 607*cdf0e10cSrcweir // changes have been applied to the view here 608*cdf0e10cSrcweir XChangesBatch xUpdateControl = 609*cdf0e10cSrcweir (XChangesBatch) UnoRuntime.queryInterface(XChangesBatch.class,xViewRoot); 610*cdf0e10cSrcweir 611*cdf0e10cSrcweir try 612*cdf0e10cSrcweir { 613*cdf0e10cSrcweir xUpdateControl.commitChanges(); 614*cdf0e10cSrcweir } 615*cdf0e10cSrcweir catch (Exception e) 616*cdf0e10cSrcweir { 617*cdf0e10cSrcweir dialog.informUserOfError( e ); 618*cdf0e10cSrcweir } 619*cdf0e10cSrcweir } 620*cdf0e10cSrcweir 621*cdf0e10cSrcweir // all changes have been handled - clean up and return 622*cdf0e10cSrcweir // listener is done now 623*cdf0e10cSrcweir xNotifier.removeChangesListener( xListener ); 624*cdf0e10cSrcweir 625*cdf0e10cSrcweir // we are done with the view - dispose it 626*cdf0e10cSrcweir ((XComponent)UnoRuntime.queryInterface(XComponent.class, xViewRoot)).dispose(); 627*cdf0e10cSrcweir } 628*cdf0e10cSrcweir 629*cdf0e10cSrcweir /** A class that changes some grid options settings 630*cdf0e10cSrcweir 631*cdf0e10cSrcweir The interface of this class is chose to resemble a possible UI dialog class 632*cdf0e10cSrcweir */ 633*cdf0e10cSrcweir private class GridOptionsEditor { 634*cdf0e10cSrcweir /// the data this editor edits 635*cdf0e10cSrcweir XMultiHierarchicalPropertySet mxModel; 636*cdf0e10cSrcweir 637*cdf0e10cSrcweir public static final int CANCELED = 0; 638*cdf0e10cSrcweir public static final int SAVE_SETTINGS = 1; 639*cdf0e10cSrcweir 640*cdf0e10cSrcweir // sets a model and updates the display 641*cdf0e10cSrcweir public void setModel(XMultiHierarchicalPropertySet xModel) { 642*cdf0e10cSrcweir mxModel = xModel; 643*cdf0e10cSrcweir updateDisplay(); 644*cdf0e10cSrcweir } 645*cdf0e10cSrcweir 646*cdf0e10cSrcweir // this method 'runs' the 'dialog' 647*cdf0e10cSrcweir public int execute() { 648*cdf0e10cSrcweir try 649*cdf0e10cSrcweir { 650*cdf0e10cSrcweir System.out.println("-- GridEditor executing --"); 651*cdf0e10cSrcweir // simulate a user action changing some data 652*cdf0e10cSrcweir toggleVisibility(); 653*cdf0e10cSrcweir System.out.println("-- GridEditor done --"); 654*cdf0e10cSrcweir return SAVE_SETTINGS; 655*cdf0e10cSrcweir } 656*cdf0e10cSrcweir catch (Exception e) 657*cdf0e10cSrcweir { 658*cdf0e10cSrcweir informUserOfError(e); 659*cdf0e10cSrcweir return CANCELED; 660*cdf0e10cSrcweir } 661*cdf0e10cSrcweir } 662*cdf0e10cSrcweir 663*cdf0e10cSrcweir /// this method is called to report an error during dialog execution to the zuser 664*cdf0e10cSrcweir public void informUserOfError(Exception e) { 665*cdf0e10cSrcweir System.err.println("ERROR in GridEditor:"); 666*cdf0e10cSrcweir e.printStackTrace(); 667*cdf0e10cSrcweir } 668*cdf0e10cSrcweir 669*cdf0e10cSrcweir /// this method is called to allow the dialog to get feedback about changes occurring elsewhere 670*cdf0e10cSrcweir public XChangesListener createChangesListener() { 671*cdf0e10cSrcweir if (mxModel == null) return null; 672*cdf0e10cSrcweir 673*cdf0e10cSrcweir return (new XChangesListener () { 674*cdf0e10cSrcweir public void changesOccurred( ChangesEvent event ) { 675*cdf0e10cSrcweir System.out.println("GridEditor - Listener received changes event containing " + 676*cdf0e10cSrcweir event.Changes.length + " change(s)."); 677*cdf0e10cSrcweir updateDisplay(); 678*cdf0e10cSrcweir } 679*cdf0e10cSrcweir 680*cdf0e10cSrcweir public void disposing(EventObject event) { 681*cdf0e10cSrcweir System.out.println("GridEditor - Listener received disposed event: releasing model"); 682*cdf0e10cSrcweir setModel(null); 683*cdf0e10cSrcweir } 684*cdf0e10cSrcweir }); 685*cdf0e10cSrcweir } 686*cdf0e10cSrcweir /// this method is called when data has changed to display the updated data 687*cdf0e10cSrcweir private void updateDisplay() { 688*cdf0e10cSrcweir if (mxModel != null) 689*cdf0e10cSrcweir System.out.println("Grid options editor: data=" + readModel()); 690*cdf0e10cSrcweir else 691*cdf0e10cSrcweir System.out.println("Grid options editor: no model set"); 692*cdf0e10cSrcweir } 693*cdf0e10cSrcweir 694*cdf0e10cSrcweir // this method is used to read all relevant data from the model 695*cdf0e10cSrcweir private GridOptions readModel() 696*cdf0e10cSrcweir { 697*cdf0e10cSrcweir try 698*cdf0e10cSrcweir { 699*cdf0e10cSrcweir String [] aOptionNames = new String [5]; 700*cdf0e10cSrcweir aOptionNames[0] = "Option/VisibleGrid"; 701*cdf0e10cSrcweir aOptionNames[1] = "Subdivision/XAxis"; 702*cdf0e10cSrcweir aOptionNames[2] = "Subdivision/YAxis"; 703*cdf0e10cSrcweir aOptionNames[3] = "Resolution/XAxis/Metric"; 704*cdf0e10cSrcweir aOptionNames[4] = "Resolution/YAxis/Metric"; 705*cdf0e10cSrcweir 706*cdf0e10cSrcweir Object [] aValues = mxModel.getHierarchicalPropertyValues(aOptionNames); 707*cdf0e10cSrcweir 708*cdf0e10cSrcweir GridOptions result = new GridOptions(); 709*cdf0e10cSrcweir result.visible = ((Boolean)aValues[0]).booleanValue(); 710*cdf0e10cSrcweir result.subdivision_x = ((Integer)aValues[1]).intValue(); 711*cdf0e10cSrcweir result.subdivision_y = ((Integer)aValues[2]).intValue(); 712*cdf0e10cSrcweir result.resolution_x = ((Integer)aValues[3]).intValue(); 713*cdf0e10cSrcweir result.resolution_y = ((Integer)aValues[4]).intValue(); 714*cdf0e10cSrcweir 715*cdf0e10cSrcweir return result; 716*cdf0e10cSrcweir } 717*cdf0e10cSrcweir catch (Exception e) 718*cdf0e10cSrcweir { 719*cdf0e10cSrcweir informUserOfError(e); 720*cdf0e10cSrcweir return null; 721*cdf0e10cSrcweir } 722*cdf0e10cSrcweir } 723*cdf0e10cSrcweir 724*cdf0e10cSrcweir // this method executes an edit 725*cdf0e10cSrcweir private void toggleVisibility() 726*cdf0e10cSrcweir { 727*cdf0e10cSrcweir try 728*cdf0e10cSrcweir { 729*cdf0e10cSrcweir XHierarchicalPropertySet xHPS = 730*cdf0e10cSrcweir (XHierarchicalPropertySet)UnoRuntime.queryInterface(XHierarchicalPropertySet.class, mxModel); 731*cdf0e10cSrcweir 732*cdf0e10cSrcweir final String sSetting = "Option/VisibleGrid"; 733*cdf0e10cSrcweir 734*cdf0e10cSrcweir System.out.println("GridEditor: toggling Visibility"); 735*cdf0e10cSrcweir 736*cdf0e10cSrcweir Boolean bOldValue = (Boolean)xHPS.getHierarchicalPropertyValue(sSetting); 737*cdf0e10cSrcweir 738*cdf0e10cSrcweir Boolean bNewValue = new Boolean( ! bOldValue.booleanValue() ); 739*cdf0e10cSrcweir 740*cdf0e10cSrcweir xHPS.setHierarchicalPropertyValue(sSetting,bNewValue); 741*cdf0e10cSrcweir } 742*cdf0e10cSrcweir catch (Exception e) 743*cdf0e10cSrcweir { 744*cdf0e10cSrcweir informUserOfError(e); 745*cdf0e10cSrcweir } 746*cdf0e10cSrcweir } 747*cdf0e10cSrcweir } 748*cdf0e10cSrcweir 749*cdf0e10cSrcweir /** This method creates an extra updatable view to change some data 750*cdf0e10cSrcweir and trigger the listener of the GridEditor 751*cdf0e10cSrcweir */ 752*cdf0e10cSrcweir void changeSomeData(String xKey) 753*cdf0e10cSrcweir { 754*cdf0e10cSrcweir try 755*cdf0e10cSrcweir { 756*cdf0e10cSrcweir Object xOtherViewRoot = createUpdatableView(xKey); 757*cdf0e10cSrcweir 758*cdf0e10cSrcweir XNameReplace aReplace = (XNameReplace)UnoRuntime.queryInterface(XNameReplace.class, xOtherViewRoot); 759*cdf0e10cSrcweir 760*cdf0e10cSrcweir String aItemNames [] = aReplace.getElementNames(); 761*cdf0e10cSrcweir for (int i=0; i < aItemNames.length; ++i) { 762*cdf0e10cSrcweir Object aItem = aReplace.getByName( aItemNames [i] ); 763*cdf0e10cSrcweir AnyConverter aAnyConv = new AnyConverter(); 764*cdf0e10cSrcweir // replace integers by a 'complement' value 765*cdf0e10cSrcweir if ( aAnyConv.isInt(aItem) ) 766*cdf0e10cSrcweir { 767*cdf0e10cSrcweir int nOld = aAnyConv.toInt(aItem); 768*cdf0e10cSrcweir int nNew = 9999 - nOld; 769*cdf0e10cSrcweir 770*cdf0e10cSrcweir System.out.println("Replacing integer value: " + aItemNames [i]); 771*cdf0e10cSrcweir aReplace.replaceByName( aItemNames [i], new Integer( nNew ) ); 772*cdf0e10cSrcweir } 773*cdf0e10cSrcweir 774*cdf0e10cSrcweir // and booleans by their negated value 775*cdf0e10cSrcweir else if ( aAnyConv.isBoolean(aItem) ) 776*cdf0e10cSrcweir { 777*cdf0e10cSrcweir boolean bOld = aAnyConv.toBoolean(aItem); 778*cdf0e10cSrcweir boolean bNew = ! bOld; 779*cdf0e10cSrcweir 780*cdf0e10cSrcweir System.out.println("Replacing boolean value: " + aItemNames [i]); 781*cdf0e10cSrcweir aReplace.replaceByName( aItemNames [i], new Boolean( bNew ) ); 782*cdf0e10cSrcweir } 783*cdf0e10cSrcweir } 784*cdf0e10cSrcweir 785*cdf0e10cSrcweir // commit the changes 786*cdf0e10cSrcweir XChangesBatch xUpdateControl = 787*cdf0e10cSrcweir (XChangesBatch) UnoRuntime.queryInterface(XChangesBatch.class,xOtherViewRoot); 788*cdf0e10cSrcweir 789*cdf0e10cSrcweir xUpdateControl.commitChanges(); 790*cdf0e10cSrcweir 791*cdf0e10cSrcweir // we are done with the view - dispose it 792*cdf0e10cSrcweir ((XComponent)UnoRuntime.queryInterface(XComponent.class, xOtherViewRoot)).dispose(); 793*cdf0e10cSrcweir } 794*cdf0e10cSrcweir catch (Exception e) 795*cdf0e10cSrcweir { 796*cdf0e10cSrcweir System.err.println("Could not change some data in a different view. An exception occurred:"); 797*cdf0e10cSrcweir e.printStackTrace(); 798*cdf0e10cSrcweir } 799*cdf0e10cSrcweir } 800*cdf0e10cSrcweir 801*cdf0e10cSrcweir // GROUP RESET EXAMPLE 802*cdf0e10cSrcweir /// This method resets the grid settings to their default values 803*cdf0e10cSrcweir protected void resetGridConfiguration() 804*cdf0e10cSrcweir throws com.sun.star.uno.Exception 805*cdf0e10cSrcweir { 806*cdf0e10cSrcweir // The path to the root element 807*cdf0e10cSrcweir final String cGridOptionsPath = "/org.openoffice.Office.Calc/Grid"; 808*cdf0e10cSrcweir 809*cdf0e10cSrcweir // create the view 810*cdf0e10cSrcweir Object xViewRoot = createUpdatableView(cGridOptionsPath); 811*cdf0e10cSrcweir 812*cdf0e10cSrcweir // resetting a single nested value 813*cdf0e10cSrcweir XHierarchicalNameAccess xHierarchicalAccess = 814*cdf0e10cSrcweir (XHierarchicalNameAccess)UnoRuntime.queryInterface(XHierarchicalNameAccess.class, xViewRoot); 815*cdf0e10cSrcweir 816*cdf0e10cSrcweir // get using absolute name 817*cdf0e10cSrcweir Object xOptions = xHierarchicalAccess.getByHierarchicalName(cGridOptionsPath + "/Option"); 818*cdf0e10cSrcweir 819*cdf0e10cSrcweir XPropertyState xOptionState = 820*cdf0e10cSrcweir (XPropertyState)UnoRuntime.queryInterface(XPropertyState.class, xOptions); 821*cdf0e10cSrcweir 822*cdf0e10cSrcweir xOptionState.setPropertyToDefault("VisibleGrid"); 823*cdf0e10cSrcweir 824*cdf0e10cSrcweir // resetting more deeply nested values 825*cdf0e10cSrcweir Object xResolutionX = xHierarchicalAccess.getByHierarchicalName("Resolution/XAxis"); 826*cdf0e10cSrcweir Object xResolutionY = xHierarchicalAccess.getByHierarchicalName("Resolution/YAxis"); 827*cdf0e10cSrcweir 828*cdf0e10cSrcweir XPropertyState xResolutionStateX = 829*cdf0e10cSrcweir (XPropertyState)UnoRuntime.queryInterface(XPropertyState.class, xResolutionX); 830*cdf0e10cSrcweir XPropertyState xResolutionStateY = 831*cdf0e10cSrcweir (XPropertyState)UnoRuntime.queryInterface(XPropertyState.class, xResolutionY); 832*cdf0e10cSrcweir 833*cdf0e10cSrcweir xResolutionStateX.setPropertyToDefault("Metric"); 834*cdf0e10cSrcweir xResolutionStateY.setPropertyToDefault("Metric"); 835*cdf0e10cSrcweir 836*cdf0e10cSrcweir // resetting multiple sibling values 837*cdf0e10cSrcweir Object xSubdivision = xHierarchicalAccess.getByHierarchicalName("Subdivision"); 838*cdf0e10cSrcweir 839*cdf0e10cSrcweir XMultiPropertyStates xSubdivisionStates = 840*cdf0e10cSrcweir (XMultiPropertyStates)UnoRuntime.queryInterface(XMultiPropertyStates.class, xSubdivision); 841*cdf0e10cSrcweir 842*cdf0e10cSrcweir xSubdivisionStates.setAllPropertiesToDefault(); 843*cdf0e10cSrcweir 844*cdf0e10cSrcweir // commit the changes 845*cdf0e10cSrcweir XChangesBatch xUpdateControl = 846*cdf0e10cSrcweir (XChangesBatch) UnoRuntime.queryInterface(XChangesBatch.class,xViewRoot); 847*cdf0e10cSrcweir 848*cdf0e10cSrcweir xUpdateControl.commitChanges(); 849*cdf0e10cSrcweir 850*cdf0e10cSrcweir // we are done with the view - dispose it 851*cdf0e10cSrcweir ((XComponent)UnoRuntime.queryInterface(XComponent.class, xViewRoot)).dispose(); 852*cdf0e10cSrcweir } 853*cdf0e10cSrcweir 854*cdf0e10cSrcweir 855*cdf0e10cSrcweir // SET UPDATE EXAMPLE 856*cdf0e10cSrcweir private static boolean SET_EXAMPLE_BROKEN_IN_THIS_RELEASE = true; 857*cdf0e10cSrcweir 858*cdf0e10cSrcweir /** This method stores a sample data source given some connection data. 859*cdf0e10cSrcweir 860*cdf0e10cSrcweir ATTENTION: This example requires an older version of the 861*cdf0e10cSrcweir org.openoffice.Office.DataAccess schema. 862*cdf0e10cSrcweir It does not work with the current schema. 863*cdf0e10cSrcweir Because of this, the method currenty does nothing. 864*cdf0e10cSrcweir You can still use the techniques shown in the example code. 865*cdf0e10cSrcweir */ 866*cdf0e10cSrcweir void storeSampleDataSource() 867*cdf0e10cSrcweir throws com.sun.star.uno.Exception 868*cdf0e10cSrcweir { 869*cdf0e10cSrcweir if (SET_EXAMPLE_BROKEN_IN_THIS_RELEASE) 870*cdf0e10cSrcweir { 871*cdf0e10cSrcweir System.out.println("- DISABLED: (the existing example does not work with this version) -"); 872*cdf0e10cSrcweir return; // this function does not work 873*cdf0e10cSrcweir } 874*cdf0e10cSrcweir 875*cdf0e10cSrcweir String sSampleDataSourceName = "SampleTextDatabase"; 876*cdf0e10cSrcweir 877*cdf0e10cSrcweir String sSampleDataSourceURL = "sdbc:flat:$(userurl)/database/SampleTextDatabase"; 878*cdf0e10cSrcweir // String sSampleDataSourceURL = "sdbc:flat:file:///usr/local/database/SampleTextDatabase"; 879*cdf0e10cSrcweir // String sSampleDataSourceURL = "sdbc:flat:file:///C:/data/database/SampleTextDatabase"; 880*cdf0e10cSrcweir 881*cdf0e10cSrcweir com.sun.star.beans.NamedValue [] aSettings = new com.sun.star.beans.NamedValue [2]; 882*cdf0e10cSrcweir aSettings[0] = new com.sun.star.beans.NamedValue("HeaderLine",new Boolean(true)); 883*cdf0e10cSrcweir aSettings[1] = new com.sun.star.beans.NamedValue("FieldDelimiter",";"); 884*cdf0e10cSrcweir 885*cdf0e10cSrcweir String [] aTableFilter = new String[2]; 886*cdf0e10cSrcweir aTableFilter[0] = "table.txt"; 887*cdf0e10cSrcweir aTableFilter[1] = "othertable.txt"; 888*cdf0e10cSrcweir 889*cdf0e10cSrcweir storeDataSource(sSampleDataSourceName,sSampleDataSourceURL,"",false,0,aSettings,aTableFilter); 890*cdf0e10cSrcweir } 891*cdf0e10cSrcweir 892*cdf0e10cSrcweir /// This method stores a data source given some connection data 893*cdf0e10cSrcweir void storeDataSource( 894*cdf0e10cSrcweir String sDataSourceName, 895*cdf0e10cSrcweir String sDataSourceURL, 896*cdf0e10cSrcweir String sUser, 897*cdf0e10cSrcweir boolean bNeedsPassword, 898*cdf0e10cSrcweir int nTimeout, 899*cdf0e10cSrcweir com.sun.star.beans.NamedValue [] aDriverSettings, 900*cdf0e10cSrcweir String [] aTableFilter 901*cdf0e10cSrcweir ) 902*cdf0e10cSrcweir throws com.sun.star.uno.Exception 903*cdf0e10cSrcweir { 904*cdf0e10cSrcweir // create the view and get the data source element 905*cdf0e10cSrcweir Object xDataSource = createDataSourceDescription(getProvider(),sDataSourceName); 906*cdf0e10cSrcweir 907*cdf0e10cSrcweir // set the values 908*cdf0e10cSrcweir XPropertySet xDataSourceProperties = 909*cdf0e10cSrcweir (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xDataSource); 910*cdf0e10cSrcweir 911*cdf0e10cSrcweir xDataSourceProperties.setPropertyValue("URL", sDataSourceURL ); 912*cdf0e10cSrcweir xDataSourceProperties.setPropertyValue("User", sUser ); 913*cdf0e10cSrcweir xDataSourceProperties.setPropertyValue("IsPasswordRequired", new Boolean( bNeedsPassword ) ); 914*cdf0e10cSrcweir xDataSourceProperties.setPropertyValue("LoginTimeout", new Integer( nTimeout ) ); 915*cdf0e10cSrcweir 916*cdf0e10cSrcweir if ( aTableFilter != null ) 917*cdf0e10cSrcweir xDataSourceProperties.setPropertyValue("TableFilter", aTableFilter ); 918*cdf0e10cSrcweir 919*cdf0e10cSrcweir // store the driver-specific settings 920*cdf0e10cSrcweir if (aDriverSettings != null) 921*cdf0e10cSrcweir { 922*cdf0e10cSrcweir Object xSettingsSet = xDataSourceProperties.getPropertyValue("DataSourceSettings"); 923*cdf0e10cSrcweir storeSettings( xSettingsSet, aDriverSettings); 924*cdf0e10cSrcweir } 925*cdf0e10cSrcweir 926*cdf0e10cSrcweir // save the data and dispose the view 927*cdf0e10cSrcweir // recover the view root 928*cdf0e10cSrcweir Object xViewRoot = getViewRoot(xDataSource); 929*cdf0e10cSrcweir 930*cdf0e10cSrcweir // commit the changes 931*cdf0e10cSrcweir XChangesBatch xUpdateControl = 932*cdf0e10cSrcweir (XChangesBatch) UnoRuntime.queryInterface(XChangesBatch.class,xViewRoot); 933*cdf0e10cSrcweir 934*cdf0e10cSrcweir xUpdateControl.commitChanges(); 935*cdf0e10cSrcweir 936*cdf0e10cSrcweir // now clean up 937*cdf0e10cSrcweir ((XComponent) UnoRuntime.queryInterface(XComponent.class, xViewRoot)).dispose(); 938*cdf0e10cSrcweir } 939*cdf0e10cSrcweir 940*cdf0e10cSrcweir /** This method gets the DataSourceDescription for a data source. 941*cdf0e10cSrcweir It either gets the existing entry or creates a new instance. 942*cdf0e10cSrcweir */ 943*cdf0e10cSrcweir Object createDataSourceDescription(XMultiServiceFactory xProvider, String sDataSourceName ) 944*cdf0e10cSrcweir throws com.sun.star.uno.Exception 945*cdf0e10cSrcweir { 946*cdf0e10cSrcweir // The service name: Need an update access: 947*cdf0e10cSrcweir final String cUpdatableView = "com.sun.star.configuration.ConfigurationUpdateAccess"; 948*cdf0e10cSrcweir 949*cdf0e10cSrcweir // The path to the DataSources set node 950*cdf0e10cSrcweir final String cDataSourcesPath = "/org.openoffice.Office.DataAccess/DataSources"; 951*cdf0e10cSrcweir 952*cdf0e10cSrcweir // creation arguments: nodepath 953*cdf0e10cSrcweir com.sun.star.beans.PropertyValue aPathArgument = new com.sun.star.beans.PropertyValue(); 954*cdf0e10cSrcweir aPathArgument.Name = "nodepath"; 955*cdf0e10cSrcweir aPathArgument.Value = cDataSourcesPath ; 956*cdf0e10cSrcweir 957*cdf0e10cSrcweir Object[] aArguments = new Object[1]; 958*cdf0e10cSrcweir aArguments[0] = aPathArgument; 959*cdf0e10cSrcweir 960*cdf0e10cSrcweir // create the view 961*cdf0e10cSrcweir Object xViewRoot = 962*cdf0e10cSrcweir xProvider.createInstanceWithArguments(cUpdatableView, aArguments); 963*cdf0e10cSrcweir 964*cdf0e10cSrcweir XNameAccess xSetOfDataSources = 965*cdf0e10cSrcweir (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class,xViewRoot); 966*cdf0e10cSrcweir 967*cdf0e10cSrcweir Object xDataSourceDescriptor = null; // the result 968*cdf0e10cSrcweir if ( xSetOfDataSources .hasByName( sDataSourceName )) 969*cdf0e10cSrcweir { 970*cdf0e10cSrcweir // the element is there 971*cdf0e10cSrcweir try 972*cdf0e10cSrcweir { 973*cdf0e10cSrcweir // the view should point to the element directly, so we need to extend the path 974*cdf0e10cSrcweir XHierarchicalName xComposePath = (XHierarchicalName) 975*cdf0e10cSrcweir UnoRuntime.queryInterface(XHierarchicalName.class, xSetOfDataSources ); 976*cdf0e10cSrcweir 977*cdf0e10cSrcweir String sElementPath = xComposePath.composeHierarchicalName( sDataSourceName ); 978*cdf0e10cSrcweir 979*cdf0e10cSrcweir // use the name of the element now 980*cdf0e10cSrcweir aPathArgument.Value = sElementPath; 981*cdf0e10cSrcweir 982*cdf0e10cSrcweir // create another view now 983*cdf0e10cSrcweir Object[] aDeepArguments = new Object[1]; 984*cdf0e10cSrcweir aDeepArguments[0] = aPathArgument; 985*cdf0e10cSrcweir 986*cdf0e10cSrcweir // create the view 987*cdf0e10cSrcweir xDataSourceDescriptor = 988*cdf0e10cSrcweir xProvider.createInstanceWithArguments(cUpdatableView, aDeepArguments); 989*cdf0e10cSrcweir 990*cdf0e10cSrcweir if ( xDataSourceDescriptor != null) // all went fine 991*cdf0e10cSrcweir { 992*cdf0e10cSrcweir // dispose the other view 993*cdf0e10cSrcweir ((XComponent)UnoRuntime.queryInterface(XComponent.class, xViewRoot)).dispose(); 994*cdf0e10cSrcweir xViewRoot = null; 995*cdf0e10cSrcweir } 996*cdf0e10cSrcweir } 997*cdf0e10cSrcweir catch (Exception e) 998*cdf0e10cSrcweir { 999*cdf0e10cSrcweir // something went wrong, we retry with a new element 1000*cdf0e10cSrcweir System.err.println("WARNING: An exception occurred while creating a view for an existing data source: " + e); 1001*cdf0e10cSrcweir xDataSourceDescriptor = null; 1002*cdf0e10cSrcweir } 1003*cdf0e10cSrcweir } 1004*cdf0e10cSrcweir 1005*cdf0e10cSrcweir // do we have a result element yet ? 1006*cdf0e10cSrcweir if ( xDataSourceDescriptor == null) 1007*cdf0e10cSrcweir { 1008*cdf0e10cSrcweir // get the container 1009*cdf0e10cSrcweir XNameContainer xSetUpdate = 1010*cdf0e10cSrcweir (XNameContainer)UnoRuntime.queryInterface(XNameContainer.class, xViewRoot); 1011*cdf0e10cSrcweir 1012*cdf0e10cSrcweir // create a new detached set element (instance of DataSourceDescription) 1013*cdf0e10cSrcweir XSingleServiceFactory xElementFactory = 1014*cdf0e10cSrcweir (XSingleServiceFactory)UnoRuntime.queryInterface(XSingleServiceFactory.class, xSetUpdate); 1015*cdf0e10cSrcweir 1016*cdf0e10cSrcweir // the new element is the result ! 1017*cdf0e10cSrcweir xDataSourceDescriptor = xElementFactory.createInstance(); 1018*cdf0e10cSrcweir 1019*cdf0e10cSrcweir // insert it - this also names the element 1020*cdf0e10cSrcweir xSetUpdate.insertByName( sDataSourceName , xDataSourceDescriptor ); 1021*cdf0e10cSrcweir } 1022*cdf0e10cSrcweir 1023*cdf0e10cSrcweir return xDataSourceDescriptor ; 1024*cdf0e10cSrcweir } 1025*cdf0e10cSrcweir 1026*cdf0e10cSrcweir /// this method stores a number of settings in a set node containing DataSourceSetting objects 1027*cdf0e10cSrcweir void storeSettings(Object xSettingsSet, com.sun.star.beans.NamedValue [] aSettings ) 1028*cdf0e10cSrcweir throws com.sun.star.uno.Exception 1029*cdf0e10cSrcweir { 1030*cdf0e10cSrcweir if (aSettings == null) 1031*cdf0e10cSrcweir return; 1032*cdf0e10cSrcweir 1033*cdf0e10cSrcweir // get the settings set as a container 1034*cdf0e10cSrcweir XNameContainer xSettingsContainer = 1035*cdf0e10cSrcweir (XNameContainer) UnoRuntime.queryInterface( XNameContainer.class, xSettingsSet); 1036*cdf0e10cSrcweir 1037*cdf0e10cSrcweir // and get a factory interface for creating the entries 1038*cdf0e10cSrcweir XSingleServiceFactory xSettingsFactory = 1039*cdf0e10cSrcweir (XSingleServiceFactory) UnoRuntime.queryInterface(XSingleServiceFactory.class, xSettingsSet); 1040*cdf0e10cSrcweir 1041*cdf0e10cSrcweir // now insert the individual settings 1042*cdf0e10cSrcweir for (int i = 0; i < aSettings.length; ++i) { 1043*cdf0e10cSrcweir // create a DataSourceSetting object 1044*cdf0e10cSrcweir XPropertySet xSetting = (XPropertySet) 1045*cdf0e10cSrcweir UnoRuntime.queryInterface( XPropertySet.class, xSettingsFactory.createInstance() ); 1046*cdf0e10cSrcweir 1047*cdf0e10cSrcweir // can set the value before inserting 1048*cdf0e10cSrcweir xSetting.setPropertyValue( "Value", aSettings[i].Value ); 1049*cdf0e10cSrcweir 1050*cdf0e10cSrcweir // and now insert or replace as appropriate 1051*cdf0e10cSrcweir if (xSettingsContainer.hasByName( aSettings[i].Name )) 1052*cdf0e10cSrcweir xSettingsContainer.replaceByName( aSettings[i].Name, xSetting ); 1053*cdf0e10cSrcweir else 1054*cdf0e10cSrcweir xSettingsContainer.insertByName( aSettings[i].Name, xSetting ); 1055*cdf0e10cSrcweir } 1056*cdf0e10cSrcweir } 1057*cdf0e10cSrcweir 1058*cdf0e10cSrcweir // HELPER FUNCTIONS 1059*cdf0e10cSrcweir 1060*cdf0e10cSrcweir /// This method get the view root node given an interface to any node in the view 1061*cdf0e10cSrcweir public static Object getViewRoot(Object xElement) 1062*cdf0e10cSrcweir { 1063*cdf0e10cSrcweir Object xResult = xElement; 1064*cdf0e10cSrcweir 1065*cdf0e10cSrcweir // set the result to its parent until that would be null 1066*cdf0e10cSrcweir Object xParent; 1067*cdf0e10cSrcweir do 1068*cdf0e10cSrcweir { 1069*cdf0e10cSrcweir XChild xParentAccess = 1070*cdf0e10cSrcweir (XChild) UnoRuntime.queryInterface(XChild.class,xResult); 1071*cdf0e10cSrcweir 1072*cdf0e10cSrcweir if (xParentAccess != null) 1073*cdf0e10cSrcweir xParent = xParentAccess.getParent(); 1074*cdf0e10cSrcweir else 1075*cdf0e10cSrcweir xParent = null; 1076*cdf0e10cSrcweir 1077*cdf0e10cSrcweir if (xParent != null) 1078*cdf0e10cSrcweir xResult = xParent; 1079*cdf0e10cSrcweir } 1080*cdf0e10cSrcweir while (xParent != null); 1081*cdf0e10cSrcweir 1082*cdf0e10cSrcweir return xResult; 1083*cdf0e10cSrcweir } 1084*cdf0e10cSrcweir 1085*cdf0e10cSrcweir // workaround methods for unimplemented functionality 1086*cdf0e10cSrcweir 1087*cdf0e10cSrcweir /// WORKAROUND: does the same as xNamedItem.setName(sNewName) should do 1088*cdf0e10cSrcweir void renameSetItem(XNamed xNamedItem, String sNewName) 1089*cdf0e10cSrcweir throws com.sun.star.uno.Exception 1090*cdf0e10cSrcweir { 1091*cdf0e10cSrcweir XChild xChildItem = (XChild) 1092*cdf0e10cSrcweir UnoRuntime.queryInterface(XChild.class, xNamedItem); 1093*cdf0e10cSrcweir 1094*cdf0e10cSrcweir XNameContainer xParentSet = (XNameContainer) 1095*cdf0e10cSrcweir UnoRuntime.queryInterface( XNameContainer.class, xChildItem.getParent() ); 1096*cdf0e10cSrcweir 1097*cdf0e10cSrcweir String sOldName = xNamedItem.getName(); 1098*cdf0e10cSrcweir 1099*cdf0e10cSrcweir // now rename the item 1100*cdf0e10cSrcweir xParentSet.removeByName(sOldName); 1101*cdf0e10cSrcweir xParentSet.insertByName(sNewName,xNamedItem); 1102*cdf0e10cSrcweir } 1103*cdf0e10cSrcweir 1104*cdf0e10cSrcweir /// WORKAROUND: does the same as xChildItem.setParent( xNewParent ) should do 1105*cdf0e10cSrcweir void moveSetItem(XChild xChildItem, XNameContainer xNewParent) 1106*cdf0e10cSrcweir throws com.sun.star.uno.Exception 1107*cdf0e10cSrcweir { 1108*cdf0e10cSrcweir XNamed xNamedItem = (XNamed) 1109*cdf0e10cSrcweir UnoRuntime.queryInterface(XNamed.class, xChildItem); 1110*cdf0e10cSrcweir 1111*cdf0e10cSrcweir XNameContainer xOldParent = (XNameContainer) 1112*cdf0e10cSrcweir UnoRuntime.queryInterface( XNameContainer.class, xChildItem.getParent() ); 1113*cdf0e10cSrcweir 1114*cdf0e10cSrcweir String sItemName = xNamedItem.getName(); 1115*cdf0e10cSrcweir 1116*cdf0e10cSrcweir // now rename the item 1117*cdf0e10cSrcweir xOldParent.removeByName(sItemName); 1118*cdf0e10cSrcweir xNewParent.insertByName(sItemName,xChildItem); 1119*cdf0e10cSrcweir } 1120*cdf0e10cSrcweir 1121*cdf0e10cSrcweir 1122*cdf0e10cSrcweir // ------- the end ----------- 1123*cdf0e10cSrcweir } 1124