1 /*************************************************************************
3 * $RCSfile: PathSettingsTest.java,v $
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
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
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
= {
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;
97 // get the remote office context. If necessary a new office
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
){
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
++ ) {
128 /* Retrieve values for path properties from path settings
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 -
151 // If you want to revert the changes, you can also do it with the
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");