1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 import com.sun.star.uno.UnoRuntime;
25 import com.sun.star.uno.XComponentContext;
26 import com.sun.star.lang.XMultiComponentFactory;
27 import com.sun.star.beans.XPropertySet;
28 import com.sun.star.beans.PropertyValue;
29 import com.sun.star.beans.UnknownPropertyException;
30 
31 /*
32  *
33  * @author  Carsten Driesner
34  * Provides example code how to access and use the
35  * path pathsettings servce.
36  */
37 public class PathSettingsTest extends java.lang.Object {
38 
39     /*
40      * List of pre-defined path variables supported by
41      * the path settings service.
42      */
43     private static String[] predefinedPathProperties = {
44         "Addin",
45         "AutoCorrect",
46         "AutoText",
47         "Backup",
48         "Basic",
49         "Bitmap",
50         "Config",
51         "Dictionary",
52         "Favorite",
53         "Filter",
54         "Gallery",
55         "Graphic",
56         "Help",
57         "Linguistic",
58         "Module",
59         "Palette",
60         "Plugin",
61         "Storage",
62         "Temp",
63         "Template",
64         "UIConfig",
65         "UserConfig",
66         "UserDictionary",
67         "Work"
68     };
69 
70     /*
71      * @param args the command line arguments
72      */
main(String[] args)73     public static void main(String[] args) {
74 
75         XComponentContext xRemoteContext = null;
76         XMultiComponentFactory xRemoteServiceManager = null;
77         XPropertySet xPathSettingsService = null;
78 
79         try {
80             // get the remote office context. If necessary a new office
81             // process is started
82             xRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
83             System.out.println("Connected to a running office ...");
84             xRemoteServiceManager = xRemoteContext.getServiceManager();
85 
86             Object pathSubst = xRemoteServiceManager.createInstanceWithContext(
87                 "com.sun.star.comp.framework.PathSettings", xRemoteContext );
88             xPathSettingsService = (XPropertySet)UnoRuntime.queryInterface(
89                 XPropertySet.class, pathSubst);
90 
91             /* Work with path settings */
92             workWithPathSettings( xPathSettingsService );
93         }
94         catch (java.lang.Exception e){
95             e.printStackTrace();
96         }
97         finally {
98             System.exit(0);
99         }
100     }
101 
102     /*
103      * Retrieve and set path properties from path settings service
104      * @param xPathSettingsService the path settings service
105      */
workWithPathSettings( XPropertySet xPathSettingsService )106     public static void workWithPathSettings( XPropertySet xPathSettingsService )
107     {
108         if ( xPathSettingsService != null ) {
109             for ( int i=0; i<predefinedPathProperties.length; i++ ) {
110                 try {
111                         /* Retrieve values for path properties from path settings
112                          * service*/
113                         Object aValue = xPathSettingsService.getPropertyValue(
114                                             predefinedPathProperties[i] );
115 
116                         // getPropertyValue returns an Object, you have to cast
117                         // it to type that you need
118                         String aPath = (String)aValue;
119                         System.out.println( "Property="+ predefinedPathProperties[i]
120                                             + " Path=" + aPath );
121                 }
122                 catch ( com.sun.star.beans.UnknownPropertyException e) {
123                     System.err.println( "UnknownPropertyException has been thrown accessing "+predefinedPathProperties[i]);
124                 }
125                 catch ( com.sun.star.lang.WrappedTargetException e ) {
126                     System.err.println( "WrappedTargetException has been thrown accessing "+predefinedPathProperties[i]);
127                 }
128             }
129 
130             // Try to modfiy the work path property. After running this example
131             // you should see the new value of "My Documents" in the path options
132             // tab page, accessible via "Tools - Options - [Star|Open]Office -
133             // Paths".
134             // If you want to revert the changes, you can also do it with the
135             // path tab page.
136             try {
137                 xPathSettingsService.setPropertyValue( "Work", "$(temp)" );
138                 String aValue = (String)xPathSettingsService.getPropertyValue( "Work" );
139                 System.out.println( "\nNote: The example changes your current "
140                                     +"setting of the work path!\nThe work path "
141                                     +"should be now=" + aValue );
142             }
143             catch ( com.sun.star.beans.UnknownPropertyException e) {
144                 System.err.println( "UnknownPropertyException has been thrown accessing PathSettings service");
145             }
146             catch ( com.sun.star.lang.WrappedTargetException e ) {
147                 System.err.println( "WrappedTargetException has been thrown accessing PathSettings service");
148             }
149             catch ( com.sun.star.beans.PropertyVetoException e ) {
150                 System.err.println( "PropertyVetoException has been thrown accessing PathSettings service");
151             }
152             catch ( com.sun.star.lang.IllegalArgumentException e ) {
153                 System.err.println( "IllegalArgumentException has been thrown accessing PathSettings service");
154             }
155         }
156     }
157 }
158