1 5 package org.jdesktop.swing.binding; 6 7 import java.util.ArrayList ; 8 import java.util.List ; 9 10 import javax.swing.JList ; 11 import javax.swing.JTable ; 12 13 import junit.framework.TestCase; 14 15 import org.jdesktop.dataset.DataSelector; 16 import org.jdesktop.dataset.DataSet; 17 import org.jdesktop.dataset.DataTable; 18 import org.jdesktop.dataset.adapter.SelectionModelAdapter; 19 import org.jdesktop.dataset.adapter.TabularDataModelAdapter; 20 import org.jdesktop.swing.data.DataConstants; 21 import org.jdesktop.swing.data.DefaultDataModel; 22 import org.jdesktop.swing.data.MetaData; 23 import org.jdesktop.swing.data.SelectionModel; 24 import org.jdesktop.swing.data.TabularDataModel; 25 import org.jdesktop.swing.util.DataSetTestSupport; 26 27 30 public class DirectBindingTest extends TestCase { 31 32 private DataSet dataSet; 33 private DataSelector dataSelector; 34 private DataTable dataTable; 35 36 37 public void testDirectListBinding() { 38 TabularDataModel model = createFilledDataModel(); 39 JList list = new JList (); 40 Binding directListBinding = new DirectListBinding(list, model, 41 "firstName", new SelectionModelAdapter(dataSelector)); 42 directListBinding.pull(); 43 model.setValueAt("firstName", 0, "other"); 44 assertFalse("direct Binding must not be modified", directListBinding.isModified()); 45 assertTrue("direct binding must be valid", directListBinding.isValid()); 46 directListBinding.push(); 47 } 48 49 public void testListBinding() { 50 TabularDataModel model = createFilledDataModel(); 51 JList list = new JList (); 52 SelectionModel selection = new SelectionModelAdapter(dataSelector); 53 MetaData metaData = new MetaData("list", TabularDataModel.class); 54 metaData.setCustomProperty(DataConstants.SELECTION_MODEL, selection); 55 DefaultDataModel dataModel = new DefaultDataModel(new MetaData[] {metaData}); 56 dataModel.setValue("list", model); 57 Binding listBinding = new ListBinding(list, dataModel, "list"); 58 int[] rowIndices = selection.getSelectionIndices(); 59 List selectionIndices = asList(selection.getSelectionIndices()); 60 assertEquals(dataSelector.getRowIndices(), selectionIndices); 61 assertTrue("initial empty selection", list.isSelectionEmpty()); 62 int selectedIndex = 1; 63 dataSelector.setRowIndices(new int[] {selectedIndex}); 64 assertEquals("index in selectionModel", selectedIndex, selection.getSelectionIndices()[0]); 65 assertEquals("index in list selection", selectedIndex, list.getSelectedIndex()); 66 67 } 68 private List asList(int[] selectionIndices) { 69 List list = new ArrayList (); 70 for (int i = 0; i < selectionIndices.length; i++) { 71 list.add(selectionIndices[i]); 72 } 73 return list; 74 } 75 76 public void testDirectTableBinding() { 77 TabularDataModel model = createFilledDataModel(); 78 JTable table = new JTable (); 79 Binding directTableBinding = new DirectTableBinding(table, model); 80 directTableBinding.pull(); 81 model.setValueAt("firstName", 0, "other"); 82 assertFalse("direct Binding must not be modified", directTableBinding.isModified()); 83 assertTrue("direct binding must be valid", directTableBinding.isValid()); 84 directTableBinding.push(); 85 } 86 87 protected TabularDataModel createFilledDataModel() { 88 DataSet dataSet = DataSetTestSupport.createDefaultDataSet(3); 89 DataSetTestSupport.fillDefaultTable(dataSet); 90 DataTable table = dataSet.getTable(DataSetTestSupport.ADAPTEE_TABLE_NAME); 91 return new TabularDataModelAdapter(table); 92 } 93 94 95 protected void setUp() throws Exception { 96 super.setUp(); 97 dataSet = DataSetTestSupport.createDefaultDataSet(3); 98 DataSetTestSupport.fillDefaultTable(dataSet); 99 dataTable = dataSet.getTable(DataSetTestSupport.ADAPTEE_TABLE_NAME); 100 dataSelector = dataTable.getSelector(DataSetTestSupport.ADAPTEE_SELECTOR_NAME); 101 } 102 } 103 | Popular Tags |