3 import java.util.HashMap;
6 import swingjs.api.DOMNode;
7 import jsjava.awt.Dimension;
8 import jsjavax.swing.AbstractButton;
9 import jsjavax.swing.ButtonGroup;
10 import jsjavax.swing.DefaultButtonModel;
11 import jsjavax.swing.JRadioButton;
13 public class JSRadioButtonUI extends JSButtonUI {
15 private DOMNode label;
16 private static Map<ButtonGroup, String> groupNames;
20 public DOMNode getDOMObject() {
21 return getButtonObject("radio");
25 protected String getPropertyPrefix() {
26 return "RadioButton.";
29 protected Dimension setHTMLSize(DOMNode obj, boolean addCSS) {
30 // "absolute" is required for positioning of button, but must not be there for setting the size.
31 DOMNode.setStyles(domBtn, "position", null);
32 DOMNode.setStyles(label, "position", null);
33 Dimension d = setHTMLSize1(obj, addCSS, false);
34 DOMNode.setStyles(domBtn, "position", "absolute");
35 DOMNode.setStyles(label, "position", "absolute");
39 protected DOMNode getButtonObject(String myType) {
40 JRadioButton b = (JRadioButton) c;
41 boolean isNew = false;
42 boolean doAll = false;
43 if (domNode == null) {
45 if (groupNames == null)
46 groupNames = new HashMap<ButtonGroup, String>();
47 ButtonGroup bg = null;
50 if (b.getModel() instanceof DefaultButtonModel) {
51 bg = ((DefaultButtonModel) b.getModel()).getGroup();
52 name = groupNames.get(bg);
54 groupNames.put(bg, name = id);
58 domBtn = enableNode = createDOMObject("input", id, "type", myType, "name",
60 label = textNode = createDOMObject("label", id + "l", "htmlFor", id);
62 if (b.isSelected() || isNew)
63 DOMNode.setAttr(domBtn, "checked", "true");
65 DOMNode.setAttr(label, "innerHTML", ((AbstractButton) c).getText()),
67 // now wrap the two with a span and get its dimensions
68 // along with the dimensions of the radio button by itself.
69 // This is a hack, for sure.
71 Dimension drad = setHTMLSize1(domBtn, false, false);
72 /*Dimension dlab = */ setHTMLSize1(label, false, false);
74 DOMNode obj = wrap("div", "", domBtn, label);
75 Dimension dobj = setHTMLSize1(obj, true, true);
78 DOMNode.setStyles(label, "position", "absolute", "left", drad.width + "px");
79 DOMNode.setStyles(domBtn, "position", "absolute");
81 // now wrap these in a div
82 obj = wrap("div", id + "_0", domBtn, label);
83 DOMNode.setStyles(obj, "position", "absolute");
85 // must re-introduce these to the original object
87 obj.appendChild(domBtn);
88 obj.appendChild(label);
90 return DOMNode.setSize(obj, dobj.width, dobj.height);