Skip to content

Commit 53fc021

Browse files
committed
Deprecate methods for removal in future releases
1 parent de764e6 commit 53fc021

File tree

5 files changed

+86
-27
lines changed

5 files changed

+86
-27
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,20 @@ public void setMenuItems(List<MenuItem> menuItems) {
8181
private Component[] createComponents(List<MenuItem> menuItems) {
8282
List<Component> components = new ArrayList<>();
8383
for (MenuItem menuItem : menuItems) {
84-
if (menuItem.isSubMenuFolder()) {
84+
if (!menuItem.getSubMenuItems().isEmpty()) {
8585
components.add(collectMenus(menuItem));
8686
} else {
87-
if (menuItem.getIcon()==null && menuItem.getImageURL()==null) {
87+
if (menuItem.getIcon()==null && menuItem.getImage()==null) {
8888
PaperItem pi = new PaperItem(menuItem.getLabel(),menuItem.getCommand(), this);
8989
pi.setEnabled(menuItem.isEnabled());
9090
components.add(pi);
9191
menuItem.setRefreshCallback(()->pi.setText(menuItem.getLabel()));
9292
} else {
9393
PaperIconItem pi;
94-
if (menuItem.getImageURL()!=null) {
95-
pi = new PaperIconItem(menuItem.getLabel(), menuItem.getImageURL() ,menuItem.getCommand(), this);
94+
if (menuItem.getImage()!=null) {
95+
pi = new PaperIconItem(menuItem.getLabel(), menuItem.getImage(), menuItem.getCommand(), this);
9696
} else {
97-
pi = new PaperIconItem(menuItem.getLabel(), menuItem.getIcon() ,menuItem.getCommand(), this);
97+
pi = new PaperIconItem(menuItem.getLabel(), menuItem.getIcon(), menuItem.getCommand(), this);
9898
}
9999

100100
components.add(pi);
@@ -112,7 +112,7 @@ private Component[] createComponents(List<MenuItem> menuItems) {
112112
private CollapseButton collectMenus(MenuItem topMenuItem) {
113113
List<MenuItem> menuItems = topMenuItem.getSubMenuItems();
114114
Component[] components = createComponents(menuItems);
115-
CollapseButton collapseButton = new CollapseButton(topMenuItem.getLabel(), topMenuItem.getIcon(), topMenuItem.getImageURL(), components);
115+
CollapseButton collapseButton = new CollapseButton(topMenuItem.getLabel(), topMenuItem.getIcon(), topMenuItem.getImage(), components);
116116
collapseButton.setEnabled(topMenuItem.isEnabled());
117117
return collapseButton;
118118
}

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

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

2323

2424
import java.net.URL;
25+
import java.util.Optional;
2526

2627
import com.vaadin.flow.component.Component;
2728
import com.vaadin.flow.component.HasComponents;
@@ -43,15 +44,19 @@ public CollapseButton(String label, PaperItem[] items) {
4344
this(label, null, null, items);
4445
}
4546

47+
/** @deprecated (for removal) */
48+
@Deprecated
4649
public CollapseButton(String label, URL image, Component...items) {
47-
this(label, null, image, items);
50+
this(label, null, Optional.ofNullable(image).map(URL::toExternalForm).orElse(null), items);
4851
}
4952

53+
/** @deprecated (for removal) */
54+
@Deprecated
5055
public CollapseButton(String label, String icon, Component...items) {
5156
this(label,icon,null,items);
5257
}
5358

54-
public CollapseButton(String label, String icon, URL image, Component...items) {
59+
public CollapseButton(String label, String icon, String image, Component...items) {
5560
if (icon==null && image==null) {
5661
PaperItem pi = new PaperItem(label);
5762
configureAndAddItem(pi);
@@ -74,5 +79,5 @@ private <T extends Component & HasSize> void configureAndAddItem(T pi) {
7479
pi.setWidth("100%");
7580
this.add(pi);
7681
}
77-
82+
7883
}

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

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

2626
import com.vaadin.flow.component.Component;
27-
import com.vaadin.flow.component.HasComponents;
27+
import com.vaadin.flow.component.HasOrderedComponents;
2828
import com.vaadin.flow.component.Tag;
2929
import com.vaadin.flow.component.dependency.HtmlImport;
3030

@@ -37,7 +37,7 @@
3737
@SuppressWarnings("serial")
3838
@HtmlImport("bower_components/paper-listbox/paper-listbox.html")
3939
@Tag("paper-listbox")
40-
public class PaperListbox extends Component implements HasComponents {
40+
public class PaperListbox extends Component implements HasOrderedComponents<PaperListbox> {
4141

4242
public PaperListbox(List<Component> components) {
4343
components.stream().forEach(this::add);

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

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121

2222

2323

24+
import java.net.MalformedURLException;
2425
import java.net.URL;
2526
import java.util.ArrayList;
2627
import java.util.Arrays;
2728
import java.util.List;
29+
import java.util.Optional;
30+
import java.util.stream.Stream;
2831

2932
import com.vaadin.flow.component.icon.VaadinIcon;
3033
import com.vaadin.flow.server.Command;
@@ -39,38 +42,55 @@ public class MenuItem {
3942

4043
private String label;
4144
private String icon;
42-
private URL imageURL;
45+
private String image;
4346
private Command command;
4447
private List<MenuItem> subMenuItems = new ArrayList<>();
4548
private Runnable refreshCallback;
4649
private boolean enabled = true;
50+
51+
52+
/** No argument constructor */
53+
public MenuItem() {}
54+
55+
/** Create a new instance of {@code MenuItem} with a label. */
56+
public MenuItem(String label) {
57+
this.setLabel(label);
58+
}
4759

60+
/** @deprecated (for removal) Use fluent API. */
61+
@Deprecated
4862
public MenuItem(String label, MenuItem... subMenuItems) {
4963
this.label = label;
5064
this.subMenuItems = Arrays.asList(subMenuItems);
5165
}
52-
66+
67+
/** @deprecated (for removal) Use fluent API. */
68+
@Deprecated
5369
public MenuItem(String label, String icon, MenuItem... subMenuItems) {
5470
this.label = label;
5571
this.icon = icon;
5672
this.subMenuItems = Arrays.asList(subMenuItems);
5773
}
5874

75+
/** @deprecated (for removal) Use fluent API */
76+
@Deprecated
5977
public MenuItem(String label, URL image, MenuItem... subMenuItems) {
6078
this.label = label;
6179
this.setImageURL(image);
6280
this.subMenuItems = Arrays.asList(subMenuItems);
6381
}
6482

83+
/** Create a new instance of {@code MenuItem} with a label and left-button command. */
6584
public MenuItem(String label, Command command) {
66-
this.label = label;
67-
this.command = command;
85+
this.setLabel(label);
86+
this.setCommand(command);
6887
}
6988

89+
/** Create a new instance of {@code MenuItem} with a label, an icon, and left-button command. */
7090
public MenuItem(String label, String icon, Command command) {
71-
this.label = label;
72-
this.command = command;
73-
this.icon = icon;
91+
setLabel(label);
92+
setCommand(command);
93+
setIcon(icon);
7494
}
7595

7696
public MenuItem(String label, URL image, Command command) {
@@ -79,10 +99,17 @@ public MenuItem(String label, URL image, Command command) {
7999
this.setImageURL(image);
80100
}
81101

102+
/** Create a new instance of {@code MenuItem} with a label, a {@code VaadinIcon}, and left-button command. */
82103
public MenuItem(String label, VaadinIcon icon, Command command) {
83104
this(label, getIconName(icon), command);
84105
}
85106

107+
/**Adds the given menu items as children of this component.*/
108+
public MenuItem add(MenuItem... items) {
109+
Stream.of(items).forEach(Optional.ofNullable(subMenuItems).orElseGet(()->subMenuItems = new ArrayList<>())::add);
110+
return this;
111+
}
112+
86113
private static String getIconName(VaadinIcon icon) {
87114
return icon.create().getElement().getAttribute("icon");
88115
}
@@ -91,30 +118,33 @@ public String getLabel() {
91118
return label;
92119
}
93120

94-
public void setLabel(String label) {
121+
public MenuItem setLabel(String label) {
95122
this.label = label;
96123
if (refreshCallback!=null) {
97124
this.refreshCallback.run();
98-
}
125+
}
126+
return this;
99127
}
100128

101129
public Command getCommand() {
102130
return command;
103131
}
104132

105-
public void setCommand(Command command) {
133+
public MenuItem setCommand(Command command) {
106134
this.command = command;
135+
return this;
107136
}
108137

109138
public String getIcon() {
110139
return icon;
111140
}
112141

113-
public void setIcon(String icon) {
142+
public MenuItem setIcon(String icon) {
114143
this.icon = icon;
115144
if (refreshCallback!=null) {
116145
this.refreshCallback.run();
117146
}
147+
return this;
118148
}
119149

120150
public List<MenuItem> getSubMenuItems() {
@@ -127,7 +157,9 @@ public void setSubMenuItems(List<MenuItem> subMenuItems) {
127157

128158
/**
129159
* @return true if this item has sub menu items
160+
* @deprecated (for removal)
130161
*/
162+
@Deprecated
131163
public boolean isSubMenuFolder() {
132164
return !getSubMenuItems().isEmpty();
133165
}
@@ -148,12 +180,33 @@ public void setEnabled(boolean enabled) {
148180
this.enabled = enabled;
149181
}
150182

183+
public String getImage() {
184+
return image;
185+
}
186+
187+
public MenuItem setImage(String image) {
188+
this.image = image;
189+
return this;
190+
}
191+
192+
/** @deprecated (for removal) Use {@link #getImage}*/
193+
@Deprecated
151194
public URL getImageURL() {
152-
return imageURL;
195+
if (this.image!=null) {
196+
try {
197+
return new URL(image);
198+
} catch (MalformedURLException e) {
199+
throw new IllegalStateException();
200+
}
201+
} else {
202+
return null;
203+
}
153204
}
154205

206+
/** @deprecated (for removal) Use {@link #setImage(String)}*/
207+
@Deprecated
155208
public void setImageURL(URL imageURL) {
156-
this.imageURL = imageURL;
209+
this.image = Optional.ofNullable(imageURL).map(URL::toExternalForm).orElse(null);
157210
}
158211

159212
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ private Component createAvatarComponent() {
163163

164164
private MenuItem[] createMenuItems() {
165165
MenuItem mi = new MenuItem("Say hello", "star", () -> showContent("Hello!"));
166-
MenuItem toggleSettings = new MenuItem("", "settings");
166+
MenuItem toggleSettings = new MenuItem().setIcon("settings");
167167
toggleSettings.setCommand(() -> {
168168
settings.setEnabled(!settings.isEnabled());
169169
miSettings.setEnabled(settings.isEnabled());
@@ -177,6 +177,7 @@ private MenuItem[] createMenuItems() {
177177
toggleSettings.getCommand().execute();
178178

179179
return new MenuItem[] {
180+
//icon as VaadinIcon enum
180181
new MenuItem("Content", VaadinIcon.BOOK, () -> showHamletContent()),
181182
toggleSettings,
182183
mi,
@@ -191,10 +192,10 @@ private MenuItem[] createMenuItems() {
191192
mi.setLabel("Say hello");
192193
}
193194
}),
194-
new MenuItem("SubMenu", "build",
195+
new MenuItem("SubMenu").setIcon("build").add(
195196
new MenuItem("Hello Again", "inbox",()->showContent("Hello Again!")),
196197
new MenuItem("And Again",()->showContent("And Again!")),
197-
new MenuItem("SubMenu",
198+
new MenuItem("SubMenu").add(
198199
new MenuItem("Hello Again",()->showContent("Hello Again!")),
199200
new MenuItem("And Again",()->showContent("And Again!")))
200201
),

0 commit comments

Comments
 (0)