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: SwNumberTree.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 _SW_NUMBER_TREE_HXX
32 #define _SW_NUMBER_TREE_HXX
36 #include <tools/string.hxx>
38 #include <SwNumberTreeTypes.hxx>
40 class SwNumberTreeNode
;
42 bool SwNumberTreeNodeLessThan (const SwNumberTreeNode
* pA
,
43 const SwNumberTreeNode
* pB
);
45 struct compSwNumberTreeNodeLessThan
47 bool operator()(const SwNumberTreeNode
* pA
,
48 const SwNumberTreeNode
* pB
) const
49 { return SwNumberTreeNodeLessThan(pA
, pB
); }
53 A tree of numbered nodes.
76 The root contains the nodes of the first level. Each node A of the
77 first level contains those nodes of the second level that have the
78 same first level number as A and so on for the subsidiary levels.
80 The numbering label of a node A is resolved by concatenating the
81 numbers of the nodes on the path from the root to A.
83 ------------------------------------------
87 A phantom is an auxiliary node that is used to emulate numberings
88 starting with nodes not at top level. The phantom contains the
89 number for the level but is not considered part of the numbering.
91 Constraint 1: A phantom is always the first child node.
92 Constraint 2: At each node there is at most one child that is a phantom.
93 Constraint 3: A phantom is the smallest of all numbering nodes.
102 + 0 (phantom, not counted)
107 The phantom gets numbered with 0. The first non-phantom node gets
108 numbered with the start value.
110 -----------------------------------------
116 6.1. ljdflaksjflkjasflkjsf
119 + 5 (phantom, counted)
122 + 1 ljdflaksjflkjasflkjsf
124 The phantom gets numbered with the start value.
126 class SW_DLLPUBLIC SwNumberTreeNode
129 typedef std::set
<SwNumberTreeNode
*, compSwNumberTreeNodeLessThan
>
130 tSwNumberTreeChildren
;
135 virtual ~SwNumberTreeNode();
140 @param pChild child to add
141 @param nDepth depth in which to add the child
143 void AddChild( SwNumberTreeNode
* pChild
,
144 const int nDepth
= 0 );
149 OD 2008-02-19 #refactorlists# - no longer virtual
151 @param pChild child to be removed
153 void RemoveChild( SwNumberTreeNode
* pChild
);
156 Remove this child from the tree.
161 Returns the parent of this node.
165 inline SwNumberTreeNode
* GetParent() const
171 Returns the first child of this node.
175 SwNumberTreeNode
* GetFirstChild() const;
178 Returns number of this node.
180 @param bValidate validate the number?
182 @return number of this node
184 SwNumberTree::tSwNumTreeNumber
GetNumber( bool bValidate
= true ) const;
186 // --> OD 2008-11-26 #158694#
187 bool IsContinueingPreviousSubTree() const;
191 Returns level numbers of this node.
193 @return level numbers of this node
195 SwNumberTree::tNumberVector
GetNumberVector() const;
198 Return if numbering is restartet at this node.
200 virtual bool IsRestart() const = 0;
207 virtual SwNumberTree::tSwNumTreeNumber
GetStartValue() const = 0;
210 Return if this node is counted.
212 @retval true this node is counted
213 @retval false this node is NOT counted
215 virtual bool IsCounted() const;
218 Return if this node is counted continuous.
220 @retval true This node is counted continuous.
223 virtual bool IsContinuous() const = 0;
226 Return if a node is first non-phantom child of this node.
228 @param pNode the node to check
230 @retval true pNode is first child of this node
233 virtual bool IsFirst(const SwNumberTreeNode
* pNode
) const;
236 Return if this node if the first non-phantom node in the tree.
238 @retval true this node is the first non-phantom node in the tree
241 virtual bool IsFirst() const;
244 Return if this node is a phantom.
246 @retval true this node is a phantom
247 @retval false this node is NOT a phantom
249 bool IsPhantom() const;
251 /** set level of this node
253 OD 2008-03-13 #refactorlists#
254 precondition: node is already member of a list tree
258 void SetLevelInListTree( const int nLevel
);
261 Return level of this node.
263 The level of this node is the length of the path from the root
266 @return the level of this node
268 int GetLevelInListTree() const;
271 Returns if this node is less than another node.
273 @param rTreeNode node to compare with
275 @attention A phantom node is considered the least element with
278 @retval true this node is less than rTreeNode
281 virtual bool LessThan(const SwNumberTreeNode
& rTreeNode
) const;
284 Invalidate this node and all its descendants.
286 All iterators holding the last valid node in the according list
287 of childs are set to the end of this list, thereby stating all
288 children in the list are invalid.
289 OD 2007-10-26 #i83479# - made public
291 void InvalidateTree() const;
294 Notifies all invalid children of this node.
295 OD 2007-10-26 #i83479# - made public
297 void NotifyInvalidChildren();
302 Calls Invalidate(this) on parent.
309 Validates all nodes in this subtree.
316 Calls Validate(this) on parent.
321 Notifies all invalid siblings of this node.
323 void NotifyInvalidSiblings();
325 /** notification of all nodes in the list tree on certain list level
327 OD 2008-04-17 #refactorlists#
329 void NotifyNodesOnListLevel( const int nListLevel
);
331 /** Invalidation and notification of complete numbering tree
333 OD 2006-04-26 #i64010#
334 Usage: on <IsCounted()> state change its needed to invalidate the
335 complete numbering tree due to wide influence of this change.
337 inline void InvalidateAndNotifyTree()
341 GetRoot()->InvalidateTree();
347 Returns the greatest descendant of the root that is smaller than
348 this node, aka the predecessor of this node.
350 @return the predecessor
352 SwNumberTreeNode
* GetPred( bool bSibling
= false ) const;
354 /** determines the node, which is preceding the node
356 OD 2007-09-06 #i81002#
357 The search for the preceding node is performed for the tree below the
358 <this> node. To search the complete tree, the method has been called for
359 the root of the tree.
363 const SwNumberTreeNode
* GetPrecedingNodeOf( const SwNumberTreeNode
& rNode
) const;
366 // Returns a string representation of this node.
368 // @return the string representation of this node
370 // virtual String ToString() const = 0;
373 // Print this subtree.
375 // @param o output stream to direct output to
376 // @param rIndent additional indent for the children of this node
377 // @param rMyIndent indent to use for this node
378 // @param nDepth number of levels to print (-1 means all levels)
380 // @return output stream after output of this subtree
382 // String print(const String & rIndent = String(" ",
383 // RTL_TEXTENCODING_ASCII_US),
384 // const String & rMyIndent = String(" ",
385 // RTL_TEXTENCODING_ASCII_US),
386 // int nDepth = -1) const;
389 static unsigned long GetInstances();
390 unsigned long GetSerial();
393 #ifdef __SW_NUMBER_TREE_SANITY_CHECK
397 @param bRecursive descend to children
399 @retval true the structure of this node is sane
402 bool IsSane(bool bRecursive
) const;
403 #endif // __SW_NUMBER_TREE_SANITY_CHECK
409 tSwNumberTreeChildren mChildren
;
412 Returns the root node of the tree this node is part of.
414 Important note: method call <GetRoot()->GetRoot()> returns NULL.
418 SwNumberTreeNode
* GetRoot() const;
421 Return if the notification is not disabled on global conditions
423 @retval true Notification enabled in general.
426 virtual bool IsNotificationEnabled() const = 0;
429 Returns how many children this node has got.
431 @return number of children
433 tSwNumberTreeChildren::size_type
GetChildCount() const;
435 // --> OD 2006-04-26 #i64010# - made pure virtual
436 virtual bool HasCountedChildren() const = 0;
439 // --> OD 2006-04-26 #i64010#
440 virtual bool IsCountedForNumbering() const = 0;
443 // --> OD 2008-02-19 #refactorlists#
444 // method called before this tree node has been added to the list tree
445 virtual void PreAdd() = 0;
446 // method called after this tree node has been removed from the list tree
447 virtual void PostRemove() = 0;
450 #ifdef __SW_NUMBER_TREE_SANITY_CHECK
452 Sanity check with loop detection.
454 @param bRecursive descend to children
455 @param rParents vector for recording path
457 @retval true this node is sane
458 @retval false else */
460 (bool bRecursive
, std::vector
<const SwNumberTreeNode
*> rParents
) const;
461 #endif // __SW_NUMBER_TREE_SANITY_CHECK
466 SwNumberTreeNode
* mpParent
;
469 the number of the node
471 mutable SwNumberTree::tSwNumTreeNumber mnNumber
;
473 // --> OD 2008-11-26 #158694#
474 // boolean indicating, that a node of a not counted parent node is continueing
475 // the numbering of parent's previous node sub tree.
479 // sdfjlksaf <-- not counted parent node
480 // 1.2. lfjlaskf <-- <mbContinueingPreviousSubTree = true>
481 mutable bool mbContinueingPreviousSubTree
;
485 true this node is a phantom
486 false this node is NOT a phantom
491 Iterator to the last valid element. All children that are less
492 than or equal to the referenced child are valid. All children
493 greater than the referenced child are invalid.
495 mutable tSwNumberTreeChildren::iterator mItLastValid
;
499 Counter for the number of created instances.
501 static unsigned long nInstances
;
506 unsigned long mnSerial
;
509 SwNumberTreeNode(const SwNumberTreeNode
& );
510 SwNumberTreeNode
& operator=( const SwNumberTreeNode
& );
513 Calls _GetNumberVector on parent and adds number of this node
516 @param rVector return value
517 @param bValidate validate the number?
519 void _GetNumberVector( SwNumberTree::tNumberVector
& rVector
,
520 bool bValidate
= true ) const;
525 Calls SetLastValid for the preceeding sibling of the child and
526 notifies all invalid children.
528 @param pChild the child to invalidate
530 void Invalidate( SwNumberTreeNode
* pChild
);
532 /** Invalidation of all children
534 OD 2005-10-19 #126009#
535 Usage: on <IsCounted()> state change the children have to be invalidated
537 inline void InvalidateChildren()
539 SetLastValid( mChildren
.end() );
542 /** Invalidation of parent node, if its not counted.
544 OD 2005-10-19 #126009#
545 Usage: on <IsCounted()> state change the parent have to be invalidated
547 inline void InvalidateNotCountedParent()
549 if ( GetParent() && !GetParent()->IsCountedForNumbering() )
551 GetParent()->InvalidateMe();
556 Set the last valid child of this node.
558 @param aItLastValid iterator pointing to the new last valid child
559 @param bValidating - true always set the last valid node to
561 - false only set if aItLastValid is preceeding
562 the current last valid node
564 void SetLastValid(tSwNumberTreeChildren::iterator aItLastValid
,
565 bool bValidating
= false) const;
568 Set this node as last valid child of its parent.
570 @param bValidation see aboce
572 void SetLastValid(bool bValidating
) const;
575 Return if this node is notifiable.
577 @attention If a not is not notifiable a notify request is *not*
578 forwarded to its descendants.
580 @retval true This node is notifiable.
583 virtual bool IsNotifiable() const = 0;
588 Called when the number of the node got invalid.
590 virtual void NotifyNode() = 0;
593 Notifies this node (NotifyNode) and all descendants.
597 /** Notification of parent node siblings, if its not counted.
599 OD 2005-10-19 #126009#
600 Usage: on <IsCounted()> state change the parent node and its siblings
603 inline void NotifyNotCountedParentSiblings()
605 if ( GetParent() && !GetParent()->IsCountedForNumbering() )
607 GetParent()->NotifyInvalidSiblings();
611 /** notification of children nodes on certain depth
613 OD 2008-04-17 #refactorlists#
617 void NotifyChildrenOnDepth( const int nDepth
);
620 Returns if a child A this node is valid.
622 A is valid if aItLastValid in parent refers to a node
623 greater than of equal to A.
625 @param pChild child to be tested
627 @retval true this node is valid
628 @retval false this node is NOT valid
630 bool IsValid(const SwNumberTreeNode
* pChild
) const;
633 Returns if this node is valid.
635 @retval true this node is valid
638 bool IsValid() const;
643 @param pNode child to be validated
645 @attention All invalid children preceding pNode are validated, too.
647 void Validate(const SwNumberTreeNode
* pNode
) const;
650 Validates a child using hierarchical numbering.
652 @param pNode child to be validated
654 @attention All invalid children preceding pNode are validated, too.
656 void ValidateHierarchical(const SwNumberTreeNode
* pNode
) const;
659 Validates a child using continuous numbering.
661 @param pNode child to be validated
663 @attention All invalid children preceding pNode are validated, too.
665 void ValidateContinuous(const SwNumberTreeNode
* pNode
) const;
668 Creates a new node of the same class.
672 virtual SwNumberTreeNode
* Create() const = 0;
677 @return the created phantom
679 SwNumberTreeNode
* CreatePhantom();
682 Set if this node is a phantom.
684 @param bPhantom - true this node is a phantom
685 - false this node is a phantom
687 void SetPhantom(bool bPhantom
= true);
690 Return if phantoms are counted.
692 OD 2008-02-19 #refactorlists# - pure virtual now
694 @retval true phantoms are counted
697 virtual bool IsCountPhantoms() const = 0;
700 Return if all descendants of this node are phantoms.
702 @retval true all descendants are phantoms
705 bool HasOnlyPhantoms() const;
707 // --> OD 2005-10-27 #126009#
708 bool HasPhantomCountedParent() const;
712 HB, OD : return node, if it isn't a phantom, otherwise return first
713 non-phantom descendant.
714 Returns the first child of this node that is NOT a phantom.
716 @return the first non phantom child
718 SwNumberTreeNode
* GetFirstNonPhantomChild();
721 Removes recursively phantoms that have no children.
723 The resulting tree has no phantoms that either have no children or
724 whose descendancy consist entirely of phantoms.
726 void ClearObsoletePhantoms();
728 tSwNumberTreeChildren::iterator
GetIterator(const SwNumberTreeNode
* pChild
) const;
731 Moves all children to a given destination node.
733 @param pDest the destination node
735 void MoveChildren(SwNumberTreeNode
* pDest
);
737 /** Moves all children of this node that are greater than a given node
738 to the destination node.
740 OD 2005-10-14 #125991#
741 distinguish between node for comparing, whose children are greater,
742 and the destination node.
745 input parameter - reference to the node, which is used to determine
749 input parameter - reference to the node, which is the destination for
752 void MoveGreaterChildren( SwNumberTreeNode
& _rCompareNode
,
753 SwNumberTreeNode
& _rDestNode
);
756 Returns the last descendant of a node, if it has children.
758 @return last descendant of the node
760 SwNumberTreeNode
* GetLastDescendant() const;
765 Functor. Checks if a certain node is less than the functor's member.
767 struct SwNumberTreeNodeIsLessThan
769 const SwNumberTreeNode
* pNode
;
771 SwNumberTreeNodeIsLessThan(const SwNumberTreeNode
* _pNode
)
774 bool operator()(const SwNumberTreeNode
* _pNode
) const
775 { return SwNumberTreeNodeLessThan(_pNode
, pNode
); }
777 #endif // _SW_NUMBER_TREE_HXX