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 package info.magnolia.cms.util;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.core.ItemType;
38 import info.magnolia.cms.core.Content.ContentFilter;
39
40 import javax.jcr.RepositoryException;
41 import java.util.ArrayList;
42 import java.util.Collection;
43 import java.util.List;
44
45
46
47
48
49
50
51
52 public class SiblingsHelper {
53
54
55
56 public static SiblingsHelper of(Content node) throws RepositoryException {
57 return new SiblingsHelper(node, filterForTypeOf(node));
58 }
59
60
61
62
63
64 public static SiblingsHelper childrenOf(Content parent) throws RepositoryException {
65 return childrenOf(parent, parent.getItemType());
66 }
67
68
69
70
71
72 public static SiblingsHelper childrenOf(Content parent, ItemType childType) throws RepositoryException {
73 final NodeTypeFilter filter = new NodeTypeFilter(childType);
74 return childrenOf(parent, filter);
75 }
76
77
78
79
80
81 public static SiblingsHelper childrenOf(Content parent, ContentFilter filter) throws RepositoryException {
82 final Collection<Content> children = parent.getChildren(filter);
83 if (children.size() < 1) {
84 throw new IllegalStateException(parent + " has no children for " + filter);
85 }
86 final Content firstChild = children.iterator().next();
87 return new SiblingsHelper(firstChild, filter);
88 }
89
90 private static NodeTypeFilter filterForTypeOf(Content node) throws RepositoryException {
91 final ItemType type = node.getItemType();
92 return new NodeTypeFilter(type);
93 }
94
95 private final List<Content> siblings;
96 private final int lastIndex;
97 private Content current = null;
98 private int currentIndex = -1;
99
100 private SiblingsHelper(Content node, Content.ContentFilter filter) throws RepositoryException {
101 this.siblings = new ArrayList<Content>(node.getParent().getChildren(filter));
102 this.lastIndex = siblings.size() - 1;
103 this.current = node;
104
105 for (int i = 0; i <= lastIndex; i++) {
106 final Content c = siblings.get(i);
107 if (c.getUUID().equals(current.getUUID())) {
108 this.currentIndex = i;
109 break;
110 }
111 }
112 if (currentIndex < 0) {
113 throw new IllegalStateException("Given node not found in its own siblings.");
114 }
115 }
116
117
118 public Content next() {
119
120 this.currentIndex = currentIndex + 1;
121 this.current = siblings.get(currentIndex);
122 return current;
123 }
124
125 public Content prev() {
126
127 this.currentIndex = currentIndex - 1;
128 this.current = siblings.get(currentIndex);
129 return current;
130 }
131
132 public Content goTofirst() {
133 this.currentIndex = 0;
134 this.current = siblings.get(currentIndex);
135 return current;
136 }
137
138 public Content goTolast() {
139 this.currentIndex = siblings.size() - 1;
140 this.current = siblings.get(currentIndex);
141 return current;
142 }
143
144 public Content getCurrent() {
145 return current;
146 }
147
148
149
150
151 public int getIndex() {
152 return currentIndex;
153 }
154
155 public boolean isFirst() {
156 return currentIndex == 0;
157 }
158
159 public boolean isLast() {
160 return currentIndex == lastIndex;
161 }
162
163 }