1 19 20 package org.netbeans.modules.db.util; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import javax.swing.AbstractListModel ; 25 import javax.swing.ComboBoxModel ; 26 import javax.swing.JComboBox ; 27 import javax.swing.ListModel ; 28 import org.netbeans.junit.NbTestCase; 29 30 34 public class DataComboBoxSupportTest extends NbTestCase { 35 36 public DataComboBoxSupportTest(String testName) { 37 super(testName); 38 } 39 40 public boolean runInEQ() { 41 return true; 42 } 43 44 public void testBasic() { 45 JComboBox comboBox = new JComboBox (); 46 ListModelImpl listModel = new ListModelImpl(); 47 DataModelImpl dataModel = new DataModelImpl(listModel); 48 DataComboBoxSupport support = new DataComboBoxSupport(comboBox, dataModel, true); 49 50 assertSame(support.NEW_ITEM, comboBox.getItemAt(0)); 51 assertEquals("Add", comboBox.getItemAt(0).toString()); 52 assertEquals(-1, comboBox.getSelectedIndex()); 53 54 List items = new ArrayList (); 55 items.add("foo"); 56 items.add("bar"); 57 listModel.setItems(items); 58 59 assertEquals("foo", comboBox.getItemAt(0)); 60 assertEquals("bar", comboBox.getItemAt(1)); 61 assertSame(support.SEPARATOR_ITEM, comboBox.getItemAt(2)); 62 assertEquals("Add", comboBox.getItemAt(3).toString()); 63 assertEquals("The old selected item was removed, nothing should be selected now", -1, comboBox.getSelectedIndex()); 64 65 comboBox.setSelectedIndex(1); items.remove("foo"); 67 listModel.setItems(items); 68 69 assertEquals("bar", comboBox.getItemAt(0)); 70 assertSame(support.SEPARATOR_ITEM, comboBox.getItemAt(1)); 71 assertEquals("Add", comboBox.getItemAt(2).toString()); 72 assertEquals("Bar should still be selected", 0, comboBox.getSelectedIndex()); 73 74 items.add("new"); 75 listModel.setItems(items, "new"); 76 77 assertEquals("bar", comboBox.getItemAt(0)); 78 assertEquals("new", comboBox.getItemAt(1)); 79 assertSame(support.SEPARATOR_ITEM, comboBox.getItemAt(2)); 80 assertEquals("Add", comboBox.getItemAt(3).toString()); 81 assertEquals("new", comboBox.getSelectedItem()); 82 assertEquals("New should be selected", 1, comboBox.getSelectedIndex()); 83 } 84 85 public void testNoAddition() { 86 JComboBox comboBox = new JComboBox (); 87 ListModelImpl listModel = new ListModelImpl(); 88 DataModelImpl dataModel = new DataModelImpl(listModel); 89 DataComboBoxSupport support = new DataComboBoxSupport(comboBox, dataModel, false); 90 91 assertEquals(0, comboBox.getItemCount()); 92 93 List items = new ArrayList (); 94 items.add("foo"); 95 items.add("bar"); 96 listModel.setItems(items); 97 98 assertEquals(2, comboBox.getItemCount()); 99 assertEquals("foo", comboBox.getItemAt(0)); 100 assertEquals("bar", comboBox.getItemAt(1)); 101 } 102 103 private static final class DataModelImpl implements DataComboBoxModel { 104 105 private ComboBoxModel listModel; 106 107 public DataModelImpl(ComboBoxModel listModel) { 108 this.listModel = listModel; 109 } 110 111 public String getItemTooltipText(Object item) { 112 return null; 113 } 114 115 public String getItemDisplayName(Object item) { 116 return (String )item + "-display"; 117 } 118 119 public void newItemActionPerformed() { 120 System.out.println("action performed"); 121 } 122 123 public String getNewItemDisplayName() { 124 return "Add"; 125 } 126 127 public ComboBoxModel getListModel() { 128 return listModel; 129 } 130 } 131 132 private static final class ListModelImpl extends AbstractListModel implements ComboBoxModel { 133 134 Object [] items = new Object [0]; 135 Object selectedItem; 136 137 public Object getElementAt(int index) { 138 return items[index]; 139 } 140 141 public int getSize() { 142 return items.length; 143 } 144 145 public Object getSelectedItem() { 146 return selectedItem; 147 } 148 149 public void setSelectedItem(Object selectedItem) { 150 this.selectedItem = selectedItem; 151 } 152 153 private void setItems(List items) { 154 this.items = (Object [])items.toArray(new Object [items.size()]); 155 fireContentsChanged(this, 0, this.items.length); 156 } 157 158 private void setItems(List items, Object selectedItem) { 159 this.items = (Object [])items.toArray(new Object [items.size()]); 160 this.selectedItem = selectedItem; 161 fireContentsChanged(this, 0, this.items.length); 162 } 163 } 164 } 165 | Popular Tags |