FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add Ordered interface · patternfly-java/patternfly-java@7fa221b · GitHub

Commit 7fa221b

Browse files
committed
Add Ordered interface
1 parent 02d5c26 commit 7fa221b

5 files changed

Lines changed: 151 additions & 18 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66

77
## [Unreleased]
88

9+
### Added
10+
11+
- Add [`Ordered`](https://patternfly-java.github.io/patternfly-java/org/patternfly/component/Ordered.html) interface and implementations for `List`, and `DataList`.
12+
913
## [0.4.16] - 2026-02-05
1014

1115
### Added
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2023 Red Hat
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.patternfly.component;
17+
18+
import java.util.Comparator;
19+
import java.util.TreeSet;
20+
21+
import org.jboss.elemento.ElementContainerMethods;
22+
import org.jboss.elemento.IsElement;
23+
import org.jboss.elemento.TypedBuilder;
24+
import elemental2.dom.Element;
25+
import elemental2.dom.HTMLElement;
26+
27+
import static java.util.Comparator.comparing;
28+
import static org.jboss.elemento.Elements.insertBefore;
29+
30+
/**
31+
* The Ordered interface can be implemented by components that maintain its elements in a specific order. Typically, the
32+
* components also implements {@link HasItems}. By default, the order is determined by the {@link #defaultOrder()} method which
33+
* in turn determines the order based on the elements data {@value #DATA_ORDER} attribute. The order is maintained as elements
34+
* are added to the component.
35+
*
36+
* @param <E> the type of the main element
37+
* @param <B> the type of the builder for chaining methods
38+
* @param <T> the type of components being ordered
39+
*/
40+
public interface Ordered<E extends Element, B extends TypedBuilder<E, B>, T extends IsElement<? extends HTMLElement>>
41+
extends TypedBuilder<E, B>, IsElement<E> {
42+
43+
String DATA_ORDER = "order";
44+
45+
/**
46+
* Adds an item to a container in a specific order determined by the provided comparator. The item's position is calculated
47+
* relative to the existing items in the container.
48+
*
49+
* @param hasItems the source of the current items to determine the insertion position
50+
* @param container the container to which the item will be added
51+
* @param item the item to be added to the container
52+
* @param comparator the comparator used to determine the order of the items
53+
*/
54+
default void addOrdered(HasItems<E, B, T> hasItems, ElementContainerMethods<E, B> container, T item,
55+
Comparator<T> comparator) {
56+
HTMLElement elementBefore = null;
57+
TreeSet<T> orderedItems = new TreeSet<>(comparator);
58+
orderedItems.addAll(hasItems.items());
59+
for (T existing : orderedItems) {
60+
if (comparator.compare(item, existing) < 0) {
61+
elementBefore = existing.element();
62+
break;
63+
}
64+
}
65+
if (elementBefore != null) {
66+
insertBefore(item.element(), elementBefore);
67+
} else {
68+
container.add(item.element());
69+
}
70+
}
71+
72+
/**
73+
* Provides a default comparator for ordering elements based on their {@value #DATA_ORDER} dataset value.
74+
*
75+
* @return a comparator that compares elements using the {@value #DATA_ORDER} attribute from their dataset
76+
*/
77+
default Comparator<T> defaultOrder() {
78+
return comparing(o -> o.element().dataset.get(DATA_ORDER));
79+
}
80+
81+
/**
82+
* Orders the elements contained in the component using the default comparator. The default comparator is based on the
83+
* {@value #DATA_ORDER} dataset attribute of the elements.
84+
*
85+
* @return the builder instance with the elements ordered
86+
*/
87+
default B ordered() {
88+
return ordered(defaultOrder());
89+
}
90+
91+
/**
92+
* Orders the elements contained in the component using the specified comparator. The comparator determines the order in
93+
* which the elements are arranged.
94+
*
95+
* @param comparator the comparator used to define the ordering of the elements
96+
* @return the builder instance with the elements ordered
97+
*/
98+
B ordered(Comparator<T> comparator);
99+
}

‎components/src/main/java/org/patternfly/component/list/DataList.java‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.patternfly.component.list;
1717

1818
import java.util.ArrayList;
19+
import java.util.Comparator;
1920
import java.util.Iterator;
2021
import java.util.LinkedHashMap;
2122
import java.util.List;
@@ -25,6 +26,7 @@
2526
import org.patternfly.component.BaseComponent;
2627
import org.patternfly.component.ComponentType;
2728
import org.patternfly.component.HasItems;
29+
import org.patternfly.component.Ordered;
2830
import org.patternfly.component.table.Wrap;
2931
import org.patternfly.style.GridBreakpoint;
3032
import org.patternfly.style.Modifiers.Compact;
@@ -59,7 +61,8 @@
5961
*/
6062
public class DataList extends BaseComponent<HTMLUListElement, DataList> implements
6163
Compact<HTMLUListElement, DataList>,
62-
HasItems<HTMLUListElement, DataList, DataListItem> {
64+
HasItems<HTMLUListElement, DataList, DataListItem>,
65+
Ordered<HTMLUListElement, DataList, DataListItem> {
6366

6467
// ------------------------------------------------------ factory
6568

@@ -72,6 +75,7 @@ public static DataList dataList() {
7275
private final Map<String, DataListItem> items;
7376
private final List<BiConsumer<DataList, DataListItem>> onAdd;
7477
private final List<BiConsumer<DataList, DataListItem>> onRemove;
78+
private Comparator<DataListItem> comparator;
7579

7680
DataList() {
7781
super(ComponentType.DataList, ul().css(component(dataList))
@@ -87,10 +91,14 @@ public static DataList dataList() {
8791

8892
@Override
8993
public DataList add(DataListItem item) {
94+
if (comparator != null) {
95+
addOrdered(this, this, item, comparator);
96+
} else {
97+
add(item.element());
98+
}
9099
items.put(item.identifier(), item);
91-
DataList result = add(item.element());
92100
onAdd.forEach(bc -> bc.accept(this, item));
93-
return result;
101+
return this;
94102
}
95103

96104
// ------------------------------------------------------ builder
@@ -103,6 +111,12 @@ public DataList gridBreakpoint(GridBreakpoint breakpoint) {
103111
return this;
104112
}
105113

114+
@Override
115+
public DataList ordered(Comparator<DataListItem> comparator) {
116+
this.comparator = comparator;
117+
return this;
118+
}
119+
106120
/** Determines which wrapping modifier to apply to the data list */
107121
public DataList wrap(Wrap wrap) {
108122
if (verifyEnum(element(), "wrap", wrap, nowrap, truncate, breakWord)) {

‎components/src/main/java/org/patternfly/component/list/List.java‎

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515
*/
1616
package org.patternfly.component.list;
1717

18+
import java.util.Comparator;
1819
import java.util.Iterator;
1920
import java.util.LinkedHashMap;
2021
import java.util.Map;
2122
import java.util.function.BiConsumer;
22-
import java.util.function.Function;
2323

2424
import org.jboss.elemento.HTMLContainerBuilder;
2525
import org.patternfly.component.BaseComponent;
2626
import org.patternfly.component.ComponentType;
2727
import org.patternfly.component.HasItems;
28+
import org.patternfly.component.Ordered;
2829
import org.patternfly.core.Roles;
2930
import org.patternfly.style.Classes;
3031
import org.patternfly.style.Modifiers.Bordered;
@@ -53,6 +54,7 @@ public class List extends BaseComponent<HTMLElement, List> implements
5354
Bordered<HTMLElement, List>,
5455
HasItems<HTMLElement, List, ListItem>,
5556
Inline<HTMLElement, List>,
57+
Ordered<HTMLElement, List, ListItem>,
5658
Plain<HTMLElement, List> {
5759

5860
// ------------------------------------------------------ factory
@@ -71,6 +73,7 @@ public static <E extends HTMLElement> List list(HTMLContainerBuilder<E> builder)
7173
private final Map<String, ListItem> items;
7274
private final java.util.List<BiConsumer<List, ListItem>> onAdd;
7375
private final java.util.List<BiConsumer<List, ListItem>> onRemove;
76+
private Comparator<ListItem> comparator;
7477

7578
<E extends HTMLElement> List(HTMLContainerBuilder<E> builder) {
7679
super(ComponentType.List, builder.css(Classes.component(list))
@@ -83,22 +86,14 @@ <E extends HTMLElement> List(HTMLContainerBuilder<E> builder) {
8386

8487
// ------------------------------------------------------ add
8588

86-
public <T> List addItems(Iterable<T> items, Function<T, ListItem> display) {
87-
for (T item : items) {
88-
ListItem li = display.apply(item);
89-
addItem(li);
90-
}
91-
return this;
92-
}
93-
94-
public List addItem(ListItem item) {
95-
return add(item);
96-
}
97-
9889
@Override
9990
public List add(ListItem item) {
91+
if (comparator != null) {
92+
addOrdered(this, this, item, comparator);
93+
} else {
94+
add(item.element());
95+
}
10096
items.put(item.identifier(), item);
101-
add(item.element());
10297
onAdd.forEach(bc -> bc.accept(this, item));
10398
return this;
10499
}
@@ -107,12 +102,18 @@ public List addDivider() {
107102
return add(divider(li));
108103
}
109104

110-
111105
// ------------------------------------------------------ builder
106+
112107
public List largeIcons() {
113108
return css(modifier(icon, lg));
114109
}
115110

111+
@Override
112+
public List ordered(Comparator<ListItem> comparator) {
113+
this.comparator = comparator;
114+
return this;
115+
}
116+
116117
@Override
117118
public List that() {
118119
return this;

‎showcase/images.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2023 Red Hat
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
import * as fs from 'fs';
217
import * as path from 'path';
318
import { fileURLToPath } from 'url';

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL