KickJava   Java API By Example, From Geeks To Geeks.

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


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.JComboBox JavaDoc;
14 import javax.swing.JLabel JavaDoc;
15 import javax.swing.JPanel JavaDoc;
16 import javax.swing.SwingConstants JavaDoc;
17
18 import org.oddjob.designer.Looks;
19 import org.oddjob.designer.model.SelectionList;
20
21 /**
22  * A view which renders a SelectionList. This Swing view uses a ComboBox
23  * to rener the view.
24  */

25 public class SelectionListView implements ViewProducer {
26         
27     private final SelectionList selectionList;
28     
29     private final JComboBox JavaDoc comboBox;
30     private final JLabel JavaDoc label;
31     
32     /**
33      * Constructor.
34      *
35      * @param selection A SelectionList.
36      */

37     public SelectionListView(SelectionList selection) {
38         this.selectionList = selection;
39                 
40         label = new JLabel JavaDoc(ViewHelper.padLabel(selectionList.getTitle()),
41                 SwingConstants.LEADING);
42
43         comboBox = new JComboBox JavaDoc();
44         comboBox.addItem("");
45         String JavaDoc[] types = selection.getOptions();
46         for (int i = 0; i < types.length; ++i) {
47             comboBox.addItem(types[i]);
48         }
49         if (selection.getSelected() != null) {
50             comboBox.setSelectedItem(selection.getSelected());
51         }
52         
53         comboBox.addActionListener(new ActionListener JavaDoc() {
54             public void actionPerformed(ActionEvent JavaDoc e) {
55                 JComboBox JavaDoc cb = (JComboBox JavaDoc)e.getSource();
56                 selectionList.setSelected((String JavaDoc)cb.getSelectedItem());
57             }
58         });
59     }
60     
61     public Component JavaDoc dialog() {
62         return group();
63     }
64     
65     public Component JavaDoc group() {
66         JPanel JavaDoc group = new JPanel JavaDoc();
67         group.setBorder(Looks.groupBorder(selectionList.getTitle()));
68         group.add(comboBox);
69         return group;
70     }
71
72     /* (non-Javadoc)
73      * @see org.oddjob.designer.view.ViewProducer#detailEdit(org.oddjob.designer.view.ActionWrapper)
74      */

75     public Component JavaDoc detailEdit() {
76         return ViewHelper.createDetailButton(selectionList);
77     }
78     
79     public Component JavaDoc cell() {
80         return comboBox;
81     }
82
83     /* (non-Javadoc)
84      * @see org.oddjob.designer.view.ViewProducer#inline(java.awt.Container, int, int, boolean)
85      */

86     public int inline(Container JavaDoc container, int row, int column,
87             boolean selectionInGroup) {
88         int columnCount = column;
89         
90         GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
91
92         c.weightx = 1.0;
93         c.weighty = 0.0;
94         
95         c.fill = GridBagConstraints.HORIZONTAL;
96         c.anchor = GridBagConstraints.NORTHWEST;
97         c.gridx = columnCount++;
98         c.gridy = row;
99         if (selectionInGroup) {
100             c.gridwidth = 2;
101             columnCount++;
102         }
103         
104         c.insets = new Insets JavaDoc(3, 3, 3, 20);
105
106         container.add(label, c);
107         
108         c.fill = GridBagConstraints.NONE;
109         c.anchor = GridBagConstraints.NORTHWEST;
110         c.gridx = columnCount++;
111         c.gridwidth = 1;
112         c.insets = new Insets JavaDoc(3, 0, 3, 0);
113         
114         container.add(comboBox, c);
115         
116         return row + 1;
117     }
118
119     /* (non-Javadoc)
120      * @see org.oddjob.designer.view.ViewProducer#setEnabled(boolean)
121      */

122     public void setEnabled(boolean enabled) {
123         comboBox.setEditable(enabled);
124         if (!enabled) {
125             selectionList.setSelected(null);
126         }
127     }
128     
129 }
130
Popular Tags