|
import java.util.List; |
|
|
|
import com.google.gwt.cell.client.AbstractSafeHtmlCell; |
|
import com.google.gwt.core.client.GWT; |
|
import com.google.gwt.dom.client.NativeEvent; |
|
import com.google.gwt.safehtml.client.SafeHtmlTemplates; |
|
import com.google.gwt.safehtml.shared.SafeHtml; |
|
import com.google.gwt.text.shared.SafeHtmlRenderer; |
|
import com.google.gwt.text.shared.SimpleSafeHtmlRenderer; |
|
|
|
import com.google.gwt.cell.client.ValueUpdater; |
|
import com.google.gwt.dom.client.Element; |
|
import com.google.gwt.dom.client.EventTarget; |
|
import com.google.gwt.safehtml.shared.SafeHtmlBuilder; |
|
|
|
public class MultipleAnchorCell extends AbstractSafeHtmlCell { |
|
|
|
interface Templates extends SafeHtmlTemplates { |
|
@Template(" {0} ") |
|
SafeHtml alink(String text); |
|
} |
|
|
|
private final List items; |
|
|
|
public void clear() { |
|
this.items.clear(); |
|
} |
|
|
|
public void addLink(String newOp) { |
|
String item = new String(newOp); |
|
items.add(item); |
|
} |
|
|
|
public MultipleAnchorCell(List items) { |
|
super(SimpleSafeHtmlRenderer.getInstance(), "click", "keydown"); |
|
this.items = items; |
|
} |
|
|
|
public MultipleAnchorCell(SafeHtmlRenderer renderer, |
|
List items) { |
|
super(renderer, "click", "keydown"); |
|
this.items = items; |
|
|
|
} |
|
|
|
private static Templates templates = GWT.create(Templates.class); |
|
|
|
@Override |
|
public void onBrowserEvent(Context context, Element parent, String value, |
|
NativeEvent event, |
|
com.google.gwt.cell.client.ValueUpdater valueUpdater) { |
|
super.onBrowserEvent(context, parent, value, event, valueUpdater); |
|
|
|
// Handle the click event. |
|
if ("click".equals(event.getType())) { |
|
EventTarget eventTarget = event.getEventTarget(); |
|
if (parent.isOrHasChild(Element.as(eventTarget))) { |
|
Element el = Element.as(eventTarget); |
|
if (el.getNodeName().equalsIgnoreCase("a")) { |
|
doAction(el.getAttribute("aid"), valueUpdater); |
|
} |
|
} |
|
} |
|
}; |
|
|
|
@Override |
|
protected void onEnterKeyDown(Context context, Element parent, |
|
String value, NativeEvent event, ValueUpdater valueUpdater) { |
|
doAction(value, valueUpdater); |
|
} |
|
|
|
private void doAction(String value, ValueUpdater valueUpdater) { |
|
if (valueUpdater != null) |
|
valueUpdater.update(value); |
|
} |
|
|
|
@Override |
|
protected void render(com.google.gwt.cell.client.Cell.Context context, |
|
SafeHtml data, SafeHtmlBuilder sb) { |
|
if (data == null) { |
|
return; |
|
} |
|
for (String item : items) { |
|
sb.append(templates.alink(item)); |
|
} |
|
} |
|
} |