IDEMPIERE-2352 Unaccented Menu Search

This commit is contained in:
Carlos Ruiz 2014-12-04 10:54:02 -05:00
parent 65591f0ccb
commit 000825464c
4 changed files with 33 additions and 18 deletions

View File

@ -22,12 +22,14 @@ import java.io.UnsupportedEncodingException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.text.AttributedCharacterIterator; import java.text.AttributedCharacterIterator;
import java.text.AttributedString; import java.text.AttributedString;
import java.text.Normalizer;
import java.util.Calendar; import java.util.Calendar;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.regex.Pattern;
import javax.swing.Action; import javax.swing.Action;
import javax.swing.ActionMap; import javax.swing.ActionMap;
@ -237,7 +239,19 @@ public class Util
else else
return str.length() == 0; return str.length() == 0;
} // isEmpty } // isEmpty
/**
* Remove accents from string
* @param str string
* @return Unaccented String
*/
public static String deleteAccents(String text) {
String nfdNormalizedString = Normalizer.normalize(text, Normalizer.Form.NFD);
Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
text = pattern.matcher(nfdNormalizedString).replaceAll("");
return text;
}
/************************************************************************** /**************************************************************************
* Find index of search character in str. * Find index of search character in str.
* This ignores content in () and 'texts' * This ignores content in () and 'texts'

View File

@ -84,4 +84,9 @@ public class MenuItem {
public void setType(String type) { public void setType(String type) {
this.type = type; this.type = type;
} }
@Override
public String toString() {
return label != null ? label : super.toString();
}
} }

View File

@ -313,7 +313,7 @@ public class MenuSearchController implements EventListener<Event>{
} }
public void search(String value) { public void search(String value) {
listbox.setModel((ListModel)null); listbox.setModel((ListModel<?>)null);
Events.echoEvent(ON_SEARCH_ECHO, layout, value); Events.echoEvent(ON_SEARCH_ECHO, layout, value);
} }
@ -349,20 +349,20 @@ public class MenuSearchController implements EventListener<Event>{
private String compare; private String compare;
private MenuListComparator(String compare) { private MenuListComparator(String compare) {
this.compare = compare; this.compare = Util.deleteAccents(compare.toLowerCase().trim());
} }
@Override @Override
public int compare(MenuItem o1, MenuItem o2) { public int compare(MenuItem o1, MenuItem o2) {
compare = compare.toLowerCase().trim(); String label2 = Util.deleteAccents(o2.getLabel().toLowerCase());
boolean match = false; boolean match = false;
if (compare.length() < 3) if (compare.length() < 3)
{ {
match = o2.getLabel().toLowerCase().startsWith(compare); match = label2.startsWith(compare);
} }
else else
{ {
match = o2.getLabel().toLowerCase().contains(compare); match = label2.contains(compare);
} }
return match ? 0 : -1; return match ? 0 : -1;
} }

View File

@ -16,10 +16,8 @@
*****************************************************************************/ *****************************************************************************/
package org.adempiere.webui.apps.form; package org.adempiere.webui.apps.form;
import java.text.Normalizer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.regex.Pattern;
import org.adempiere.util.Callback; import org.adempiere.util.Callback;
import org.adempiere.webui.LayoutUtils; import org.adempiere.webui.LayoutUtils;
@ -43,6 +41,7 @@ import org.compiere.model.MTreeNode;
import org.compiere.util.Env; import org.compiere.util.Env;
import org.compiere.util.KeyNamePair; import org.compiere.util.KeyNamePair;
import org.compiere.util.Msg; import org.compiere.util.Msg;
import org.compiere.util.Util;
import org.zkoss.zk.ui.event.Event; import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener; import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events; import org.zkoss.zk.ui.event.Events;
@ -262,17 +261,10 @@ public class WTreeMaintenance extends TreeMaintenance implements IFormController
private void searchElement() { private void searchElement() {
String filter = searchBox.getText() == null ? "" : searchBox.getText(); String filter = searchBox.getText() == null ? "" : searchBox.getText();
filter = deleteAccents(filter.trim().toUpperCase()); filter = Util.deleteAccents(filter.trim().toUpperCase());
action_loadTree(filter); action_loadTree(filter);
} }
private String deleteAccents(String text) {
String nfdNormalizedString = Normalizer.normalize(text, Normalizer.Form.NFD);
Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
text = pattern.matcher(nfdNormalizedString).replaceAll("");
return text;
}
private void action_loadTree() { private void action_loadTree() {
action_loadTree(null); action_loadTree(null);
} }
@ -304,9 +296,13 @@ public class WTreeMaintenance extends TreeMaintenance implements IFormController
SimpleListModel model = new SimpleListModel(); SimpleListModel model = new SimpleListModel();
ArrayList<ListItem> items = getTreeItemData(); ArrayList<ListItem> items = getTreeItemData();
for (ListItem item : items) { for (ListItem item : items) {
String valueItem = item.toString() == null ? "" : deleteAccents(item.toString().toUpperCase()); if (Util.isEmpty(filter)) {
if (filter == null || filter.length() == 0 || valueItem.contains(filter)) {
model.addElement(item); model.addElement(item);
} else {
String valueItem = item.toString() == null ? "" : Util.deleteAccents(item.toString().toUpperCase());
if (valueItem.contains(filter)) {
model.addElement(item);
}
} }
} }