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.JComboBox ; 14 import javax.swing.JLabel ; 15 import javax.swing.JPanel ; 16 import javax.swing.SwingConstants ; 17 18 import org.oddjob.designer.Looks; 19 import org.oddjob.designer.model.SelectionList; 20 21 25 public class SelectionListView implements ViewProducer { 26 27 private final SelectionList selectionList; 28 29 private final JComboBox comboBox; 30 private final JLabel label; 31 32 37 public SelectionListView(SelectionList selection) { 38 this.selectionList = selection; 39 40 label = new JLabel (ViewHelper.padLabel(selectionList.getTitle()), 41 SwingConstants.LEADING); 42 43 comboBox = new JComboBox (); 44 comboBox.addItem(""); 45 String [] 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 () { 54 public void actionPerformed(ActionEvent e) { 55 JComboBox cb = (JComboBox )e.getSource(); 56 selectionList.setSelected((String )cb.getSelectedItem()); 57 } 58 }); 59 } 60 61 public Component dialog() { 62 return group(); 63 } 64 65 public Component group() { 66 JPanel group = new JPanel (); 67 group.setBorder(Looks.groupBorder(selectionList.getTitle())); 68 group.add(comboBox); 69 return group; 70 } 71 72 75 public Component detailEdit() { 76 return ViewHelper.createDetailButton(selectionList); 77 } 78 79 public Component cell() { 80 return comboBox; 81 } 82 83 86 public int inline(Container container, int row, int column, 87 boolean selectionInGroup) { 88 int columnCount = column; 89 90 GridBagConstraints c = new GridBagConstraints (); 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 (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 (3, 0, 3, 0); 113 114 container.add(comboBox, c); 115 116 return row + 1; 117 } 118 119 122 public void setEnabled(boolean enabled) { 123 comboBox.setEditable(enabled); 124 if (!enabled) { 125 selectionList.setSelected(null); 126 } 127 } 128 129 } 130 | Popular Tags |