1 4 package org.oddjob.designer.view; 5 6 import java.awt.BorderLayout ; 7 import java.awt.Component ; 8 import java.awt.Container ; 9 import java.awt.GridBagConstraints ; 10 import java.awt.Insets ; 11 import java.awt.event.ActionEvent ; 12 import java.awt.event.ActionListener ; 13 import java.util.Observable ; 14 import java.util.Observer ; 15 16 import javax.swing.Box ; 17 import javax.swing.BoxLayout ; 18 import javax.swing.JComboBox ; 19 import javax.swing.JPanel ; 20 import javax.swing.JTextField ; 21 22 import org.oddjob.designer.Looks; 23 import org.oddjob.designer.model.DesignElement; 24 import org.oddjob.designer.model.SingleTypeSelection; 25 26 30 public class TypeSelectionView 31 implements ViewProducer, Observer { 32 33 private final SingleTypeSelection typeSelection; 34 35 final JComboBox comboBox; 36 final JPanel cell; 38 39 final JPanel form; 40 41 46 public TypeSelectionView(SingleTypeSelection selection) { 47 this.typeSelection = selection; 48 49 52 comboBox = new JComboBox (); 53 comboBox.addItem(""); 54 String [] 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 (new BorderLayout ()); 63 JTextField dummy = new JTextField (Looks.TEXT_FIELD_SIZE); 64 dummy.setEnabled(false); 65 cell.add(dummy); 66 67 comboBox.addActionListener(new ActionListener () { 68 public void actionPerformed(ActionEvent e) { 69 JComboBox cb = (JComboBox )e.getSource(); 70 String type = (String )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 (); 86 form.setLayout(new BoxLayout (form, BoxLayout.X_AXIS)); 87 form.add(comboBox); 88 form.add(Box.createHorizontalStrut(5)); 89 form.add(cell); 90 } 91 92 public Component dialog() { 93 return group(); 94 } 95 96 public Component group() { 97 JPanel group = new JPanel (); 98 group.setBorder(Looks.groupBorder(typeSelection.getTitle())); 99 group.add(form); 100 return group; 101 } 102 103 106 public Component detailEdit() { 107 return ViewHelper.createDetailButton(typeSelection); 108 } 109 110 public Component cell() { 111 return ViewHelper.createDetailButton(typeSelection); 112 } 113 114 117 public int inline(Container container, int row, int column, 118 boolean selectionInGroup) { 119 int columnCount = column; 120 121 GridBagConstraints c = new GridBagConstraints (); 122 123 c.weightx = 1.0; 124 c.weighty = 0.0; 125 126 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 (3, 3, 3, 20); 137 138 container.add(comboBox, c); 139 140 c.fill = GridBagConstraints.HORIZONTAL; 142 c.anchor = GridBagConstraints.NORTHWEST; 143 c.gridx = columnCount++; 144 c.gridwidth = GridBagConstraints.REMAINDER; 145 c.insets = new Insets (3, 0, 3, 0); 146 147 container.add(cell, c); 148 149 return row + 1; 150 } 151 152 155 public void setEnabled(boolean enabled) { 156 throw new UnsupportedOperationException ("Probably will need to do this soon!"); 157 } 158 159 162 public void update(Observable o, Object arg) { 163 if (typeSelection.getChildDesignElement() == null) { 164 comboBox.setSelectedItem(""); 165 } 166 } 167 } 168 | Popular Tags |