merge the formfield patch from ooo-build
[ooovba.git] / configmgr / source / treemgr / setnodeimpl.hxx
blobba1bfaafd9ffaed12cc94e6b056ff704a425f4d2
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: setnodeimpl.hxx,v $
10 * $Revision: 1.13 $
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_SETNODEBEHAVIOR_HXX_
32 #define CONFIGMGR_SETNODEBEHAVIOR_HXX_
34 #include "nodeimpl.hxx"
35 #include "tree.hxx"
36 #include "template.hxx"
37 #include <rtl/ref.hxx>
39 #ifndef INCUDED_MAP
40 #include <map>
41 #define INCUDED_MAP
42 #endif
43 #ifndef INCLUDED_MEMORY
44 #include <memory>
45 #define INCLUDED_MEMORY
46 #endif
48 namespace configmgr
50 //-----------------------------------------------------------------------------
51 class SubtreeChange;
52 class AddNode;
53 class RemoveNode;
54 namespace view { class ViewTreeAccess; }
55 //-----------------------------------------------------------------------------
57 namespace configuration
59 //-----------------------------------------------------------------------------
60 class SetElementChangeImpl;
62 //-----------------------------------------------------------------------------
64 struct SetEntry
66 SetEntry(ElementTree* _pTree);
68 bool isValid() const { return m_pTree != 0; }
70 ElementTree* tree() const { return m_pTree; };
72 view::ViewTreeAccess getTreeView() const;
73 private:
74 ElementTree* m_pTree;
76 //-------------------------------------------------------------------------
78 struct SetNodeVisitor
80 enum Result { DONE, CONTINUE };
81 virtual Result visit(SetEntry const& anEntry) = 0;
84 //-----------------------------------------------------------------------------
86 // basic implementations of set node contents
87 //-----------------------------------------------------------------------------
89 struct ElementTreeData
91 // construction
92 ElementTreeData() : tree(), inDefault(false) {}
94 ElementTreeData(rtl::Reference<ElementTree> const& _tree, bool _bDefault)
95 : tree(_tree), inDefault(_bDefault) {}
97 // ORef compatibility
98 sal_Bool isValid() const { return this->tree.is(); }
99 ElementTree* get() const { return this->tree.get(); }
100 rtl::Reference<ElementTree> const& operator->() const { return this->tree; }
101 ElementTree& operator*() const { return *get(); }
103 // data
104 rtl::Reference<ElementTree> tree;
105 bool inDefault;
107 //-----------------------------------------------------------------------------
109 class ElementSet
111 struct FastLess
113 bool operator ()(
114 rtl::OUString const & a, rtl::OUString const & b) const
116 // first sort by length; order is immaterial, and it is fast
117 return a.getLength() == b.getLength()
118 ? a < b : a.getLength() < b.getLength();
122 public:
123 typedef std::map<rtl::OUString, ElementTreeData, FastLess> Data;
125 // the following must be implemented by derived classes
126 bool isEmpty() const { return m_aData.empty(); }
128 bool hasElement(rtl::OUString const& aName) const;
129 ElementTreeData* getElement(rtl::OUString const& aName);
130 ElementTreeData const* getElement(rtl::OUString const& aName) const;
131 ElementTreeData findElement(rtl::OUString const& aName);
133 void insertElement(rtl::OUString const& aName, ElementTreeData const& aNewEntry);
134 ElementTreeData replaceElement(rtl::OUString const& aName, ElementTreeData const& aNewEntry);
135 ElementTreeData removeElement(rtl::OUString const& aName);
137 void clearElements() { m_aData.clear(); }
139 void swap(ElementSet& aOther) { m_aData.swap(aOther.m_aData); }
141 // STL style iteration
142 class ConstIterator
144 public:
145 ConstIterator(Data::const_iterator const& it) : m_base(it) {}
147 ElementTreeData const& operator* () const { return m_base->second; }
148 ElementTreeData const* operator->() const { return &m_base->second; }
150 ConstIterator& operator++() { ++m_base; return *this; }
151 ConstIterator operator++(int) { return ConstIterator(m_base++); }
153 ConstIterator& operator--() { --m_base; return *this; }
154 ConstIterator operator--(int) { return ConstIterator(m_base--); }
156 friend bool operator ==(ConstIterator const& lhs, ConstIterator const& rhs)
157 { return lhs.m_base == rhs.m_base; }
158 friend bool operator !=(ConstIterator const& lhs, ConstIterator const& rhs)
159 { return lhs.m_base != rhs.m_base; }
160 private:
161 Data::const_iterator m_base;
163 ConstIterator begin() const { return ConstIterator(m_aData.begin()); }
164 ConstIterator end() const { return ConstIterator(m_aData.end()); }
166 class Iterator
168 public:
169 Iterator(Data::iterator const& it) : m_base(it) {}
171 ElementTreeData& operator* () const { return m_base->second; }
172 ElementTreeData* operator->() const { return &m_base->second; }
174 Iterator& operator++() { ++m_base; return *this; }
175 Iterator operator++(int) { return Iterator(m_base++); }
177 Iterator& operator--() { --m_base; return *this; }
178 Iterator operator--(int) { return Iterator(m_base--); }
180 friend bool operator ==(Iterator const& lhs, Iterator const& rhs)
181 { return lhs.m_base == rhs.m_base; }
182 friend bool operator !=(Iterator const& lhs, Iterator const& rhs)
183 { return lhs.m_base != rhs.m_base; }
185 operator ConstIterator() const { return ConstIterator(m_base); }
186 private:
187 Data::iterator m_base;
189 Iterator begin() { return Iterator(m_aData.begin()); }
190 Iterator end() { return Iterator(m_aData.end()); }
192 Data::const_iterator beginNative() const { return m_aData.begin(); }
193 Data::const_iterator endNative() const { return m_aData.end(); }
194 private:
195 Data m_aData;
197 //-------------------------------------------------------------------------
199 // Basic implementation of a set node
200 //-------------------------------------------------------------------------
202 class SetNodeImpl : public NodeImpl
204 friend class view::ViewStrategy;
205 ElementSet m_aDataSet;
206 rtl::Reference<Template> m_aTemplate;
207 TemplateProvider m_aTemplateProvider;
208 Tree* m_pParentTree;
209 unsigned int m_nContextPos;
211 unsigned int m_aInit;
213 public:
214 SetNodeImpl(sharable::SetNode * _pNodeRef, Template* pTemplate);
216 sharable::SetNode * getDataAccess() const;
218 /// Get the template that describes elements of this set
219 rtl::Reference<Template> getElementTemplate() const { return m_aTemplate; }
221 /// Get a template provider that can create new elements for this set
222 TemplateProvider getTemplateProvider() const { return m_aTemplateProvider; }
224 void convertChanges(NodeChangesInformation& rLocalChanges, SubtreeChange const& rExternalChange, unsigned int nDepth);
226 void insertElement(rtl::OUString const& aName, ElementTreeData const& aNewElement);
227 ElementTreeData replaceElement(rtl::OUString const& aName, ElementTreeData const& aNewElement);
228 ElementTreeData removeElement(rtl::OUString const& aName);
230 void rebuildFrom(SetNodeImpl& rOldData, sharable::SetNode * newNode);
232 protected:
233 ~SetNodeImpl();
235 protected:
236 // new overrideables
237 virtual bool doIsEmpty() const;
238 virtual ElementTree* doFindElement(rtl::OUString const& aName) ;
239 virtual SetNodeVisitor::Result doDispatchToElements( SetNodeVisitor& aVisitor);
240 virtual void doDifferenceToDefaultState( SubtreeChange& _rChangeToDefault, ISubtree& _rDefaultTree);
242 virtual SetElementChangeImpl* doAdjustToAddedElement( rtl::OUString const& aName, AddNode const& aAddNodeChange, ElementTreeData const & aNewElement);
243 virtual SetElementChangeImpl* doAdjustToRemovedElement( rtl::OUString const& aName, RemoveNode const& aRemoveNodeChange);
244 virtual SetElementChangeImpl* doAdjustChangedElement( NodeChangesInformation& rLocalChanges, rtl::OUString const& aName, Change const& aChange);
246 virtual void doTransferElements(ElementSet& rReplacement);
248 protected:
249 // helpers
250 Tree* getParentTree() const;
251 unsigned int getContextOffset() const;
253 ElementTreeData makeElement( SetEntry const & _anEntry);
254 static ElementTreeData entryToElement(SetEntry const& _anEntry);
255 view::ViewTreeAccess getElementView();
257 /// Initialize the set data: Set context information, and build the view (actually loading the elements may be deferred)
258 friend class TreeImplBuilder;
259 void initElements(TemplateProvider const& aTemplateProvider, Tree& rParentTree, unsigned int nPos, unsigned int nDepth);
261 protected:
262 /// does this set contain any elements (loads elements if needed)
263 bool implHasLoadedElements() const;
264 bool implLoadElements();
265 void implEnsureElementsLoaded();
266 void implInitElements(sharable::SetNode * node, unsigned int nDepth);
267 void implInitElement(ElementTreeData const& aNewElement);
269 void implRebuildElements(sharable::SetNode * newNode);
270 protected:
271 SetElementChangeImpl* implCreateInsert ( rtl::OUString const& aName, ElementTreeData const& aNewElement) const;
272 SetElementChangeImpl* implCreateReplace ( rtl::OUString const& aName, ElementTreeData const& aNewElement, ElementTreeData const& aOldElement) const;
273 SetElementChangeImpl* implCreateRemove ( rtl::OUString const& aName, ElementTreeData const& aOldElement) const;
275 SetElementChangeImpl* implAdjustToAddedElement( rtl::OUString const& aName, ElementTreeData const& aNewElement, bool _bReplacing);
276 SetElementChangeImpl* implAdjustToRemovedElement( rtl::OUString const& aName);
278 ElementTreeData makeAdditionalElement( rtl::Reference<view::ViewStrategy> const& _xStrategy, AddNode const& aAddNodeChange, unsigned int nDepth);
280 ElementTreeData implValidateElement(ElementTreeData const& aNewElement);
282 void implDifferenceToDefaultState( SubtreeChange& _rChangeToDefault, ISubtree& _rDefaultTree) const;
283 protected:
284 bool hasStoredElement(rtl::OUString const& aName) const
285 { return m_aDataSet.hasElement(aName); }
286 ElementTreeData* getStoredElement(rtl::OUString const& aName)
287 { return m_aDataSet.getElement(aName); }
288 ElementTreeData const* getStoredElement(rtl::OUString const& aName) const
289 { return m_aDataSet.getElement(aName); }
291 ElementSet::Data::const_iterator beginElementSet() const
292 { return m_aDataSet.beginNative(); }
293 ElementSet::Data::const_iterator endElementSet() const
294 { return m_aDataSet.endNative(); }
296 void attach(ElementTreeData const& aNewElement, rtl::OUString const& aName);
297 void detach(ElementTreeData const& aNewElement);
300 //-----------------------------------------------------------------------------
301 // domain-specific 'dynamic_cast' replacement
302 SetNodeImpl& AsSetNode (NodeImpl& rNode);
304 //-----------------------------------------------------------------------------
305 //-----------------------------------------------------------------------------
309 #endif // CONFIGMGR_SETNODEBEHAVIOR_HXX_