001: package org.enhydra.jawe.base.panel.panels;
002:
003: import java.awt.GridLayout;
004: import java.awt.event.ActionEvent;
005: import java.awt.event.ActionListener;
006: import java.util.Enumeration;
007: import java.util.List;
008:
009: import javax.swing.Box;
010: import javax.swing.ButtonGroup;
011: import javax.swing.JRadioButton;
012:
013: import org.enhydra.jawe.base.panel.PanelContainer;
014: import org.enhydra.shark.xpdl.XMLAttribute;
015: import org.enhydra.shark.xpdl.XMLComplexChoice;
016: import org.enhydra.shark.xpdl.XMLComplexElement;
017: import org.enhydra.shark.xpdl.XMLElement;
018:
019: /**
020: * Creates a panel with radio buttons.
021: * @author Sasa Bojanic
022: * @author Zoran Milakovic
023: */
024: public class XMLRadioPanel extends XMLBasicPanel {
025: protected ButtonGroup rGroup;
026:
027: public XMLRadioPanel(PanelContainer pc, XMLElement myOwner,
028: String title, boolean isVertical, boolean hasBorder,
029: boolean hasEmptyBorder, boolean isEnabled) {
030:
031: super (pc, myOwner, title, false, hasBorder, hasEmptyBorder);
032:
033: boolean rightAllignment = false;
034: if (pc != null) {
035: rightAllignment = pc.getSettings().getSettingBoolean(
036: "XMLBasicPanel.RightAllignment");
037: }
038:
039: List choices = null;
040: Object moChoosen = null;
041: if (myOwner instanceof XMLAttribute) {
042: choices = PanelUtilities.toXMLElementViewList(pc,
043: ((XMLAttribute) myOwner).getChoices(), true);
044: moChoosen = new XMLElementView(pc, myOwner.toValue(), true);
045: } else {
046: choices = PanelUtilities.toXMLElementViewList(pc,
047: ((XMLComplexChoice) myOwner).getChoices(), true);
048: XMLElement choosen = ((XMLComplexChoice) myOwner)
049: .getChoosen();
050: if (choosen instanceof XMLComplexElement) {
051: moChoosen = new XMLElementView(pc, choosen,
052: XMLElementView.TONAME);
053: } else {
054: moChoosen = new XMLElementView(pc, choosen,
055: XMLElementView.TOVALUE);
056: }
057: }
058: XMLPanel bgPanel = new XMLBasicPanel(pc, myOwner, "",
059: isVertical, false, false);
060:
061: int noOfElements = choices.size();
062:
063: if (noOfElements > 2) {
064: int half = noOfElements / 2;
065: if (half != noOfElements / 2)
066: half++;
067: if (half > 1) {
068: bgPanel.setLayout(new GridLayout(half, half));
069: } else {
070: int m, n;
071: if (isVertical) {
072: m = 2 * half;
073: n = 1;
074: } else {
075: m = 1;
076: n = 2 * half;
077: }
078: bgPanel.setLayout(new GridLayout(m, n));
079: }
080: }
081:
082: rGroup = new ButtonGroup();
083: final XMLPanel p = this ;
084:
085: for (int i = 0; i < choices.size(); i++) {
086: JRadioButton jr = new JRadioButton(choices.get(i)
087: .toString());
088: if (choices.get(i).equals(moChoosen)) {
089: jr.setSelected(true);
090: }
091: jr.setEnabled(isEnabled);
092: jr.addActionListener(new ActionListener() {
093: public void actionPerformed(ActionEvent evt) {
094: if (getPanelContainer() == null)
095: return;
096: getPanelContainer().panelChanged(p, evt);
097: }
098: });
099: // Group the radio buttons.
100: rGroup.add(jr);
101: bgPanel.add(jr);
102: }
103:
104: if (rightAllignment) {
105: add(Box.createHorizontalGlue());
106: }
107: add(bgPanel);
108: if (!rightAllignment) {
109: add(Box.createHorizontalGlue());
110: }
111: }
112:
113: public void setElements() {
114: if (!getOwner().isReadOnly()) {
115: if (myOwner instanceof XMLAttribute) {
116: myOwner.setValue(getSelectedItem().toString());
117: } else {
118: ((XMLComplexChoice) getOwner())
119: .setChoosen((XMLElement) getSelectedItem());
120:
121: }
122: }
123: }
124:
125: public Object getSelectedItem() {
126: Enumeration e = rGroup.getElements();
127: int i = 0;
128: List choices = null;
129: if (getOwner() instanceof XMLAttribute) {
130: choices = ((XMLAttribute) getOwner()).getChoices();
131: } else {
132: choices = ((XMLComplexChoice) getOwner()).getChoices();
133: }
134: while (e.hasMoreElements()) {
135: JRadioButton jr = (JRadioButton) e.nextElement();
136: if (jr.isSelected()) {
137: return choices.get(i);
138: }
139: i++;
140: }
141: return null;
142: }
143:
144: public ButtonGroup getButtonGroup() {
145: return rGroup;
146: }
147:
148: public Object getValue() {
149: return getSelectedItem();
150: }
151:
152: }
|