Last active
August 29, 2015 14:16
-
-
Save Paniov/d4cbbc26e6b407f6d5a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.gwt.cell.client.AbstractCell; | |
import com.google.gwt.cell.client.ValueUpdater; | |
import com.google.gwt.core.client.GWT; | |
import com.google.gwt.dom.client.Element; | |
import com.google.gwt.dom.client.NativeEvent; | |
import com.google.gwt.resources.client.ImageResource; | |
import com.google.gwt.safehtml.client.SafeHtmlTemplates; | |
import com.google.gwt.safehtml.shared.SafeHtml; | |
import com.google.gwt.safehtml.shared.SafeHtmlBuilder; | |
import com.google.gwt.safehtml.shared.SafeUri; | |
import java.util.HashSet; | |
import java.util.Set; | |
/** | |
* This class creates a cell with a one clickable image inside. | |
* The class is abstract thus it should implement event handler for the image. | |
* Generic type supposed to be a same type of the model that is used in e.g. grid, | |
* so the value argument in render method would be the whole model of selected grid's row. | |
* Created by Paniov on 02.03.2015. | |
*/ | |
public abstract class ClickableIconCell<T> extends AbstractCell<T> { | |
private ImageResource image; | |
private Template template; | |
private T model; | |
private static final Template DEFAULT_TEMPLATE = GWT.create(Template.class); | |
interface Template extends SafeHtmlTemplates { | |
@SafeHtmlTemplates.Template("<img src='{0}' border='0' width='{1}' height='{2}' style='cursor:pointer;'>") | |
SafeHtml draw(SafeUri imageUri, int width, int height); | |
} | |
public ClickableIconCell(ImageResource imageResource, Template template) { | |
this.image = imageResource; | |
this.template = template; | |
} | |
public ClickableIconCell(ImageResource imageResource) { | |
this.image = imageResource; | |
this.template = DEFAULT_TEMPLATE; | |
} | |
@Override | |
public void render(Context context, T value, SafeHtmlBuilder sb) { | |
if (value != null) { | |
SafeHtml sh = template.draw(image.getSafeUri(), image.getWidth(), image.getHeight()); | |
sb.appendHtmlConstant(sh.asString()); | |
} | |
} | |
@Override | |
public Set<String> getConsumedEvents() { | |
Set<String> events = new HashSet<String>(); | |
events.add("click"); | |
return events; | |
} | |
@Override | |
public void onBrowserEvent(Context context, Element parent, T value, NativeEvent event, ValueUpdater<T> valueUpdater) { | |
super.onBrowserEvent(context, parent, value, event, valueUpdater); | |
this.model = value; | |
if (parent.getFirstChildElement().isOrHasChild(Element.as(event.getEventTarget()))) { | |
handleEvent(); | |
} | |
} | |
public abstract void handleEvent(); | |
public T getModel() { | |
return model; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment