Skip to main content
summaryrefslogtreecommitdiffstats
blob: 12f0faf3a7d5fb6a800489c5245cc7b503153d63 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
/*******************************************************************************
 * Copyright (c) 2007, 2010 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.utility.internal.model.value.swing;

import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.List;

import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreePath;

import org.eclipse.jpt.common.utility.internal.StringTools;
import org.eclipse.jpt.common.utility.internal.model.listener.awt.AWTListChangeListenerWrapper;
import org.eclipse.jpt.common.utility.internal.model.listener.awt.AWTPropertyChangeListenerWrapper;
import org.eclipse.jpt.common.utility.internal.model.listener.awt.AWTStateChangeListenerWrapper;
import org.eclipse.jpt.common.utility.internal.model.value.StaticPropertyValueModel;
import org.eclipse.jpt.common.utility.model.event.ListAddEvent;
import org.eclipse.jpt.common.utility.model.event.ListChangeEvent;
import org.eclipse.jpt.common.utility.model.event.ListClearEvent;
import org.eclipse.jpt.common.utility.model.event.ListEvent;
import org.eclipse.jpt.common.utility.model.event.ListMoveEvent;
import org.eclipse.jpt.common.utility.model.event.ListRemoveEvent;
import org.eclipse.jpt.common.utility.model.event.ListReplaceEvent;
import org.eclipse.jpt.common.utility.model.event.PropertyChangeEvent;
import org.eclipse.jpt.common.utility.model.event.StateChangeEvent;
import org.eclipse.jpt.common.utility.model.listener.ListChangeListener;
import org.eclipse.jpt.common.utility.model.listener.PropertyChangeListener;
import org.eclipse.jpt.common.utility.model.listener.StateChangeListener;
import org.eclipse.jpt.common.utility.model.value.ListValueModel;
import org.eclipse.jpt.common.utility.model.value.PropertyValueModel;
import org.eclipse.jpt.common.utility.model.value.TreeNodeValueModel;

/**
 * This javax.swing.tree.TreeModel can be used to keep a TreeModelListener
 * (e.g. a JTree) in synch with a tree of TreeNodeValueModel objects. Unlike
 * javax.swing.tree.DefaultTreeModel, you do not add and remove nodes with
 * methods implemented here. You can add and remove nodes by adding and
 * removing them directly to/from the nodes (or, more typically, the domain
 * objects the nodes are wrapping and listening to).
 * 
 * Due to limitations in JTree, the root of the tree can never be null,
 * which, typically, should not be a problem. (If you want to display an empty
 * tree you can set the JTree's treeModel to null.)
 */
public class TreeModelAdapter<T>
	extends AbstractTreeModel
{
	/**
	 * A value model on the underlying tree's root node and its
	 * corresponding listener. This allows clients to swap out
	 * the entire tree. Due to limitations in JTree, the root should
	 * never be set to null while we have listeners.
	 */
	private final PropertyValueModel<TreeNodeValueModel<T>> rootHolder;
	private final PropertyChangeListener rootListener;

	/**
	 * A listener that notifies us when a node's internal
	 * "state" changes (as opposed to the node's value or list of
	 * children), allowing us to forward notification to our listeners.
	 */
	private final StateChangeListener nodeStateListener;

	/**
	 * A listener that notifies us when a node's "value"
	 * changes (as opposed to the node's state or list of
	 * children), allowing us to forward notification to our listeners.
	 * Typically, this will only happen with nodes that hold
	 * primitive data.
	 */
	private final PropertyChangeListener nodeValueListener;

	/**
	 * A listener that notifies us when an underlying node's
	 * "list" of children changes, allowing us to keep our
	 * internal tree in synch with the underlying tree model.
	 */
	private final ListChangeListener childrenListener;

	/* these attributes make up our internal tree */
	/**
	 * The root cannot be null while we have listeners, which is
	 * most of the time. The root is cached so we can disengage
	 * from it when it has been swapped out.
	 */
	private TreeNodeValueModel<T> root;

	/**
	 * Map the nodes to their lists of children.
	 * We cache these so we can swap out the entire list of children
	 * when we receive a #listChanged() event (which does not include
	 * the items that were affected).
	 * @see EventChangePolicy#rebuildChildren()
	 */
	final IdentityHashMap<TreeNodeValueModel<T>, List<TreeNodeValueModel<T>>> childrenLists;

	/**
	 * Map the children models to their parents.
	 * We cache these so we can figure out the "real" source of the
	 * list change events (the parent).
	 * @see EventChangePolicy#parent()
	 */
	final IdentityHashMap<ListValueModel<TreeNodeValueModel<T>>, TreeNodeValueModel<T>> parents;


	// ********** constructors **********

	/**
	 * Construct a tree model for the specified root.
	 */
	public TreeModelAdapter(PropertyValueModel<TreeNodeValueModel<T>> rootHolder) {
		super();
		if (rootHolder == null) {
			throw new NullPointerException();
		}
		this.rootHolder = rootHolder;
		this.rootListener = this.buildRootListener();
		this.nodeStateListener = this.buildNodeStateListener();
		this.nodeValueListener = this.buildNodeValueListener();
		this.childrenListener = this.buildChildrenListener();
		this.childrenLists = new IdentityHashMap<TreeNodeValueModel<T>, List<TreeNodeValueModel<T>>>();
		this.parents = new IdentityHashMap<ListValueModel<TreeNodeValueModel<T>>, TreeNodeValueModel<T>>();
	}

	/**
	 * Construct a tree model for the specified root.
	 */
	public TreeModelAdapter(TreeNodeValueModel<T> root) {
		this(new StaticPropertyValueModel<TreeNodeValueModel<T>>(root));
	}


	// ********** initialization **********

	protected PropertyChangeListener buildRootListener() {
		return new AWTPropertyChangeListenerWrapper(this.buildRootListener_());
	}

	protected PropertyChangeListener buildRootListener_() {
		return new PropertyChangeListener() {
			public void propertyChanged(PropertyChangeEvent event) {
				TreeModelAdapter.this.rootChanged();
			}
			@Override
			public String toString() {
				return "root listener"; //$NON-NLS-1$
			}
		};
	}

	protected PropertyChangeListener buildNodeValueListener() {
		return new AWTPropertyChangeListenerWrapper(this.buildNodeValueListener_());
	}

	protected PropertyChangeListener buildNodeValueListener_() {
		return new PropertyChangeListener() {
			@SuppressWarnings("unchecked")
			public void propertyChanged(PropertyChangeEvent event) {
				TreeModelAdapter.this.nodeChanged((TreeNodeValueModel<T>) event.getSource());
			}
			@Override
			public String toString() {
				return "node value listener"; //$NON-NLS-1$
			}
		};
	}

	protected StateChangeListener buildNodeStateListener() {
		return new AWTStateChangeListenerWrapper(this.buildNodeStateListener_());
	}

	protected StateChangeListener buildNodeStateListener_() {
		return new StateChangeListener() {
			@SuppressWarnings("unchecked")
			public void stateChanged(StateChangeEvent event) {
				TreeModelAdapter.this.nodeChanged((TreeNodeValueModel<T>) event.getSource());
			}
			@Override
			public String toString() {
				return "node state listener"; //$NON-NLS-1$
			}
		};
	}

	protected ListChangeListener buildChildrenListener() {
		return new AWTListChangeListenerWrapper(this.buildChildrenListener_());
	}

	protected ListChangeListener buildChildrenListener_() {
		return new ListChangeListener() {
			public void itemsAdded(ListAddEvent event) {
				new AddEventChangePolicy(event).addChildren();
			}
			public void itemsRemoved(ListRemoveEvent event) {
				new RemoveEventChangePolicy(event).removeChildren();
			}
			public void itemsReplaced(ListReplaceEvent event) {
				new ReplaceEventChangePolicy(event).replaceChildren();
			}
			public void itemsMoved(ListMoveEvent event) {
				new MoveEventChangePolicy(event).moveChildren();
			}
			public void listCleared(ListClearEvent event) {
				new ClearEventChangePolicy(event).clearChildren();
			}
			public void listChanged(ListChangeEvent event) {
				new ChangeEventChangePolicy(event).rebuildChildren();
			}
			@Override
			public String toString() {
				return "children listener"; //$NON-NLS-1$
			}
		};
	}


	// ********** TreeModel implementation **********

	public Object getRoot() {
		return this.root;
	}

	@SuppressWarnings("unchecked")
	public Object getChild(Object parent, int index) {
		return ((TreeNodeValueModel<T>) parent).child(index);
	}

	@SuppressWarnings("unchecked")
	public int getChildCount(Object parent) {
		return ((TreeNodeValueModel<T>) parent).childrenSize();
	}

	@SuppressWarnings("unchecked")
	public boolean isLeaf(Object node) {
		return ((TreeNodeValueModel<T>) node).isLeaf();
	}

	@SuppressWarnings("unchecked")
	public void valueForPathChanged(TreePath path, Object newValue) {
		((TreeNodeValueModel<T>) path.getLastPathComponent()).setValue((T) newValue);
	}

	@SuppressWarnings("unchecked")
	public int getIndexOfChild(Object parent, Object child) {
		return ((TreeNodeValueModel<T>) parent).indexOfChild((TreeNodeValueModel<T>) child);
	}

	/**
	 * Extend to start listening to the underlying model if necessary.
	 */
    @Override
	public void addTreeModelListener(TreeModelListener l) {
		if (this.hasNoTreeModelListeners()) {
			this.engageModel();
		}
		super.addTreeModelListener(l);
	}

	/**
	 * Extend to stop listening to the underlying model if appropriate.
	 */
    @Override
	public void removeTreeModelListener(TreeModelListener l) {
		super.removeTreeModelListener(l);
		if (this.hasNoTreeModelListeners()) {
			this.disengageModel();
		}
	}


	// ********** behavior **********

	/**
	 * Listen to the root and all the other nodes
	 * in the underlying tree model.
	 */
	private void engageModel() {
		this.rootHolder.addPropertyChangeListener(PropertyValueModel.VALUE, this.rootListener);
		this.root = this.rootHolder.getValue();
		if (this.root == null) {
			throw new NullPointerException();	// the root cannot be null while we have listeners
		}
		this.engageNode(this.root);
		this.addRoot();
	}

	/**
	 * Add the root and all of the nodes to the underlying tree.
	 */
	private void addRoot() {
		this.addNode(0, this.root);
	}

	/**
	 * Stop listening to the root and all the other
	 * nodes in the underlying tree model.
	 */
	private void disengageModel() {
		this.removeRoot();
		this.disengageNode(this.root);
		this.root = null;
		this.rootHolder.removePropertyChangeListener(PropertyValueModel.VALUE, this.rootListener);
	}

	/**
	 * Remove the root and all of the nodes from the underlying tree.
	 */
	private void removeRoot() {
		this.removeNode(0, this.root);
	}

	/**
	 * The root has been swapped.
	 * This method is a bit gnarly because the API for notifying listeners
	 * that the root has changed is a bit inconsistent with that used for
	 * non-root nodes.
	 */
	void rootChanged() {
		TreeNodeValueModel<T> newRoot = this.rootHolder.getValue();
		if (newRoot == null) {
			throw new NullPointerException();	// the root cannot be null while we have listeners
		}
		// remove all the current root's children from the tree
		// and remove the it from the internal tree
		this.removeRoot(); 

		// save the old root and swap in the new root
		TreeNodeValueModel<T> oldRoot = this.root;
		this.root = newRoot;

		// we must be listening to both the old and new roots when we fire the event
		// because their values can be affected by whether they have listeners
		this.engageNode(this.root);
		this.fireTreeRootReplaced(this.root);
		// now we can stop listening to the old root
		this.disengageNode(oldRoot);

		// add the new root to the internal tree and
		// add all its children to the tree also
		this.addRoot();
	}

	/**
	 * Either the "value" or the "state" of the specified node has changed,
	 * forward notification to our listeners.
	 */
	void nodeChanged(TreeNodeValueModel<T> node) {
		TreeNodeValueModel<T> parent = node.parent();
		if (parent == null) {
			this.fireTreeRootChanged(node);
		} else {
			this.fireTreeNodeChanged(parent.path(), parent.indexOfChild(node), node);
		}
	}

	/**
	 * Listen to the nodes, notify our listeners that the nodes were added,
	 * and then add the nodes to our internal tree.
	 * We must listen to the nodes before notifying anybody, because
	 * adding a listener can change the value of a node.
	 */
	void addChildren(TreeNodeValueModel<T>[] path, int[] childIndices, TreeNodeValueModel<T>[] children) {
		int len = childIndices.length;
		for (int i = 0; i < len; i++) {
			this.engageNode(children[i]);
		}
		this.fireTreeNodesInserted(path, childIndices, children);
		for (int i = 0; i < len; i++) {
			this.addNode(childIndices[i], children[i]);
		}
	}

	/**
	 * Listen to the node and its children model.
	 */
	private void engageNode(TreeNodeValueModel<T> node) {
		node.addStateChangeListener(this.nodeStateListener);
		node.addPropertyChangeListener(PropertyValueModel.VALUE, this.nodeValueListener);
		node.childrenModel().addListChangeListener(ListValueModel.LIST_VALUES, this.childrenListener);
	}

	/**
	 * Add the node to our internal tree;
	 * then recurse down through the node's children,
	 * adding them to the internal tree also.
	 */
	private void addNode(int index, TreeNodeValueModel<T> node) {
		this.addNodeToInternalTree(node.parent(), index, node, node.childrenModel());
		new NodeChangePolicy(node).addChildren();
	}

	/**
	 * Add the specified node to our internal tree.
	 */
	private void addNodeToInternalTree(TreeNodeValueModel<T> parent, int index, TreeNodeValueModel<T> node, ListValueModel<TreeNodeValueModel<T>> childrenModel) {
		List<TreeNodeValueModel<T>> siblings = this.childrenLists.get(parent);
		if (siblings == null) {
			siblings = new ArrayList<TreeNodeValueModel<T>>();
			this.childrenLists.put(parent, siblings);
		}
		siblings.add(index, node);

		this.parents.put(childrenModel, node);
	}

	/**
	 * Remove nodes from our internal tree, notify our listeners that the
	 * nodes were removed, then stop listening to the nodes.
	 * We must listen to the nodes until after notifying anybody, because
	 * removing a listener can change the value of a node.
	 */
	void removeChildren(TreeNodeValueModel<T>[] path, int[] childIndices, TreeNodeValueModel<T>[] children) {
		int len = childIndices.length;
		for (int i = 0; i < len; i++) {
			// the indices slide down a notch each time we remove a child
			this.removeNode(childIndices[i] - i, children[i]);
		}
		this.fireTreeNodesRemoved(path, childIndices, children);
		for (int i = 0; i < len; i++) {
			this.disengageNode(children[i]);
		}
	}

	/**
	 * First, recurse down through the node's children,
	 * removing them from our internal tree;
	 * then remove the node itself from our internal tree.
	 */
	private void removeNode(int index, TreeNodeValueModel<T> node) {
		new NodeChangePolicy(node).removeChildren();
		this.removeNodeFromInternalTree(node.parent(), index, node.childrenModel());
	}

	/**
	 * Remove the specified node from our internal tree.
	 */
	private void removeNodeFromInternalTree(TreeNodeValueModel<T> parent, int index, ListValueModel<TreeNodeValueModel<T>> childrenModel) {
		this.parents.remove(childrenModel);

		List<TreeNodeValueModel<T>> siblings = this.childrenLists.get(parent);
		siblings.remove(index);
		if (siblings.isEmpty()) {
			this.childrenLists.remove(parent);
		}
	}

	/**
	 * Stop listening to the node and its children model.
	 */
	private void disengageNode(TreeNodeValueModel<T> node) {
		node.childrenModel().removeListChangeListener(ListValueModel.LIST_VALUES, this.childrenListener);
		node.removePropertyChangeListener(PropertyValueModel.VALUE, this.nodeValueListener);
		node.removeStateChangeListener(this.nodeStateListener);
	}

	void moveChildren(TreeNodeValueModel<T> parent, int targetIndex, int sourceIndex, int length) {
		List<TreeNodeValueModel<T>> childrenList = this.childrenLists.get(parent);
		ArrayList<TreeNodeValueModel<T>> temp = new ArrayList<TreeNodeValueModel<T>>(length);
		for (int i = 0; i < length; i++) {
			temp.add(childrenList.remove(sourceIndex));
		}
		childrenList.addAll(targetIndex, temp);

		this.fireTreeStructureChanged(parent.path());
	}


	// ********** standard methods **********

	@Override
	public String toString() {
		return StringTools.buildToStringFor(this, this.root);
	}


	// ********** inner classes **********

	/**
	 * Coalesce some of the common change policy behavior.
	 */
	abstract class ChangePolicy {

		ChangePolicy() {
			super();
		}

		/**
		 * Add the current set of children.
		 */
		void addChildren() {
			TreeModelAdapter.this.addChildren(this.parent().path(), this.childIndices(), this.childArray());
		}

		/**
		 * Remove the current set of children.
		 */
		void removeChildren() {
			TreeModelAdapter.this.removeChildren(this.parent().path(), this.childIndices(), this.childArray());
		}

		/**
		 * Return an array of the indices of the current set of children,
		 * which should be contiguous.
		 */
		int[] childIndices() {
			return this.buildIndices(this.childrenStartIndex(), this.childrenSize());
		}

		/**
		 * Return an array of the current set of children.
		 */
		TreeNodeValueModel<T>[] childArray() {
			return this.buildArray(this.getChildren(), this.childrenSize());
		}

		/**
		 * Build an array to hold the elements in the specified iterator.
		 * If they are different sizes, something is screwed up...
		 */
		TreeNodeValueModel<T>[] buildArray(Iterable<TreeNodeValueModel<T>> elements, int size) {
			@SuppressWarnings("unchecked")
			TreeNodeValueModel<T>[] array = new TreeNodeValueModel[size];
			int i = 0;
			for (TreeNodeValueModel<T> element : elements) {
				array[i++] = element;
			}
			return array;
		}

		/**
		 * Return a set of indices, starting at zero and
		 * continuing for the specified size.
		 */
		int[] buildIndices(int size) {
			return buildIndices(0, size);
		}

		/**
		 * Return a set of indices, starting at the specified index and
		 * continuing for the specified size.
		 */
		int[] buildIndices(int start, int size) {
			int[] indices = new int[size];
			int index = start;
			for (int i = 0; i < size; i++) {
				indices[i] = index++;
			}
			return indices;
		}

		/**
		 * Return the parent of the current set of children.
		 */
		abstract TreeNodeValueModel<T> parent();

		/**
		 * Return the starting index for the current set of children.
		 */
		abstract int childrenStartIndex();

		/**
		 * Return the size of the current set of children.
		 */
		abstract int childrenSize();

		/**
		 * Return the current set of children.
		 */
		abstract Iterable<TreeNodeValueModel<T>> getChildren();

	}


	/**
	 * Wraps a ListEvent for adding, removing, replacing,
	 * and changing children.
	 */
	abstract class EventChangePolicy extends ChangePolicy {
		final ListEvent event;

		EventChangePolicy(ListEvent event) {
			super();
			this.event = event;
		}

		/**
		 * Map the ListChangeEvent's source to the corresponding parent.
		 */
		@Override
		TreeNodeValueModel<T> parent() {
			return TreeModelAdapter.this.parents.get(this.event.getSource());
		}

	}


	/**
	 * Wraps a ListAddEvent for adding children.
	 */
	class AddEventChangePolicy extends EventChangePolicy {

		AddEventChangePolicy(ListAddEvent event) {
			super(event);
		}

		private ListAddEvent getEvent() {
			return (ListAddEvent) this.event;
		}

		/**
		 * The ListAddEvent's item index is the children start index.
		 */
		@Override
		int childrenStartIndex() {
			return this.getEvent().getIndex();
		}

		/**
		 * The ListAddEvent's size is the children size.
		 */
		@Override
		int childrenSize() {
			return this.getEvent().getItemsSize();
		}

		/**
		 * The ListAddEvent's items are the children.
		 */
		@Override
		@SuppressWarnings("unchecked")
		Iterable<TreeNodeValueModel<T>> getChildren() {
			return (Iterable<TreeNodeValueModel<T>>) this.getEvent().getItems();
		}

	}


	/**
	 * Wraps a ListRemoveEvent for adding children.
	 */
	class RemoveEventChangePolicy extends EventChangePolicy {

		RemoveEventChangePolicy(ListRemoveEvent event) {
			super(event);
		}

		private ListRemoveEvent getEvent() {
			return (ListRemoveEvent) this.event;
		}

		/**
		 * The ListRemoveEvent's item index is the children start index.
		 */
		@Override
		int childrenStartIndex() {
			return this.getEvent().getIndex();
		}

		/**
		 * The ListRemoveEvent's size is the children size.
		 */
		@Override
		int childrenSize() {
			return this.getEvent().getItemsSize();
		}

		/**
		 * The ListRemoveEvent's items are the children.
		 */
		@Override
		@SuppressWarnings("unchecked")
		Iterable<TreeNodeValueModel<T>> getChildren() {
			return (Iterable<TreeNodeValueModel<T>>) this.getEvent().getItems();
		}

	}


	/**
	 * Wraps a ListReplaceEvent for replacing children.
	 */
	class ReplaceEventChangePolicy extends EventChangePolicy {

		ReplaceEventChangePolicy(ListReplaceEvent event) {
			super(event);
		}

		private ListReplaceEvent getEvent() {
			return (ListReplaceEvent) this.event;
		}

		/**
		 * The ListReplaceEvent's item index is the children start index.
		 */
		@Override
		int childrenStartIndex() {
			return this.getEvent().getIndex();
		}

		/**
		 * The ListReplaceEvent's size is the children size.
		 */
		@Override
		int childrenSize() {
			return this.getEvent().getItemsSize();
		}

		/**
		 * The ListReplaceEvent's items are the children.
		 */
		@Override
		@SuppressWarnings("unchecked")
		Iterable<TreeNodeValueModel<T>> getChildren() {
			return (Iterable<TreeNodeValueModel<T>>) this.getEvent().getNewItems();
		}

		/**
		 * Remove the old nodes and add the new ones.
		 */
		void replaceChildren() {
			TreeNodeValueModel<T>[] parentPath = this.parent().path();
			int[] childIndices = this.childIndices();
			TreeModelAdapter.this.removeChildren(parentPath, childIndices, this.getOldChildren());
			TreeModelAdapter.this.addChildren(parentPath, childIndices, this.childArray());
		}

		TreeNodeValueModel<T>[] getOldChildren() {
			return this.buildArray(this.getOldItems(), this.getEvent().getItemsSize());
		}

		// minimized scope of suppressed warnings
		@SuppressWarnings("unchecked")
		protected Iterable<TreeNodeValueModel<T>> getOldItems() {
			return (Iterable<TreeNodeValueModel<T>>) this.getEvent().getOldItems();
		}

	}


	/**
	 * Wraps a ListMoveEvent for moving children.
	 */
	class MoveEventChangePolicy extends EventChangePolicy {

		MoveEventChangePolicy(ListMoveEvent event) {
			super(event);
		}

		private ListMoveEvent getEvent() {
			return (ListMoveEvent) this.event;
		}

		void moveChildren() {
			TreeModelAdapter.this.moveChildren(this.parent(), this.getEvent().getTargetIndex(), this.getEvent().getSourceIndex(), this.getEvent().getLength());
		}

		@Override
		int childrenStartIndex() {
			throw new UnsupportedOperationException();
		}

		@Override
		int childrenSize() {
			throw new UnsupportedOperationException();
		}

		@Override
		Iterable<TreeNodeValueModel<T>> getChildren() {
			throw new UnsupportedOperationException();
		}

	}


	/**
	 * Wraps a ListClearEvent for clearing children.
	 */
	class ClearEventChangePolicy extends EventChangePolicy {

		ClearEventChangePolicy(ListClearEvent event) {
			super(event);
		}

		/**
		 * Clear all the nodes.
		 */
		void clearChildren() {
			TreeNodeValueModel<T> parent = this.parent();
			TreeNodeValueModel<T>[] parentPath = parent.path();
			List<TreeNodeValueModel<T>> childrenList = TreeModelAdapter.this.childrenLists.get(parent);
			int[] childIndices = this.buildIndices(childrenList.size());
			TreeNodeValueModel<T>[] childArray = this.buildArray(childrenList, childrenList.size());
			TreeModelAdapter.this.removeChildren(parentPath, childIndices, childArray);
		}

		@Override
		int childrenStartIndex() {
			throw new UnsupportedOperationException();
		}

		@Override
		int childrenSize() {
			throw new UnsupportedOperationException();
		}

		@Override
		Iterable<TreeNodeValueModel<T>> getChildren() {
			throw new UnsupportedOperationException();
		}

	}


	/**
	 * Wraps a ListChangeEvent for clearing children.
	 */
	class ChangeEventChangePolicy extends EventChangePolicy {

		ChangeEventChangePolicy(ListChangeEvent event) {
			super(event);
		}

		/**
		 * Remove all the old nodes and add all the new nodes.
		 */
		void rebuildChildren() {
			TreeNodeValueModel<T> parent = this.parent();
			TreeNodeValueModel<T>[] parentPath = parent.path();
			List<TreeNodeValueModel<T>> childrenList = TreeModelAdapter.this.childrenLists.get(parent);
			int[] childIndices = this.buildIndices(childrenList.size());
			TreeNodeValueModel<T>[] childArray = this.buildArray(childrenList, childrenList.size());
			TreeModelAdapter.this.removeChildren(parentPath, childIndices, childArray);

			childIndices = this.buildIndices(parent.childrenModel().size());
			childArray = this.buildArray(parent.childrenModel(), parent.childrenSize());
			TreeModelAdapter.this.addChildren(parentPath, childIndices, childArray);
		}

		@Override
		int childrenStartIndex() {
			throw new UnsupportedOperationException();
		}

		@Override
		int childrenSize() {
			throw new UnsupportedOperationException();
		}

		@Override
		Iterable<TreeNodeValueModel<T>> getChildren() {
			throw new UnsupportedOperationException();
		}

	}


	/**
	 * Wraps a TreeNodeValueModel for adding and removing its children.
	 */
	class NodeChangePolicy extends ChangePolicy {
		private final TreeNodeValueModel<T> node;

		NodeChangePolicy(TreeNodeValueModel<T> node) {
			super();
			this.node = node;
		}

		/**
		 * The node itself is the parent.
		 */
		@Override
		TreeNodeValueModel<T> parent() {
			return this.node;
		}

		/**
		 * Since we will always be dealing with all of the node's
		 * children, the children start index is always zero.
		 */
		@Override
		int childrenStartIndex() {
			return 0;
		}

		/**
		 * Since we will always be dealing with all of the node's
		 * children, the children size is always equal to the size
		 * of the children model.
		 */
		@Override
		int childrenSize() {
			return this.node.childrenModel().size();
		}

		/**
		 * Since we will always be dealing with all of the node's
		 * children, the children are all the objects held by
		 * the children model.
		 */
		@Override
		Iterable<TreeNodeValueModel<T>> getChildren() {
			return this.node.childrenModel();
		}

	}

}

Back to the top