JAL-1807 Bob
[jalviewjs.git] / site / j2s / swingjs / plaf / JSRadioButtonUI.java
1 package swingjs.plaf;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
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;
12
13 public class JSRadioButtonUI extends JSButtonUI {
14
15         private DOMNode label;
16         private static Map<ButtonGroup, String> groupNames;
17         
18
19         @Override
20         public DOMNode getDOMObject() {
21                 return getButtonObject("radio");
22         }
23
24         @Override
25         protected String getPropertyPrefix() {
26                 return "RadioButton.";
27         }
28
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");
36                 return d;
37         }
38
39         protected DOMNode getButtonObject(String myType) {
40                 JRadioButton b = (JRadioButton) c;
41                 boolean isNew = false;
42                 boolean doAll = false;
43                 if (domNode == null) {
44                         doAll = true;
45                         if (groupNames == null)
46                                 groupNames = new HashMap<ButtonGroup, String>();
47                         ButtonGroup bg = null;
48                         String name = id;
49                         isNew = true;
50                         if (b.getModel() instanceof DefaultButtonModel) {
51                                 bg = ((DefaultButtonModel) b.getModel()).getGroup();
52                                 name = groupNames.get(bg);
53                                 if (name == null)
54                                         groupNames.put(bg, name = id);
55                                 else
56                                         isNew = false;
57                         }
58                         domBtn = enableNode = createDOMObject("input", id, "type", myType, "name",
59                                         name);
60                         label = textNode = createDOMObject("label", id + "l", "htmlFor", id);
61                 }
62                 if (b.isSelected() || isNew)
63                         DOMNode.setAttr(domBtn, "checked", "true");
64                 setCssFont(
65                                 DOMNode.setAttr(label, "innerHTML", ((AbstractButton) c).getText()),
66                                 c.getFont());
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.
70
71                 Dimension drad = setHTMLSize1(domBtn, false, false);
72                 /*Dimension dlab = */ setHTMLSize1(label, false, false);
73                 
74                 DOMNode obj = wrap("div", "", domBtn, label);
75                 Dimension dobj = setHTMLSize1(obj, true, true);
76                 vCenter(domBtn, -75);
77                 vCenter(label, -50);
78                 DOMNode.setStyles(label, "position", "absolute", "left", drad.width + "px");
79                 DOMNode.setStyles(domBtn, "position", "absolute");
80                 if (doAll) {
81                         // now wrap these in a div
82                         obj = wrap("div", id + "_0", domBtn, label);
83                         DOMNode.setStyles(obj, "position", "absolute");
84                 } else {
85                         // must re-introduce these to the original object
86                         obj = domNode;
87                         obj.appendChild(domBtn);
88                         obj.appendChild(label);
89                 }
90                 return DOMNode.setSize(obj, dobj.width, dobj.height);
91         }
92
93         
94 }