merge the formfield patch from ooo-build
[ooovba.git] / odk / examples / DevelopersGuide / OfficeDev / PathSettings / PathSettingsTest.java
blobb155b7e2f3aeccea20350d6642215a950f662c6c
1 /*************************************************************************
3 * $RCSfile: PathSettingsTest.java,v $
5 * $Revision: 1.5 $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:48:02 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.uno.XComponentContext;
43 import com.sun.star.lang.XMultiComponentFactory;
44 import com.sun.star.beans.XPropertySet;
45 import com.sun.star.beans.PropertyValue;
46 import com.sun.star.beans.UnknownPropertyException;
50 * @author Carsten Driesner
51 * Provides example code how to access and use the
52 * path pathsettings servce.
54 public class PathSettingsTest extends java.lang.Object {
57 * List of pre-defined path variables supported by
58 * the path settings service.
60 private static String[] predefinedPathProperties = {
61 "Addin",
62 "AutoCorrect",
63 "AutoText",
64 "Backup",
65 "Basic",
66 "Bitmap",
67 "Config",
68 "Dictionary",
69 "Favorite",
70 "Filter",
71 "Gallery",
72 "Graphic",
73 "Help",
74 "Linguistic",
75 "Module",
76 "Palette",
77 "Plugin",
78 "Storage",
79 "Temp",
80 "Template",
81 "UIConfig",
82 "UserConfig",
83 "UserDictionary",
84 "Work"
88 * @param args the command line arguments
90 public static void main(String[] args) {
92 XComponentContext xRemoteContext = null;
93 XMultiComponentFactory xRemoteServiceManager = null;
94 XPropertySet xPathSettingsService = null;
96 try {
97 // get the remote office context. If necessary a new office
98 // process is started
99 xRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
100 System.out.println("Connected to a running office ...");
101 xRemoteServiceManager = xRemoteContext.getServiceManager();
103 Object pathSubst = xRemoteServiceManager.createInstanceWithContext(
104 "com.sun.star.comp.framework.PathSettings", xRemoteContext );
105 xPathSettingsService = (XPropertySet)UnoRuntime.queryInterface(
106 XPropertySet.class, pathSubst);
108 /* Work with path settings */
109 workWithPathSettings( xPathSettingsService );
111 catch (java.lang.Exception e){
112 e.printStackTrace();
114 finally {
115 System.exit(0);
120 * Retrieve and set path properties from path settings service
121 * @param xPathSettingsService the path settings service
123 public static void workWithPathSettings( XPropertySet xPathSettingsService )
125 if ( xPathSettingsService != null ) {
126 for ( int i=0; i<predefinedPathProperties.length; i++ ) {
127 try {
128 /* Retrieve values for path properties from path settings
129 * service*/
130 Object aValue = xPathSettingsService.getPropertyValue(
131 predefinedPathProperties[i] );
133 // getPropertyValue returns an Object, you have to cast
134 // it to type that you need
135 String aPath = (String)aValue;
136 System.out.println( "Property="+ predefinedPathProperties[i]
137 + " Path=" + aPath );
139 catch ( com.sun.star.beans.UnknownPropertyException e) {
140 System.err.println( "UnknownPropertyException has been thrown accessing "+predefinedPathProperties[i]);
142 catch ( com.sun.star.lang.WrappedTargetException e ) {
143 System.err.println( "WrappedTargetException has been thrown accessing "+predefinedPathProperties[i]);
147 // Try to modfiy the work path property. After running this example
148 // you should see the new value of "My Documents" in the path options
149 // tab page, accessible via "Tools - Options - [Star|Open]Office -
150 // Paths".
151 // If you want to revert the changes, you can also do it with the
152 // path tab page.
153 try {
154 xPathSettingsService.setPropertyValue( "Work", "$(temp)" );
155 String aValue = (String)xPathSettingsService.getPropertyValue( "Work" );
156 System.out.println( "\nNote: The example changes your current "
157 +"setting of the work path!\nThe work path "
158 +"should be now=" + aValue );
160 catch ( com.sun.star.beans.UnknownPropertyException e) {
161 System.err.println( "UnknownPropertyException has been thrown accessing PathSettings service");
163 catch ( com.sun.star.lang.WrappedTargetException e ) {
164 System.err.println( "WrappedTargetException has been thrown accessing PathSettings service");
166 catch ( com.sun.star.beans.PropertyVetoException e ) {
167 System.err.println( "PropertyVetoException has been thrown accessing PathSettings service");
169 catch ( com.sun.star.lang.IllegalArgumentException e ) {
170 System.err.println( "IllegalArgumentException has been thrown accessing PathSettings service");