下面列出了怎么用com.vaadin.server.ResourceReference的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Initialize the ComboBoxMultiselect with default settings and register client to server RPC implementation.
*/
private void init() {
registerRpc(this.rpc);
registerRpc(new FocusAndBlurServerRpcDecorator(this, this::fireEvent));
addDataGenerator((final T data, final JsonObject jsonObject) -> {
String caption = getItemCaptionGenerator().apply(data);
if (caption == null) {
caption = "";
}
jsonObject.put(DataCommunicatorConstants.NAME, caption);
final String style = this.itemStyleGenerator.apply(data);
if (style != null) {
jsonObject.put(ComboBoxMultiselectConstants.STYLE, style);
}
final Resource icon = getItemIconGenerator().apply(data);
if (icon != null) {
final String iconUrl = ResourceReference.create(icon, ComboBoxMultiselect.this, null)
.getURL();
jsonObject.put(ComboBoxMultiselectConstants.ICON, iconUrl);
}
});
}
/**
* Initialize the ComboBoxMultiselect with default settings and register client to server RPC implementation.
*/
private void init() {
registerRpc(this.rpc);
registerRpc(new FocusAndBlurServerRpcDecorator(this, this::fireEvent));
addDataGenerator((final T data, final JsonObject jsonObject) -> {
String caption = getItemCaptionGenerator().apply(data);
if (caption == null) {
caption = "";
}
jsonObject.put(DataCommunicatorConstants.NAME, caption);
final String style = this.itemStyleGenerator.apply(data);
if (style != null) {
jsonObject.put(ComboBoxMultiselectConstants.STYLE, style);
}
final Resource icon = getItemIconGenerator().apply(data);
if (icon != null) {
final String iconUrl = ResourceReference.create(icon, ComboBoxMultiselect.this, null)
.getURL();
jsonObject.put(ComboBoxMultiselectConstants.ICON, iconUrl);
}
});
}
private List<ContextMenuItemState> convertItemsToState(List<MenuItem> items,
Map<Integer, MenuItem> itemRegistry) {
if (items == null || items.size() == 0) {
return null;
}
List<ContextMenuItemState> stateItems = new ArrayList<>(items.size());
for (MenuItem item : items) {
ContextMenuItemState menuItemState = new ContextMenuItemState();
if (!item.isVisible()) {
continue;
}
menuItemState.id = item.getId();
menuItemState.text = item.getText();
menuItemState.checkable = item.isCheckable();
menuItemState.command = item.getCommand() != null;
menuItemState.checked = item.isChecked();
menuItemState.description = item.getDescription();
menuItemState.descriptionContentMode = item
.getDescriptionContentMode();
menuItemState.enabled = item.isEnabled();
menuItemState.separator = item.isSeparator();
menuItemState.icon = ResourceReference.create(item.getIcon(), this,
"");
menuItemState.styleName = item.getStyleName();
menuItemState.childItems = convertItemsToState(item.getChildren(),
itemRegistry);
stateItems.add(menuItemState);
itemRegistry.put(item.getId(), item);
}
return stateItems;
}
@Override
public JsonValue encode(Resource value) {
ResourceReference resourceReference = ResourceReference.create(value, this, ComponentConstants.ICON_RESOURCE);
return super.encode(resourceReference, URLReference.class);
}
private List<ContextMenuItemState> convertItemsToState(List<MenuItem> items,
Map<Integer, MenuItem> itemRegistry) {
if (items == null || items.size() == 0) {
return null;
}
List<ContextMenuItemState> stateItems = new ArrayList<>(items.size());
for (MenuItem item : items) {
ContextMenuItemState menuItemState = new ContextMenuItemState();
if (!item.isVisible()) {
continue;
}
menuItemState.id = item.getId();
menuItemState.text = item.getText();
menuItemState.checkable = item.isCheckable();
menuItemState.command = item.getCommand() != null;
menuItemState.checked = item.isChecked();
menuItemState.description = item.getDescription();
menuItemState.descriptionContentMode = item
.getDescriptionContentMode();
menuItemState.enabled = item.isEnabled();
menuItemState.separator = item.isSeparator();
String key= item.getIcon()!= null ?
String.valueOf(item.getIcon().hashCode())
: "icon";
ResourceReference resourceReference = ResourceReference.create(item.getIcon(), this, key);
if (item.getIcon()!=null && (item.getIcon() instanceof ConnectorResource)) {
super.getState().resources.put(key,resourceReference);
}
menuItemState.icon = resourceReference;
menuItemState.styleName = item.getStyleName();
menuItemState.childItems = convertItemsToState(item.getChildren(),
itemRegistry);
stateItems.add(menuItemState);
itemRegistry.put(item.getId(), item);
}
return stateItems;
}