Custom Zk Combobox - java

as seen in:
http://www.zkoss.org/zkdemo/combobox/simple_combobox
When the item is selected only text appears.
How can I make the image + name appear when the item is selected?
which method from Combobox do i need to override?
ty

There is no build-in way for you to do things like that. Because a Combobox is just a textbox with a customizable drop-down list.
But this can be done pretty easily. You can just add an Image at left of the combobox, and listen combobox Events.ON_SELECT event. Then update the Image according to selected item.
Here is an example code (assuming use SelectorComposer)
#Listen("onSelect = combobox#mycmb")
public void onComboboxSelected(SelectEvent event) {
Set<MyObject> selectedObjects = event.getSelectedObjects();
MyObject obj = selectedObjects.get(0);
image.setSrc(getImagePath(obj)); // image are Image component you wired.
}
Note : I didn't test the code, but you should get the idea

Related

vaadin CheckBoxGroup does not render

My program can add or update profiles. In the update profile dialog, after selecting which profile u want to update. Fields should fill with selected profile values. The problem starts with filling the CheckboxGroup field. (My CheckboxGroup field name is listBox so after that I'm gonna call it that way). when I check my listBox selected items I can see the listBox selected values are true but in the frontend, I can't see any selection. So basically I want to reselect lists in addValueChangeListener but CheckboxGroup not rerendering.
listBox = new CheckboxGroup<>();
listBox.setItemLabelGenerator(Lists::getListName);
listBox.setItems();
listBox.setItems(listsList);
listBox.addThemeVariants(CheckboxGroupVariant.LUMO_VERTICAL);
listBox.setHeight("200px");
listBox.getStyle().set("overflow","AUTO");
listBox.setHelperText("Seçilen Listeler");
listBox.addThemeVariants(CheckboxGroupVariant.LUMO_HELPER_ABOVE_FIELD);
updateProfile.addValueChangeListener(e -> {
setUpdateProfileValues();
});
}
private void setUpdateProfileValues() {
if (updateProfile.getValue() == null)
return;
updateProfileName.setValue(updateProfile.getValue().getProfileName());
simAlgorithm.setValue(updateProfile.getValue().getSimAlgorithm());
searchType.setValue(updateProfile.getValue().getSearchType());
operationType.setValue(updateProfile.getValue().getOperatorType());
simPercentage.setValue(updateProfile.getValue().getSimPercentage());
numberOfResult.setValue(updateProfile.getValue().getNumberOfResults());
List<Lists> selectedLists = updateProfile.getValue().getLists();
selectedLists.forEach(lists -> log.error(lists.getListName()));
//selectedLists.forEach(listBox::select);
listBox.setValue(new HashSet<>(selectedLists));
listBox.getSelectedItems().forEach(selectedList -> log.error(selectedList.getListName()));
}
Here is my related code piece for this question. BTW when I check the logs I can see listBox has true selected values. But I need to show on frontend page.
I ran a quick piece of code using your listBox initialization and I was able to set all of the Checkboxes to true on the front end. This might help you:
listBox.getChildren().filter(Checkbox.class::isInstance)
.map(Checkbox.class::cast)
.forEach(c -> c.setValue(true));
This retrieves all the Checkboxes within the CheckboxGroup, checks that they are an instance of the Vaadin Checkbox component, and then sets their T/F value as desired.

Adding/creating a click event for rows to a Vaadin Grid with SelectionMode.MULTI?

I am trying to create a small application where I have various file locations stored in a Vaadin grid, the grid currently only shows two columns - ID and File Name, and I would like to see as a notification the file location too whenever I click on a row.
Meaning, every time I click a row from the grid I would like it to show me the location, using Notification.show(), like that:
(Please ignore the <b></b>s, they are irrelevant.)
As my grids selection model is MULTI, per default the click listener cannot register which row it is clicked on and selecting the row via checkbox is not what I want to have the data displayed. Simply, I would like to get the item for every row I click and have the location displayed as a notification.
So far, I found a solution for a similar issue but it deals with columns and the rows in Vaadin grids are rather different from what I am used to.
public static <T> void addColumnClickListener(Grid<T> grid, Consumer<Column<T>> listener)
{
String expression = "function(){const col=element.getEventContext(event).column;return col ? col.id : '';}()";
grid.getElement().addEventListener("click", e ->
{
String colId = e.getEventData().getString(expression);
Optional<Column<T>> column = grid.getColumns().stream().filter(col -> colId.equals(col.getId().get())).findFirst();
column.ifPresent(listener);
}
).addEventData(expression);
}
And I am to call the function like that:
addColumnClickListener(grid, column -> Notification.show("fubar"));
This code snippet is from the Vaadin forums and I do not quite understand it. The string expression seems to be that it contains possible JavaScript code and the rest overrides the type of the column. (I think, I really do not understand this snippet fully)
Is there any way to do something similar to the snippet above but for rows?
You can do this with an ItemClickListener on the Grid:
grid.addItemClickListener(item -> {
Notification.show(String.format("File location: %s", item.getLocation()));
});

How can i disable a button when the model is not updated?

In my UI i have 2 text field and and 2 buttons .I am using jface data binding to bind the text field and i am doing the validation and depending on the validation success the model is updated other wise it will not.I want my button to respond accordingly .Like if my model is not updated than i want to disable my button.One more thing that i do anot want to do hard coding .so is there any way to that without hard coding.
In other way I want to bind a button to text field so that when the text field has some unwanted value than the button should be disabled .In the other way i am doing data binding on text field which will take care when the text field does not have proper value than it will not update its model.Then i want to disable the button when the value is not proper can i do that.Any pointer on this helps me a lot.
You can make use of below listener. Add listener to your Observable
org.eclipse.core.databinding.observable.value.IValueChangeListener
After some research, I found that I have to observe the enable/disable property of the button and bind it with the current emf databinding context. Detail of the function which I have used is given below:
void bindEnablementButtonToValidationStatus(Button button, EMFDataBindingContext ctx) {
IObservableValue buttonEnable = SWTObservables.observeEnabled(button);
IObservableValue validationStatus = new AggregateValidationStatus(
ctx.getValidationRealm(),
ctx.getBindings(),
AggregateValidationStatus.MAX_SEVERITY);
ctx.bindValue(buttonEnable, validationStatus,
new EMFUpdateValueStrategy(
UpdateValueStrategy.POLICY_NEVER),
new EMFUpdateValueStrategy().setConverter(
new Converter(IStatus.class, Boolean.TYPE) {
public Object convert(Object fromObject) {
return new Boolean(((IStatus)fromObject).isOK());
}
}));
}

Disabling Selection with CellList

I have a CellList:
friendCellList = new CellList<PlayerDataEntity>(new PlayerCell());
friendCellList.setSelectionModel(new NoSelectionModel<PlayerDataEntity>());
I am hoping that passing the NoSelectionModel will prevent the UI from reacting to the user selecting items in the cell list. However, the user is able to select elements normally. Am I not applying the selection model correctly?
From the Javadoc of NoSelectionModel:
A selection model that does not allow selection, but fires selection change
events. Use this model if you want to know when a user selects an item, but
do not want the view to update based on the selection.
That's what it does: In the Standard theme, this will result in the row not being highlighted in blue anymore ("cellListSelectedItem" style class). However, it will still be highlighted in yellow ("cellListKeyboardSelectedItem" style class). Also, the SelectionChangeEvent will still be fired.
To turn off the SelectionChangeEvent, use
cellList.setSelectionModel(new NoSelectionModel<String>(),
DefaultSelectionEventManager.<PlayerDataEntity>createWhitelistManager());
The whitelist manager without arguments means, that you can't select any column.
If you also want to turn off the "yellow" highlighting, you should instantiate CellList with a different CellList.Resources instance:
public interface MyResources extends CellList.Resources {
#Override
#Source("com/mypackage/my.css")
Style cellListStyle();
}
...
friendCellList = new CellList<PlayerDataEntity>(new PlayerCell(),
(MyResources) GWT.create(MyResources.class);
my.css:
.cellListEvenItem {}
.cellListKeyboardSelectedItem {}
.cellListOddItem {}
.cellListSelectedItem {}
.cellListWidget {}

Items decorations in a TreeViewer

I have the following problem:
I'm preparing an editor in Eclipse and one of the tab contains TreeViewer to show items in the tree. Each item has a name and a value, which is editable.
The problem I need to indicate to user that value is incorrect (e.g. exceeds a given range). My idea is to decorate incorrect cells with a warning or error icon which will be shown also after editing is complete.
Does anybody have an idea how to decorate items in the tree? I was experimenting with ControlDecoration class but without success.
Thanks in advance,
Marcin
PS. I'm limited to Eclipse 3.4
There are two ways that this can be done. If your TreeViewer displays objects that are instances of EObject (generated by EMF. If your don't understand this part, skip to the next paragraph :)), you can change these EObject's "XyzItemProvider" so that their "getImage" method return a decorated image instead of the "plain" image... and that's it for EMF objects, nothing else needs to be changed.
If you're displaying "classic" Java Objects, you'll have to change your TreeViewer's LabelProvider in order to decorate the Image. This is done through the TreeViewer#setLabelProvider() method.
What you will need then is "how to decorate an Image", which is done through code such as this :
public class MyLabelProvider extends DecoratingLabelProvider {
public Image getImage(Object element) {
Image image = super.getImage(element);
List<Object> images = new ArrayList<Object>(2);
images.add(image);
images.add(<Image of the decorator>);
labelImage = new ComposedImage(images); // This will put the second of the "images" list (the decorator) above the first (the element's image)
return decoratedImage;
}
[...]
}
You then need to give your tree viewer this label provider :
TreeViewer treeViewer = new TreeViewer(...);
treeViewer.setLabelProvider(new MyLabelProvider(new LabelProvider()); // new LabelProvider()... or your previous label provider if you have one.

Categories