KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > designer > view > FieldSelectionView


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.designer.view;
5
6 import java.awt.Component JavaDoc;
7 import java.awt.Container JavaDoc;
8 import java.awt.GridBagConstraints JavaDoc;
9 import java.awt.Insets JavaDoc;
10 import java.awt.event.ActionEvent JavaDoc;
11 import java.awt.event.ActionListener JavaDoc;
12
13 import javax.swing.ButtonGroup JavaDoc;
14 import javax.swing.JRadioButton JavaDoc;
15
16 import org.oddjob.designer.model.DesignDefinition;
17 import org.oddjob.designer.model.FieldSelection;
18
19 /**
20  * Used to select between different option elements.
21  */

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     /* (non-Javadoc)
31      * @see org.oddjob.designer.view.ViewProducer#dialog()
32      */

33     public Component JavaDoc dialog() {
34         throw new UnsupportedOperationException JavaDoc("Add the FieldSelection to a FieldGroup!");
35     }
36     
37     /* (non-Javadoc)
38      * @see org.oddjob.designer.view.ViewProducer#form()
39      */

40     public Component JavaDoc group() {
41         throw new UnsupportedOperationException JavaDoc("Add the FieldSelection to a FieldGroup!");
42     }
43
44     /* (non-Javadoc)
45      * @see org.oddjob.designer.view.ViewProducer#detailEdit(org.oddjob.designer.view.ActionWrapper)
46      */

47     public Component JavaDoc detailEdit() {
48         throw new UnsupportedOperationException JavaDoc("Add the FieldSelection to a FieldGroup!");
49     }
50     
51     /* (non-Javadoc)
52      * @see org.oddjob.designer.view.ViewProducer#cell()
53      */

54     public Component JavaDoc cell() {
55         throw new UnsupportedOperationException JavaDoc("Add the FieldSelection to a FieldGroup!");
56     }
57
58     /* (non-Javadoc)
59      * @see org.oddjob.designer.view.ViewProducer#inline(java.awt.Container, int, int, boolean)
60      */

61     public int inline(Container JavaDoc container, int row, int column,
62             boolean selectionInGroup) {
63         ButtonGroup JavaDoc bg = new ButtonGroup JavaDoc();
64         
65         ViewProducer[] views = new ViewProducer[fieldSelection.size()];
66         // remember which design definition was populated, if any.
67
JRadioButton JavaDoc populated = null;
68         for (int groupNum = 0; groupNum < fieldSelection.size(); ++groupNum) {
69
70             JRadioButton JavaDoc button = new JRadioButton JavaDoc();
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     /* (non-Javadoc)
99      * @see org.oddjob.designer.view.ViewProducer#setEnabled(boolean)
100      */

101     public void setEnabled(boolean enabled) {
102         throw new UnsupportedOperationException JavaDoc("Not supporting nested selections yet!");
103     }
104     
105     void addOptionButton(Container JavaDoc container, int row, int height, Component JavaDoc button) {
106         GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
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 JavaDoc(3, 1, 3, 1);
117         
118         container.add(button, c);
119
120     }
121     
122     static class GroupActionListener implements ActionListener JavaDoc {
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 JavaDoc 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