KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > binding > DirectBindingTest


1 /*
2  * Created on 28.02.2005
3  *
4  */

5 package org.jdesktop.swing.binding;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.List JavaDoc;
9
10 import javax.swing.JList JavaDoc;
11 import javax.swing.JTable JavaDoc;
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 /**
28  * @author Jeanette Winzenburg
29  */

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 JavaDoc list = new JList JavaDoc();
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 JavaDoc list = new JList JavaDoc();
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 JavaDoc 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 JavaDoc asList(int[] selectionIndices) {
69         List JavaDoc list = new ArrayList JavaDoc();
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 JavaDoc table = new JTable JavaDoc();
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 JavaDoc {
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