1 10 11 12 package org.enhydra.jawe.xml.panels; 13 14 import org.enhydra.jawe.xml.*; 15 16 import java.util.*; 17 import javax.swing.*; 18 19 import java.awt.*; 20 23 public class XMLRadioPanel extends XMLPanel { 24 private ButtonGroup rGroup; 25 26 public XMLRadioPanel (XMLChoice myOwner) { 27 this(myOwner,""); 28 } 29 30 public XMLRadioPanel (XMLChoice myOwner,String title) { 31 this(myOwner,title,XMLPanel.BOX_LAYOUT); 32 } 33 34 public XMLRadioPanel (XMLChoice myOwner,String title, 35 int layout) { 36 this(myOwner,title,layout,true); 37 } 38 39 public XMLRadioPanel (XMLChoice myOwner,String title, 40 int layout,boolean isVertical) { 41 this(myOwner,title,layout,isVertical,true); 42 } 43 44 public XMLRadioPanel (XMLChoice myOwner,String title, 45 int layout,boolean isVertical,boolean hasBorder) { 46 47 super(myOwner,2,title,layout,false,hasBorder); 48 49 XMLPanel bgPanel=new XMLPanel(myOwner,myOwner.getChoices().length,"",layout,isVertical,false); 50 51 int noOfElements=myOwner.getChoices().length; 52 53 if (noOfElements>2) { 54 int half=(int)noOfElements/2; 55 if ((double)half!=noOfElements/2) half++; 56 if (half>1) { 57 bgPanel.setLayout(new GridLayout(half,half)); 58 } 59 else { 60 int m,n; 61 if (isVertical) { 62 m=2*half; n=1; 63 } 64 else { 65 m=1; n=2*half; 66 } 67 bgPanel.setLayout(new GridLayout(m,n)); 68 } 69 } 70 71 rGroup = new ButtonGroup(); 72 Object [] c=myOwner.getChoices(); 73 for (int i=0; i<c.length; i++) { 74 JRadioButton jr=new JRadioButton(c[i].toString()); 75 if (c[i]==myOwner.getChoosen()) { 76 jr.setSelected(true); 77 } 78 jr.setEnabled(!myOwner.isReadOnly()); 79 rGroup.add(jr); 81 bgPanel.add(jr); 82 } 83 add(Box.createHorizontalGlue()); 84 add(bgPanel); 85 } 86 87 public void setElements () { 88 getOwner().setValue(getSelectedItem()); 90 } 92 93 public Object getSelectedItem() { 94 Enumeration e=rGroup.getElements(); 95 int i=0; 96 while (e.hasMoreElements()) { 97 JRadioButton jr=(JRadioButton)e.nextElement(); 98 if (jr.isSelected()) { 99 return ((XMLChoice)getOwner()).getChoices()[i]; 100 } 101 i++; 102 } 103 return null; 104 } 105 106 public ButtonGroup getButtonGroup () { 107 return rGroup; 108 } 109 110 } 111 112 113 | Popular Tags |