Skip to content

Commit 3308bc7

Browse files
committed
Unify MenuItem model and component
1 parent ad921a8 commit 3308bc7

File tree

10 files changed

+238
-319
lines changed

10 files changed

+238
-319
lines changed

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@
115115
<artifactId>iron-collapse-button</artifactId>
116116
<version>1.0.1</version>
117117
</dependency>
118+
119+
<!-- DEMO -->
120+
<dependency>
121+
<groupId>com.flowingcode.addons</groupId>
122+
<artifactId>iron-icons</artifactId>
123+
<version>1.0.1</version>
124+
<scope>test</scope>
125+
</dependency>
118126
</dependencies>
119127

120128
<build>

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

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,12 @@
2323

2424
import java.util.Collections;
2525
import java.util.List;
26-
import java.util.Optional;
2726

28-
import com.flowingcode.addons.applayout.menu.MenuItem;
2927
import com.vaadin.flow.component.Component;
3028
import com.vaadin.flow.component.HasComponents;
3129
import com.vaadin.flow.component.Tag;
3230
import com.vaadin.flow.component.dependency.HtmlImport;
3331
import com.vaadin.flow.component.html.H4;
34-
import com.vaadin.flow.server.Command;
3532
import com.vaadin.flow.shared.Registration;
3633

3734
/**
@@ -58,9 +55,7 @@ public AppDrawer(Component headerComponent) {
5855
this.header = headerComponent;
5956
getElement().setAttribute("id", "drawer");
6057
setSwipeOpen(true);
61-
this.add(headerComponent);
62-
this.add(pm);
63-
58+
6459
Registration[] r = new Registration[1];
6560
r[0] = getElement().addEventListener("app-drawer-transitioned", ev->{
6661
//need to adjust the height after the drawer has been rendered
@@ -69,43 +64,47 @@ public AppDrawer(Component headerComponent) {
6964
});
7065

7166
getElement().getStyle().set("--fc-separator-background-color", "var(--app-drawer-content-container_-_background-color)");
67+
removeAll();
7268
}
7369

7470
public void setSwipeOpen(boolean swipeOpen) {
7571
getElement().setAttribute("swipe-open", swipeOpen);
7672
}
7773

74+
@Override
75+
public void add(Component... components) {
76+
for (Component c : components) {
77+
if (c instanceof MenuItem) {
78+
pm.add(c);
79+
} else {
80+
HasComponents.super.add(components);
81+
}
82+
}
83+
}
84+
85+
@Override
86+
public void remove(Component... components) {
87+
for (Component c : components) {
88+
if (c instanceof MenuItem) {
89+
pm.removeAll();
90+
} else {
91+
HasComponents.super.remove(components);
92+
}
93+
}
94+
}
95+
96+
@Override
97+
public void removeAll() {
98+
HasComponents.super.removeAll();
99+
this.add(header);
100+
this.add(pm);
101+
}
102+
78103
public void setMenuItems(List<MenuItem> menuItems) {
79104
pm.removeAll();
80-
menuItems.stream().map(this::createComponent).forEach(pm::add);
105+
menuItems.stream().forEach(pm::add);
81106
}
82107

83-
private MenuItemComponent createComponent(MenuItem menuItem) {
84-
MenuItemComponent mi = new MenuItemComponent(menuItem);
85-
menuItem.getSubMenuItems().stream().map(this::createComponent).forEach(mi::add);
86-
87-
if (menuItem.getSubMenuItems().isEmpty()) {
88-
mi.addMouseClickEvent(ev->{
89-
switch (ev.getButton()) {
90-
case LEFT:
91-
Optional.ofNullable(menuItem.getCommand()).ifPresent(Command::execute);
92-
close();
93-
break;
94-
case MIDDLE:
95-
Optional.ofNullable(menuItem.getMiddleButtonCommand()).ifPresent(Command::execute);
96-
close();
97-
break;
98-
case RIGHT:
99-
Optional.ofNullable(menuItem.getRightButtonCommand()).ifPresent(Command::execute);
100-
break;
101-
}
102-
});
103-
}
104-
105-
menuItem.setRefreshCallback(()->mi.configure(menuItem));
106-
return mi;
107-
}
108-
109108
/**Close the app-drawer.*/
110109
public void close() {
111110
getUI().ifPresent(ui->ui.getPage().executeJavaScript("$0.close()", this));

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222

2323

24-
import com.flowingcode.addons.applayout.menu.MenuItem;
2524
import com.vaadin.flow.component.Component;
2625
import com.vaadin.flow.component.HasComponents;
2726
import com.vaadin.flow.component.Tag;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.ArrayList;
2424
import java.util.Arrays;
2525

26-
import com.flowingcode.addons.applayout.menu.MenuItem;
2726
import com.vaadin.flow.component.Component;
2827
import com.vaadin.flow.component.dependency.HtmlImport;
2928
import com.vaadin.flow.component.html.Div;

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.ArrayList;
2525
import java.util.List;
2626

27-
import com.flowingcode.addons.applayout.menu.MenuItem;
2827
import com.vaadin.flow.component.Component;
2928
import com.vaadin.flow.component.HasComponents;
3029
import com.vaadin.flow.component.Tag;
@@ -69,25 +68,25 @@ public void setTitle(String title) {
6968
divTitle.setText(title);
7069
}
7170

72-
public void setToolbarIconButtons(MenuItem[] menuItems) {
73-
List<PaperIconButton> toolbarIconButtons = createToolbarIconButtons(menuItems);
71+
public void setToolbarIconButtons(MenuItem[] menuItems) {
7472
this.removeAll();
7573
this.add(menu);
7674
if (ctitle!=null) this.add(ctitle);
7775
if (divTitle!=null) this.add(divTitle);
78-
toolbarIconButtons.forEach(this::add);
76+
this.add(menuItems);
7977
}
80-
78+
79+
/*
8180
private static List<PaperIconButton> createToolbarIconButtons(MenuItem[] menuItems) {
81+
8282
List<PaperIconButton> result = new ArrayList<>();
8383
for (MenuItem menuItem : menuItems) {
8484
PaperIconButton paperIconButton = new PaperIconButton(menuItem.getIcon(), menuItem.getCommand());
8585
paperIconButton.setEnabled(menuItem.isEnabled());
86-
menuItem.setRefreshCallback(() -> paperIconButton.setIcon(menuItem.getIcon()));
8786
result.add(paperIconButton);
8887
}
8988
return result;
90-
}
89+
}*/
9190

9291
public void setMenuIconVisible(boolean visible) {
9392
menu.setVisible(visible);
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*-
2+
* #%L
3+
* App Layout Addon
4+
* %%
5+
* Copyright (C) 2018 - 2019 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.flowingcode.addons.applayout;
21+
22+
23+
24+
import java.util.EnumMap;
25+
import java.util.Optional;
26+
27+
import org.apache.commons.lang3.tuple.Pair;
28+
29+
import com.flowingcode.addons.applayout.MouseClickEvent.MouseButton;
30+
import com.vaadin.flow.component.Component;
31+
import com.vaadin.flow.component.HasEnabled;
32+
import com.vaadin.flow.component.HasOrderedComponents;
33+
import com.vaadin.flow.component.Tag;
34+
import com.vaadin.flow.component.dependency.HtmlImport;
35+
import com.vaadin.flow.component.icon.VaadinIcon;
36+
import com.vaadin.flow.server.Command;
37+
import com.vaadin.flow.shared.Registration;
38+
39+
/**
40+
* Menu item component.
41+
*
42+
* @author mlopez
43+
*
44+
*/
45+
@Tag("fc-menuitem")
46+
@HtmlImport("bower_components/fc-applayout/fc-menuitem.html")
47+
@SuppressWarnings("serial")
48+
public class MenuItem extends Component implements HasEnabled, HasOrderedComponents<MenuItem> {
49+
50+
private EnumMap<MouseButton, Pair<Command, Registration>> commands;
51+
52+
/** No argument constructor */
53+
public MenuItem() {}
54+
55+
/** Create a new instance of {@code MenuItem} with a labelicon. */
56+
public MenuItem(String label) {
57+
this.setLabel(label);
58+
}
59+
60+
/** Create a new instance of {@code MenuItem} with a label and left-button command. */
61+
public MenuItem(String label, Command command) {
62+
this.setLabel(label);
63+
this.setCommand(command);
64+
}
65+
66+
/** Create a new instance of {@code MenuItem} with a label, a {@code VaadinIcon}, and left-button command. */
67+
public MenuItem(String label, VaadinIcon icon, Command command) {
68+
this(label, command);
69+
setIcon(icon.create().getElement().getAttribute("icon"));
70+
}
71+
72+
/** Create a new instance of {@code MenuItem} with a label, an icon, and left-button command. */
73+
public MenuItem(String label, String icon, Command command) {
74+
this(label, command);
75+
setIcon(icon);
76+
}
77+
78+
@Override
79+
public void add(Component... components) {
80+
HasOrderedComponents.super.add(components);
81+
}
82+
83+
/**Adds the given menu items as children of this component.*/
84+
public final MenuItem add(MenuItem... items) {
85+
this.add((Component[])items);
86+
return this;
87+
}
88+
89+
public String getLabel() {
90+
return getElement().getAttribute("label");
91+
}
92+
93+
public MenuItem setLabel(String label) {
94+
getElement().setAttribute("label", label);
95+
return this;
96+
}
97+
98+
public String getIcon() {
99+
return getElement().getAttribute("icon");
100+
}
101+
102+
public MenuItem setIcon(String icon) {
103+
if (icon!=null) {
104+
getElement().setAttribute("icon", icon);
105+
getElement().removeAttribute("image");
106+
} else {
107+
getElement().removeAttribute("icon");
108+
}
109+
return this;
110+
}
111+
112+
public String getImage() {
113+
return getElement().getAttribute("image");
114+
}
115+
116+
public MenuItem setImage(String imageUrl) {
117+
if (imageUrl!=null) {
118+
getElement().setAttribute("image", imageUrl);
119+
getElement().removeAttribute("icon");
120+
} else {
121+
getElement().removeAttribute("image");
122+
}
123+
return this;
124+
}
125+
126+
public MenuItem setCommand(Command command) {
127+
setCommand(MouseButton.LEFT, command);
128+
return this;
129+
}
130+
131+
public MenuItem setCommand(MouseButton button, Command command) {
132+
if (command != null || commands != null && commands.containsKey(button)) {
133+
Optional.ofNullable(commands).orElseGet(()->commands=new EnumMap<>(MouseButton.class)).compute(button, (_button, commandAndRegistration)->{
134+
Optional.ofNullable(commandAndRegistration).map(Pair::getRight).ifPresent(Registration::remove);
135+
return command!=null ? Pair.of(command, addMouseClickEvent(ev->{if (ev.getButton()==button) command.execute();})) : null;
136+
});
137+
}
138+
return this;
139+
}
140+
141+
private Registration addMouseClickEvent(MouseClickListener<MenuItem> listener) {
142+
return getElement().addEventListener("item-click", ev->{
143+
int btnIndex = (int) ev.getEventData().getNumber("event.detail");
144+
MouseButton button = MouseButton.values()[btnIndex];
145+
listener.onComponentEvent(new MouseClickEvent<MenuItem>(this, button, true));
146+
}).addEventData("event.detail");
147+
}
148+
149+
public Command getCommand() {
150+
return getCommand(MouseButton.LEFT);
151+
}
152+
153+
public Command getCommand(MouseButton button) {
154+
return Optional.ofNullable(commands).map(m->m.get(button)).map(Pair::getLeft).orElse(null);
155+
}
156+
157+
}
158+

0 commit comments

Comments
 (0)