merge the formfield patch from ooo-build
[ooovba.git] / configmgr / source / treemgr / deferredview.cxx
blob5e82a08f3666edb9e6918f86a037ad9c2dec9fb6
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: deferredview.cxx,v $
10 * $Revision: 1.6 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_configmgr.hxx"
33 #include <stdio.h>
34 #include "deferredview.hxx"
35 #include "viewfactory.hxx"
36 #include "nodeimplobj.hxx"
38 namespace configmgr
40 namespace view
42 //-----------------------------------------------------------------------------
43 static inline configuration::DeferredGroupNodeImpl* deferredGroupNode(Node const& _aNode)
45 return static_cast<configuration::DeferredGroupNodeImpl*>(_aNode.get_impl());
47 //-----------------------------------------------------------------------------
49 static inline configuration::DeferredGroupNodeImpl* deferredGroupNode(GroupNode const& _aNode)
51 return static_cast<configuration::DeferredGroupNodeImpl*>(_aNode.get_impl());
53 //-----------------------------------------------------------------------------
55 static inline configuration::DeferredSetNodeImpl* deferredSetNode(Node const& _aNode)
57 return static_cast<configuration::DeferredSetNodeImpl*>(_aNode.get_impl());
59 //-----------------------------------------------------------------------------
61 static inline configuration::DeferredSetNodeImpl* deferredSetNode(SetNode const& _aNode)
63 return static_cast<configuration::DeferredSetNodeImpl*>(_aNode.get_impl());
65 //-----------------------------------------------------------------------------
67 static inline void deferredValueNode(Node const& _aNode)
69 { (void)_aNode; }
70 OSL_ENSURE( _aNode.isValueNode(),"Unknown Node type in deferred view");
71 OSL_ENSURE(_aNode.get_offset() == configuration::Tree::ROOT, "Tree: Unexpected node type - non-root value element");
73 //-----------------------------------------------------------------------------
75 static void deferredValueNodeChanged(Node const& _aNode)
77 { (void)_aNode; }
78 OSL_ENSURE( _aNode.isValueNode(),"Unknown Node type in deferred view");
79 OSL_ENSURE(_aNode.get_offset() == configuration::Tree::ROOT, "Tree: Unexpected node type - non-root value element");
80 OSL_ENSURE(!_aNode.isValueNode(),"Value node changes not supported in deferred view");
81 throw configuration::Exception("Internal Error: Invalid operation applied to element");
83 //-----------------------------------------------------------------------------
85 bool DeferredViewStrategy::doHasChanges(Node const& _aNode) const
87 if (_aNode.isGroupNode())
88 return deferredGroupNode(_aNode)->hasChanges();
90 else if (_aNode.isSetNode())
91 return deferredSetNode(_aNode)->hasChanges();
93 else
94 deferredValueNode(_aNode);
96 return false;
99 //-----------------------------------------------------------------------------
100 void DeferredViewStrategy::doMarkChanged(Node const& _aNode)
102 if (_aNode.isGroupNode())
103 deferredGroupNode(_aNode)->markChanged();
105 else if (_aNode.isSetNode())
106 deferredSetNode(_aNode)->markChanged();
108 else
109 deferredValueNodeChanged(_aNode);
112 //-----------------------------------------------------------------------------
113 void DeferredViewStrategy::doCollectChanges(Node const& _aNode, configuration::NodeChanges& _rChanges) const
115 implCollectChangesIn(_aNode,_rChanges);
118 //-----------------------------------------------------------------------------
119 void DeferredViewStrategy::implCollectChangesIn(Node const& _aNode, configuration::NodeChanges& _rChanges) const
121 if ( hasChanges(_aNode) )
123 if (_aNode.isSetNode())
125 deferredSetNode(_aNode)->collectElementChanges( _rChanges);
127 else if (_aNode.isGroupNode())
129 GroupNode aGroup(_aNode);
131 deferredGroupNode(aGroup)->collectValueChanges( _rChanges, _aNode.tree(), _aNode.get_offset());
133 for( Node aChild = aGroup.getFirstChild();
134 aChild.is();
135 aChild = aGroup.getNextChild(aChild)
138 this->implCollectChangesIn( aChild, _rChanges );
141 else
142 OSL_ASSERT(!"Unreachable code");
146 //-----------------------------------------------------------------------------
147 std::auto_ptr<SubtreeChange> DeferredViewStrategy::doPreCommitChanges(configuration::Tree * tree, std::vector< rtl::Reference<configuration::ElementTree> >& _rRemovedElements)
149 std::auto_ptr<SubtreeChange> pRet;
150 if (hasChanges(tree))
152 pRet = implPreCommitChanges(getRootNode(tree),_rRemovedElements);
153 if (pRet.get() != NULL)
155 pRet->setNodeName(getSimpleRootName(tree));
158 return pRet;
161 //-----------------------------------------------------------------------------
162 void DeferredViewStrategy::doFailedCommit(configuration::Tree * tree, SubtreeChange& rChanges)
164 implFailedCommit(getRootNode(tree),rChanges);
167 //-----------------------------------------------------------------------------
168 void DeferredViewStrategy::doFinishCommit(configuration::Tree * tree, SubtreeChange& rChanges)
170 implFinishCommit(getRootNode(tree),rChanges);
173 //-----------------------------------------------------------------------------
174 void DeferredViewStrategy::doRevertCommit(configuration::Tree * tree, SubtreeChange& rChanges)
176 implRevertCommit(getRootNode(tree),rChanges);
179 //-----------------------------------------------------------------------------
180 configuration::ValueChangeImpl* DeferredViewStrategy::doAdjustToValueChange(GroupNode const& _aGroupNode, rtl::OUString const& _aName, ValueChange const& rExternalChange)
182 rtl::Reference<configuration::ValueMemberNode::DeferredImpl> aChange = deferredGroupNode(_aGroupNode)->findValueChange(_aName);
184 if (aChange.is())
186 if (configuration::ValueChangeImpl* pValueChange = aChange->adjustToChange(rExternalChange))
188 OSL_ENSURE(aChange->isChange(), "Got an adjusted change from a non-changing value");
190 return pValueChange;
192 else // a non-change
194 OSL_ENSURE(!aChange->isChange(), "Got no adjusted change from a changing value") ;
195 OSL_ENSURE(false, "Got a non-change from a (deferred) group") ;
196 // then do as without deferred change
200 return ViewStrategy::doAdjustToValueChange(_aGroupNode, _aName, rExternalChange);
204 //-----------------------------------------------------------------------------
205 node::Attributes DeferredViewStrategy::doAdjustAttributes(node::Attributes const& _aAttributes) const
207 return _aAttributes;
210 //-----------------------------------------------------------------------------
211 configuration::ValueMemberNode DeferredViewStrategy::doGetValueMember(GroupNode const& _aNode, rtl::OUString const& _aName, bool _bForUpdate) const
213 return deferredGroupNode(_aNode)->makeValueMember(_aName,_bForUpdate);
216 //-----------------------------------------------------------------------------
217 void DeferredViewStrategy::doInsertElement(SetNode const& _aNode, rtl::OUString const& aName, configuration::SetEntry const& _aNewEntry)
219 // move to this memory segment
220 // should be direct (as any free-floating one)
222 //implMakeElement(aNewEntry)
223 configuration::ElementTreeData aNewElement = implMakeElement(_aNode, _aNewEntry );
225 deferredSetNode(_aNode)->insertNewElement(aName, aNewElement);
228 //-----------------------------------------------------------------------------
229 void DeferredViewStrategy::doRemoveElement(SetNode const& _aNode, rtl::OUString const& aName)
231 deferredSetNode(_aNode)->removeOldElement(aName);
234 //-----------------------------------------------------------------------------
235 extern NodeFactory& getDeferredChangeFactory();
237 NodeFactory& DeferredViewStrategy::doGetNodeFactory()
239 return getDeferredChangeFactory();
242 //-----------------------------------------------------------------------------
243 //-----------------------------------------------------------------------------
245 rtl::Reference<ViewStrategy> createDeferredChangeStrategy()
247 return new DeferredViewStrategy();
250 //-----------------------------------------------------------------------------
251 //-----------------------------------------------------------------------------
253 std::auto_ptr<SubtreeChange> DeferredViewStrategy::implPreCommitChanges(Node const& _aNode, std::vector< rtl::Reference<configuration::ElementTree> >& _rRemovedElements)
255 std::auto_ptr<SubtreeChange> aRet;
257 OSL_ASSERT (this->hasChanges( _aNode) );
259 if (_aNode.isSetNode())
261 aRet = deferredSetNode(_aNode)->preCommitChanges(_rRemovedElements);
263 else if (_aNode.isGroupNode())
265 std::auto_ptr<SubtreeChange> aGroupChange(deferredGroupNode(_aNode)->preCommitValueChanges());
267 OSL_ASSERT(aGroupChange.get());
268 if (aGroupChange.get())
269 implPreCommitSubChanges( GroupNode(_aNode), _rRemovedElements, *aGroupChange);
271 aRet = aGroupChange;
273 else
274 deferredValueNode(_aNode);
276 return aRet;
278 //-----------------------------------------------------------------------------
280 void DeferredViewStrategy::implFinishCommit(Node const& _aNode, SubtreeChange& rSubtreeChange)
282 OSL_PRECOND( getSimpleNodeName(_aNode) == rSubtreeChange.getNodeName(), "ERROR: Change name does not match node");
284 if (_aNode.isSetNode())
286 OSL_ENSURE(rSubtreeChange.isSetNodeChange(),"ERROR: Change type GROUP does not match set");
288 deferredSetNode(_aNode)->finishCommit(rSubtreeChange);
290 else if (_aNode.isGroupNode())
292 OSL_ENSURE(!rSubtreeChange.isSetNodeChange(),"ERROR: Change type SET does not match group");
294 deferredGroupNode(_aNode)->finishCommit(rSubtreeChange);
295 implFinishSubCommitted( GroupNode(_aNode), rSubtreeChange );
297 else
299 deferredValueNode(_aNode);
300 OSL_ENSURE(false, "Tree: Cannot finish commit: Unexpected node type");
303 //-----------------------------------------------------------------------------
305 void DeferredViewStrategy::implRevertCommit(Node const& _aNode, SubtreeChange& rSubtreeChange)
307 OSL_PRECOND( getSimpleNodeName(_aNode) == rSubtreeChange.getNodeName(), "ERROR: Change name does not match node");
309 if (_aNode.isSetNode())
311 OSL_ENSURE(rSubtreeChange.isSetNodeChange(),"ERROR: Change type GROUP does not match set");
313 deferredSetNode(_aNode)->revertCommit(rSubtreeChange);
315 else if (_aNode.isGroupNode())
317 OSL_ENSURE(!rSubtreeChange.isSetNodeChange(),"ERROR: Change type SET does not match group");
319 deferredGroupNode(_aNode)->revertCommit(rSubtreeChange);
320 implRevertSubCommitted( GroupNode(_aNode), rSubtreeChange );
322 else
324 deferredValueNode(_aNode);
325 OSL_ENSURE(false, "Tree: Cannot revert commit: Unexpected node type");
328 //-----------------------------------------------------------------------------
330 void DeferredViewStrategy::implFailedCommit(Node const& _aNode, SubtreeChange& rSubtreeChange)
332 OSL_PRECOND( getSimpleNodeName(_aNode) == rSubtreeChange.getNodeName(), "ERROR: Change name does not match node");
334 if (_aNode.isSetNode())
336 OSL_ENSURE(rSubtreeChange.isSetNodeChange(),"ERROR: Change type GROUP does not match set");
338 deferredSetNode(_aNode)->failedCommit(rSubtreeChange);
340 else if (_aNode.isGroupNode())
342 OSL_ENSURE(!rSubtreeChange.isSetNodeChange(),"ERROR: Change type SET does not match group");
344 deferredGroupNode(_aNode)->failedCommit(rSubtreeChange);
345 implFailedSubCommitted( GroupNode(_aNode), rSubtreeChange );
347 else
349 deferredValueNode(_aNode);
350 OSL_ENSURE(false, "Tree: Cannot handle commit failure: Unexpected node type");
353 //-----------------------------------------------------------------------------
355 void DeferredViewStrategy::implPreCommitSubChanges(GroupNode const & _aGroup, std::vector< rtl::Reference<configuration::ElementTree> >& _rRemovedElements, SubtreeChange& _rParentChange)
357 for( Node aChild = _aGroup.getFirstChild(); aChild.is(); aChild = _aGroup.getNextChild(aChild) )
359 if (this->hasChanges(aChild))
361 std::auto_ptr<SubtreeChange> aSubChanges( this->implPreCommitChanges(aChild,_rRemovedElements) );
362 std::auto_ptr<Change> aSubChangesBase( aSubChanges.release() );
363 _rParentChange.addChange( aSubChangesBase );
367 //-----------------------------------------------------------------------------
369 void DeferredViewStrategy::implFinishSubCommitted(GroupNode const & _aGroup, SubtreeChange& aChangesParent)
371 for(SubtreeChange::MutatingChildIterator
372 it = aChangesParent.begin_changes(),
373 stop = aChangesParent.end_changes();
374 it != stop;
375 ++it)
377 if (dynamic_cast< SubtreeChange * >(&*it) != 0)
379 Node aChild = _aGroup.findChild( it->getNodeName() );
380 OSL_ENSURE( aChild.is(), "Changed sub-node not found in tree");
382 this->implFinishCommit(aChild, static_cast<SubtreeChange&>(*it));
384 else
386 OSL_ENSURE(dynamic_cast< ValueChange * >(&*it) != 0, "Unexpected change type for inner node; change is ignored");
387 OSL_ENSURE(! _aGroup.findChild(it->getNodeName()).is() ,
388 "Found sub(tree) node where a value was changed");
392 //-----------------------------------------------------------------------------
394 void DeferredViewStrategy::implRevertSubCommitted(GroupNode const & _aGroup, SubtreeChange& aChangesParent)
396 for(SubtreeChange::MutatingChildIterator
397 it = aChangesParent.begin_changes(),
398 stop = aChangesParent.end_changes();
399 it != stop;
400 ++it)
402 if (dynamic_cast< SubtreeChange * >(&*it) != 0)
404 Node aChild = _aGroup.findChild( it->getNodeName() );
405 OSL_ENSURE( aChild.is(), "Changed sub-node not found in tree");
407 this->implRevertCommit(aChild, static_cast<SubtreeChange&>(*it));
409 else
411 OSL_ENSURE(dynamic_cast< ValueChange * >(&*it) != 0, "Unexpected change type for inner node; change is ignored");
412 OSL_ENSURE(! _aGroup.findChild(it->getNodeName()).is() ,
413 "Found sub(tree) node where a value was changed");
417 //-----------------------------------------------------------------------------
419 void DeferredViewStrategy::implFailedSubCommitted(GroupNode const & _aGroup, SubtreeChange& aChangesParent)
421 for(SubtreeChange::MutatingChildIterator
422 it = aChangesParent.begin_changes(),
423 stop = aChangesParent.end_changes();
424 it != stop;
425 ++it)
427 if (dynamic_cast< SubtreeChange * >(&*it) != 0)
429 Node aChild = _aGroup.findChild( it->getNodeName() );
430 OSL_ENSURE( aChild.is(), "Changed sub-node not found in tree");
432 this->implFailedCommit(aChild, static_cast<SubtreeChange&>(*it));
434 else
436 OSL_ENSURE(dynamic_cast< ValueChange * >(&*it) != 0, "Unexpected change type for inner node; change is ignored");
437 OSL_ENSURE(! _aGroup.findChild(it->getNodeName()).is() ,
438 "Found sub(tree) node where a value was changed");
442 //-----------------------------------------------------------------------------
444 //-----------------------------------------------------------------------------