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