merge the formfield patch from ooo-build
[ooovba.git] / configmgr / source / inc / valuenode.hxx
blobb2645b504c140961e348136aa1e24e3d8b680a3c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: valuenode.hxx,v $
10 * $Revision: 1.32 $
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>
39 #include <string.h>
40 #ifndef INCLUDED_MEMORY
41 #include <memory>
42 #define INCLUDED_MEMORY
43 #endif
45 namespace configmgr
48 namespace css = com::sun::star;
49 namespace uno = css::uno;
51 class INode;
52 class ISubtree;
53 class ValueNode;
55 // helper (tag) class
56 namespace treeop { struct NoChildCopy {}; struct DeepChildCopy {}; enum { ALL_LEVELS = -1 }; }
57 //==========================================================================
58 //= Visitors
59 //==========================================================================
60 struct NodeAction
62 virtual void handle(ValueNode const&) = 0;
63 virtual void handle(ISubtree const&) = 0;
65 void applyToNode(INode const&);
66 void applyToChildren(ISubtree const&);
67 protected:
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&);
78 protected:
79 virtual ~NodeModification() {}
82 class INode
84 rtl::OUString m_aName;
85 node::Attributes m_aAttributes;
87 protected:
88 INode(){}
90 void markAsDefault(bool _bDefault = true)
92 m_aAttributes.markAsDefault(_bDefault);
94 public:
95 explicit
96 INode(rtl::OUString const& aName, node::Attributes);
98 virtual ~INode();
100 virtual std::auto_ptr<INode> clone() const = 0;
101 public:
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 //==========================================================================
132 //= ISubtree
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
138 rtl::OUString m_sId;
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;
144 protected:
145 ISubtree():m_nLevel(0){}
147 ISubtree(ISubtree const& other)
148 :INode(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)
156 public:
157 // Ctor for group trees
158 ISubtree(const rtl::OUString& aName, const node::Attributes& _rAttrs)
159 :INode(aName, _rAttrs)
160 ,m_nLevel(0)
161 ,m_nDefaultLevels(0)
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)
170 ,m_nLevel(0)
171 ,m_nDefaultLevels(0)
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 //==========================================================================
208 //= ValueNode
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;
217 public:
218 //ValueNode(){}
220 //explicit ValueNode(node::Attributes _aAttrs):INode(_aAttrs){}
223 ValueNode(rtl::OUString const& aName, node::Attributes _aAttrs)
224 : INode(aName, _aAttrs)
225 , m_aValuePair()
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);
257 void setDefault();
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); }
270 private:
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 //==========================================================================
281 //= inlines
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
296 #endif