Skip to content

Commit 78fd914

Browse files
committed
Refactoring
1 parent cbe317e commit 78fd914

File tree

21 files changed

+483
-497
lines changed

21 files changed

+483
-497
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<description>Integration of app-layout for Vaadin Flow</description>
1010

1111
<properties>
12-
<vaadin.version>14.0.11</vaadin.version>
12+
<vaadin.version>14.0.14</vaadin.version>
1313

1414
<maven.compiler.source>1.8</maven.compiler.source>
1515
<maven.compiler.target>1.8</maven.compiler.target>

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121

2222

2323

24+
import java.util.Collection;
2425
import java.util.Collections;
25-
import java.util.List;
26+
import java.util.Optional;
2627

2728
import com.vaadin.flow.component.Component;
2829
import com.vaadin.flow.component.HasComponents;
@@ -104,14 +105,24 @@ public void removeAll() {
104105
this.add(pm);
105106
}
106107

107-
public void setMenuItems(List<MenuItem> menuItems) {
108+
public void setMenuItems(Collection<MenuItem> menuItems) {
108109
pm.removeAll();
109110
menuItems.stream().forEach(pm::add);
110111
}
111112

112113
/**Close the app-drawer.*/
113114
public void close() {
114-
getUI().ifPresent(ui->ui.getPage().executeJavaScript("$0.close()", this));
115+
getUI().ifPresent(ui->ui.getPage().executeJs("$0.close()", this));
116+
}
117+
118+
static Optional<AppDrawer> findAppDrawer(Component component) {
119+
while (component!=null) {
120+
if (component instanceof AppDrawer) {
121+
return Optional.of((AppDrawer)component);
122+
} else {
123+
component = component.getParent().orElse(null);
124+
}
125+
}
126+
return Optional.empty();
115127
}
116-
117128
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public AppToolbar getAppToolbar() {
6464
return appToolbar;
6565
}
6666

67-
public void setToolbarIconButtons(MenuItem[] menuItems) {
68-
appToolbar.setToolbarIconButtons(menuItems);
67+
public void setToolbarIconButtons(Component[] components) {
68+
appToolbar.setToolbarIconButtons(components);
6969
}
7070

7171
public void setMenuIconVisible(boolean visible) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ public void clearMenuItems() {
9393
drawer.setMenuItems(new ArrayList<MenuItem>());
9494
}
9595

96-
public void setToolbarIconButtons(MenuItem... menuItems) {
97-
header.setToolbarIconButtons(menuItems);
96+
public void setToolbarIconButtons(Component... components) {
97+
header.setToolbarIconButtons(components);
9898
}
9999

100100
public void setMenuVisible(boolean visible) {

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

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -21,9 +21,6 @@
2121

2222

2323

24-
import java.util.ArrayList;
25-
import java.util.List;
26-
2724
import com.vaadin.flow.component.Component;
2825
import com.vaadin.flow.component.HasComponents;
2926
import com.vaadin.flow.component.Tag;
@@ -35,7 +32,7 @@
3532

3633
/**
3734
* Component that renders the app toolbar
38-
*
35+
*
3936
* @author mlopez
4037
*
4138
*/
@@ -48,17 +45,17 @@
4845
@JsModule("@polymer/iron-icons/iron-icons.js")
4946
@Tag("app-toolbar")
5047
public class AppToolbar extends Component implements HasComponents {
51-
52-
private PaperIconButton menu;
48+
49+
private ToolbarIconButton menu;
5350
private Component ctitle;
5451
private Div divTitle;
55-
52+
5653
public AppToolbar(String title, AppDrawer drawer) {
5754
this(null,title, drawer);
5855
}
5956

6057
public AppToolbar(Image logo, String title, AppDrawer drawer) {
61-
menu = new PaperIconButton("menu");
58+
menu = new ToolbarIconButton().setIcon("menu");
6259
drawer.getId().ifPresent(id -> menu.getElement().setAttribute("onclick", id + ".toggle()"));
6360
this.add(menu);
6461
if (logo!=null) {
@@ -75,29 +72,17 @@ public void setTitle(String title) {
7572
divTitle.setText(title);
7673
}
7774

78-
public void setToolbarIconButtons(MenuItem[] menuItems) {
75+
public void setToolbarIconButtons(Component... components) {
7976
this.removeAll();
8077
this.add(menu);
8178
if (ctitle!=null) this.add(ctitle);
8279
if (divTitle!=null) this.add(divTitle);
83-
this.add(menuItems);
80+
this.add(components);
8481
}
8582

86-
/*
87-
private static List<PaperIconButton> createToolbarIconButtons(MenuItem[] menuItems) {
88-
89-
List<PaperIconButton> result = new ArrayList<>();
90-
for (MenuItem menuItem : menuItems) {
91-
PaperIconButton paperIconButton = new PaperIconButton(menuItem.getIcon(), menuItem.getCommand());
92-
paperIconButton.setEnabled(menuItem.isEnabled());
93-
result.add(paperIconButton);
94-
}
95-
return result;
96-
}*/
97-
9883
public void setMenuIconVisible(boolean visible) {
9984
menu.setVisible(visible);
10085
}
10186

102-
87+
10388
}

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

Lines changed: 0 additions & 74 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.flowingcode.addons.applayout;
2+
3+
import java.util.EnumMap;
4+
5+
import org.apache.commons.lang3.tuple.Pair;
6+
7+
import com.flowingcode.addons.applayout.MouseClickEvent.MouseButton;
8+
import com.vaadin.flow.component.Component;
9+
import com.vaadin.flow.component.ComponentUtil;
10+
import com.vaadin.flow.component.HasElement;
11+
import com.vaadin.flow.server.Command;
12+
import com.vaadin.flow.shared.Registration;
13+
14+
interface HasMenuItemCommands<T extends Component & HasMenuItemCommands<T>> extends HasElement {
15+
16+
class Data {
17+
private final EnumMap<MouseButton, Pair<Command, Registration>> commands = new EnumMap<>(MouseButton.class);
18+
}
19+
20+
public default T setCommand(Command command) {
21+
return setCommand(MouseButton.LEFT, command);
22+
}
23+
24+
public default T setCommand(MouseButton button, Command command) {
25+
Data data = ComponentUtil.getData((Component)this, HasMenuItemCommands.Data.class);
26+
if (data==null) {
27+
data = new Data();
28+
}
29+
30+
data.commands.remove(button);
31+
if (command!=null) {
32+
Registration registration = getElement().addEventListener("item-click", ev->{
33+
command.execute();
34+
AppDrawer.findAppDrawer((Component)this).ifPresent(AppDrawer::close);
35+
}).setFilter("event.detail=="+button.ordinal());
36+
data.commands.put(button, Pair.of(command, registration));
37+
}
38+
39+
if (data.commands.isEmpty()) {
40+
data = null;
41+
}
42+
43+
ComponentUtil.setData((Component)this, HasMenuItemCommands.Data.class, data);
44+
45+
@SuppressWarnings("unchecked")
46+
T self = (T) this;
47+
return self;
48+
}
49+
50+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.flowingcode.addons.applayout;
2+
3+
import com.vaadin.flow.component.HasElement;
4+
5+
interface HasMenuItemIcon<T extends HasMenuItemIcon<T>> extends HasElement {
6+
7+
public default String getIcon() {
8+
return getElement().getAttribute("icon");
9+
}
10+
11+
public default T setIcon(String icon) {
12+
if (icon!=null) {
13+
getElement().setAttribute("icon", icon);
14+
getElement().removeAttribute("image");
15+
} else {
16+
getElement().removeAttribute("icon");
17+
}
18+
19+
@SuppressWarnings("unchecked")
20+
T self = (T) this;
21+
return self;
22+
}
23+
24+
public default String getImage() {
25+
return getElement().getAttribute("image");
26+
}
27+
28+
public default T setImage(String imageUrl) {
29+
if (imageUrl!=null) {
30+
getElement().setAttribute("image", imageUrl);
31+
getElement().removeAttribute("icon");
32+
} else {
33+
getElement().removeAttribute("image");
34+
}
35+
36+
@SuppressWarnings("unchecked")
37+
T self = (T) this;
38+
return self;
39+
}
40+
41+
}

0 commit comments

Comments
 (0)