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