1 4 package org.oddjob.designer.view; 5 6 import java.awt.Component ; 7 import java.awt.Container ; 8 import java.awt.GridBagConstraints ; 9 import java.awt.Insets ; 10 import java.awt.event.ActionEvent ; 11 import java.awt.event.ActionListener ; 12 13 import javax.swing.ButtonGroup ; 14 import javax.swing.JRadioButton ; 15 16 import org.oddjob.designer.model.DesignDefinition; 17 import org.oddjob.designer.model.FieldSelection; 18 19 22 public class FieldSelectionView implements ViewProducer { 23 24 private final FieldSelection fieldSelection; 25 26 public FieldSelectionView(FieldSelection fieldSelection) { 27 this.fieldSelection = fieldSelection; 28 } 29 30 33 public Component dialog() { 34 throw new UnsupportedOperationException ("Add the FieldSelection to a FieldGroup!"); 35 } 36 37 40 public Component group() { 41 throw new UnsupportedOperationException ("Add the FieldSelection to a FieldGroup!"); 42 } 43 44 47 public Component detailEdit() { 48 throw new UnsupportedOperationException ("Add the FieldSelection to a FieldGroup!"); 49 } 50 51 54 public Component cell() { 55 throw new UnsupportedOperationException ("Add the FieldSelection to a FieldGroup!"); 56 } 57 58 61 public int inline(Container container, int row, int column, 62 boolean selectionInGroup) { 63 ButtonGroup bg = new ButtonGroup (); 64 65 ViewProducer[] views = new ViewProducer[fieldSelection.size()]; 66 JRadioButton populated = null; 68 for (int groupNum = 0; groupNum < fieldSelection.size(); ++groupNum) { 69 70 JRadioButton button = new JRadioButton (); 71 button.addActionListener(new GroupActionListener(views, groupNum)); 72 bg.add(button); 73 74 DesignDefinition group = fieldSelection.get(groupNum); 75 76 int startRow = row; 77 78 ViewProducer view = ViewFactory.create(group); 79 views[groupNum] = view; 80 row = view.inline(container, row, column + 1, false); 81 addOptionButton(container, startRow, row - startRow, button); 82 83 if (group.isPopulated()) { 84 populated = button; 85 } 86 } 87 if (populated != null) { 88 populated.doClick(); 89 } 90 else { 91 for (int i = 0; i < views.length; ++i) { 92 views[i].setEnabled(false); 93 } 94 } 95 return row; 96 } 97 98 101 public void setEnabled(boolean enabled) { 102 throw new UnsupportedOperationException ("Not supporting nested selections yet!"); 103 } 104 105 void addOptionButton(Container container, int row, int height, Component button) { 106 GridBagConstraints c = new GridBagConstraints (); 107 c.weightx = 1.0; 108 c.weighty = 0.0; 109 110 c.gridx = 0; 111 c.gridy = row; 112 c.fill = GridBagConstraints.NONE; 113 c.anchor = GridBagConstraints.CENTER; 114 115 c.gridheight = height; 116 c.insets = new Insets (3, 1, 3, 1); 117 118 container.add(button, c); 119 120 } 121 122 static class GroupActionListener implements ActionListener { 123 final int ourGroupNum; 124 125 final ViewProducer[] components; 126 127 GroupActionListener(ViewProducer[] components, int ourGroupNum) { 128 this.components = components; 129 this.ourGroupNum = ourGroupNum; 130 } 131 132 public void actionPerformed(ActionEvent e) { 133 for (int groupNum = 0; groupNum < components.length; ++groupNum) { 134 if (groupNum == ourGroupNum) { 135 components[groupNum].setEnabled(true); 136 } 137 else { 138 components[groupNum].setEnabled(false); 139 } 140 } 141 } 142 } 143 } 144 | Popular Tags |