1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: valuenode.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef _CONFIGMGR_TREE_VALUENODE_HXX
32 #define _CONFIGMGR_TREE_VALUENODE_HXX
34 #include "attributes.hxx"
35 #include "anypair.hxx"
36 #include <com/sun/star/uno/Any.h>
37 #include <rtl/ustring.hxx>
40 #ifndef INCLUDED_MEMORY
42 #define INCLUDED_MEMORY
48 namespace css
= com::sun::star
;
49 namespace uno
= css::uno
;
56 namespace treeop
{ struct NoChildCopy
{}; struct DeepChildCopy
{}; enum { ALL_LEVELS
= -1 }; }
57 //==========================================================================
59 //==========================================================================
62 virtual void handle(ValueNode
const&) = 0;
63 virtual void handle(ISubtree
const&) = 0;
65 void applyToNode(INode
const&);
66 void applyToChildren(ISubtree
const&);
68 virtual ~NodeAction() {}
71 struct NodeModification
73 virtual void handle(ValueNode
&) = 0;
74 virtual void handle(ISubtree
&) = 0;
76 void applyToNode(INode
&);
77 void applyToChildren(ISubtree
&);
79 virtual ~NodeModification() {}
84 rtl::OUString m_aName
;
85 node::Attributes m_aAttributes
;
90 void markAsDefault(bool _bDefault
= true)
92 m_aAttributes
.markAsDefault(_bDefault
);
96 INode(rtl::OUString
const& aName
, node::Attributes
);
100 virtual std::auto_ptr
<INode
> clone() const = 0;
103 const rtl::OUString
& getName() const { return m_aName
; }
104 node::Attributes
getAttributes() const { return m_aAttributes
; }
106 bool isDefault() const { return m_aAttributes
.isDefault(); }
107 bool isLocalized() const { return m_aAttributes
.isLocalized(); }
109 void modifyState(node::State _eNewState
);
110 void modifyAccess(node::Access _aAccessLevel
);
111 void markMandatory();
112 void markRemovable();
113 void promoteAccessToDefault();
115 // to be used with caution. If the node is referenced from somewhere else under it's old name,
116 // you may have problems with this inconsistence
117 void setName(const rtl::OUString
& _rNewName
) { m_aName
= _rNewName
; }
119 virtual ValueNode
* asValueNode();
120 virtual ValueNode
const* asValueNode() const;
121 virtual ISubtree
* asISubtree();
122 virtual ISubtree
const* asISubtree() const;
124 // double dispatch support
125 virtual void dispatch(NodeAction
&) const = 0;
126 virtual void dispatch(NodeModification
&) = 0;
129 // -----------------------------------------------------------------------------
131 //==========================================================================
133 //==========================================================================
134 class ISubtree
: public INode
136 sal_Int16 m_nLevel
; /// determines if everything is read
137 sal_Int16 m_nDefaultLevels
; /// determines if defaults are read
139 rtl::OUString m_sTemplateName
; /// path of the template for child instantiation
140 rtl::OUString m_sTemplateModule
; /// module of the template for child instantiation
142 virtual INode
* doGetChild(rtl::OUString
const& name
) const = 0;
145 ISubtree():m_nLevel(0){}
147 ISubtree(ISubtree
const& other
)
149 ,m_nLevel(other
.m_nLevel
)
150 ,m_nDefaultLevels(other
.m_nDefaultLevels
)
151 ,m_sId() // do not copy ID while cloning !
152 ,m_sTemplateName(other
.m_sTemplateName
)
153 ,m_sTemplateModule(other
.m_sTemplateModule
)
157 // Ctor for group trees
158 ISubtree(const rtl::OUString
& aName
, const node::Attributes
& _rAttrs
)
159 :INode(aName
, _rAttrs
)
164 // Ctor for set trees
165 ISubtree(const rtl::OUString
& aName
,
166 const rtl::OUString
& _rTemplateName
,
167 const rtl::OUString
& _rTemplateModule
,
168 const node::Attributes
& _rAttrs
)
169 :INode(aName
, _rAttrs
)
172 ,m_sTemplateName(_rTemplateName
)
173 ,m_sTemplateModule(_rTemplateModule
){}
175 INode
* getChild(rtl::OUString
const& name
) { return doGetChild(name
); }
176 INode
const* getChild(rtl::OUString
const& name
) const { return doGetChild(name
); }
178 ISubtree
* asISubtree();
179 ISubtree
const* asISubtree() const;
181 using INode::markAsDefault
;
183 sal_Int16
getLevel() const { return m_nLevel
; }
185 void setLevels(sal_Int16 _nLevel
,sal_Int16 _nDefaultsLevel
);
187 bool isSetNode() const { return m_sTemplateName
.getLength() != 0; }
189 void makeSetNode(rtl::OUString
const& _sTemplateName
, rtl::OUString
const& _sTemplateModule
)
190 { m_sTemplateName
= _sTemplateName
; m_sTemplateModule
= _sTemplateModule
; }
192 rtl::OUString
const& getElementTemplateName() const { return m_sTemplateName
; }
193 rtl::OUString
const& getElementTemplateModule() const { return m_sTemplateModule
; }
195 virtual INode
* addChild(std::auto_ptr
<INode
> node
) =0; // takes ownership
196 virtual ::std::auto_ptr
<INode
> removeChild(rtl::OUString
const& name
) =0; // releases ownership
198 // Iteration support, stop if (action returns true)
199 virtual void forEachChild(NodeAction
& anAction
) const = 0;
200 virtual void forEachChild(NodeModification
& anAction
) = 0;
202 // double dispatch support
203 virtual void dispatch(NodeAction
& anAction
) const { anAction
.handle(*this); }
204 virtual void dispatch(NodeModification
& anAction
) { anAction
.handle(*this); }
207 //==========================================================================
209 //==========================================================================
210 class ValueNode
: public INode
212 AnyPair m_aValuePair
;
213 // uno::Type m_aType;
214 // uno::Any m_aValue;
215 // uno::Any m_aDefaultValue;
220 //explicit ValueNode(node::Attributes _aAttrs):INode(_aAttrs){}
223 ValueNode(rtl::OUString const& aName, node::Attributes _aAttrs)
224 : INode(aName, _aAttrs)
228 ValueNode(rtl::OUString
const& aName
,uno::Type
const& aType
, node::Attributes _aAttrs
)
229 : INode(aName
, _aAttrs
)
230 , m_aValuePair(aType
)
233 ValueNode(rtl::OUString
const& aName
,uno::Any
const& anAny
, node::Attributes _aAttrs
)
234 : INode(aName
, _aAttrs
)
235 , m_aValuePair(anAny
, selectMember(_aAttrs
.isDefault()))
238 ValueNode(rtl::OUString
const& aName
,uno::Any
const& anAny
,uno::Any
const& aDefault
, node::Attributes _aAttrs
)
239 : INode(aName
, _aAttrs
)
240 , m_aValuePair(anAny
, aDefault
)
244 bool isEmpty() const {return m_aValuePair
.isEmpty();}
245 bool isValid() const {return !m_aValuePair
.isEmpty();}
247 bool isNull() const {return m_aValuePair
.isNull();}
248 bool hasUsableDefault() const {return getAttributes().isNullable() || m_aValuePair
.hasSecond();}
250 uno::Type
getValueType() const {return m_aValuePair
.getValueType();}
251 uno::Any
getValue() const {return m_aValuePair
.getValue( selectMember(this->isDefault()) );}
252 uno::Any
getUserValue() const {return m_aValuePair
.getFirst();}
253 uno::Any
getDefault() const {return m_aValuePair
.getSecond();}
255 bool setValueType(uno::Type
const& _aType
);
256 bool setValue(uno::Any
const& _aValue
);
259 bool changeDefault(uno::Any
const& _aValue
);
260 void promoteToDefault();
262 virtual std::auto_ptr
<INode
> clone() const;
264 ValueNode
* asValueNode();
265 ValueNode
const* asValueNode() const;
266 // double dispatch support
267 virtual void dispatch(NodeAction
& anAction
) const { anAction
.handle(*this); }
268 virtual void dispatch(NodeModification
& anAction
) { anAction
.handle(*this); }
271 static AnyPair::SelectMember
selectValue() { return AnyPair::SELECT_FIRST
; }
272 static AnyPair::SelectMember
selectDeflt() { return AnyPair::SELECT_SECOND
; }
273 static AnyPair::SelectMember
selectMember(bool bDeflt
)
274 { return bDeflt
? AnyPair::SELECT_SECOND
: AnyPair::SELECT_FIRST
; }
276 //==========================================================================
278 extern bool isLocalizedValueSet(ISubtree
const& _aSubtree
);
280 //==========================================================================
282 //==========================================================================
283 inline void NodeAction::applyToNode(INode
const& aNode
)
284 { aNode
.dispatch(*this); }
285 inline void NodeAction::applyToChildren(ISubtree
const& aSubtree
)
286 { aSubtree
.forEachChild(*this); }
288 inline void NodeModification::applyToNode(INode
& aNode
)
289 { aNode
.dispatch(*this); }
290 inline void NodeModification::applyToChildren(ISubtree
& aSubtree
)
291 { aSubtree
.forEachChild(*this); }
294 } // namespace configmgr