Added text filter(Soundex, Begins and Ends)

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/tabular-data-widgetx@113552 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Giancarlo Panichi 2015-03-10 17:37:38 +00:00 committed by Giancarlo Panichi
parent ada62e42ee
commit 714bbb6996
21 changed files with 588 additions and 2 deletions

View File

@ -3,6 +3,7 @@ package org.gcube.portlets.user.tdwx.client.filter;
import java.util.ArrayList;
import java.util.Date;
import org.gcube.portlets.user.tdwx.client.filter.text.TextFilter;
import org.gcube.portlets.user.tdwx.client.model.grid.DataRowColumnConfig;
import org.gcube.portlets.user.tdwx.shared.model.DataRow;
import org.gcube.portlets.user.tdwx.shared.model.ValueType;
@ -15,7 +16,6 @@ import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.filters.DateFilter;
import com.sencha.gxt.widget.core.client.grid.filters.Filter;
import com.sencha.gxt.widget.core.client.grid.filters.NumericFilter;
import com.sencha.gxt.widget.core.client.grid.filters.StringFilter;
/**
@ -79,7 +79,7 @@ public class FiltersGenerator {
break;
case STRING:
@SuppressWarnings("unchecked")
StringFilter<DataRow> stringFilt = new StringFilter<DataRow>(
TextFilter<DataRow> stringFilt = new TextFilter<DataRow>(
(ValueProvider<DataRow, String>) dataRowColConfig
.getValueProvider());
filters.add(stringFilt);

View File

@ -0,0 +1,251 @@
package org.gcube.portlets.user.tdwx.client.filter.text;
import java.util.ArrayList;
import java.util.List;
import org.gcube.portlets.user.tdwx.client.filter.text.TextMenu.TextItem;
import com.sencha.gxt.core.client.ValueProvider;
import com.sencha.gxt.data.shared.loader.FilterConfig;
import com.sencha.gxt.messages.client.DefaultMessages;
import com.sencha.gxt.widget.core.client.grid.filters.Filter;
/**
*
* @author giancarlo
* email: <a href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
*
* @param <M>
*/
public class TextFilter<M> extends Filter<M, String> {
/**
* The default locale-sensitive messages used by this class.
*/
public class DefaultTextFilterMessages implements TextFilterMessages {
@Override
public String emptyText() {
return DefaultMessages.getMessages().stringFilter_emptyText();
}
}
/**
* The locale-sensitive messages used by this class.
*/
public interface TextFilterMessages {
String emptyText();
}
private List<TextItem> textItems = new ArrayList<TextItem>();
private TextMenu<M> textMenu;
private TextFilterMessages messages = new DefaultTextFilterMessages();
/**
* Creates a text filter for the specified value provider. See
* {@link Filter#Filter(ValueProvider)} for more information.
*
* @param valueProvider
* the value provider
*/
public TextFilter(ValueProvider<? super M, String> valueProvider) {
super(valueProvider);
setHandler(new TextFilterHandler());
textItems.add(TextItem.CONTAINS);
textItems.add(TextItem.BEGINS);
textItems.add(TextItem.ENDS);
textItems.add(TextItem.SOUNDEX);
textMenu = new TextMenu<M>(this);
menu = textMenu;
textMenu.setTextItems(textItems);
}
/**
* Sets the contains value.
*
* @param value
* the value
*/
public void setContainsValue(String value) {
textMenu.ct.setValue(value);
}
/**
* Sets the begins value.
*
* @param value
* the value
*/
public void setBeginsValue(String value) {
textMenu.bg.setValue(value);
}
/**
* Sets the ends value.
*
* @param value
* the value
*/
public void setEndsValue(String value) {
textMenu.en.setValue(value);
}
/**
* Sets the soundex value.
*
* @param value
* the value
*/
public void setSoundexValue(String value) {
textMenu.sd.setValue(value);
}
public void setValue(List<FilterConfig> values) {
textMenu.setValue(values);
}
@SuppressWarnings("unchecked")
@Override
public List<FilterConfig> getFilterConfig() {
return (List<FilterConfig>) getValue();
}
/**
* Returns the locale-sensitive messages used by this class.
*
* @return the local-sensitive messages used by this class.
*/
public TextFilterMessages getMessages() {
return messages;
}
@Override
public Object getValue() {
return textMenu.getValue();
}
@Override
public boolean isActivatable() {
if (textMenu.ct != null
&& textMenu.ct.getCurrentValue() != null) {
return true;
}
if (textMenu.bg != null
&& textMenu.bg.getCurrentValue() != null) {
return true;
}
if (textMenu.en != null
&& textMenu.en.getCurrentValue() != null) {
return true;
}
if (textMenu.sd != null
&& textMenu.sd.getCurrentValue() != null) {
return true;
}
return false;
}
public void setMessages(TextFilterMessages messages) {
this.messages = messages;
textMenu.setEmptyText(messages.emptyText());
}
@Override
protected Class<String> getType() {
return String.class;
}
@Override
protected boolean validateModel(M model) {
boolean isValid = true;
String modelValue = getValueProvider().getValue(model);
if (textMenu.ct != null) {
String filterValue = textMenu.ct.getCurrentValue();
String v = filterValue == null ? "" : filterValue.toString();
if (v.length() == 0
&& (modelValue == null || modelValue.length() == 0)) {
isValid = true;
} else if (modelValue == null) {
isValid = false;
} else {
isValid = modelValue.toLowerCase().indexOf(v.toLowerCase()) > -1;
}
}
if (textMenu.bg != null) {
String filterValue = textMenu.bg.getCurrentValue();
String v = filterValue == null ? "" : filterValue.toString();
if (v.length() == 0
&& (modelValue == null || modelValue.length() == 0)) {
isValid = true;
} else if (modelValue == null) {
isValid = false;
} else {
isValid = modelValue.toLowerCase().indexOf(v.toLowerCase()) > -1;
}
}
if (textMenu.en != null) {
String filterValue = textMenu.en.getCurrentValue();
String v = filterValue == null ? "" : filterValue.toString();
if (v.length() == 0
&& (modelValue == null || modelValue.length() == 0)) {
isValid = true;
} else if (modelValue == null) {
isValid = false;
} else {
isValid = modelValue.toLowerCase().indexOf(v.toLowerCase()) > -1;
}
}
if (textMenu.sd != null) {
String filterValue = textMenu.sd.getCurrentValue();
String v = filterValue == null ? "" : filterValue.toString();
if (v.length() == 0
&& (modelValue == null || modelValue.length() == 0)) {
isValid = true;
} else if (modelValue == null) {
isValid = false;
} else {
isValid = modelValue.toLowerCase().indexOf(v.toLowerCase()) > -1;
}
}
return isValid;
}
@Override
public void setFilterConfig(List<FilterConfig> configs) {
boolean hasValue = false;
for (int i = 0; i < configs.size(); i++) {
FilterConfig config = configs.get(i);
if (config.getValue() != null && !"".equals(config.getValue())) {
hasValue = true;
}
}
setValue(configs);
setActive(hasValue, false);
}
@Override
protected void fireUpdate() {
super.fireUpdate();
}
}

View File

@ -0,0 +1,24 @@
package org.gcube.portlets.user.tdwx.client.filter.text;
import com.sencha.gxt.data.shared.loader.FilterHandler;
/**
*
* @author giancarlo
* email: <a href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
*
*/
public class TextFilterHandler extends FilterHandler<String> {
@Override
public String convertToObject(String value) {
return value;
}
@Override
public String convertToString(String object) {
return object;
}
}

View File

@ -0,0 +1,286 @@
package org.gcube.portlets.user.tdwx.client.filter.text;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.gcube.portlets.user.tdwx.client.resources.ResourceBundle;
import com.google.gwt.dom.client.Style.Cursor;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.Event;
import com.sencha.gxt.core.client.resources.CommonStyles;
import com.sencha.gxt.core.client.util.DelayedTask;
import com.sencha.gxt.data.shared.loader.FilterConfig;
import com.sencha.gxt.data.shared.loader.FilterConfigBean;
import com.sencha.gxt.widget.core.client.event.BeforeHideEvent;
import com.sencha.gxt.widget.core.client.event.BeforeHideEvent.BeforeHideHandler;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.sencha.gxt.widget.core.client.menu.Menu;
import com.sencha.gxt.widget.core.client.menu.MenuItem;
/**
*
* @author giancarlo email: <a
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
*
* @param <M>
*/
public class TextMenu<M> extends Menu {
/**
* A menu of string items for use with a {@link TextFilter}.
*
* @param <M>
* the model type
*/
public enum TextItem {
CONTAINS("ct"), BEGINS("bg"), ENDS("en"), SOUNDEX("sd");
private final String key;
private TextItem(String key) {
this.key = key;
}
public String getKey() {
return key;
}
}
protected TextField ct, bg, en, sd;
private TextFilter<M> filter;
private List<TextItem> textItems = new ArrayList<TextItem>();
private DelayedTask updateTask = new DelayedTask() {
@Override
public void onExecute() {
fireUpdate();
}
};
/**
* Creates text menu for use with the specified text filter.
*
* @param filter
* the filter that uses this text menu
*/
public TextMenu(TextFilter<M> filter) {
this.filter = filter;
addBeforeHideHandler(new BeforeHideHandler() {
@Override
public void onBeforeHide(BeforeHideEvent event) {
// blur the field because of empty text
if (ct != null) {
ct.getElement().selectNode("input").blur();
}
if (bg != null) {
bg.getElement().selectNode("input").blur();
}
if (en != null) {
en.getElement().selectNode("input").blur();
}
if (sd != null) {
sd.getElement().selectNode("input").blur();
}
}
});
}
/**
* Returns the menu's text items.
*
* @return the text items
*/
public List<TextItem> getTextItems() {
return Collections.unmodifiableList(textItems);
}
/**
* Returns the menu's value.
*
* @return the value
*/
public List<FilterConfig> getValue() {
List<FilterConfig> configs = new ArrayList<FilterConfig>();
if (ct != null && ct.getCurrentValue() != null && ct.isCurrentValid()) {
FilterConfig config = new FilterConfigBean();
config.setType("string");
config.setComparison("contains");
config.setValue(ct.getCurrentValue().toString());
configs.add(config);
}
if (bg != null && bg.getCurrentValue() != null && bg.isCurrentValid()) {
FilterConfig config = new FilterConfigBean();
config.setType("string");
config.setComparison("begins");
config.setValue(bg.getCurrentValue().toString());
configs.add(config);
}
if (en != null && en.getCurrentValue() != null && en.isCurrentValid()) {
FilterConfig config = new FilterConfigBean();
config.setType("string");
config.setComparison("ends");
config.setValue(en.getCurrentValue().toString());
configs.add(config);
}
if (sd != null && sd.getCurrentValue() != null && sd.isCurrentValid()) {
FilterConfig config = new FilterConfigBean();
config.setType("string");
config.setComparison("soundex");
config.setValue(sd.getCurrentValue().toString());
configs.add(config);
}
return configs;
}
/**
* Sets the text to display in the menu's text fields if they do not contain
* a value.
*
* @param emptyText
* the text to display if the fields are empty
*/
public void setEmptyText(String emptyText) {
if (ct != null) {
ct.setEmptyText(emptyText);
}
if (bg != null) {
bg.setEmptyText(emptyText);
}
if (en != null) {
en.setEmptyText(emptyText);
}
if (sd != null) {
sd.setEmptyText(emptyText);
}
}
/**
* Sets the menu's text items (defaults to CONTAINS, BEGINS, ENDS, SOUNDEX).
*
* @param textItems
* the text items
*/
public void setTextItems(List<TextItem> textItems) {
this.textItems = textItems;
clear();
ImageResource icon = null;
String toolTip = null;
for (TextItem item : textItems) {
TextField field = createTextField();
field.setEmptyText(filter.getMessages().emptyText());
switch (item) {
case CONTAINS:
icon = ResourceBundle.INSTANCE.textContains();
toolTip = new String("Text Contains");
ct = field;
break;
case BEGINS:
icon = ResourceBundle.INSTANCE.textBegins();
toolTip = new String("Text Begins");
bg = field;
break;
case ENDS:
icon = ResourceBundle.INSTANCE.textEnds();
toolTip = new String("Text Ends");
en = field;
break;
case SOUNDEX:
icon = ResourceBundle.INSTANCE.textSoundex();
toolTip = new String("Soundex Algorithm");
sd = field;
break;
}
MenuItem menuItem = new MenuItem();
menuItem.setCanActivate(false);
menuItem.setHideOnClick(false);
menuItem.setIcon(icon);
menuItem.setWidget(field);
menuItem.setToolTip(toolTip);
menuItem.getElement().removeClassName(
CommonStyles.get().unselectable());
menuItem.getElement().getStyle().setCursor(Cursor.DEFAULT);
add(menuItem);
}
}
/**
* Sets the menu's values
*
* @param values
* the values
*/
public void setValue(List<FilterConfig> values) {
for (FilterConfig config : values) {
String c = config.getComparison();
String v = config.getValue();
if (v == null) {
v = "";
}
if ("st".equals(c)) {
ct.setValue(v);
} else if ("bg".equals(c)) {
bg.setValue(v);
} else if ("en".equals(c)) {
en.setValue(v);
} else if ("sd".equals(c)) {
sd.setValue(v);
}
}
fireUpdate();
}
protected TextField createTextField() {
TextField field = new TextField() {
@Override
protected void onKeyUp(Event event) {
super.onKeyUp(event);
onFieldKeyUp(this, event);
}
};
return field;
}
@Override
protected void onAttach() {
super.onAttach();
updateTask.delay(filter.getUpdateBuffer());
}
protected void onFieldKeyUp(TextField field, Event event) {
int kc = event.getKeyCode();
if (kc == KeyCodes.KEY_ENTER && field.isCurrentValid()) {
event.preventDefault();
event.stopPropagation();
hide(true);
return;
}
updateTask.delay(filter.getUpdateBuffer());
}
private void fireUpdate() {
filter.fireUpdate();
}
}

View File

@ -2,6 +2,7 @@ package org.gcube.portlets.user.tdwx.client.resources;
import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
/**
@ -19,4 +20,28 @@ public interface ResourceBundle extends ClientBundle {
TDGridCSS tdGridCSS();
@Source("text-contains_32.png")
ImageResource textContains32();
@Source("text-contains.png")
ImageResource textContains();
@Source("text-begins_32.png")
ImageResource textBegins32();
@Source("text-begins.png")
ImageResource textBegins();
@Source("text-ends_32.png")
ImageResource textEnds32();
@Source("text-ends.png")
ImageResource textEnds();
@Source("text-soundex_32.png")
ImageResource textSoundex32();
@Source("text-soundex.png")
ImageResource textSoundex();
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 373 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 530 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 372 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 585 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 351 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 311 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 606 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 988 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 373 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 530 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 372 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 585 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 351 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 311 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 606 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 988 B