2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import com
.sun
.star
.uno
.*;
22 import com
.sun
.star
.lang
.*;
23 import com
.sun
.star
.container
.*;
24 import com
.sun
.star
.beans
.*;
25 import com
.sun
.star
.util
.*;
28 * This <CODE>ConfigHelper</CODE> makes it possible to access the
29 * configuration and change their content.<P>
32 * Listing of the <CODE>Configuration</CODE> Views.xcu:<P>
33 * <oor:component-data xmlns:oor="http://openoffice.org/2001/registry" xmlns:xs="http://www.w3.org/2001/XMLSchema" oor:name="Views" oor:package="org.openoffice.Office"><p>
34 * <node oor:name="Windows"><P>
35 * <<node oor:name="SplitWindow0" oor:op="replace"><P>
36 * <prop oor:name="Visible" oor:type="xs:boolean"><P>
37 * <value>false</value><P>
39 * <prop oor:name="WindowState" oor:type="xs:string"><P>
42 * <node oor:name="UserData"><P>
43 * <prop oor:name="UserItem" oor:op="replace"
44 * oor:type="xs:string"><P>
45 * <value>V1,2,0</value><P>
51 * <CODE>Definition</CODE><P>
53 * <li><CODE><node oor:name="Windows"></CODE>
54 * represents a <CODE>Set</CODE> and is a <CODE>XNameContainer</CODE></LI>
55 * <li><CODE><node oor:name="SplitWindow0"></CODE>
56 * represents a <CODE>Group</CODE> and is a <CODE>XNameReplace</CODE></LI>
57 * <li><CODE><prop oor:name="Visible"></CODE>
58 * represents a pr<CODE></CODE>operty of the group</li>
59 * <li><CODE><node oor:name="UserData"></CODE>
60 * represents a <CODE>extensible group</CODE> and is a <CODE>XNameContainer</CODE></LI>
61 * <li><CODE><prop oor:name="UserItem"></CODE>
62 * represents a <CODE>property</CODE> of the extensible group</LI>
64 * We assume in the following examples the existence of:<P>
65 * <CODE>ConfigHelper aConfig = new ConfigHelper(xMSF, "org.openoffice.Office.Views", false);</CODE>
67 * <li>If you like to insert a new <CODE>Group</CODE> into the <CODE>Set</CODE> "Windows":<p>
68 * <CODE>XNameReplace xMyGroup = aConfig.getOrInsertGroup("Windows", "myGroup");</CODE><P>
69 * The method <CODE>getOrInsertGroup()</CODE> uses the
70 * <CODE>XSingleServiceFactory</CODE> to create the skeleton of a new group.
73 * <li>If you like to change the property "WindowState" of "myGroup"
74 * of the Set "Windows"<p>
75 * <CODE>aConfig.updateGroupProperty(
76 * "Windows","myGroup", "WindowState", "952,180,244,349;1;0,0,0,0;");</CODE>
78 * <li>If you like to change the property "myProp" of the extensible group
79 * "myExtGroup" which is an extensible group of "my2ndGroup" of the
81 * <CODE>aConfig.insertOrUpdateExtensibleGroupProperty(
82 * "Windows", "my2ndGroup", "myExtGroup", "myProp","TheValue");</CODE>
86 public class ConfigHelper
88 private final XHierarchicalNameAccess m_xConfig
;
91 public ConfigHelper(XMultiServiceFactory xSMGR
,
94 throws com
.sun
.star
.uno
.Exception
96 XMultiServiceFactory xConfigRoot
= UnoRuntime
.queryInterface(
97 XMultiServiceFactory
.class,
98 xSMGR
.createInstance("com.sun.star.configuration.ConfigurationProvider"));
100 PropertyValue
[] lParams
= new PropertyValue
[1];
101 lParams
[0] = new PropertyValue();
102 lParams
[0].Name
= "nodepath";
103 lParams
[0].Value
= sConfigPath
;
107 aConfig
= xConfigRoot
.createInstanceWithArguments(
108 "com.sun.star.configuration.ConfigurationAccess",
111 aConfig
= xConfigRoot
.createInstanceWithArguments(
112 "com.sun.star.configuration.ConfigurationUpdateAccess",
115 m_xConfig
= UnoRuntime
.queryInterface(
116 XHierarchicalNameAccess
.class,
119 if (m_xConfig
== null)
120 throw new com
.sun
.star
.uno
.Exception("Could not open configuration \""+sConfigPath
+"\"");
125 * Updates the configuration.<p>
126 * This must be called after you have changed the configuration
127 * else you changes will be lost.
133 XChangesBatch xBatch
= UnoRuntime
.queryInterface(
136 xBatch
.commitChanges();
138 catch(com
.sun
.star
.uno
.Exception ex
)
150 * Insert a structured node (group) in a name container (set)
151 * or else update it and return the <CODE>XNameReplace</CODE> of it.<P>
152 * The <CODE>XSingleServiceFacttory</CODE> of the <CODE>set</CODE> will be used
153 * to create a new group. This group is specific to its set and
154 * creates defined entries.
155 * @return The [inserted] group of the set
156 * @param groupName The name of the group which should be returned
157 * @param setName The name of the set
158 * @throws com.sun.star.uno.Exception throws
159 * <CODE>com.sun.star.uno.Exception</CODE> on any error.
161 public XNameReplace
getOrInsertGroup(String setName
, String groupName
)
162 throws com
.sun
.star
.uno
.Exception
165 XNameContainer xSetCont
= this.getSet(setName
);
167 XNameReplace xChildAccess
= null;
170 xSetCont
.getByName(groupName
);
171 xChildAccess
= UnoRuntime
.queryInterface(
172 XNameReplace
.class,xSetCont
);
173 } catch(com
.sun
.star
.container
.NoSuchElementException e
) {
174 // proceed with inserting
177 if (xChildAccess
== null) {
178 XSingleServiceFactory xChildfactory
= UnoRuntime
.queryInterface(XSingleServiceFactory
.class,xSetCont
);
180 Object xNewChild
= xChildfactory
.createInstance();
182 xSetCont
.insertByName(groupName
, xNewChild
);
184 xChildAccess
= UnoRuntime
.queryInterface(XNameContainer
.class,xNewChild
);
191 * Update a property of a group container of a set container
192 * @param setName the name of the <CODE>set</CODE> which contains the <CODE>group</CODE>
193 * @param groupName the name of the <CODE>group</CODE> which property should be changed
194 * @param propName the name of the property which should be changed
195 * @param propValue the value the property should get
196 * @throws com.sun.star.uno.Exception throws <CODE>com.sun.star.uno.Exception</CODE> on any error.
198 public void updateGroupProperty(String setName
,
202 throws com
.sun
.star
.uno
.Exception
204 XNameContainer xSetCont
= this.getSet(setName
);
206 XPropertySet xProp
= null;
208 xProp
= UnoRuntime
.queryInterface(
210 xSetCont
.getByName(groupName
));
211 } catch (com
.sun
.star
.container
.NoSuchElementException ex
){
212 throw new com
.sun
.star
.uno
.Exception(ex
,
213 "could not get group '" + groupName
+
214 "' from set '"+ setName
+"'");
217 xProp
.setPropertyValue(propName
, propValue
);
218 } catch (com
.sun
.star
.uno
.Exception ex
) {
219 throw new com
.sun
.star
.uno
.Exception(ex
,
220 "could not set property '" + propName
+
221 "' from group '"+ groupName
+
222 "' from set '"+ setName
+"'");
228 * Insert a property in an extensible group container or else update it
229 * @param setName the name of the <CODE>set</CODE> which contains the <CODE>group</CODE>
230 * @param group The name of the <CODE>group</CODE> which contains the <CODE>extensible group</CODE>.
231 * @param extGroup The name of the <CODE>extensible group</CODE> which
232 * [should] contain the property
233 * @param propName The name of the property.
234 * @param propValue The value of the property.
235 * @throws com.sun.star.uno.Exception throws <CODE>com.sun.star.uno.Exception</CODE> on any error.
237 public void insertOrUpdateExtensibleGroupProperty(
243 throws com
.sun
.star
.uno
.Exception
245 XNameContainer xSetCont
= this.getSet(setName
);
247 XNameReplace xGroupAccess
= null;
248 XNameContainer xExtGroupCont
= null;
251 Object xGroup
=xSetCont
.getByName(group
);
252 xGroupAccess
= UnoRuntime
.queryInterface(
253 XNameReplace
.class,xGroup
);
254 } catch(com
.sun
.star
.container
.NoSuchElementException ex
) {
255 throw new com
.sun
.star
.uno
.Exception(ex
,
256 "could not get group '" + group
+
257 "' from set '"+ setName
+"'");
261 Object xGroup
=xGroupAccess
.getByName(extGroup
);
262 xExtGroupCont
= UnoRuntime
.queryInterface(
263 XNameContainer
.class,xGroup
);
264 } catch(com
.sun
.star
.container
.NoSuchElementException ex
) {
265 throw new com
.sun
.star
.uno
.Exception(ex
,
266 "could not get extensible group '"+extGroup
+
267 "' from group '"+ group
+
268 "' from set '"+ setName
+"'");
272 xExtGroupCont
.insertByName(propName
, propValue
);
274 catch(com
.sun
.star
.container
.ElementExistException e
) {
275 xExtGroupCont
.replaceByName(propName
, propValue
);
282 * Returns a <CODE>XNameContainer</CODE> of the <CODE>Set</CODE>
283 * of the <CODE>Configuration</CODE>
284 * @param setName the name of the Set which should be returned
285 * @throws com.sun.star.uno.Exception on any error
286 * @return A XNameContainer of the Set
288 private XNameContainer
getSet(String setName
)
289 throws com
.sun
.star
.uno
.Exception
291 XNameReplace xCont
= UnoRuntime
.queryInterface(XNameReplace
.class, m_xConfig
);
293 Object oSet
= xCont
.getByName(setName
);
296 throw new com
.sun
.star
.uno
.Exception(
297 "could not get set '" + setName
+ ": null");
299 return UnoRuntime
.queryInterface(
300 XNameContainer
.class, oSet
);