PDA

View Full Version : ImageBundleButton



haplo
8 Oct 2008, 10:23 AM
Hi everyone,

i extended the GXT Button to support ImageBundels.



public class ImageBundleButton extends Button {

private AbstractImagePrototype imagePrototype;

/**
* used for image width calculation
*/
private int additionalImageWidth = 0;

public ImageBundleButton() {

}

public ImageBundleButton(AbstractImagePrototype imagePrototype) {
this.imagePrototype = imagePrototype;
}


public ImageBundleButton(String text, AbstractImagePrototype imagePrototype) {
super(text);
this.imagePrototype = imagePrototype;
}

public void setImagePrototype(AbstractImagePrototype imagePrototype) {
this.imagePrototype = imagePrototype;
}

/**
* if margins are added to the icon style, use this method to correct the autowidth calculation in ie
*/
public void setAdditionalImageWidth(int additionalImageWidth) {
this.additionalImageWidth = additionalImageWidth;
}

protected void autoWidth() {
if (rendered) {
el().setWidth("auto");
if (GXT.isIE) {
if (buttonEl != null && buttonEl.getWidth() > 20) {
buttonEl.clip();
TextMetrics.get().bind(buttonEl.dom);
int adj = getIconStyle() != null ? 8 : 0;
adj += (imagePrototype!=null) ? imagePrototype.createImage().getWidth() + additionalImageWidth + 6: 0;
int w = TextMetrics.get().getWidth(text) + buttonEl.getFrameWidth("lr") + adj;
buttonEl.setWidth(w);
}
}
if (getMinWidth() != Style.DEFAULT) {
if (el().getWidth() < getMinWidth()) {
el().setWidth(getMinWidth());
}
}
}
}

protected void setElement(Element elem, Element parent, int index) {
super.setElement(template.create((text != null && !text.equals("")) ? text : "&nbsp;", getType(), baseStyle, (imagePrototype !=null) ? imagePrototype.getHTML() : ""), parent, index);
}

protected void onRender(Element target, int index) {
if(buttonTemplate == null){
buttonTemplate = new Template(this.getTemplateString());
}
super.onRender(target, index);
}

private String getTemplateString(){
StringBuffer sb = new StringBuffer();
sb.append("<table border=0 cellpadding=0 cellspacing=0 class='{2}-wrap'><tbody><tr>");
sb.append("<td class={2}-left><i>&#160;</i></td><td class='{2}-center'><em unselectable=on><button class={2}-text type={1}><span class={2}-image>{3}</span> {0}</button></em></td><td class={2}-right><i>&#160;</i></td>");
sb.append("</tr></tbody></table>");
return sb.toString();
}
}

DavidHoffer
6 Jan 2009, 1:41 PM
How could I do something similar for SplitButton?