-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from parttio/example/sortingwithcustomlogic
added generic Vaadin example
- Loading branch information
Showing
2 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.example; | ||
|
||
import com.vaadin.flow.component.Component; | ||
import com.vaadin.flow.component.HasElement; | ||
import com.vaadin.flow.component.html.Anchor; | ||
import com.vaadin.flow.component.html.H1; | ||
import com.vaadin.flow.component.orderedlayout.HorizontalLayout; | ||
import com.vaadin.flow.component.orderedlayout.VerticalLayout; | ||
import com.vaadin.flow.dom.Style; | ||
import com.vaadin.flow.router.RoutePrefix; | ||
import com.vaadin.flow.router.RouterLayout; | ||
import org.vaadin.firitin.appframework.MainLayout; | ||
import org.vaadin.firitin.components.orderedlayout.VHorizontalLayout; | ||
|
||
@RoutePrefix("examples") | ||
public class ExamplesLayout extends VerticalLayout implements RouterLayout { | ||
|
||
Anchor viewSource = new Anchor("", "View source"); | ||
|
||
private static final String baseSourceUrl = "https://github.com/parttio/addon-demos/blob/main/src/main/java/%s.java"; | ||
|
||
@Override | ||
public void showRouterLayoutContent(HasElement content) { | ||
String name = content.getClass().getName(); | ||
String simpleName = content.getClass().getSimpleName(); | ||
viewSource.setHref(baseSourceUrl.formatted(name.replace(".", "/"))); | ||
add(new VHorizontalLayout().withExpanded(new H1(simpleName)).withComponent(viewSource).withFullWidth()); | ||
RouterLayout.super.showRouterLayoutContent(content); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
src/main/java/org/example/otherexamples/SortByRomanNumbers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package org.example.otherexamples; | ||
|
||
import com.vaadin.flow.component.grid.Grid; | ||
import com.vaadin.flow.component.orderedlayout.VerticalLayout; | ||
import com.vaadin.flow.data.provider.QuerySortOrder; | ||
import com.vaadin.flow.data.provider.SortDirection; | ||
import com.vaadin.flow.router.Route; | ||
import org.example.ExamplesLayout; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
@Route(layout = ExamplesLayout.class) | ||
public class SortByRomanNumbers extends VerticalLayout { | ||
public SortByRomanNumbers() { | ||
Grid<String> grid = new Grid<>(); | ||
grid.addColumn(s -> s).setHeader("roman") | ||
.setSortProperty("roman"); // makes column sortable & gives it a property name | ||
grid.addColumn(s -> romanToInt(s)).setHeader("int value"); | ||
grid.addColumn(s -> s.length()).setHeader("Str length"); | ||
|
||
List<String> values = Arrays.asList("IIC", "CXI", "XI", "IV", "V", "VI", "I", "II", "III", "X"); | ||
// Don't do this! Basic setItems fails sorting naturally | ||
// grid.setItems(values); | ||
|
||
// Do this instead, using the "lazy loading API", you do the sorting! | ||
grid.setItems(q -> { | ||
Stream<String> stream = values.stream(); | ||
// the sorting part | ||
if(!q.getSortOrders().isEmpty()) { | ||
// here we sort on one column only, in real life probably bit | ||
// more complex logic | ||
QuerySortOrder so = q.getSortOrders().get(0); | ||
stream = stream.sorted((a, b) -> | ||
(so.getDirection() == SortDirection.ASCENDING) ? | ||
romanToInt(a) - romanToInt(b) : | ||
romanToInt(b) - romanToInt(a) | ||
); | ||
} | ||
// the lazy loading part, skip & limit | ||
return stream.skip(q.getOffset()).limit(q.getLimit()); | ||
}); | ||
|
||
|
||
add(grid); | ||
|
||
} | ||
|
||
|
||
int numValue(char rom) { | ||
if (rom == 'I') | ||
return 1; | ||
if (rom == 'V') | ||
return 5; | ||
if (rom == 'X') | ||
return 10; | ||
if (rom == 'L') | ||
return 50; | ||
if (rom == 'C') | ||
return 100; | ||
if (rom == 'D') | ||
return 500; | ||
if (rom == 'M') | ||
return 1000; | ||
return -1; | ||
} | ||
int romanToInt(String str) { | ||
int sum = 0; | ||
for (int i=0; i<str.length(); i++) { | ||
int s1 = numValue(str.charAt(i)); | ||
if (i+1 <str.length()) { | ||
int s2 = numValue(str.charAt(i+1)); | ||
if (s1 >= s2) { | ||
sum = sum + s1; | ||
} | ||
else{ | ||
sum = sum - s1; | ||
} | ||
} | ||
else { | ||
sum = sum + s1; | ||
} | ||
} | ||
return sum; | ||
} | ||
} |