ItEr29S07CUConsultaModelosDePlanificacionItEr28S08: [FixBug] When an event onChange is triggered in an Autocomplete component, searches for text selected into list of items, and select object if found, raises an Exception otherwise
This commit is contained in:
parent
eb1478b100
commit
1aa1ca5dc4
3 changed files with 59 additions and 2 deletions
|
|
@ -20,6 +20,10 @@
|
|||
|
||||
package org.navalplanner.web.common.components;
|
||||
|
||||
import static org.navalplanner.web.I18nHelper._;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
|
|
@ -28,7 +32,11 @@ import org.navalplanner.web.common.components.finders.IFinder;
|
|||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.support.WebApplicationContextUtils;
|
||||
import org.zkoss.zk.ui.Executions;
|
||||
import org.zkoss.zk.ui.WrongValueException;
|
||||
import org.zkoss.zk.ui.event.Event;
|
||||
import org.zkoss.zk.ui.event.EventListener;
|
||||
import org.zkoss.zul.Combobox;
|
||||
import org.zkoss.zul.Comboitem;
|
||||
|
||||
/**
|
||||
* Autocomplete component
|
||||
|
|
@ -56,6 +64,53 @@ public class Autocomplete extends Combobox {
|
|||
finder = (IFinder) getBean(StringUtils.uncapitalize(classname));
|
||||
setModel(finder.getModel());
|
||||
setItemRenderer(finder.getItemRenderer());
|
||||
bindOnChangeAutocomplete(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* When there's only one possible item, autocomplete option automatically
|
||||
* fills text with that option, but it doesn't set the compenent with the
|
||||
* option selected
|
||||
*
|
||||
* To solve this problem, what I did was, when an onChange happens, search
|
||||
* among all options the text filled by in the textbox. If there's one that
|
||||
* matches, select that option. Otherwise, raise a WrongValueException
|
||||
* prompting user to select a valid option
|
||||
*
|
||||
* @param autocomplete
|
||||
*/
|
||||
private void bindOnChangeAutocomplete(final Autocomplete autocomplete) {
|
||||
autocomplete.addEventListener("onChange", new EventListener() {
|
||||
|
||||
@Override
|
||||
public void onEvent(Event event) throws Exception {
|
||||
String text = autocomplete.getValue();
|
||||
Object object = getItemByText(text);
|
||||
if (object == null) {
|
||||
throw new WrongValueException(autocomplete,
|
||||
_("Please, select an item"));
|
||||
}
|
||||
autocomplete.setSelectedItem(object);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches for text among list of items, and returns item.value that
|
||||
* matches
|
||||
*
|
||||
* @param text
|
||||
* @return
|
||||
*/
|
||||
public Object getItemByText(String text) {
|
||||
final List<Comboitem> items = this.getItems();
|
||||
for (Comboitem item: items) {
|
||||
final String itemtext = finder._toString(item.getValue());
|
||||
if (itemtext.toLowerCase().equals(text.toLowerCase())) {
|
||||
return item.getValue();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setSelectedItem(Object object) {
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ public class WorkerFinder extends Finder implements IFinder {
|
|||
@Override
|
||||
public String _toString(Object value) {
|
||||
final Worker worker = (Worker) value;
|
||||
return worker.getName() + " - " + worker.getNif();
|
||||
return (worker != null) ? worker.getName() + " - " + worker.getNif() : "";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -459,6 +459,7 @@ public class WorkReportCRUDController extends GenericForwardComposer implements
|
|||
*/
|
||||
private void appendAutocompleteResource(final Listitem listItem) {
|
||||
final Autocomplete autocomplete = new Autocomplete();
|
||||
autocomplete.setAutodrop(true);
|
||||
autocomplete.applyProperties();
|
||||
autocomplete.setFinder("WorkerFinder");
|
||||
|
||||
|
|
@ -474,7 +475,8 @@ public class WorkReportCRUDController extends GenericForwardComposer implements
|
|||
public void onEvent(Event event) throws Exception {
|
||||
final Comboitem comboitem = autocomplete.getSelectedItem();
|
||||
if (comboitem == null) {
|
||||
throw new WrongValueException(_("Please, select a worker"));
|
||||
throw new WrongValueException(autocomplete,
|
||||
_("Please, select an item"));
|
||||
}
|
||||
// Update worker
|
||||
WorkReportLine workReportLine = (WorkReportLine) listItem
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue