Unverified Commit 7f0e2484 authored by Evan W. Patton's avatar Evan W. Patton Committed by GitHub

Perform menu item operations using identifiers as references (#2053)

Change-Id: Ib52c9c26f468c25b8c52fa3a99720898dc1b5c9a
parent 2c98e799
...@@ -16,7 +16,9 @@ import com.google.gwt.user.client.ui.MenuItem; ...@@ -16,7 +16,9 @@ import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.Image; import com.google.gwt.user.client.ui.Image;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* Class representing a drop-down button with its associated menu. Note * Class representing a drop-down button with its associated menu. Note
...@@ -26,6 +28,7 @@ import java.util.List; ...@@ -26,6 +28,7 @@ import java.util.List;
public class DropDownButton extends TextButton { public class DropDownButton extends TextButton {
private final ContextMenu menu; private final ContextMenu menu;
private final Map<String, MenuItem> itemsById = new HashMap<>();
private final List<MenuItem> items; private final List<MenuItem> items;
private final boolean rightAlign; private final boolean rightAlign;
...@@ -187,7 +190,22 @@ public class DropDownButton extends TextButton { ...@@ -187,7 +190,22 @@ public class DropDownButton extends TextButton {
if (item == null) { if (item == null) {
menu.addSeparator(); menu.addSeparator();
} else { } else {
items.add(menu.addItem(item.caption, true, item.command)); MenuItem menuItem = menu.addItem(item.caption, true, item.command);
itemsById.put(item.widgetName, menuItem);
items.add(menuItem);
}
}
/**
* Removes a menu item identified by {@code id} if it exists.
*
* @param id the identifier of the menu item
*/
public void removeItemById(String id) {
if (itemsById.containsKey(id)) {
MenuItem item = itemsById.remove(id);
items.remove(item);
menu.removeItem(item);
} }
} }
...@@ -201,6 +219,19 @@ public class DropDownButton extends TextButton { ...@@ -201,6 +219,19 @@ public class DropDownButton extends TextButton {
} }
} }
/**
* Enables or disables a menu item identified by {@code id}.
*
* @param id the identifier of the menu item
* @param enabled true if the menu item should be enabled, false for disabled
*/
public void setItemEnabledById(String id, boolean enabled) {
MenuItem item = itemsById.get(id);
if (item != null) {
item.setEnabled(enabled);
}
}
public void setItemEnabled(String itemName, boolean enabled) { public void setItemEnabled(String itemName, boolean enabled) {
for (MenuItem item : items) { for (MenuItem item : items) {
if (item.getText().equals(itemName)) { if (item.getText().equals(itemName)) {
...@@ -216,6 +247,19 @@ public class DropDownButton extends TextButton { ...@@ -216,6 +247,19 @@ public class DropDownButton extends TextButton {
items.add(menu.addItem(item.caption, true, item.command)); items.add(menu.addItem(item.caption, true, item.command));
} }
/**
* Sets the HTML content of a menu item, identified by {@code id}, to the given {@code html}.
*
* @param id the identifier of the menu item
* @param html the HTML content to use for the menu item
*/
public void setItemHtmlById(String id, String html) {
MenuItem item = itemsById.get(id);
if (item != null) {
item.setHTML(html);
}
}
public void setCaption(String caption) { public void setCaption(String caption) {
this.setText(caption + " \u25BE "); this.setText(caption + " \u25BE ");
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment