/
MarkupContainer.java
2218 lines (1979 loc) · 59.8 KB
/
MarkupContainer.java
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
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.wicket;
import java.io.Serializable;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.apache.commons.collections4.map.LinkedMap;
import org.apache.wicket.behavior.OutputMarkupContainerClassNameBehavior;
import org.apache.wicket.core.util.string.ComponentStrings;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.ComponentTag.IAutoComponentFactory;
import org.apache.wicket.markup.IMarkupFragment;
import org.apache.wicket.markup.Markup;
import org.apache.wicket.markup.MarkupElement;
import org.apache.wicket.markup.MarkupException;
import org.apache.wicket.markup.MarkupFactory;
import org.apache.wicket.markup.MarkupNotFoundException;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.MarkupType;
import org.apache.wicket.markup.WicketTag;
import org.apache.wicket.markup.html.border.Border;
import org.apache.wicket.markup.html.form.AutoLabelResolver;
import org.apache.wicket.markup.resolver.ComponentResolvers;
import org.apache.wicket.model.IComponentInheritedModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.IWrapModel;
import org.apache.wicket.settings.DebugSettings;
import org.apache.wicket.util.lang.Args;
import org.apache.wicket.util.lang.Classes;
import org.apache.wicket.util.lang.Generics;
import org.apache.wicket.util.string.Strings;
import org.apache.wicket.util.visit.ClassVisitFilter;
import org.apache.wicket.util.visit.IVisit;
import org.apache.wicket.util.visit.IVisitor;
import org.apache.wicket.util.visit.Visits;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A MarkupContainer holds a map of child components.
* <ul>
* <li><b>Children </b>- Children can be added by calling the {@link #add(Component...)} method, and
* they can be looked up using a colon separated path. For example, if a container called "a" held a
* nested container "b" which held a nested component "c", then a.get("b:c") would return the
* Component with id "c". The number of children in a MarkupContainer can be determined by calling
* size(), and the whole hierarchy of children held by a MarkupContainer can be traversed by calling
* visitChildren(), passing in an implementation of IVisitor.</li>
*
* <li><b>Markup Rendering </b>- A MarkupContainer also holds/references associated markup which is
* used to render the container. As the markup stream for a container is rendered, component
* references in the markup are resolved by using the container to look up Components in the
* container's component map by id. Each component referenced by the markup stream is given an
* opportunity to render itself using the markup stream.</li>
* </ul>
* <p>
* Components may alter their referring tag, replace the tag's body or insert markup after the tag.
* But components cannot remove tags from the markup stream. This is an important guarantee because
* graphic designers may be setting attributes on component tags that affect visual presentation.
* <p>
* The type of markup held in a given container subclass can be determined by calling
* {@link #getMarkupType()}. Markup is accessed via a MarkupStream object which allows a component
* to traverse ComponentTag and RawMarkup MarkupElements while rendering a response. Markup in the
* stream may be HTML or some other kind of markup, such as VXML, as determined by the specific
* container subclass.
* <p>
* A markup stream may be directly associated with a container via setMarkupStream. However, a
* container which does not have a markup stream (its getMarkupStream() returns null) may inherit a
* markup stream from a container above it in the component hierarchy. The
* {@link #findMarkupStream()} method will locate the first container at or above this container
* which has a markup stream.
* <p>
* All Page containers set a markup stream before rendering by calling the method
* {@link #getAssociatedMarkupStream(boolean)} to load the markup associated with the page. Since
* Page is at the top of the container hierarchy, it is guaranteed that {@link #findMarkupStream()}
* will always return a valid markup stream.
*
* @see MarkupStream
* @author Jonathan Locke
*/
public abstract class MarkupContainer extends Component implements Iterable<Component>
{
private static final long serialVersionUID = 1L;
private static final int INITIAL_CHILD_LIST_CAPACITY = 12;
/**
* The threshold where we start using a Map to store children in, replacing a List. Adding
* components to a list is O(n), and to a map O(1). The magic number is 24, due to a Map using
* more memory to store its elements and below 24 children there's no discernible difference
* between adding to a Map or a List.
*
* We have focused on adding elements to a list, instead of indexed lookups because adding is an
* action that is performed very often, and lookups often are done by component IDs, not index.
*/
static final int MAPIFY_THRESHOLD = 24; // 32 * 0.75
/** Log for reporting. */
private static final Logger log = LoggerFactory.getLogger(MarkupContainer.class);
/**
* Metadata key for looking up the list of removed children necessary for tracking modifications
* during iteration of the children of this markup container.
*
* This is stored in meta data because it only is necessary when a child is removed, and this
* saves the memory necessary for a field on a widely used class.
*/
private static final MetaDataKey<LinkedList<RemovedChild>> REMOVALS_KEY = new MetaDataKey<>()
{
private static final long serialVersionUID = 1L;
};
/**
* Administrative class for detecting removed children during child iteration. Not intended to
* be serializable but for e.g. determining the size of the component it has to be serializable.
*/
private static class RemovedChild implements Serializable
{
private static final long serialVersionUID = 1L;
private transient final Component removedChild;
private transient final Component previousSibling;
private RemovedChild(Component removedChild, Component previousSibling)
{
this.removedChild = removedChild;
this.previousSibling = previousSibling;
}
}
/**
* Administrative counter to keep track of modifications to the list of children during
* iteration.
*
* When the {@link #children_size()} changes due to an addition or removal of a child component,
* the modCounter is increased. This way iterators that iterate over the children of this
* container can keep track when they need to change their iteration strategy.
*/
private transient int modCounter = 0;
/**
* The children of this markup container, if any. Can be a Component when there's only one
* child, a List when the number of children is fewer than {@link #MAPIFY_THRESHOLD} or a Map
* when there are more children.
*/
private Object children;
public MarkupContainer(final String id)
{
this(id, null);
}
public MarkupContainer(final String id, IModel<?> model)
{
super(id, model);
}
/**
* Adds the child component(s) to this container.
*
* @param children
* The child(ren) to add.
* @throws IllegalArgumentException
* Thrown if a child with the same id is replaced by the add operation.
* @return This
*/
public MarkupContainer add(final Component... children)
{
for (Component child : children)
{
Args.notNull(child, "child");
if (this == child)
{
throw new IllegalArgumentException(
exceptionMessage("Trying to add this component to itself."));
}
MarkupContainer parent = getParent();
while (parent != null)
{
if (child == parent)
{
String msg = "You can not add a component's parent as child to the component (loop): Component: " +
this.toString(false) + "; parent == child: " + parent.toString(false);
if (child instanceof Border.BorderBodyContainer)
{
msg += ". Please consider using Border.addToBorder(new " +
Classes.simpleName(this.getClass()) + "(\"" + this.getId() +
"\", ...) instead of add(...)";
}
throw new WicketRuntimeException(msg);
}
parent = parent.getParent();
}
checkHierarchyChange(child);
if (log.isDebugEnabled())
{
log.debug("Add " + child.getId() + " to " + this);
}
// Add the child to my children
Component previousChild = children_put(child);
if (previousChild != null && previousChild != child)
{
throw new IllegalArgumentException(
exceptionMessage("A child '" + previousChild.getClass().getSimpleName() +
"' with id '" + child.getId() + "' already exists"));
}
addedComponent(child);
}
return this;
}
/**
* Replaces a child component of this container with another or just adds it in case no child
* with the same id existed yet.
*
* @param children
* The child(ren) to be added or replaced
* @return this markup container
*/
public MarkupContainer addOrReplace(final Component... children)
{
for (Component child : children)
{
Args.notNull(child, "child");
checkHierarchyChange(child);
if (get(child.getId()) == null)
{
add(child);
}
else
{
replace(child);
}
}
return this;
}
/**
* This method allows a component to be added by an auto-resolver such as AutoLinkResolver.
* While the component is being added, the component's FLAG_AUTO boolean is set. The isAuto()
* method of Component returns true if a component or any of its parents has this bit set. When
* a component is added via autoAdd(), the logic in Page that normally (a) checks for
* modifications during the rendering process, and (b) versions components, is bypassed if
* Component.isAuto() returns true.
* <p>
* The result of all this is that components added with autoAdd() are free from versioning and
* can add their own children without the usual exception that would normally be thrown when the
* component hierarchy is modified during rendering.
*
* @param component
* The component to add
* @param markupStream
* Null, if the parent container is able to provide the markup. Else the markup
* stream to be used to render the component.
* @return True, if component has been added
*/
public final boolean autoAdd(final Component component, MarkupStream markupStream)
{
Args.notNull(component, "component");
// Replace strategy
component.setAuto(true);
if (markupStream != null)
{
component.setMarkup(markupStream.getMarkupFragment());
}
// Add the child to the parent.
// Arguably child.setParent() can be used as well. It connects the child to the parent and
// that's all what most auto-components need. Unfortunately child.onDetach() will not / can
// not be invoked, since the parent doesn't known its one of his children. Hence we need to
// properly add it.
children_remove(component.getId());
add(component);
return true;
}
/**
* @param component
* The component to check
* @param recurse
* True if all descendents should be considered
* @return True if the component is contained in this container
*/
public boolean contains(final Component component, final boolean recurse)
{
Args.notNull(component, "component");
if (recurse)
{
// Start at component and continue while we're not out of parents
for (Component current = component; current != null;)
{
// Get parent
final MarkupContainer parent = current.getParent();
// If this container is the parent, then the component is
// recursively contained by this container
if (parent == this)
{
// Found it!
return true;
}
// Move up the chain to the next parent
current = parent;
}
// Failed to find this container in component's ancestry
return false;
}
else
{
// Is the component contained in this container?
return component.getParent() == this;
}
}
/**
* Get a child component by looking it up with the given path.
* <p>
* A component path consists of component ids separated by colons, e.g. "b:c" identifies a
* component "c" inside container "b" inside this container.
*
* @param path
* path to component
* @return The component at the path
*/
@Override
public final Component get(String path)
{
// Reference to this container
if (Strings.isEmpty(path))
{
return this;
}
// process parent .. references
MarkupContainer container = this;
String id = Strings.firstPathComponent(path, Component.PATH_SEPARATOR);
while (Component.PARENT_PATH.equals(id))
{
container = container.getParent();
if (container == null)
{
return null;
}
path = path.length() == id.length() ? "" : path.substring(id.length() + 1);
id = Strings.firstPathComponent(path, Component.PATH_SEPARATOR);
}
if (Strings.isEmpty(id))
{
return container;
}
// Get child by id
Component child = container.children_get(id);
// Found child?
if (child != null)
{
String path2 = Strings.afterFirstPathComponent(path, Component.PATH_SEPARATOR);
// Recurse on latter part of path
return child.get(path2);
}
return null;
}
/**
* Gets a fresh markup stream that contains the (immutable) markup resource for this class.
*
* @param throwException
* If true, throw an exception, if markup could not be found
* @return A stream of MarkupElement elements
*/
public MarkupStream getAssociatedMarkupStream(final boolean throwException)
{
IMarkupFragment markup = getAssociatedMarkup();
// If we found markup for this container
if (markup != null)
{
return new MarkupStream(markup);
}
if (throwException == true)
{
// throw exception since there is no associated markup
throw new MarkupNotFoundException(
"Markup of type '" +
getMarkupType().getExtension() +
"' for component '" +
getClass().getName() +
"' not found." +
" Enable debug messages for org.apache.wicket.util.resource to get a list of all filenames tried.: " +
toString());
}
return null;
}
/**
* Gets a fresh markup stream that contains the (immutable) markup resource for this class.
*
* @return A stream of MarkupElement elements. Null if not found.
*/
public Markup getAssociatedMarkup()
{
try
{
Markup markup = MarkupFactory.get().getMarkup(this, false);
// If we found markup for this container
if ((markup != null) && (markup != Markup.NO_MARKUP))
{
return markup;
}
return null;
}
catch (MarkupException ex)
{
// re-throw it. The exception contains already all the information
// required.
throw ex;
}
catch (MarkupNotFoundException ex)
{
// re-throw it. The exception contains already all the information
// required.
throw ex;
}
catch (WicketRuntimeException ex)
{
// throw exception since there is no associated markup
throw new MarkupNotFoundException(
exceptionMessage("Markup of type '" + getMarkupType().getExtension() +
"' for component '" + getClass().getName() + "' not found." +
" Enable debug messages for org.apache.wicket.util.resource to get a list of all filenames tried"),
ex);
}
}
/**
* Get the markup of the child.
*
* @see Component#getMarkup()
*
* @param child
* The child component. If null, the container's markup will be returned. See Border,
* Panel or Enclosure where getMarkup(null) != getMarkup().
* @return The child's markup
*/
public IMarkupFragment getMarkup(final Component child)
{
// Delegate request to attached markup sourcing strategy.
return getMarkupSourcingStrategy().getMarkup(this, child);
}
/**
* Get the type of associated markup for this component. The markup type for a component is
* independent of whether or not the component actually has an associated markup resource file
* (which is determined at runtime).
*
* @return The type of associated markup for this component (for example, "html", "wml" or
* "vxml"). If there is no markup type for a component, null may be returned, but this
* means that no markup can be loaded for the class. Null is also returned if the
* component, or any of its parents, has not been added to a Page.
*/
public MarkupType getMarkupType()
{
MarkupContainer parent = getParent();
if (parent != null)
{
return parent.getMarkupType();
}
return null;
}
/**
* THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT USE IT.
*
* Adds a child component to this container.
*
* @param child
* The child
* @throws IllegalArgumentException
* Thrown if a child with the same id is replaced by the add operation.
*/
public void internalAdd(final Component child)
{
if (log.isDebugEnabled())
{
log.debug("internalAdd " + child.getId() + " to " + this);
}
// Add to map
children_put(child);
addedComponent(child);
}
/**
* Gives an iterator that allow you to iterate through the children of this markup container in
* the order the children were added. The iterator supports additions and removals from the list
* of children during iteration.
*
* @return Iterator that iterates through children in the order they were added
*/
@Override
public Iterator<Component> iterator()
{
/**
* Iterator that knows how to change between a single child, list of children and map of
* children. Keeps track when the iterator was last sync'd with the markup container's
* tracking of changes to the list of children.
*/
class MarkupChildIterator implements Iterator<Component>
{
private int indexInRemovalsSinceLastUpdate;
private int expectedModCounter = -1;
private Component currentComponent = null;
private Iterator<Component> internalIterator = null;
@Override
public boolean hasNext()
{
refreshInternalIteratorIfNeeded();
return internalIterator.hasNext();
}
@Override
public Component next()
{
refreshInternalIteratorIfNeeded();
return currentComponent = internalIterator.next();
}
@Override
public void remove()
{
MarkupContainer.this.remove(currentComponent);
refreshInternalIteratorIfNeeded();
}
private void refreshInternalIteratorIfNeeded()
{
if (expectedModCounter >= modCounter) {
// no new modifications
return;
}
if (children == null)
{
internalIterator = Collections.emptyIterator();
}
else if (children instanceof Component)
{
internalIterator = Collections.singleton((Component)children).iterator();
}
else if (children instanceof List)
{
List<Component> childrenList = children();
internalIterator = childrenList.iterator();
}
else
{
Map<String, Component> childrenMap = children();
internalIterator = childrenMap.values().iterator();
}
// since we now have a new iterator, we need to set it to the last known position
currentComponent = findLastExistingChildAlreadyReturned(currentComponent);
expectedModCounter = modCounter;
if (currentComponent != null)
{
// move the new internal iterator to the place of the last processed component
while (internalIterator.hasNext() &&
internalIterator.next() != currentComponent)
// noop
;
}
}
private Component findLastExistingChildAlreadyReturned(Component current)
{
if (current == null) {
indexInRemovalsSinceLastUpdate = 0;
} else {
LinkedList<RemovedChild> removals = removals_get();
if (removals != null) {
check_removed:
while (current != null)
{
for (int i = indexInRemovalsSinceLastUpdate; i < removals.size(); i++)
{
RemovedChild removal = removals.get(i);
if (removal.removedChild == current ||
removal.removedChild == null)
{
current = removal.previousSibling;
// current was removed, use its sibling instead
continue check_removed;
}
}
// current wasn't removed, keep it
break;
}
indexInRemovalsSinceLastUpdate = removals.size();
}
}
return current;
}
};
return new MarkupChildIterator();
}
/**
* Creates an iterator that iterates over children in the order specified by comparator. This
* works on a copy of the children list.
*
* @param comparator
* The comparator
* @return Iterator that iterates over children in the order specified by comparator
*/
public final Iterator<Component> iterator(Comparator<Component> comparator)
{
final List<Component> sorted = copyChildren();
Collections.sort(sorted, comparator);
return sorted.iterator();
}
/**
* Removes a component from the children identified by the {@code component.getId()}
*
* @param component
* Component to remove from this container
* @return {@code this} for chaining
*/
public MarkupContainer remove(final Component component)
{
checkHierarchyChange(component);
Args.notNull(component, "component");
children_remove(component.getId());
removedComponent(component);
return this;
}
/**
* Removes the given component
*
* @param id
* The id of the component to remove
* @return {@code this} for chaining
*/
public MarkupContainer remove(final String id)
{
Args.notNull(id, "id");
final Component component = get(id);
if (component != null)
{
remove(component);
}
else
{
throw new WicketRuntimeException("Unable to find a component with id '" + id +
"' to remove");
}
return this;
}
/**
* Removes all children from this container.
* <p>
* Note: implementation does not call {@link MarkupContainer#remove(Component) } for each
* component.
*
* @return {@code this} for method chaining
*/
public MarkupContainer removeAll()
{
if (children != null)
{
addStateChange();
for (Component child : this)
{
// Do not call remove() because the state change would then be
// recorded twice.
child.internalOnRemove();
child.detach();
child.setParent(null);
}
children = null;
removals_add(null, null);
}
return this;
}
/**
* Renders the entire associated markup for a container such as a Border or Panel. Any leading
* or trailing raw markup in the associated markup is skipped.
*
* @param openTagName
* the tag to render the associated markup for
*/
public final void renderAssociatedMarkup(final String openTagName)
{
// Get associated markup file for the Border or Panel component
final MarkupStream associatedMarkupStream = new MarkupStream(getMarkup(null));
// Get open tag in associated markup of border component
MarkupElement elem = associatedMarkupStream.get();
if ((elem instanceof ComponentTag) == false)
{
associatedMarkupStream.throwMarkupException("Expected the open tag. Markup for a "
+ openTagName + " component must begin a tag like '<wicket:" + openTagName + ">'");
}
// Check for required open tag name
ComponentTag associatedMarkupOpenTag = (ComponentTag)elem;
if (!(associatedMarkupOpenTag.isOpen() && (associatedMarkupOpenTag instanceof WicketTag)))
{
associatedMarkupStream.throwMarkupException("Markup for a " + openTagName
+ " component must begin a tag like '<wicket:" + openTagName + ">'");
}
try
{
setIgnoreAttributeModifier(true);
final DebugSettings.ClassOutputStrategy outputClassName = getApplication().getDebugSettings()
.getOutputMarkupContainerClassNameStrategy();
if (outputClassName == DebugSettings.ClassOutputStrategy.TAG_ATTRIBUTE)
{
associatedMarkupOpenTag.addBehavior(OutputMarkupContainerClassNameBehavior.INSTANCE);
}
renderComponentTag(associatedMarkupOpenTag);
associatedMarkupStream.next();
String className = null;
if (outputClassName == DebugSettings.ClassOutputStrategy.HTML_COMMENT)
{
className = Classes.name(getClass());
getResponse().write("<!-- MARKUP FOR ");
getResponse().write(className);
getResponse().write(" BEGIN -->");
}
renderComponentTagBody(associatedMarkupStream, associatedMarkupOpenTag);
if (outputClassName == DebugSettings.ClassOutputStrategy.HTML_COMMENT)
{
getResponse().write("<!-- MARKUP FOR ");
getResponse().write(className);
getResponse().write(" END -->");
}
renderClosingComponentTag(associatedMarkupStream, associatedMarkupOpenTag, false);
}
finally
{
setIgnoreAttributeModifier(false);
}
}
/**
* Replaces a child component of this container with another
*
* @param child
* The child
* @throws IllegalArgumentException
* Thrown if there was no child with the same id.
* @return This
*/
public MarkupContainer replace(final Component child)
{
Args.notNull(child, "child");
checkHierarchyChange(child);
if (log.isDebugEnabled())
{
log.debug("Replacing " + child.getId() + " in " + this);
}
if (child.getParent() != this)
{
final Component replaced = children_put(child);
// Look up to make sure it was already in the map
if (replaced == null)
{
throw new WicketRuntimeException(
exceptionMessage("Cannot replace a component which has not been added: id='" +
child.getId() + "', component=" + child));
}
// first remove the component.
removedComponent(replaced);
// The generated markup id remains the same
child.setMarkupId(replaced);
// then add the other one.
addedComponent(child);
}
return this;
}
@Override
public MarkupContainer setDefaultModel(final IModel<?> model)
{
final IModel<?> previous = getModelImpl();
super.setDefaultModel(model);
if (previous instanceof IComponentInheritedModel)
{
visitChildren(new IVisitor<Component, Void>()
{
@Override
public void component(final Component component, final IVisit<Void> visit)
{
IModel<?> compModel = component.getDefaultModel();
if (compModel instanceof IWrapModel)
{
compModel = ((IWrapModel<?>)compModel).getWrappedModel();
}
if (compModel == previous)
{
component.setDefaultModel(null);
}
else if (compModel == model)
{
component.modelChanged();
}
}
});
}
return this;
}
/**
* Get the number of children in this container.
*
* @return Number of children in this container
*/
public int size()
{
return children_size();
}
@Override
public String toString()
{
return toString(false);
}
/**
* @param detailed
* True if a detailed string is desired
* @return String representation of this container
*/
@Override
public String toString(final boolean detailed)
{
final StringBuilder buffer = new StringBuilder();
buffer.append('[').append(Classes.simpleName(this.getClass())).append(' ');
buffer.append(super.toString(detailed));
if (detailed && children_size() != 0)
{
buffer.append(", children = ");
// Loop through child components
boolean first = true;
for (Component child : this)
{
if (first)
{
buffer.append(' ');
first = false;
}
buffer.append(child.toString());
}
}
buffer.append(']');
return buffer.toString();
}
/**
* Traverses all child components of the given class in this container, calling the visitor's
* visit method at each one.
*
* Make sure that if you give a type S that the clazz parameter will only resolve to those
* types. Else a class cast exception will occur.
*
* @param <S>
* The type that goes into the Visitor.component() method.
* @param <R>
* @param clazz
* The class of child to visit
* @param visitor
* The visitor to call back to
* @return The return value from a visitor which halted the traversal, or null if the entire
* traversal occurred
*/
public final <S extends Component, R> R visitChildren(final Class<?> clazz,
final IVisitor<S, R> visitor)
{
return Visits.visitChildren(this, visitor, new ClassVisitFilter(clazz));
}
/**
* Traverses all child components in this container, calling the visitor's visit method at each
* one.
*
* @param <R>
* @param visitor
* The visitor to call back to
* @return The return value from a visitor which halted the traversal, or null if the entire
* traversal occurred
*/
public final <R> R visitChildren(final IVisitor<Component, R> visitor)
{
return Visits.visitChildren(this, visitor);
}
/**
* @param child
* Component being added
*/
private void addedComponent(final Component child)
{
// Check for degenerate case
Args.notNull(child, "child");
MarkupContainer parent = child.getParent();