KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package org.oddjob.designer.view;
5
6 import java.awt.BorderLayout JavaDoc;
7 import java.awt.Component JavaDoc;
8 import java.awt.Container JavaDoc;
9 import java.awt.GridBagConstraints JavaDoc;
10 import java.awt.Insets JavaDoc;
11 import java.awt.event.ActionEvent JavaDoc;
12 import java.awt.event.ActionListener JavaDoc;
13 import java.util.Observable JavaDoc;
14 import java.util.Observer JavaDoc;
15
16 import javax.swing.Box JavaDoc;
17 import javax.swing.BoxLayout JavaDoc;
18 import javax.swing.JComboBox JavaDoc;
19 import javax.swing.JPanel JavaDoc;
20 import javax.swing.JTextField JavaDoc;
21
22 import org.oddjob.designer.Looks;
23 import org.oddjob.designer.model.DesignElement;
24 import org.oddjob.designer.model.SingleTypeSelection;
25
26 /**
27  * A view which renders a SelectionList. This Swing view uses a ComboBox
28  * to rener the view.
29  */

30 public class TypeSelectionView
31 implements ViewProducer, Observer JavaDoc {
32         
33     private final SingleTypeSelection typeSelection;
34     
35     final JComboBox JavaDoc comboBox;
36 // final JLabel label;
37
final JPanel JavaDoc cell;
38     
39     final JPanel JavaDoc form;
40     
41     /**
42      * Constructor.
43      *
44      * @param typeSelection A SelectionList.
45      */

46     public TypeSelectionView(SingleTypeSelection selection) {
47         this.typeSelection = selection;
48             
49 // label = new JLabel(ViewHelper.padLabel(typeSelection.getTitle()),
50
// SwingConstants.LEADING);
51

52         comboBox = new JComboBox JavaDoc();
53         comboBox.addItem("");
54         String JavaDoc[] types = typeSelection.getOptions();
55         for (int i = 0; i < types.length; ++i) {
56             comboBox.addItem(types[i]);
57         }
58         if (typeSelection.getSelected() != null) {
59             comboBox.setSelectedItem(typeSelection.getSelected());
60         }
61
62         cell = new JPanel JavaDoc(new BorderLayout JavaDoc());
63         JTextField JavaDoc dummy = new JTextField JavaDoc(Looks.TEXT_FIELD_SIZE);
64         dummy.setEnabled(false);
65         cell.add(dummy);
66         
67         comboBox.addActionListener(new ActionListener JavaDoc() {
68             public void actionPerformed(ActionEvent JavaDoc e) {
69                 JComboBox JavaDoc cb = (JComboBox JavaDoc)e.getSource();
70                 String JavaDoc type = (String JavaDoc)cb.getSelectedItem();
71                 typeSelection.setSelected(type);
72                 
73                 DesignElement child = typeSelection.getChildDesignElement();
74                 if (child != null) {
75                     cell.removeAll();
76                     cell.add(ViewFactory.create(child.detail()).cell(),
77                             BorderLayout.CENTER);
78                     cell.validate();
79                     cell.repaint();
80                 }
81             }
82
83         });
84
85         form = new JPanel JavaDoc();
86         form.setLayout(new BoxLayout JavaDoc(form, BoxLayout.X_AXIS));
87         form.add(comboBox);
88         form.add(Box.createHorizontalStrut(5));
89         form.add(cell);
90     }
91         
92     public Component JavaDoc dialog() {
93         return group();
94     }
95     
96     public Component JavaDoc group() {
97         JPanel JavaDoc group = new JPanel JavaDoc();
98         group.setBorder(Looks.groupBorder(typeSelection.getTitle()));
99         group.add(form);
100         return group;
101     }
102
103     /* (non-Javadoc)
104      * @see org.oddjob.designer.view.ViewProducer#detailEdit(org.oddjob.designer.view.ActionWrapper)
105      */

106     public Component JavaDoc detailEdit() {
107         return ViewHelper.createDetailButton(typeSelection);
108     }
109     
110     public Component JavaDoc cell() {
111         return ViewHelper.createDetailButton(typeSelection);
112     }
113
114     /* (non-Javadoc)
115      * @see org.oddjob.designer.view.ViewProducer#inline(java.awt.Container, int, int, boolean)
116      */

117     public int inline(Container JavaDoc container, int row, int column,
118             boolean selectionInGroup) {
119         int columnCount = column;
120         
121         GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
122
123         c.weightx = 1.0;
124         c.weighty = 0.0;
125         
126         // combo box.
127
c.fill = GridBagConstraints.HORIZONTAL;
128         c.anchor = GridBagConstraints.NORTHWEST;
129         c.gridx = columnCount++;
130         c.gridy = row;
131         if (selectionInGroup) {
132             c.gridwidth = 2;
133             columnCount++;
134         }
135         
136         c.insets = new Insets JavaDoc(3, 3, 3, 20);
137
138         container.add(comboBox, c);
139         
140         // ccell
141
c.fill = GridBagConstraints.HORIZONTAL;
142         c.anchor = GridBagConstraints.NORTHWEST;
143         c.gridx = columnCount++;
144         c.gridwidth = GridBagConstraints.REMAINDER;
145         c.insets = new Insets JavaDoc(3, 0, 3, 0);
146         
147         container.add(cell, c);
148                 
149         return row + 1;
150     }
151
152     /* (non-Javadoc)
153      * @see org.oddjob.designer.view.ViewProducer#setEnabled(boolean)
154      */

155     public void setEnabled(boolean enabled) {
156         throw new UnsupportedOperationException JavaDoc("Probably will need to do this soon!");
157     }
158     
159     /* (non-Javadoc)
160      * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
161      */

162     public void update(Observable JavaDoc o, Object JavaDoc arg) {
163         if (typeSelection.getChildDesignElement() == null) {
164             comboBox.setSelectedItem("");
165         }
166     }
167 }
168
Popular Tags