Skip to content

Commit c704aa4

Browse files
committed
Add support for using external images as icons in each MenuItem, ensuring backward compatibility (fixes #32)
1 parent 558828f commit c704aa4

File tree

5 files changed

+102
-17
lines changed

5 files changed

+102
-17
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,19 @@ private Component[] createComponents(List<MenuItem> menuItems) {
7575
if (menuItem.isSubMenuFolder()) {
7676
components.add(collectMenus(menuItem));
7777
} else {
78-
if (menuItem.getIcon()==null) {
78+
if (menuItem.getIcon()==null && menuItem.getImageURL()==null) {
7979
PaperItem pi = new PaperItem(menuItem.getLabel(),menuItem.getCommand(), this);
8080
pi.setEnabled(menuItem.isEnabled());
8181
components.add(pi);
8282
menuItem.setRefreshCallback(()->pi.setText(menuItem.getLabel()));
8383
} else {
84-
PaperIconItem pi = new PaperIconItem(menuItem.getLabel(), menuItem.getIcon(),menuItem.getCommand(), this);
84+
PaperIconItem pi;
85+
if (menuItem.getImageURL()!=null) {
86+
pi = new PaperIconItem(menuItem.getLabel(), menuItem.getImageURL() ,menuItem.getCommand(), this);
87+
} else {
88+
pi = new PaperIconItem(menuItem.getLabel(), menuItem.getIcon() ,menuItem.getCommand(), this);
89+
}
90+
8591
components.add(pi);
8692
pi.setEnabled(menuItem.isEnabled());
8793
menuItem.setRefreshCallback(()->{
@@ -97,7 +103,7 @@ private Component[] createComponents(List<MenuItem> menuItems) {
97103
private CollapseButton collectMenus(MenuItem topMenuItem) {
98104
List<MenuItem> menuItems = topMenuItem.getSubMenuItems();
99105
Component[] components = createComponents(menuItems);
100-
CollapseButton collapseButton = new CollapseButton(topMenuItem.getLabel(), topMenuItem.getIcon(), components);
106+
CollapseButton collapseButton = new CollapseButton(topMenuItem.getLabel(), topMenuItem.getIcon(), topMenuItem.getImageURL(), components);
101107
collapseButton.setEnabled(topMenuItem.isEnabled());
102108
return collapseButton;
103109
}

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

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

3+
import java.net.URL;
4+
35
/*-
46
* #%L
57
* App Layout Addon
@@ -23,6 +25,7 @@
2325

2426
import com.vaadin.flow.component.Component;
2527
import com.vaadin.flow.component.HasComponents;
28+
import com.vaadin.flow.component.HasSize;
2629
import com.vaadin.flow.component.Tag;
2730
import com.vaadin.flow.component.dependency.HtmlImport;
2831
import com.vaadin.flow.component.html.Div;
@@ -37,20 +40,27 @@
3740
public class CollapseButton extends Component implements HasComponents {
3841

3942
public CollapseButton(String label, PaperItem[] items) {
40-
this(label, null, items);
43+
this(label, null, null, items);
4144
}
4245

46+
public CollapseButton(String label, URL image, Component...items) {
47+
this(label, null, image, items);
48+
}
49+
4350
public CollapseButton(String label, String icon, Component...items) {
44-
if (icon==null) {
51+
this(label,icon,null,items);
52+
}
53+
54+
public CollapseButton(String label, String icon, URL image, Component...items) {
55+
if (icon==null && image==null) {
4556
PaperItem pi = new PaperItem(label);
46-
pi.getElement().setAttribute("slot", "collapse-trigger");
47-
pi.setWidth("100%");
48-
this.add(pi);
49-
} else {
57+
configureAndAddItem(pi);
58+
} else if(image!=null) {
59+
PaperIconItem pi = new PaperIconItem(label, image);
60+
configureAndAddItem(pi);
61+
} else if(icon!=null) {
5062
PaperIconItem pi = new PaperIconItem(label, icon);
51-
pi.getElement().setAttribute("slot", "collapse-trigger");
52-
pi.setWidth("100%");
53-
this.add(pi);
63+
configureAndAddItem(pi);
5464
}
5565
Div div = new Div();
5666
div.getElement().setAttribute("slot", "collapse-content");
@@ -59,4 +69,10 @@ public CollapseButton(String label, String icon, Component...items) {
5969
this.add(div);
6070
}
6171

72+
private <T extends Component & HasSize> void configureAndAddItem(T pi) {
73+
pi.getElement().setAttribute("slot", "collapse-trigger");
74+
pi.setWidth("100%");
75+
this.add(pi);
76+
}
77+
6278
}

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

Lines changed: 11 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.net.URL;
4+
35
/*-
46
* #%L
57
* App Layout Addon
@@ -42,6 +44,15 @@ public IronIcon(String icon) {
4244
setIcon(icon);
4345
}
4446

47+
public IronIcon(URL image) {
48+
setImage(image);
49+
}
50+
51+
public void setImage(URL image) {
52+
this.getElement().setAttribute("src", image.toString());
53+
this.getElement().setAttribute("slot", "item-icon");
54+
}
55+
4556
public void setIcon(String icon) {
4657
this.getElement().setAttribute("icon", icon);
4758
this.getElement().setAttribute("slot", "item-icon");

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

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

3+
import java.net.URL;
4+
35
/*-
46
* #%L
57
* App Layout Addon
@@ -27,8 +29,8 @@
2729
import com.vaadin.flow.component.HasText;
2830
import com.vaadin.flow.component.Tag;
2931
import com.vaadin.flow.component.Text;
30-
import com.vaadin.flow.component.UI;
3132
import com.vaadin.flow.component.dependency.HtmlImport;
33+
import com.vaadin.flow.component.html.Image;
3234
import com.vaadin.flow.server.Command;
3335

3436
/**
@@ -43,20 +45,42 @@
4345
public class PaperIconItem extends Component implements HasComponents, HasText, HasSize {
4446

4547
private IronIcon ironIcon;
48+
private Image image;
4649
private Text text;
4750

4851
public PaperIconItem(String title, String icon) {
49-
this(title, icon, null ,null);
52+
this(title, icon, null, null ,null);
5053
}
5154

5255
public PaperIconItem(String title, String icon, Command command) {
53-
this(title, icon, command, null);
56+
this(title, icon, null, command, null);
5457
}
5558

5659
public PaperIconItem(String title, String icon, Command command, AppDrawer appDrawer) {
60+
this(title, icon, null, command, appDrawer);
61+
}
62+
63+
public PaperIconItem(String title, URL image) {
64+
this(title, null, image, null ,null);
65+
}
66+
67+
public PaperIconItem(String title, URL image, Command command) {
68+
this(title, null, image, command, null);
69+
}
70+
71+
public PaperIconItem(String title, URL image, Command command, AppDrawer appDrawer) {
72+
this(title, null, image, command, appDrawer);
73+
}
74+
75+
public PaperIconItem(String title, String icon, URL image, Command command, AppDrawer appDrawer) {
5776
this.setText("");
58-
this.ironIcon = new IronIcon(icon);
59-
add(ironIcon);
77+
if (image!=null) {
78+
this.ironIcon = new IronIcon(image);
79+
add(ironIcon);
80+
} else {
81+
this.ironIcon = new IronIcon(icon);
82+
add(ironIcon);
83+
}
6084
this.text = new Text(title);
6185
add(text);
6286
if (command!=null) {
@@ -68,12 +92,17 @@ public PaperIconItem(String title, String icon, Command command, AppDrawer appDr
6892
}
6993
}
7094

95+
7196
public void setTitle(String title) {
7297
this.text.setText(title);
7398
}
7499

75100
public void setIcon(String icon) {
76101
this.ironIcon.setIcon(icon);
77102
}
103+
104+
public void setImage(Image image) {
105+
this.image = image;
106+
}
78107

79108
}

src/main/java/com/flowingcode/addons/applayout/menu/MenuItem.java

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

3+
import java.net.URL;
4+
35
/*-
46
* #%L
57
* App Layout Addon
@@ -38,6 +40,7 @@ public class MenuItem {
3840

3941
private String label;
4042
private String icon;
43+
private URL imageURL;
4144
private Command command;
4245
private List<MenuItem> subMenuItems = new ArrayList<>();
4346
private Runnable refreshCallback;
@@ -54,6 +57,12 @@ public MenuItem(String label, String icon, MenuItem... subMenuItems) {
5457
this.subMenuItems = Arrays.asList(subMenuItems);
5558
}
5659

60+
public MenuItem(String label, URL image, MenuItem... subMenuItems) {
61+
this.label = label;
62+
this.setImageURL(image);
63+
this.subMenuItems = Arrays.asList(subMenuItems);
64+
}
65+
5766
public MenuItem(String label, Command command) {
5867
this.label = label;
5968
this.command = command;
@@ -64,6 +73,12 @@ public MenuItem(String label, String icon, Command command) {
6473
this.command = command;
6574
this.icon = icon;
6675
}
76+
77+
public MenuItem(String label, URL image, Command command) {
78+
this.label = label;
79+
this.command = command;
80+
this.setImageURL(image);
81+
}
6782

6883
public MenuItem(String label, VaadinIcon icon, Command command) {
6984
this(label, getIconName(icon), command);
@@ -134,4 +149,12 @@ public void setEnabled(boolean enabled) {
134149
this.enabled = enabled;
135150
}
136151

152+
public URL getImageURL() {
153+
return imageURL;
154+
}
155+
156+
public void setImageURL(URL imageURL) {
157+
this.imageURL = imageURL;
158+
}
159+
137160
}

0 commit comments

Comments
 (0)