001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Brad Reynolds - bug 116920
011: * Brad Reynolds - bug 160000
012: *******************************************************************************/package org.eclipse.jface.tests.databinding.scenarios;
013:
014: import java.util.ArrayList;
015: import java.util.Arrays;
016:
017: import org.eclipse.core.databinding.beans.BeansObservables;
018: import org.eclipse.core.databinding.observable.list.IObservableList;
019: import org.eclipse.core.databinding.observable.list.WritableList;
020: import org.eclipse.core.databinding.observable.map.IObservableMap;
021: import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
022: import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
023: import org.eclipse.jface.examples.databinding.model.Account;
024: import org.eclipse.jface.examples.databinding.model.Catalog;
025: import org.eclipse.jface.examples.databinding.model.Category;
026: import org.eclipse.jface.examples.databinding.model.SampleData;
027: import org.eclipse.jface.viewers.TableViewer;
028: import org.eclipse.swt.SWT;
029: import org.eclipse.swt.graphics.Image;
030: import org.eclipse.swt.layout.FillLayout;
031: import org.eclipse.swt.widgets.Table;
032: import org.eclipse.swt.widgets.TableColumn;
033: import org.eclipse.swt.widgets.TableItem;
034:
035: /**
036: * To run the tests in this class, right-click and select "Run As JUnit Plug-in
037: * Test". This will also start an Eclipse instance. To clean up the launch
038: * configuration, open up its "Main" tab and select "[No Application] - Headless
039: * Mode" as the application to run.
040: */
041:
042: public class TableScenarios extends ScenariosTestCase {
043:
044: private TableViewer tableViewer;
045:
046: private Catalog catalog;
047:
048: Category category;
049:
050: private TableColumn firstNameColumn;
051:
052: private TableColumn lastNameColumn;
053:
054: private TableColumn stateColumn;
055:
056: Image[] images;
057:
058: private TableColumn fancyColumn;
059:
060: protected void setUp() throws Exception {
061: super .setUp();
062: getComposite().setLayout(new FillLayout());
063: tableViewer = new TableViewer(getComposite());
064: tableViewer.getTable().setLinesVisible(true);
065: tableViewer.getTable().setHeaderVisible(true);
066: firstNameColumn = new TableColumn(tableViewer.getTable(),
067: SWT.NONE);
068: firstNameColumn.setWidth(50);
069: lastNameColumn = new TableColumn(tableViewer.getTable(),
070: SWT.NONE);
071: lastNameColumn.setWidth(50);
072: stateColumn = new TableColumn(tableViewer.getTable(), SWT.NONE);
073: stateColumn.setWidth(50);
074: fancyColumn = new TableColumn(tableViewer.getTable(), SWT.NONE);
075: fancyColumn.setWidth(250);
076:
077: catalog = SampleData.CATALOG_2005; // Lodging source
078: category = SampleData.WINTER_CATEGORY;
079:
080: images = new Image[] {
081: getShell().getDisplay().getSystemImage(SWT.ICON_ERROR),
082: getShell().getDisplay()
083: .getSystemImage(SWT.ICON_WARNING),
084: getShell().getDisplay().getSystemImage(
085: SWT.ICON_INFORMATION), };
086: }
087:
088: protected void tearDown() throws Exception {
089: // do any teardown work here
090: super .tearDown();
091: tableViewer.getTable().dispose();
092: tableViewer = null;
093: firstNameColumn = null;
094: lastNameColumn = null;
095: stateColumn = null;
096: }
097:
098: public void testScenario01() {
099: // Show that a TableViewer with three columns renders the accounts
100: Account[] accounts = catalog.getAccounts();
101:
102: IObservableList list = new WritableList(new ArrayList(),
103: Account.class);
104: list.addAll(Arrays.asList(accounts));
105:
106: ObservableListContentProvider contentProvider = new ObservableListContentProvider();
107: IObservableMap[] attributeMaps = BeansObservables.observeMaps(
108: contentProvider.getKnownElements(), Account.class,
109: new String[] { "firstName", "lastName", "state" });
110:
111: tableViewer.setContentProvider(contentProvider);
112: tableViewer.setLabelProvider(new ObservableMapLabelProvider(
113: attributeMaps));
114: tableViewer.setInput(list);
115:
116: Table table = tableViewer.getTable();
117:
118: // Verify the data in the table columns matches the accounts
119: for (int i = 0; i < accounts.length; i++) {
120: Account account = catalog.getAccounts()[i];
121: TableItem item = table.getItem(i);
122:
123: assertEquals(account.getFirstName(), item.getText(0));
124: assertEquals(account.getLastName(), item.getText(1));
125: assertEquals(account.getState(), item.getText(2));
126: }
127: }
128:
129: public void testScenario02() throws SecurityException,
130: IllegalArgumentException {
131: // Show that a TableViewer with three columns can be used to update
132: // columns
133: // Account[] accounts = catalog.getAccounts();
134: //
135: // TableModelDescription tableModelDescription = new
136: // TableModelDescription(new Property(catalog, "accounts"), new
137: // String[]{"firstName","lastName","state"});
138: // // tableViewerDescription.addEditableColumn("firstName");
139: // // tableViewerDescription.addEditableColumn("lastName", null, null,
140: // new PhoneConverter());
141: // // tableViewerDescription.addEditableColumn("state", null, null, new
142: // StateConverter());
143: // getDbc().bind(tableViewer,
144: // tableModelDescription, null);
145: //
146: // Account account = accounts[0];
147: // // Select the first item in the table
148: // tableViewer.editElement(account, 0);
149: // // Set the text property of the cell editor which is now active over
150: // the "firstName" column
151: // CellEditor[] cellEditors = tableViewer.getCellEditors();
152: // TextCellEditor firstNameEditor = (TextCellEditor) cellEditors[0];
153: // // Change the firstName and test it goes to the model
154: // enterText((Text) firstNameEditor.getControl(), "Bill");
155: // // Check whether the model has changed
156: // assertEquals("Bill",account.getFirstName());
157: }
158:
159: public void testScenario04() {
160: // // Show that when an item is added to a collection the table gets an
161: // extra item
162: // Account[] accounts = catalog.getAccounts();
163: //
164: // TableViewerDescription tableViewerDescription = new
165: // TableViewerDescription(
166: // tableViewer);
167: // tableViewerDescription.addColumn("firstName");
168: // tableViewerDescription.addColumn("lastName");
169: // tableViewerDescription.addColumn("state");
170: // tableViewerDescription.addColumn(null,new IConverter(){
171: //
172: // public Class getModelType() {
173: // return Account.class;
174: // }
175: //
176: // public Class getTargetType() {
177: // return ViewerLabel.class;
178: // }
179: //
180: // public Object convertTargetToModel(Object targetObject) {
181: // return null;
182: // }
183: //
184: // public Object convertModelToTarget(Object modelObject) {
185: // Account account = (Account) modelObject;
186: // return new ViewerLabel(account.toString(), images[new
187: // Random().nextInt(images.length)]);
188: // }});
189: // getDbc().bind(tableViewerDescription,
190: // new Property(catalog, "accounts"), null);
191: //
192: // //interact();
193: //
194: // // Verify the number of accounts matches the number of items in the
195: // table
196: // assertEquals(tableViewer.getTable().getItemCount(),accounts.length);
197: // // Add a new account and verify that the number of items in the table
198: // increases
199: // Account newAccount = new Account();
200: // newAccount.setFirstName("Finbar");
201: // newAccount.setLastName("McGoo");
202: // newAccount.setLastName("NC");
203: // catalog.addAccount(newAccount);
204: // // The number of items should have gone up by one
205: // assertEquals(tableViewer.getTable().getItemCount(),accounts.length +
206: // 1);
207: // // The number of items should still match the number of accounts
208: // (i.e. test the model)
209: // assertEquals(tableViewer.getTable().getItemCount(),catalog.getAccounts().length);
210: // // Remove the account that was just added
211: // catalog.removeAccount(newAccount);
212: // // The number of items should match the original
213: // assertEquals(tableViewer.getTable().getItemCount(),accounts.length);
214: // // The number of items should still match the number of accounts
215: // (i.e. test the model is reset)
216: // assertEquals(tableViewer.getTable().getItemCount(),catalog.getAccounts().length);
217: //
218: // // Test adding and removing to the model on a non UI thread
219: // int numberOfAccounts = catalog.getAccounts().length;
220: // final Account barney = new Account();
221: // barney.setFirstName("Barney");
222: // barney.setLastName("Smith");
223: // barney.setLastName("CA");
224: // invokeNonUI(new Runnable(){
225: // public void run(){
226: // catalog.addAccount(barney);
227: // }
228: // });
229: // spinEventLoop(0);
230: // // The number of items should have gone up by one
231: // assertEquals(tableViewer.getTable().getItemCount(),numberOfAccounts +
232: // 1);
233: //
234: // invokeNonUI(new Runnable(){
235: // public void run(){
236: // catalog.removeAccount(barney);
237: // }
238: // });
239: // spinEventLoop(0);
240: // // The number of items should have reverted to the original number
241: // before barney was added and removed
242: // assertEquals(tableViewer.getTable().getItemCount(),numberOfAccounts);
243: //
244: }
245:
246: public void testScenario03() {
247: // // Show that converters work for table columns
248: // Account[] accounts = catalog.getAccounts();
249: //
250: // TableViewerDescription tableViewerDescription = new
251: // TableViewerDescription(
252: // tableViewer);
253: // tableViewerDescription.addEditableColumn("lastName");
254: // tableViewerDescription.addEditableColumn("phone", null, null ,
255: // new PhoneConverter());
256: // tableViewerDescription.addEditableColumn("state", null, null ,
257: // new StateConverter());
258: // getDbc().bind(tableViewerDescription,
259: // new Property(catalog, "accounts"), null);
260: //
261: // // Verify that the data in the the table columns matches the expected
262: // // What we are looking for is that the phone numbers are converterted
263: // to
264: // // nnn-nnn-nnnn and that
265: // // the state letters are converted to state names
266: // // Verify the data in the table columns matches the accounts
267: // PhoneConverter phoneConverter = new PhoneConverter();
268: // StateConverter stateConverter = new StateConverter();
269: // for (int i = 0; i < accounts.length; i++) {
270: // Account account = catalog.getAccounts()[i];
271: // // Check the phone number
272: // String col_phone = ((ITableLabelProvider) tableViewer
273: // .getLabelProvider()).getColumnText(account, 1);
274: // assertEquals(getValue((String)phoneConverter
275: // .convertModelToTarget(account.getPhone())), col_phone);
276: // String col_state = ((ITableLabelProvider) tableViewer
277: // .getLabelProvider()).getColumnText(account, 2);
278: // assertEquals(getValue((String)stateConverter
279: // .convertModelToTarget(account.getState())), col_state);
280: // }
281: }
282:
283: public void testScenario05() {
284: // // Show that when the model changes then the UI refreshes to reflect
285: // this
286: //
287: // TableViewerDescription tableViewerDescription = new
288: // TableViewerDescription(
289: // tableViewer);
290: // tableViewerDescription.addColumn("lastName");
291: // tableViewerDescription.addColumn("phone",
292: // new PhoneConverter());
293: // tableViewerDescription.addColumn("state",
294: // new StateConverter());
295: // getDbc().bind(tableViewerDescription,
296: // new Property(catalog, "accounts"), null);
297: //
298: // final Account account = catalog.getAccounts()[0];
299: // String lastName = tableViewer.getTable().getItem(0).getText(0);
300: // // Check the firstName in the TableItem is the same as the model
301: // assertEquals(lastName,account.getLastName());
302: // // Now change the model and check again
303: // account.setLastName("Gershwin");
304: // lastName = tableViewer.getTable().getItem(0).getText(0);
305: // assertEquals(lastName,account.getLastName());
306: //
307: // // Test the model update on a non UI thread
308: // invokeNonUI(new Runnable(){
309: // public void run(){
310: // account.setLastName("Mozart");
311: // }
312: // });
313: // spinEventLoop(0);
314: // lastName = tableViewer.getTable().getItem(0).getText(0);
315: // assertEquals(lastName,account.getLastName());
316: //
317: }
318:
319: public void testScenario06() {
320: // // Check that explicit type means that defaulting of converters works
321: // TableViewerDescription tableViewerDescription = new
322: // TableViewerDescription(
323: // tableViewer);
324: // tableViewerDescription.addEditableColumn("price");
325: // tableViewerDescription.getColumn(0).setPropertyType(Double.TYPE);
326: // getDbc().bind(tableViewerDescription,
327: // new Property(catalog, "transporations"), null);
328: // Transportation transporation = catalog.getTransporations()[0];
329: // tableViewer.editElement(transporation, 0);
330: // // Set the text property of the cell editor which is now active over
331: // the "firstName" column
332: // CellEditor[] cellEditors = tableViewer.getCellEditors();
333: // TextCellEditor priceEditor = (TextCellEditor) cellEditors[0];
334: // // Change the firstName and test it goes to the model
335: // enterText((Text) priceEditor.getControl(), "123.45");
336: // // Verify the model is updated
337: // assertEquals(transporation.getPrice(),123.45,0);
338:
339: }
340:
341: public void testScenario07() {
342: // // Verify that even when a column's property type is not set, that it
343: // is worked out lazily from the target type
344: // TableViewerDescription tableViewerDescription = new
345: // TableViewerDescription(
346: // tableViewer);
347: // tableViewerDescription.addEditableColumn("price");
348: // // The column's type is not set to be Double.TYPE. This will be
349: // inferred once the first Transportation object is set
350: // // into the ObservableCollection
351: // getDbc().bind(tableViewerDescription,
352: // new Property(catalog, "transporations"), null);
353: // Transportation transporation = catalog.getTransporations()[0];
354: // tableViewer.editElement(transporation, 0);
355: // // Set the text property of the cell editor which is now active over
356: // the "firstName" column
357: // CellEditor[] cellEditors = tableViewer.getCellEditors();
358: // TextCellEditor priceEditor = (TextCellEditor) cellEditors[0];
359: // // Change the firstName and test it goes to the model
360: // enterText((Text) priceEditor.getControl(), "123.45");
361: // // Verify the model is updated
362: // assertEquals(transporation.getPrice(),123.45,0);
363: //
364: }
365:
366: public void testScenario08_00() {
367: // // Verify that binding to a Collection property (rather than an
368: // array) works when specifying data type
369: // TableViewerDescription tableViewerDescription = new
370: // TableViewerDescription(
371: // tableViewer);
372: // tableViewerDescription.addEditableColumn("userId");
373: // tableViewerDescription.addEditableColumn("password");
374: // getDbc().bind(tableViewerDescription,
375: // new Property(catalog, "signons", Signon.class, null), null);
376: // Signon firstSignon = (Signon) catalog.getSignons().get(0);
377: // // Verify the UI matches the model
378: // TableItem firstTableItem = tableViewer.getTable().getItem(0);
379: // assertEquals(firstTableItem.getText(1),firstSignon.getPassword());
380: // // Change the model and ensure the UI refreshes
381: // firstSignon.setPassword("Eclipse123Rocks");
382: // assertEquals("Eclipse123Rocks",firstSignon.getPassword());
383: // assertEquals(firstTableItem.getText(1),firstSignon.getPassword());
384: // // Change the GUI and ensure the model refreshes
385: // tableViewer.editElement(firstSignon, 1);
386: // CellEditor[] cellEditors = tableViewer.getCellEditors();
387: // TextCellEditor passwordEditor = (TextCellEditor) cellEditors[1];
388: // enterText((Text) passwordEditor.getControl(), "Cricket11Players");
389: // assertEquals("Cricket11Players",firstSignon.getPassword());
390: //
391: }
392:
393: public void testScenario08_01() {
394: // // Verify that binding to a Collection property (rather than an
395: // array) works without specifying data type
396: // TableViewerDescription tableViewerDescription = new
397: // TableViewerDescription(
398: // tableViewer);
399: // tableViewerDescription.addEditableColumn("userId");
400: // tableViewerDescription.addEditableColumn("password");
401: // getDbc().bind(tableViewerDescription,
402: // new Property(catalog, "signons"), null);
403: // Signon firstSignon = (Signon) catalog.getSignons().get(0);
404: // // Verify the UI matches the model
405: // TableItem firstTableItem = tableViewer.getTable().getItem(0);
406: // assertEquals(firstTableItem.getText(1),firstSignon.getPassword());
407: // // Change the model and ensure the UI refreshes
408: // firstSignon.setPassword("Eclipse123Rocks");
409: // assertEquals("Eclipse123Rocks",firstSignon.getPassword());
410: // assertEquals(firstTableItem.getText(1),firstSignon.getPassword());
411: // // Change the GUI and ensure the model refreshes
412: // tableViewer.editElement(firstSignon, 1);
413: // CellEditor[] cellEditors = tableViewer.getCellEditors();
414: // TextCellEditor passwordEditor = (TextCellEditor) cellEditors[1];
415: // enterText((Text) passwordEditor.getControl(), "Cricket11Players");
416: // assertEquals("Cricket11Players",firstSignon.getPassword());
417: //
418: }
419:
420: public void testScenario09() {
421: // // Verify that nested properties work. Catalog has adventures.
422: // Adventure has defaultLodging. Loding has name.
423: // TableViewerDescription tableViewerDescription = new
424: // TableViewerDescription(tableViewer);
425: // tableViewerDescription.addColumn("name");
426: // tableViewerDescription.addColumn("defaultLodging.name");
427: // getDbc().bind(tableViewerDescription,new Property(category,
428: // "adventures"),null);
429: //
430: }
431: /**
432: * public void testScenario10(){ // Verify that for TIME_EARLY updating
433: * occurs on a per key basic for a TextCellEditor // Show that converters
434: * work for table columns Account[] accounts = catalog.getAccounts();
435: * Account firstAccount = accounts[0];
436: * SampleData.getSWTObservableFactory().setUpdateTime(DataBindingContext.TIME_EARLY);
437: * TableViewerDescription tableViewerDescription = new
438: * TableViewerDescription(tableViewer);
439: * tableViewerDescription.addEditableColumn("lastName");
440: * tableViewerDescription.addColumn("lastName");
441: * getDbc().bind(tableViewerDescription,new Property(catalog, "accounts"),
442: * null); // Verify that the first account is shown in the first row with
443: * the last name correctly
444: * assertEquals(tableViewer.getTable().getItem(0).getData(),firstAccount);
445: * assertEquals(tableViewer.getTable().getItem(0).getText(0),firstAccount.getLastName());
446: * assertEquals(tableViewer.getTable().getItem(0).getText(1),firstAccount.getLastName()); //
447: * Create a cell editor over the first column
448: * tableViewer.editElement(firstAccount, 0); // Set the text property of the
449: * cell editor which is now active over the "firstName" column CellEditor[]
450: * cellEditors = tableViewer.getCellEditors(); TextCellEditor
451: * lastNameCellEditor = (TextCellEditor) cellEditors[0];
452: * ((Text)lastNameCellEditor.getControl()).setText("E"); // Verify that the
453: * key press goes to the model assertEquals(firstAccount.getLastName(),"E"); }
454: */
455: }
|