Skip to content

Commit

Permalink
Merge pull request #12 from parttio/example/sortingwithcustomlogic
Browse files Browse the repository at this point in the history
added generic Vaadin example
  • Loading branch information
mstahv authored Sep 7, 2023
2 parents 7c58d04 + 9dbe5d5 commit 1daad96
Showing 2 changed files with 117 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/main/java/org/example/ExamplesLayout.java
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 src/main/java/org/example/otherexamples/SortByRomanNumbers.java
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;
}
}

0 comments on commit 1daad96

Please sign in to comment.