Dynamic combo box list issue - swt

Using switch case i wanted to add different list to combobox,when i compile i dont see any error. The following is my code, can any one please suggest what could be the reason for not updating combobox list ??
public void comboboxlist(Composite parent,String fruit) {
Combo combobox = new Combo(parent,SWT.NONE | SWT.DROP_DOWN | SWT.READ_ONLY);
switch(fruit) {
case "apple":
combobox.setItems(new String[]{"Red","green"});
combobox.addModifyListener( new ModifyListener() {
public void modifyText(final ModifyEvent e) {
}
});
break;
case "mango":
combobox.setItems(new String[]{"Yellow","green"});
combobox.addModifyListener( new ModifyListener() {
public void modifyText(final ModifyEvent e) {
}
});
break;
default : break;
}
}

The most plausible explanation for me would be that your fruit is neither "apple" nor "mango". Have you checked that?

Related

Public List In MainActivity and MVVM

I need some help with MVVM architecture.
I have a RecyclerView that receives LiveData and display it perfectly, however, my recyclerView requires another source of Data to customize colors and backgrounds of TextViews. for now I'm using a public list declared in the Mainactivity, But I've read that it's not a good practice.
is it possible to perform a non-live request to database from inside RecyclerView, in order to replace the public list ? if not I would really like some suggestions.
here is my onBindViewHolder:
#Override
public void onBindViewHolder(#NonNull ResultRecyclerViewAdapter.ResultHolder holder, int position) {
Results currentResult = results.get(position);
holder.ston1.setText(currentResult.getSton1());
holder.ston2.setText(String.valueOf(currentResult.getSton2()));
holder.ston1.setBackgroundColor(0xFF12FF45);
holder.ston2.setBackgroundColor(0xFF12FF45);
holder.ston1.getBackground().setAlpha(100);
holder.ston2.getBackground().setAlpha(100);
for (Ston ston: MainActivity.Stons){
if (currentResult.getStonCode().equals(ston.getStonCode()) && currentResult.getStonType().equals(ston.getStonType())){
switch (ston.getStonSelected()) {
case "WADS":
holder.ston1.getBackground().setAlpha(255);
break;
case "WQAS":
holder.ston2.getBackground().setAlpha(255);
break;
}
break;
}
}
holder.ston1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Boolean found = false;
for (Ston ston: MainActivity.Stons){
if (currentResult.getStonCode().equals(ston.getStonCode())){
found = true;
break;
}
}
if (!found) {
holder.ston1.getBackground().setAlpha(255);
MainActivity.Stons.add(new Stons(currentResult.getStonCode(),"WADS",
currentResult.getStonType()));
}
else {
for (Ston ston : MainActivity.Stons) {
if (currentResult.getStonCode().equals(ston.getStonCode()) && ston.getStonSelected().equals("WADS") &&
ston.getStonType().equals(currentResult.getStonType())){
MainActivity.Stons.remove(ston);
holder.ston1.getBackground().setAlpha(100);
break;
}
}
}
}
});
holder.ston2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Boolean found = false;
for (Ston ston: MainActivity.Stons){
if (currentResult.getStonCode().equals(ston.getStonCode())){
found = true;
break;
}
}
if (!found) {
holder.ston2.getBackground().setAlpha(255);
MainActivity.Stons.add(new Stons(currentResult.getStonCode(),"WQAS",
currentResult.getStonType()));
}
else {
for (Ston ston : MainActivity.Stons) {
if (currentResult.getStonCode().equals(ston.getStonCode()) && ston.getStonSelected().equals("WQAS") &&
ston.getStonType().equals(currentResult.getStonType())){
MainActivity.Stons.remove(ston);
holder.ston2.getBackground().setAlpha(100);
break;
}
}
}
}
});
One option that I see is to create new type specifically for your recyclerview adapter that will hold both Results object and information that you use for background alpha. So in your activity (or fragment) when livedata observer is triggered you don't directly pass it to adapter, but first create collection of objects of your new type, and then pass it to adapter. And I strongly suggest you to use Kotlin if possible, there you can use collection mapping to map collection from the db to your new type's collection.

GWT DataGrid: use of CheckboxCell-selection and standard line mode selection at the same time

I'm using a GWT DataGrid with a MultiSelectionModel.
The selections of the items of the grid should be achieved by
a) a CheckboxColumn with a CheckboxCell
and additionally at the same time by
b) the standard line mode selection-model (by clicking on the rest of the line).
With the CheckboxColumn the user should be enabled to multi-select different entries. But when clicking somewhere else on the datagrid-lines, a single-line-selection-policy should be done, that means, if a multiple-selection was done before using the checkboxes, this selection should be resetted and only the the clicked line should be selected afterwards.
This is what I have. Does anyone know how to enable CheckBox-Mode and line-selection-mode at the same time?
public class JobDataGrid extends DataGrid<Job>
{
private MultiSelectionModel<Job> selectionModel;
private Column<Job, Boolean> checkboxColumn;
private TextColumn<Job> idColumn;
private TextColumn<Job> titleColumn;
private TextColumn<Job> timestampColumn;
private TexTColumn<Job> ...
public JobDataGrid ()
{
super ();
checkboxColumn = new Column<Job, Boolean> (new CheckboxCell (true, false)) {
#Override
public Boolean getValue (Job job)
{
// Get the value from the selection model.
return selectionModel.isSelected (job);
}
};
checkboxColumn.setFieldUpdater (new FieldUpdater<Job, Boolean> () {
public void update (int index, Job job, Boolean value)
{
// Called when the user clicks on a checkbox.
selectionModel.setSelected (job, value);
}
});
// [...]
// [...]
// [...]
selectionModel = new MultiSelectionModel<Job> ();
setSelectionModel (selectionModel);
// setKeyboardSelectionPolicy (KeyboardSelectionPolicy.DISABLED);
// [...]
// [...]
// [...]
}
}
I've tried out all 4 variants
new CheckboxCell (false, false);
new CheckboxCell (true, false);
new CheckboxCell (false, true);
new CheckboxCell (true, true);
but none of them showed up what I need. And I've also played with
setSelectionModel (selectionModel, DefaultSelectionEventManager.<Job> createCheckboxManager ());
Maybe
createCustomManager(DefaultSelectionEventManager.EventTranslator<T> translator)
would help?
Thanx
Thomas
You can create your own "checkbox manager" and do what you want there.
table.setSelectionModel(selectModel, DefaultSelectionEventManager.<DocumentListItemDTO> createCustomManager(
new DefaultSelectionEventManager.CheckboxEventTranslator<DocumentListItemDTO>() {
#Override
public SelectAction translateSelectionEvent(CellPreviewEvent<DocumentListItemDTO> event) {
SelectAction action = super.translateSelectionEvent(event);
if (action.equals(SelectAction.IGNORE)) {
if (!event.getNativeEvent().getCtrlKey() && !event.getNativeEvent().getShiftKey())
selectionModel.clear();
return SelectAction.TOGGLE;
}
return action;
}
}));
Lista's answer showed up the right direction!
In order to show the usage of DefaultSelectionEventManager.CheckboxEventTranslator for which on the web only hardly can be found examples, here is a fully functional solution as requested:
setSelectionModel (selectionModel, DefaultSelectionEventManager.<Job> createCustomManager (
new DefaultSelectionEventManager.CheckboxEventTranslator<Job> () {
#Override
public SelectAction translateSelectionEvent (CellPreviewEvent<Job> event)
{
NativeEvent nativeEvent = event.getNativeEvent ();
// Determine if we clicked on a checkbox.
Element target = nativeEvent.getEventTarget ().cast ();
if ("input".equals (target.getTagName ().toLowerCase (Locale.ROOT)))
{
final InputElement input = target.cast ();
if ("checkbox".equals (input.getType ().toLowerCase (Locale.ROOT)))
{
// Synchronize the checkbox with the current selection state.
input.setChecked (event.getDisplay ().getSelectionModel ().isSelected (
event.getValue ()));
return SelectAction.TOGGLE;
}
}
else
{
if (BrowserEvents.CLICK.equals (nativeEvent.getType ()))
{
selectionModel.clear ();
return SelectAction.SELECT;
}
}
return SelectAction.IGNORE;
}
}));

GXT 3 GridRowEditing SimpleComboBox entries not displayed

i'm currently using a GXT3 grid to display data from a custom object EntityDAO.
This class contains 3 attributes: an id and two references to complex type objects
Let's call them
Long id;
UserInfo userInfo;
OutputInfo outputInfo;
I created an interface to explicit the desired display of these info:
interface EntityDAOProperties extends PropertyAccess<EntityDAO> {
ModelKeyProvider<EntityDAO> id();
#Path("userInfo.name")
ValueProvider<EntityDAO, String> step();
#Path("outputInfo.name")
ValueProvider<EntityDAO, String> outputInfo();
}
The display is perfectly fine. The matter is that i want to be able to add/edit rows to my grid.
To do so, I have a
GridRowEditing<EntityDAO> editing = createGridEditing(grid);
comprising a
SimpleComboBox<String> comboUser = new SimpleComboBox<String>(new LabelProvider<String>() {
#Override
public String getLabel(String item) {
return item;
}
});
for(...){
comboUser.add("entry " + i); // For instance
logger.info("entry : " +i); // For instance
i++;
}
comboUser.setEditable(false);
comboUser.setTriggerAction(TriggerAction.ALL);
When i double click on my line and make the GridRowEditing appear, the combo doesn't seem to have more than 1 row and the click on the expand arrow doesn't change anything to the matter.
I think you miss the part where you set the property editor for the combobox, here is the example code:
SimpleComboBox<Light> combo = new SimpleComboBox<Light>(new StringLabelProvider<Light>());
combo.setClearValueOnParseError(false);
combo.setPropertyEditor(new PropertyEditor<Light>() {
#Override
public Light parse(CharSequence text) throws ParseException {
return Light.parseString(text.toString());
}
#Override
public String render(Light object) {
return object == null ? Light.SUNNY.toString() : object.toString();
}
});
combo.setTriggerAction(TriggerAction.ALL);
combo.add(Light.SUNNY);
combo.add(Light.MOSTLYSUNNY);
combo.add(Light.SUNORSHADE);
combo.add(Light.MOSTLYSHADY);
combo.add(Light.SHADE);
// combo.setForceSelection(true);
editing.addEditor(cc2, new Converter<String, Light>() {
#Override
public String convertFieldValue(Light object) {
return object == null ? "" : object.toString();
}
#Override
public Light convertModelValue(String object) {
try {
return Light.parseString(object);
} catch (ParseException e) {
return null;
}
}
}, combo);
Hope this could help you.

On Gwt TreeViewModel getNodeInfo() method

I can't understand that part, neither trying the showcase examples.
I'm using an extension of AsyncDataProvider to bind my tree to RPC service. Here's my method:
public <T> NodeInfo<?> getNodeInfo(T value) {
/*
if (value instanceof Categoria) {
dataProvider.setCurrentParent((Categoria)value);
}
*/
return new DefaultNodeInfo<Categoria>(dataProvider, new CategoriaCell());
}
"currentParent" is my stuff: except for (null => root) values, I set the parent to pass via RPC to my service. Actually, in my widget code:
dataProvider = new CategorieTreeDataProvider() {
#Override
protected void onRangeChanged(HasData<Categoria> display) {
updateTree(getCurrentParent());
}
};
private void updateTree(Categoria categoria) {
rpcService.getCategorie(categoria, new AsyncCallback<Categoria[]>() {
#Override
public void onSuccess(Categoria[] result) {
dataProvider.updateRowCount(result.length, true);
dataProvider.updateRowData(0, Arrays.asList(result));
}
#Override
public void onFailure(Throwable caught) {
Window.alert(caught.toString());
}
});
}
My rpc-server code, however, is working as expected:
#Override
public Categoria[] getCategorie(Categoria parent) {
List<Categoria> categoryList = categorieDao.listByProperty("parent", parent);
for (Categoria c : categoryList) {
if (categorieDao.listByProperty("parent", c).size() == 0) {
c.setLeaf(true);
}
}
return categoryList.toArray(new Categoria[0]);
}
**Then I add some data to my Categories: 'GrandFather', 'Father' and 'Son'.
Unfortunately, after loading my widget, I see:
The grandfather correctly, with his "+" how expected;
Then I click it and...
The grandfather disappear and I see 'Father' with his '+'
same for father -> son
I suspect the bug is in updateRowCount / updateRowData usage.**
Any ideas?
The getNodeInfo is called whenever you open a node so you have to create distinct DataProvider for each of the nodes's childs.
public <T> NodeInfo<?> getNodeInfo(T value) {
if (value == null) {
return new DefaultNodeInfo<Category>(dataProvider, new CategoriaCell());
}
else if (value instanceof Categoria) {
Category category = (Category)value;
return new DefaultNodeInfo<Grandfather>(new ListDataProvider<Grandfather>(category.getGrandFathers()),new GrandFatherCell());
}
else if (value instanceof Grandfather) {
Grandfather grandfather = (Grandfather)value;
return new DefaultNodeInfo<Father>(new ListDataProvider<Father>(granfather.getFathers()),new FatherCell());
}
else if (value instanceof Father) {
//same as above but with fathers.
}
}
The category.getGrandFathers() function can for example do a RPC request to the server or just return the list if you retrieve everything in one RPC request.
UPDATE based on comment:
So in case you have only one class and want to achieve a dynamic CellTree (number of levels are not pre-determined) you could take following approach.
public <T> NodeInfo<?> getNodeInfo(T value) {
if (value == null) {
return new DefaultNodeInfo<Category>(dataProvider, new CategoriaCell());
}
else {
Category category = (Category)value;
return new DefaultNodeInfo<Category>(new ListDataProvider<Category>(category.getSubCategories()),new CategoryCell());
}
}
category.getSubCategories() is either an RPC call which retrieves the subcategories for the current category or if the Category class is a linked list type datastructure it could just return the list of subcategories.
Each data provider updates a given "list" (child nodes of a given parent node), so you have to use a distinct data provider instance for each parent node, or your calls will update some random list.

GWT Tree widgets swallow arrow keyboard events which make TextBoxes contained in TreeItems not respond to arrow keys

Easily reproducible in GWT 1.6.4:
Tree tree = new Tree();
tree.addItem(new TextBox());
The problem lies with onBrowserEvent in Tree:
switch (eventType) {
case Event.ONKEYDOWN:
case Event.ONKEYUP: {
if (isArrowKey(DOM.eventGetKeyCode(event))) {
DOM.eventCancelBubble(event, true);
DOM.eventPreventDefault(event);
return;
}
}
Like a lot of GWT widgets, they don't subclass well. There has to be a simple trick I could swing for this?
Solved this with a bit of a hack.
m_tree = new Tree()
{
#Override
protected boolean isKeyboardNavigationEnabled(TreeItem inCurrentItem)
{
return false;
}
#Override
public void onBrowserEvent(Event event) {
int eventType = DOM.eventGetType(event);
switch (eventType)
{
case Event.ONKEYDOWN:
case Event.ONKEYPRESS:
case Event.ONKEYUP:
return;
default:
break;
}
super.onBrowserEvent(event);
}
};