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 com.sun.star.uno.UnoRuntime;
25cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
26cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
27cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
28cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
29cdf0e10cSrcweir import com.sun.star.beans.UnknownPropertyException;
30cdf0e10cSrcweir 
31cdf0e10cSrcweir /*
32cdf0e10cSrcweir  *
33cdf0e10cSrcweir  * @author  Carsten Driesner
34cdf0e10cSrcweir  * Provides example code how to access and use the
35cdf0e10cSrcweir  * path pathsettings servce.
36cdf0e10cSrcweir  */
37cdf0e10cSrcweir public class PathSettingsTest extends java.lang.Object {
38cdf0e10cSrcweir 
39cdf0e10cSrcweir     /*
40cdf0e10cSrcweir      * List of pre-defined path variables supported by
41cdf0e10cSrcweir      * the path settings service.
42cdf0e10cSrcweir      */
43cdf0e10cSrcweir     private static String[] predefinedPathProperties = {
44cdf0e10cSrcweir         "Addin",
45cdf0e10cSrcweir         "AutoCorrect",
46cdf0e10cSrcweir         "AutoText",
47cdf0e10cSrcweir         "Backup",
48cdf0e10cSrcweir         "Basic",
49cdf0e10cSrcweir         "Bitmap",
50cdf0e10cSrcweir         "Config",
51cdf0e10cSrcweir         "Dictionary",
52cdf0e10cSrcweir         "Favorite",
53cdf0e10cSrcweir         "Filter",
54cdf0e10cSrcweir         "Gallery",
55cdf0e10cSrcweir         "Graphic",
56cdf0e10cSrcweir         "Help",
57cdf0e10cSrcweir         "Linguistic",
58cdf0e10cSrcweir         "Module",
59cdf0e10cSrcweir         "Palette",
60cdf0e10cSrcweir         "Plugin",
61cdf0e10cSrcweir         "Storage",
62cdf0e10cSrcweir         "Temp",
63cdf0e10cSrcweir         "Template",
64cdf0e10cSrcweir         "UIConfig",
65cdf0e10cSrcweir         "UserConfig",
66cdf0e10cSrcweir         "UserDictionary",
67cdf0e10cSrcweir         "Work"
68cdf0e10cSrcweir     };
69cdf0e10cSrcweir 
70cdf0e10cSrcweir     /*
71cdf0e10cSrcweir      * @param args the command line arguments
72cdf0e10cSrcweir      */
main(String[] args)73cdf0e10cSrcweir     public static void main(String[] args) {
74cdf0e10cSrcweir 
75cdf0e10cSrcweir         XComponentContext xRemoteContext = null;
76cdf0e10cSrcweir         XMultiComponentFactory xRemoteServiceManager = null;
77cdf0e10cSrcweir         XPropertySet xPathSettingsService = null;
78cdf0e10cSrcweir 
79cdf0e10cSrcweir         try {
80cdf0e10cSrcweir             // get the remote office context. If necessary a new office
81cdf0e10cSrcweir             // process is started
82cdf0e10cSrcweir             xRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
83cdf0e10cSrcweir             System.out.println("Connected to a running office ...");
84cdf0e10cSrcweir             xRemoteServiceManager = xRemoteContext.getServiceManager();
85cdf0e10cSrcweir 
86cdf0e10cSrcweir             Object pathSubst = xRemoteServiceManager.createInstanceWithContext(
87cdf0e10cSrcweir                 "com.sun.star.comp.framework.PathSettings", xRemoteContext );
88cdf0e10cSrcweir             xPathSettingsService = (XPropertySet)UnoRuntime.queryInterface(
89cdf0e10cSrcweir                 XPropertySet.class, pathSubst);
90cdf0e10cSrcweir 
91cdf0e10cSrcweir             /* Work with path settings */
92cdf0e10cSrcweir             workWithPathSettings( xPathSettingsService );
93cdf0e10cSrcweir         }
94cdf0e10cSrcweir         catch (java.lang.Exception e){
95cdf0e10cSrcweir             e.printStackTrace();
96cdf0e10cSrcweir         }
97cdf0e10cSrcweir         finally {
98cdf0e10cSrcweir             System.exit(0);
99cdf0e10cSrcweir         }
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     /*
103cdf0e10cSrcweir      * Retrieve and set path properties from path settings service
104cdf0e10cSrcweir      * @param xPathSettingsService the path settings service
105cdf0e10cSrcweir      */
workWithPathSettings( XPropertySet xPathSettingsService )106cdf0e10cSrcweir     public static void workWithPathSettings( XPropertySet xPathSettingsService )
107cdf0e10cSrcweir     {
108cdf0e10cSrcweir         if ( xPathSettingsService != null ) {
109cdf0e10cSrcweir             for ( int i=0; i<predefinedPathProperties.length; i++ ) {
110cdf0e10cSrcweir                 try {
111cdf0e10cSrcweir                         /* Retrieve values for path properties from path settings
112cdf0e10cSrcweir                          * service*/
113cdf0e10cSrcweir                         Object aValue = xPathSettingsService.getPropertyValue(
114cdf0e10cSrcweir                                             predefinedPathProperties[i] );
115cdf0e10cSrcweir 
116cdf0e10cSrcweir                         // getPropertyValue returns an Object, you have to cast
117cdf0e10cSrcweir                         // it to type that you need
118cdf0e10cSrcweir                         String aPath = (String)aValue;
119cdf0e10cSrcweir                         System.out.println( "Property="+ predefinedPathProperties[i]
120cdf0e10cSrcweir                                             + " Path=" + aPath );
121cdf0e10cSrcweir                 }
122cdf0e10cSrcweir                 catch ( com.sun.star.beans.UnknownPropertyException e) {
123cdf0e10cSrcweir                     System.err.println( "UnknownPropertyException has been thrown accessing "+predefinedPathProperties[i]);
124cdf0e10cSrcweir                 }
125cdf0e10cSrcweir                 catch ( com.sun.star.lang.WrappedTargetException e ) {
126cdf0e10cSrcweir                     System.err.println( "WrappedTargetException has been thrown accessing "+predefinedPathProperties[i]);
127cdf0e10cSrcweir                 }
128cdf0e10cSrcweir             }
129cdf0e10cSrcweir 
130cdf0e10cSrcweir             // Try to modfiy the work path property. After running this example
131cdf0e10cSrcweir             // you should see the new value of "My Documents" in the path options
132cdf0e10cSrcweir             // tab page, accessible via "Tools - Options - [Star|Open]Office -
133cdf0e10cSrcweir             // Paths".
134cdf0e10cSrcweir             // If you want to revert the changes, you can also do it with the
135cdf0e10cSrcweir             // path tab page.
136cdf0e10cSrcweir             try {
137cdf0e10cSrcweir                 xPathSettingsService.setPropertyValue( "Work", "$(temp)" );
138cdf0e10cSrcweir                 String aValue = (String)xPathSettingsService.getPropertyValue( "Work" );
139cdf0e10cSrcweir                 System.out.println( "\nNote: The example changes your current "
140cdf0e10cSrcweir                                     +"setting of the work path!\nThe work path "
141cdf0e10cSrcweir                                     +"should be now=" + aValue );
142cdf0e10cSrcweir             }
143cdf0e10cSrcweir             catch ( com.sun.star.beans.UnknownPropertyException e) {
144cdf0e10cSrcweir                 System.err.println( "UnknownPropertyException has been thrown accessing PathSettings service");
145cdf0e10cSrcweir             }
146cdf0e10cSrcweir             catch ( com.sun.star.lang.WrappedTargetException e ) {
147cdf0e10cSrcweir                 System.err.println( "WrappedTargetException has been thrown accessing PathSettings service");
148cdf0e10cSrcweir             }
149cdf0e10cSrcweir             catch ( com.sun.star.beans.PropertyVetoException e ) {
150cdf0e10cSrcweir                 System.err.println( "PropertyVetoException has been thrown accessing PathSettings service");
151cdf0e10cSrcweir             }
152cdf0e10cSrcweir             catch ( com.sun.star.lang.IllegalArgumentException e ) {
153cdf0e10cSrcweir                 System.err.println( "IllegalArgumentException has been thrown accessing PathSettings service");
154cdf0e10cSrcweir             }
155cdf0e10cSrcweir         }
156cdf0e10cSrcweir     }
157cdf0e10cSrcweir }
158