Skip to content

Commit 3cbff9a

Browse files
committed
Closes #7: Added support for clearing menu items dinamically
1 parent 82ad4ba commit 3cbff9a

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

src/main/java/com/flowingcode/addons/applayout/AppDrawer.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,22 @@
4141
@Tag("app-drawer")
4242
@HtmlImport("bower_components/app-layout/app-drawer/app-drawer.html")
4343
public class AppDrawer extends Component implements HasComponents {
44+
45+
private PaperListbox pm = new PaperListbox(Arrays.asList(new Component[] {}));
4446

4547
public AppDrawer(String title) {
4648
getElement().setAttribute("id", "drawer");
4749
getElement().setAttribute("swipe-open", true);
4850
H4 htitle = new H4(title);
4951
htitle.getElement().setAttribute("style", "text-align:center");
5052
this.add(htitle);
53+
this.add(pm);
5154
}
5255

5356
public void setMenuItems(List<MenuItem> menuItems) {
5457
Component[] components = createComponents(menuItems);
55-
PaperListbox pm = new PaperListbox(Arrays.asList(components));
56-
add(pm);
58+
pm.removeAll();
59+
pm.add(components);
5760
}
5861

5962
private Component[] createComponents(List<MenuItem> menuItems) {

src/main/java/com/flowingcode/addons/applayout/AppLayout.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.flowingcode.addons.applayout;
22

3+
import java.util.ArrayList;
4+
35
/*-
46
* #%L
57
* App Layout Addon
@@ -58,6 +60,10 @@ public void setMenuItems(MenuItem... menuitems) {
5860
drawer.setMenuItems(Arrays.asList(menuitems));
5961
}
6062

63+
public void clearMenuItems() {
64+
drawer.setMenuItems(new ArrayList<MenuItem>());
65+
}
66+
6167
public void setToolbarIconButtons(MenuItem... menuItems) {
6268
header.setToolbarIconButtons(menuItems);
6369
}

src/test/java/com/flowingcode/addons/applayout/DemoView.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import com.flowingcode.addons.applayout.menu.MenuItem;
2424
import com.vaadin.flow.component.html.H3;
25+
import com.vaadin.flow.component.icon.VaadinIcon;
2526
import com.vaadin.flow.component.notification.Notification;
2627
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
2728
import com.vaadin.flow.router.Route;
@@ -31,6 +32,7 @@
3132
public class DemoView extends VerticalLayout {
3233

3334
private VerticalLayout container = new VerticalLayout();
35+
private final AppLayout app = new AppLayout("AppLayout Addon for Vaadin 10 Demo");
3436

3537
public DemoView() {
3638
container.setDefaultHorizontalComponentAlignment(Alignment.CENTER);
@@ -40,10 +42,20 @@ public DemoView() {
4042
this.setSpacing(false);
4143
this.setMargin(false);
4244

43-
AppLayout app = new AppLayout("AppLayout Addon for Vaadin 10 Demo");
45+
app.setMenuItems(createMenuItems());
46+
47+
app.setToolbarIconButtons(new MenuItem("Delete", "delete", () -> Notification.show("Delete action")),
48+
new MenuItem("Search", "search", () -> Notification.show("Search action")),
49+
new MenuItem("Close", "close", () -> Notification.show("Close action")));
50+
51+
this.add(app, container);
52+
}
53+
54+
private MenuItem[] createMenuItems() {
4455
MenuItem mi = new MenuItem("Say hello", "star", () -> showContent("Hello!"));
45-
app.setMenuItems(mi ,
56+
return new MenuItem[] {mi ,
4657
new MenuItem("About", "cloud", () -> showContent("About")),
58+
new MenuItem("Clear Items", "star", () -> app.clearMenuItems()),
4759
new MenuItem("Change Text & Icon", "cloud", () -> {
4860
if (mi.getIcon().equals("star")) {
4961
mi.setIcon("cloud");
@@ -59,14 +71,7 @@ public DemoView() {
5971
new MenuItem("SubMenu",
6072
new MenuItem("Hello Again",()->showContent("Hello Again!")),
6173
new MenuItem("And Again",()->showContent("And Again!")))
62-
));
63-
64-
65-
app.setToolbarIconButtons(new MenuItem("Delete", "delete", () -> Notification.show("Delete action")),
66-
new MenuItem("Search", "search", () -> Notification.show("Search action")),
67-
new MenuItem("Close", "close", () -> Notification.show("Close action")));
68-
69-
this.add(app, container);
74+
)};
7075
}
7176

7277
private void showContent(String content) {

0 commit comments

Comments
 (0)