1 13 package com.tonbeller.wcf.toolbar; 14 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.w3c.dom.Element ; 19 20 import com.tonbeller.wcf.controller.RequestContext; 21 import com.tonbeller.wcf.controller.RequestListener; 22 import com.tonbeller.wcf.utils.DomUtils; 23 24 27 public class ToolButton extends ToolBarComponentSupport implements RequestListener { 28 private String tooltip; 29 String upImg; 30 String downImg; 31 boolean pressed; 32 String radioGroup; 33 34 ToolBar toolBar; 35 ToolButtonModel model; 36 37 String requestId; 38 39 public ToolButton(ToolButtonModel model, String upImg, String downImg) { 40 this.model = model; 41 this.upImg = upImg; 42 this.downImg = downImg; 43 } 44 45 49 public boolean isPressed() { 50 return pressed; 51 } 52 53 57 public String getRadioGroup() { 58 return radioGroup; 59 } 60 61 65 public void setRadioGroup(String radioGroup) { 66 this.radioGroup = radioGroup; 67 } 68 69 73 public String getDownImg() { 74 return downImg; 75 } 76 77 81 public String getUpImg() { 82 return upImg; 83 } 84 85 89 public void setDownImg(String downImg) { 90 this.downImg = downImg; 91 } 92 93 97 public void setUpImg(String upImg) { 98 this.upImg = upImg; 99 } 100 101 public void initialize(RequestContext context, ToolBar owner) { 102 this.toolBar = (ToolBar) owner; 103 if (toolBar.isGlobalButtonIds()) 106 this.requestId = getId(); 107 else 108 this.requestId = toolBar.getId() + "." + getId(); 109 owner.getDispatcher().addRequestListener(requestId, null, this); 110 } 111 112 public void render(RequestContext context, Element parent) throws Exception { 113 pressed = model.isPressed(context); 115 116 Element elem = DomUtils.appendElement(parent, "tool-button"); 117 elem.setAttribute("id", requestId); 118 if (pressed) 119 elem.setAttribute("img", downImg); 120 else 121 elem.setAttribute("img", upImg); 122 elem.setAttribute("title", toolBar.getTooltip(tooltip)); 123 } 124 125 public void request(RequestContext context) throws Exception { 126 List list = toolBar.getRadioGroup(this); 127 if (list != null) { 128 for (Iterator it = list.iterator(); it.hasNext();) { 129 ToolButton btn = (ToolButton) it.next(); 130 if (btn != this) 131 btn.model.setPressed(context, false); 132 } 133 } 134 135 pressed = model.isPressed(context); 137 pressed = !pressed; 138 model.setPressed(context, pressed); 139 pressed = model.isPressed(context); 141 } 142 143 146 public void setTooltip(String tooltip) { 147 this.tooltip = tooltip; 148 } 149 150 public boolean isSeparator() { 151 return false; 152 } 153 154 public void setImg(String img) { 155 setUpImg(img + "-up"); 156 setDownImg(img + "-down"); 157 } 158 159 } | Popular Tags |