1 14 package org.wings.plaf.css; 15 16 17 import org.wings.*; 18 import org.wings.script.JavaScriptListener; 19 import org.wings.script.JavaScriptEvent; 20 import org.wings.io.Device; 21 22 import java.io.IOException ; 23 24 public class ButtonCG extends LabelCG implements org.wings.plaf.ButtonCG { 25 26 29 public final static String JS_FORM_SUBMIT_SCRIPT = "javascript:this.form.submit();"; 33 34 37 public final static JavaScriptListener JS_ON_CHANGE_SUBMIT = new JavaScriptListener(JavaScriptEvent.ON_CHANGE, JS_FORM_SUBMIT_SCRIPT); 38 39 private void writeDynamicIcons(final Device device, SAbstractButton abstractButton, SIcon origIcon, 40 String iconName, boolean renderNameAttribute) 41 throws IOException { 42 if (abstractButton.isEnabled() && 43 (abstractButton.getGroup() == null || !abstractButton.isSelected())) { 44 SIcon rolloverIcon = abstractButton.getRolloverIcon(); 46 SIcon pressedIcon = abstractButton.getPressedIcon(); 47 48 if (rolloverIcon != null || pressedIcon != null) { 49 if (renderNameAttribute) { 50 51 device.print(" name=\""); 52 Utils.write(device, iconName); 53 54 device.print("\""); 55 } 57 58 if (rolloverIcon != null) { 59 60 device.print(" onMouseover=\"if(document.images){this.src='"); 61 Utils.write(device, rolloverIcon.getURL()); 62 63 device.print("';}\" onmouseout=\"if(document.images){this.src='"); 64 Utils.write(device, origIcon.getURL()); 65 66 device.print("';}\""); 67 } 68 69 if (pressedIcon != null) { 70 71 device.print(" onMousedown=\"if(document.images){this.src='"); 72 Utils.write(device, pressedIcon.getURL()); 73 74 device.print("';}\" onmouseup=\"if(document.images){this.src='"); 75 Utils.write(device, rolloverIcon != null ? rolloverIcon.getURL() : origIcon.getURL()); 76 77 device.print("';}\""); 78 } 79 } 80 } 81 82 } 83 84 public void writeContent(final Device device, final SComponent component) 85 throws IOException { 86 final SAbstractButton button = (SAbstractButton) component; 87 88 if (button.getShowAsFormComponent()) { 89 writeButtonStart(device, button); 90 device.print(" type=\"submit\" name=\""); 91 Utils.write(device, Utils.event(button)); 92 device.print("\""); 93 Utils.optAttribute(device, "tabindex", button.getFocusTraversalIndex()); 94 Utils.optAttribute(device, "accesskey", button.getMnemonic()); 95 } else { 96 RequestURL addr = button.getRequestURL(); 97 addr.addParameter(button, button.getToggleSelectionParameter()); 98 writeLinkStart(device, addr); 99 100 Utils.optAttribute(device, "accesskey", button.getMnemonic()); 101 } 102 Utils.printCSSInlineFullSize(device, component.getPreferredSize()); 103 104 StringBuffer className = new StringBuffer (); 106 if (!button.isEnabled()) { 107 className.append(component.getStyle()); 108 className.append("_disabled "); 109 } 110 if (button.isSelected()) { 111 className.append(component.getStyle()); 112 className.append("_selected "); 113 } 114 if (className.length() > 0) { 115 device.print(" class=\""); 116 device.print(className.toString()); 117 device.print("\""); 118 } 119 if (component.isFocusOwner()) 120 Utils.optAttribute(device, "focus", component.getName()); 121 122 Utils.writeEvents(device, button); 123 device.print(">"); 124 125 final String text = button.getText(); 126 final SIcon icon = getIcon(button); 127 128 if (icon == null && text != null) 129 writeText(device, text); 130 else if (icon != null && text == null) 131 writeIcon(device, icon); 132 else if (icon != null && text != null) { 133 new IconTextCompound() { 134 protected void text(Device d) throws IOException { 135 writeText(d, text); 136 } 137 138 protected void icon(Device d) throws IOException { 139 writeIcon(d, icon); 140 } 141 }.writeCompound(device, component, button.getHorizontalTextPosition(), button.getVerticalTextPosition()); 142 } 143 144 if (button.getShowAsFormComponent()) 145 device.print("</button>"); 146 else 147 device.print("</a>"); 148 } 149 150 154 protected void writeButtonStart(final Device device, final SAbstractButton comp) throws IOException { 155 device.print("<button"); 156 } 157 158 165 protected void writeLinkStart(final Device device, RequestURL addr) throws IOException { 166 device.print("<a HREF=\""); 167 addr.write(device); 168 device.print("\""); 169 } 170 171 protected SIcon getIcon(SAbstractButton abstractButton) { 172 if (abstractButton.isSelected()) { 173 return abstractButton.isEnabled() 174 ? abstractButton.getSelectedIcon() 175 : abstractButton.getDisabledSelectedIcon(); 176 } else { 177 return abstractButton.isEnabled() 178 ? abstractButton.getIcon() 179 : abstractButton.getDisabledIcon(); 180 } 181 } 182 } 183 | Popular Tags |