Subversion Repository Public Repository

litesoft

Diff Revisions 121 vs 122 for /trunk/Java/GWT/Client/src/org/litesoft/GWT/client/widgets/SizeableTree.java

Diff revisions: vs.
  @@ -77,8 +77,15 @@
77 77 return this;
78 78 }
79 79
80 + public boolean isEmpty()
81 + {
82 + return (0 == mTree.getItemCount());
83 + }
84 +
80 85 /**
81 86 * Get the Currently Selected TreeNode
87 + *
88 + * @return Selected Node or null if none Selected
82 89 */
83 90 public TreeNode getSelectedNode()
84 91 {
  @@ -96,17 +103,21 @@
96 103 return (zNode != null) ? zNode.getUserObject() : null;
97 104 }
98 105
99 - public void setSelected( Object pUserObject )
106 + /**
107 + * Set the Selected Node & return the UserObject actually Selected
108 + * @param pUserObject to select
109 + * @return pUserObject or null if it was not found!
110 + */
111 + public Object setSelected( Object pUserObject )
100 112 {
101 113 MyTreeNode tn = getTreeNode( pUserObject );
102 114 if ( tn != null )
103 115 {
104 116 tn.setSelected( true );
117 + return pUserObject;
105 118 }
106 - else
107 - {
108 - mTree.setSelectedItem( null, true );
109 - }
119 + mTree.setSelectedItem( null, true );
120 + return null;
110 121 }
111 122
112 123 /**
  @@ -157,17 +168,29 @@
157 168 @Override
158 169 public boolean removeChild( Object pUserObject )
159 170 {
160 - MyTreeNode tn = getTreeNode( pUserObject );
161 - if ( tn != null )
171 + if ( pUserObject != null )
162 172 {
163 - mTree.removeItem( tn );
164 - notifyOpenCloseChange( null );
165 - return true;
173 + int zCount = mTree.getItemCount();
174 + for ( int i = 0 ; i < zCount; i++ )
175 + {
176 + TreeItem zItem = mTree.getItem( i );
177 + if ( pUserObject == zItem.getUserObject() )
178 + {
179 + return removeWithSelectMangementAndNotification( zItem );
180 + }
181 + }
166 182 }
167 183 return false;
168 184 }
169 185
170 186 @Override
187 + public boolean removeDescendant( Object pUserObject )
188 + {
189 + MyTreeNode tn = getTreeNode( pUserObject );
190 + return (tn != null) && removeWithSelectMangementAndNotification( tn );
191 + }
192 +
193 + @Override
171 194 public void clear()
172 195 {
173 196 mTree.removeItems();
  @@ -177,11 +200,11 @@
177 200 @Override
178 201 public boolean areAnyOpen()
179 202 {
180 - int zChildCount = mTree.getItemCount();
203 + int zChildCount = mTree.getItemCount(); // Top Level!
181 204 for ( int i = 0; i < zChildCount; i++ )
182 205 {
183 - TreeItem zItem = mTree.getItem( i );
184 - if ( (zItem.getParentItem() == null) && (zItem.getChildCount() != 0) )
206 + TreeItem zItem = mTree.getItem( i ); // Top Level!
207 + if ( zItem.getChildCount() != 0 )
185 208 {
186 209 if ( zItem.getState() )
187 210 {
  @@ -195,11 +218,11 @@
195 218 @Override
196 219 public boolean areAnyClosed()
197 220 {
198 - int zChildCount = mTree.getItemCount();
221 + int zChildCount = mTree.getItemCount(); // Top Level!
199 222 for ( int i = 0; i < zChildCount; i++ )
200 223 {
201 - TreeItem zItem = mTree.getItem( i );
202 - if ( (zItem.getParentItem() == null) && (zItem.getChildCount() != 0) )
224 + TreeItem zItem = mTree.getItem( i ); // Top Level!
225 + if ( zItem.getChildCount() != 0 )
203 226 {
204 227 if ( ((TreeNode) zItem).areAnyClosed() )
205 228 {
  @@ -240,15 +263,16 @@
240 263 }
241 264
242 265 /**
243 - * @return iterator over Tree's UserObject(s)
266 + * @return iterator over Tree's UserObject(s) depth first
244 267 */
245 268 public Iterator<Object> iterator()
246 269 {
247 270 List<Object> collector = new ArrayList<Object>();
248 - int zCount = mTree.getItemCount();
271 + int zCount = mTree.getItemCount(); // Top Level!
249 272 for ( int i = 0; i < zCount; i++ )
250 273 {
251 - collector.add( mTree.getItem( i ).getUserObject() );
274 + MyTreeNode tn = (MyTreeNode) mTree.getItem( i );
275 + tn.addToDepthFirst( collector );
252 276 }
253 277 return collector.iterator();
254 278 }
  @@ -363,12 +387,12 @@
363 387 private boolean childrenUpdateOpenClosedState( boolean pOpenAll )
364 388 {
365 389 boolean changed = false;
366 - int zChildCount = mTree.getItemCount();
390 + int zChildCount = mTree.getItemCount(); // Top Level!
367 391 if ( pOpenAll )
368 392 {
369 393 for ( int i = 0; i < zChildCount; i++ ) // Open from the Top Down
370 394 {
371 - TreeItem zItem = mTree.getItem( i );
395 + TreeItem zItem = mTree.getItem( i ); // Top Level!
372 396 if ( zItem.getParentItem() == null )
373 397 {
374 398 changed |= updateOpenClosedStateRecursively( zItem, pOpenAll );
  @@ -379,7 +403,7 @@
379 403 {
380 404 for ( int i = zChildCount; 0 <= --i; ) // Close from the Bottom Up
381 405 {
382 - TreeItem zItem = mTree.getItem( i );
406 + TreeItem zItem = mTree.getItem( i ); // Top Level!
383 407 if ( zItem.getParentItem() == null )
384 408 {
385 409 changed |= updateOpenClosedStateRecursively( zItem, pOpenAll );
  @@ -426,19 +450,52 @@
426 450 {
427 451 if ( pUserObject != null )
428 452 {
429 - int zCount = mTree.getItemCount();
453 + int zCount = mTree.getItemCount(); // Top Level!
430 454 for ( int i = 0; i < zCount; i++ )
431 455 {
432 - TreeItem zItem = mTree.getItem( i );
433 - if ( (zItem != null) && (pUserObject == zItem.getUserObject()) )
456 + MyTreeNode tn = (MyTreeNode) mTree.getItem( i ); // Top Level!
457 + if ( null != (tn = tn.getTreeNode( pUserObject )) )
434 458 {
435 - return (MyTreeNode) zItem;
459 + return tn;
436 460 }
437 461 }
438 462 }
439 463 return null;
440 464 }
441 465
466 + private boolean removeWithSelectMangementAndNotification( TreeItem pTreeItem )
467 + {
468 + Object currentlySelected = getSelected();
469 +
470 + removeWithoutSelectMangementAndNotification( pTreeItem );
471 +
472 + restoreSelection( currentlySelected );
473 + return true;
474 + }
475 +
476 + private void restoreSelection( Object pPreviouslySelected )
477 + {
478 + Object restoredSelected = setSelected( pPreviouslySelected );
479 + if ( restoredSelected != pPreviouslySelected )
480 + {
481 + notifySelectionChange();
482 + }
483 + notifyOpenCloseChange( null );
484 + }
485 +
486 + private void removeWithoutSelectMangementAndNotification( TreeItem pTreeItem )
487 + {
488 + TreeItem zParentItem = pTreeItem.getParentItem();
489 + if ( zParentItem == null )
490 + {
491 + mTree.removeItem( pTreeItem );
492 + }
493 + else
494 + {
495 + zParentItem.removeItem( pTreeItem );
496 + }
497 + }
498 +
442 499 private HierarchicalNode.ChangeListener<HierarchicalNode> getChangeListener()
443 500 {
444 501 if ( mChangeListener == null )
  @@ -566,8 +623,31 @@
566 623 TreeItem zItem = getChild( i );
567 624 if ( (zItem != null) && (pUserObject == zItem.getUserObject()) )
568 625 {
569 - removeItem( zItem );
570 - notifyOpenCloseChange( null );
626 + return removeWithSelectMangementAndNotification(zItem);
627 + }
628 + }
629 + }
630 + }
631 + return false;
632 + }
633 +
634 + @Override
635 + public boolean removeDescendant( Object pUserObject )
636 + {
637 + if ( pUserObject != null )
638 + {
639 + int zCount = getChildCount();
640 + if ( zCount != 0 )
641 + {
642 + for ( int i = 0; i < zCount; i++ )
643 + {
644 + MyTreeNode tn = (MyTreeNode)getChild( i );
645 + if ( pUserObject == tn.getUserObject() )
646 + {
647 + return removeWithSelectMangementAndNotification(tn);
648 + }
649 + if ( tn.removeDescendant( pUserObject ) )
650 + {
571 651 return true;
572 652 }
573 653 }
  @@ -650,6 +730,35 @@
650 730 }
651 731 }
652 732 }
733 +
734 + public void addToDepthFirst( List<Object> pCollector )
735 + {
736 + pCollector.add( getUserObject() );
737 + int zCount = getChildCount();
738 + for ( int i = 0; i < zCount; i++ )
739 + {
740 + MyTreeNode tn = (MyTreeNode) getChild( i );
741 + tn.addToDepthFirst( pCollector );
742 + }
743 + }
744 +
745 + public MyTreeNode getTreeNode( Object pUserObject )
746 + {
747 + if ( pUserObject == getUserObject() )
748 + {
749 + return this;
750 + }
751 + int zCount = getChildCount();
752 + for ( int i = 0; i < zCount; i++ )
753 + {
754 + MyTreeNode tn = (MyTreeNode) getChild( i );
755 + if ( null != (tn = tn.getTreeNode( pUserObject )) )
756 + {
757 + return tn;
758 + }
759 + }
760 + return null;
761 + }
653 762 }
654 763
655 764 @SuppressWarnings({"unchecked"})
  @@ -666,8 +775,11 @@
666 775 List<HierarchicalNode> zChildren = getChildren( pNode );
667 776 if ( UtilsGwt.isNullOrEmpty( zChildren ) )
668 777 {
778 + Object currentlySelected = getSelected();
779 +
669 780 tn.removeItems();
670 - notifyOpenCloseChange( null );
781 +
782 + restoreSelection( currentlySelected );
671 783 return;
672 784 }
673 785 CurrentState cs = new CurrentState( tn );
  @@ -744,6 +856,8 @@
744 856 zChildren.set( at, zChild );
745 857 }
746 858 }
859 +
860 + Object currentlySelected = getSelected();
747 861 // Skip over the front of tree that is common && delete front of tree that is NOT in new Tree!
748 862 int i = 0; // MyTreeNode Child Index
749 863 while ( !zChildren.isEmpty() && !mChildren.isEmpty() && (i < pTreeNode.getChildCount()) )
  @@ -752,7 +866,7 @@
752 866 if ( !pNewChildren.contains( mChildren.get( 0 ).getUserObject() ) )
753 867 {
754 868 mChildren.remove( 0 );
755 - pTreeNode.removeItem( pTreeNode.getChild( i ) );
869 + removeWithoutSelectMangementAndNotification( pTreeNode.getChild( i ) );
756 870 continue;
757 871 }
758 872 while ( mChildren.get( 0 ).hasSameUserObject( zChildren.get( 0 ) ) )
  @@ -765,10 +879,11 @@
765 879 mChildren.clear();
766 880 while ( i < pTreeNode.getChildCount() )
767 881 {
768 - pTreeNode.removeItem( pTreeNode.getChild( i ) );
882 + removeWithoutSelectMangementAndNotification( pTreeNode.getChild( i ) );
769 883 }
770 884 addTo( pTreeNode, zChildren );
771 - notifyOpenCloseChange( null );
885 +
886 + restoreSelection( currentlySelected );
772 887 }
773 888
774 889 private void addTo( MyTreeNode pTreeNode, List<CurrentState> zChildren )