Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 45272 Details for
Bug 144260
[DataBinding] CellEditor support for TableViewer
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
[patch]
CellEditor support
tableviewerediting.patch (text/plain), 55.78 KB, created by
Brad Reynolds
on 2006-06-25 18:56:31 EDT
(
hide
)
Description:
CellEditor support
Filename:
MIME Type:
Creator:
Brad Reynolds
Created:
2006-06-25 18:56:31 EDT
Size:
55.78 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.jface.databinding >Index: src/org/eclipse/jface/internal/databinding/provisional/viewers/ViewersObservableFactory.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/provisional/viewers/ViewersObservableFactory.java,v >retrieving revision 1.5 >diff -u -r1.5 ViewersObservableFactory.java >--- src/org/eclipse/jface/internal/databinding/provisional/viewers/ViewersObservableFactory.java 16 Jun 2006 03:05:18 -0000 1.5 >+++ src/org/eclipse/jface/internal/databinding/provisional/viewers/ViewersObservableFactory.java 25 Jun 2006 22:51:54 -0000 >@@ -8,26 +8,34 @@ > * Contributors: > * IBM Corporation - initial API and implementation > * Brad Reynolds - bug 137877 >+ * Brad Reynolds - bug 144260 > *******************************************************************************/ > > package org.eclipse.jface.internal.databinding.provisional.viewers; > >+import org.eclipse.jface.internal.databinding.internal.swt.TextObservableValue; > import org.eclipse.jface.internal.databinding.internal.viewers.AbstractListViewerObservableCollectionWithLabels; > import org.eclipse.jface.internal.databinding.internal.viewers.SelectionProviderSingleSelectionObservableValue; > import org.eclipse.jface.internal.databinding.internal.viewers.TableViewerObservableCollectionWithLabels; > import org.eclipse.jface.internal.databinding.provisional.description.Property; >+import org.eclipse.jface.internal.databinding.provisional.description.TableModelDescription; > import org.eclipse.jface.internal.databinding.provisional.factories.IObservableFactory; > import org.eclipse.jface.internal.databinding.provisional.observable.IObservable; > import org.eclipse.jface.viewers.AbstractListViewer; >+import org.eclipse.jface.viewers.CellEditor; >+import org.eclipse.jface.viewers.CheckboxCellEditor; > import org.eclipse.jface.viewers.ISelectionProvider; > import org.eclipse.jface.viewers.TableViewer; >+import org.eclipse.jface.viewers.TextCellEditor; >+import org.eclipse.swt.SWT; >+import org.eclipse.swt.widgets.Text; > > /** > * A factory that supports binding to JFace viewers. This factory supports the > * following description objects: > * <ul> > * <li>{@link AbstractListViewer} - denotes the viewer's collection of elements</li> >- * <li>{@link TableViewerDescription} - TODO describe</li> >+ * <li>{@link TableModelDescription} - TODO describe</li> > * <li>org.eclipse.jface.databinding.PropertyDescription - depending on the > * property description's object and property ID: > * <ul> >@@ -37,6 +45,8 @@ > * {@link ViewersProperties#CONTENT}</li> > * <li>object instanceof AbstractListViewer, property ID is > * {@link ViewersProperties#CONTENT}</li> >+ * <li>object instanceof <code>TextCellEditor</code></li> >+ * <li>object instanceof <code>CheckboxCellEditor</code></li> > * </ul> > * </li> > * </ul> >@@ -54,7 +64,7 @@ > } > > /** >- * @param updateTime. >+ * @param updateTime > * Update policy of DataBindingContext.TIME_EARLY or TIME_LATE. > * This is only a hint that some editable viewers may support > */ >@@ -84,7 +94,24 @@ > } else if (description instanceof TableViewer) { > return new TableViewerObservableCollectionWithLabels( > (TableViewer) description); >+ } else if (description instanceof CellEditor) { >+ if (description instanceof TextCellEditor) { >+ TextCellEditor editor = (TextCellEditor) description; >+ >+ if (editor.getControl() == null) { >+ throw new IllegalArgumentException( >+ "The TextCellEditor control must be created before creating the observable."); //$NON-NLS-1$ >+ } >+ >+ //Updating of the model will need to be done manually, hence the SWT.NONE. >+ return new TextObservableValue((Text) editor.getControl(), >+ SWT.NONE); >+ } else if (description instanceof CheckboxCellEditor) { >+ return new BooleanCellEditorObservableValue( >+ (CheckboxCellEditor) description); >+ } > } >+ > return null; > } > } >Index: src/org/eclipse/jface/internal/databinding/provisional/viewers/TableViewerBindingManagerFactory.java >=================================================================== >RCS file: src/org/eclipse/jface/internal/databinding/provisional/viewers/TableViewerBindingManagerFactory.java >diff -N src/org/eclipse/jface/internal/databinding/provisional/viewers/TableViewerBindingManagerFactory.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/jface/internal/databinding/provisional/viewers/TableViewerBindingManagerFactory.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,243 @@ >+/******************************************************************************* >+ * Copyright (c) 2006 Brad Reynolds and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Brad Reynolds - initial API and implementation >+ ******************************************************************************/ >+ >+package org.eclipse.jface.internal.databinding.provisional.viewers; >+ >+import java.util.ArrayList; >+import java.util.HashMap; >+import java.util.Iterator; >+import java.util.List; >+import java.util.Map; >+ >+import org.eclipse.jface.internal.databinding.provisional.BindSpec; >+import org.eclipse.jface.internal.databinding.provisional.Binding; >+import org.eclipse.jface.internal.databinding.provisional.DataBindingContext; >+import org.eclipse.jface.internal.databinding.provisional.description.Property; >+import org.eclipse.jface.internal.databinding.provisional.description.TableModelDescription; >+import org.eclipse.jface.internal.databinding.provisional.observable.list.IObservableList; >+import org.eclipse.jface.viewers.CellEditor; >+import org.eclipse.jface.viewers.TableViewer; >+ >+/** >+ * Builds a binding for a <code>TableViewer</code>. >+ * >+ * @since 3.2 >+ */ >+public class TableViewerBindingManagerFactory { >+ /** >+ * Empty array to represent no column IDs. >+ */ >+ private static final Object[] NO_COLUMN_IDS = new Object[0]; >+ >+ /** >+ * Maintains the order of columns. >+ */ >+ private List columnIDs; >+ >+ /** >+ * Maintains columnData instances keyed by column ID. >+ */ >+ private Map columnDatas; >+ >+ /** >+ * Constructs a new instance. >+ */ >+ public TableViewerBindingManagerFactory() { >+ } >+ >+ /** >+ * Adds a column to the binding. >+ * >+ * @param columnID >+ */ >+ public void addColumn(Object columnID) { >+ addColumn(columnID, null, null, null); >+ } >+ >+ /** >+ * Adds a column that is to be edited with the provided editor data. >+ * >+ * @param columnID >+ * @param cellEditor >+ * @param propertyType >+ * @param bindSpec >+ */ >+ private void addColumn(Object columnID, CellEditor cellEditor, >+ Class propertyType, BindSpec bindSpec) { >+ if (columnID == null) { >+ throw new IllegalArgumentException( >+ "Parameter " + columnID + " was null."); //$NON-NLS-1$ //$NON-NLS-2$ >+ } >+ >+ if (columnDatas == null) { >+ columnIDs = new ArrayList(); >+ columnDatas = new HashMap(); >+ } >+ >+ columnIDs.add(columnID); >+ columnDatas.put(columnID, new ColumnData(cellEditor, propertyType, >+ bindSpec)); >+ } >+ >+ /** >+ * Adds a column that is to be edited using the editor data. >+ * >+ * @param columnID >+ * @param cellEditor >+ * @param propertyType >+ * @param bindSpec >+ * can be <code>null</code> >+ */ >+ public void addEditableColumn(Object columnID, CellEditor cellEditor, >+ Class propertyType, BindSpec bindSpec) { >+ if (columnID == null) { >+ throw new IllegalArgumentException( >+ "Parameter " + columnID + " was null."); //$NON-NLS-1$ //$NON-NLS-2$ >+ } >+ if (cellEditor == null) { >+ throw new IllegalArgumentException( >+ "Parameter " + cellEditor + " was null."); //$NON-NLS-1$ //$NON-NLS-2$ >+ } >+ if (propertyType == null) { >+ throw new IllegalArgumentException( >+ "Parameter " + propertyType + " was null."); //$NON-NLS-1$ //$NON-NLS-2$ >+ } >+ >+ addColumn(columnID, cellEditor, propertyType, bindSpec); >+ } >+ >+ /** >+ * Returns the columnID for the column index. >+ * >+ * @param columnIndex >+ * @return column ID >+ */ >+ public Object getColumnID(int columnIndex) { >+ if (columnIDs == null || columnIndex >= columnIDs.size() >+ || columnIndex < 0) { >+ throw new IllegalArgumentException( >+ "Parameter columnIndex [" + columnIndex + "] is out of bounds."); //$NON-NLS-1$//$NON-NLS-2$ >+ } >+ >+ return columnIDs.get(columnIndex); >+ } >+ >+ /** >+ * @return Number of columns added to the factory. >+ */ >+ public int getColumnCount() { >+ return (columnDatas != null) ? columnDatas.size() : 0; >+ } >+ >+ /** >+ * Binds a viewer to the provided <code>propertyName</code> of the >+ * <code>model</code> which contains objects of the provided >+ * <code>propertyType</code>. >+ * >+ * @param viewer >+ * @param model >+ * @param propertyName >+ * @param propertyType >+ * @param context >+ * @param bindSpec >+ * @return binding manager >+ */ >+ public TableViewerBindingManager bind(TableViewer viewer, Object model, >+ String propertyName, Class propertyType, >+ DataBindingContext context, BindSpec bindSpec) { >+ TableModelDescription description = new TableModelDescription( >+ new Property(model, propertyName, propertyType, Boolean.TRUE), >+ getColumnIDs()); >+ return bind(viewer, description, context, bindSpec); >+ } >+ >+ /** >+ * Binds a viewer to the provided <code>observableList</code>. >+ * >+ * @param viewer >+ * @param observableList >+ * @param context >+ * @param bindSpec >+ * >+ * @return binding manager >+ */ >+ public TableViewerBindingManager bind(TableViewer viewer, >+ IObservableList observableList, DataBindingContext context, >+ BindSpec bindSpec) { >+ TableModelDescription description = new TableModelDescription( >+ observableList, getColumnIDs()); >+ >+ return bind(viewer, description, context, bindSpec); >+ } >+ >+ private TableViewerBindingManager bind(TableViewer viewer, >+ TableModelDescription description, DataBindingContext context, >+ BindSpec bindSpec) { >+ // Perform the binding. >+ Binding binding = context.bind(viewer, description, bindSpec); >+ >+ // Setup editing. >+ TableViewerEditorManager editorManager = new TableViewerEditorManager( >+ viewer, description.getColumnIDs(), context); >+ if (columnDatas != null) { >+ for (Iterator it = columnIDs.iterator(); it.hasNext();) { >+ Object columnID = it.next(); >+ ColumnData data = (ColumnData) columnDatas.get(columnID); >+ if (data.editor != null) { >+ editorManager.setEditor(data.editor, columnID, >+ data.propertyType, data.bindSpec); >+ } >+ } >+ } >+ >+ return new TableViewerBindingManager(description, binding, >+ editorManager); >+ } >+ >+ /** >+ * @return columnIDs, empty array if none >+ */ >+ public Object[] getColumnIDs() { >+ Object[] result = NO_COLUMN_IDS; >+ >+ if (columnIDs != null) { >+ result = columnIDs.toArray(); >+ } >+ return result; >+ } >+ >+ /** >+ * Manages relationship between columnID and the editor data. >+ * >+ * @since 3.2 >+ */ >+ private class ColumnData { >+ private final CellEditor editor; >+ >+ private final Class propertyType; >+ >+ private final BindSpec bindSpec; >+ >+ /** >+ * Constructs a new instance. >+ * >+ * @param editor >+ * @param propertyType >+ * @param bindSpec >+ */ >+ public ColumnData(CellEditor editor, Class propertyType, >+ BindSpec bindSpec) { >+ this.editor = editor; >+ this.propertyType = propertyType; >+ this.bindSpec = bindSpec; >+ } >+ } >+} >Index: src/org/eclipse/jface/internal/databinding/provisional/viewers/TableViewerEditorManager.java >=================================================================== >RCS file: src/org/eclipse/jface/internal/databinding/provisional/viewers/TableViewerEditorManager.java >diff -N src/org/eclipse/jface/internal/databinding/provisional/viewers/TableViewerEditorManager.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/jface/internal/databinding/provisional/viewers/TableViewerEditorManager.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,320 @@ >+/******************************************************************************* >+ * Copyright (c) 2006 Brad Reynolds and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Brad Reynolds - initial API and implementation >+ ******************************************************************************/ >+ >+package org.eclipse.jface.internal.databinding.provisional.viewers; >+ >+import org.eclipse.jface.internal.databinding.provisional.BindSpec; >+import org.eclipse.jface.internal.databinding.provisional.Binding; >+import org.eclipse.jface.internal.databinding.provisional.DataBindingContext; >+import org.eclipse.jface.internal.databinding.provisional.description.Property; >+import org.eclipse.jface.internal.databinding.provisional.observable.IObservable; >+import org.eclipse.jface.internal.databinding.provisional.observable.value.AbstractObservableValue; >+import org.eclipse.jface.internal.databinding.provisional.observable.value.ValueDiff; >+import org.eclipse.jface.util.Assert; >+import org.eclipse.jface.viewers.CellEditor; >+import org.eclipse.jface.viewers.ICellModifier; >+import org.eclipse.jface.viewers.TableViewer; >+import org.eclipse.swt.widgets.Item; >+ >+/** >+ * Manages the editing of a bound <code>TableViewer</code>. >+ * <p> >+ * The <code>CellEditor</code> must not propagate changes to the model on its >+ * own. This is facilitated by this manager and if the editor was to propagate >+ * changes on its own the behavior is undefined. >+ * </p> >+ * >+ * @since 3.2 >+ */ >+public class TableViewerEditorManager { >+ private final Object[] columnIDs; >+ >+ /** >+ * Viewer to associate editors with. >+ */ >+ private final TableViewer viewer; >+ >+ /** >+ * Bindings that have been created for editors. >+ */ >+ private Binding[] bindings; >+ >+ /** >+ * DBC to use for creating editor bindings. >+ */ >+ private final DataBindingContext context; >+ >+ /** >+ * Observable that represents the element being edited. This is set in >+ * CellModifer getValue as this is the only place that we have access to >+ * this. >+ */ >+ private EditableElement editableElement; >+ >+ /** >+ * Constructs a new instance. >+ * >+ * @param viewer >+ * @param columnIDs >+ * @param context >+ */ >+ public TableViewerEditorManager(TableViewer viewer, Object[] columnIDs, >+ DataBindingContext context) { >+ if (viewer == null) { >+ throw new IllegalArgumentException( >+ "Parameter " + viewer + " was null."); //$NON-NLS-1$ //$NON-NLS-2$ >+ } >+ if (columnIDs == null) { >+ throw new IllegalArgumentException( >+ "Parameter " + columnIDs + " was null."); //$NON-NLS-1$ //$NON-NLS-2$ >+ } >+ if (context == null) { >+ throw new IllegalArgumentException( >+ "Parameter " + context + " was null."); //$NON-NLS-1$ //$NON-NLS-2$ >+ } >+ >+ this.viewer = viewer; >+ this.context = context; >+ this.columnIDs = columnIDs; >+ >+ int columnCount = (columnIDs != null) ? columnIDs.length : 0; >+ >+ viewer.setCellEditors(new CellEditor[columnCount]); >+ >+ String[] properties = new String[columnCount]; >+ viewer.setColumnProperties(properties); >+ >+ for (int i = 0; i < columnCount; i++) { >+ properties[i] = columnIDs[i].toString(); >+ } >+ >+ editableElement = new EditableElement(); >+ bindings = new Binding[columnCount]; >+ >+ viewer.setCellModifier(new CellModifier()); >+ } >+ >+ /** >+ * Returns the index for the column ID. >+ * >+ * @param columnID >+ * @return column index, -1 if not found >+ */ >+ private int getColumnIndex(Object columnID) { >+ int columnIndex = -1; >+ for (int i = 0; columnIndex == -1 && i < columnIDs.length; i++) { >+ if (columnID.equals(columnIDs[i])) { >+ columnIndex = i; >+ } >+ } >+ >+ return columnIndex; >+ } >+ >+ /** >+ * Returns the column index of the provided <code>columnID</code>. >+ * >+ * @param columnID >+ * @return index >+ * @throws IllegalArgumentException >+ * if columnID is invalid. >+ */ >+ private int getValidColumnIndex(Object columnID) { >+ int index = getColumnIndex(columnID); >+ >+ if (index <= -1) { >+ throw new IllegalArgumentException( >+ "ColumnID [" + columnID + "] is an invalid columnID."); //$NON-NLS-1$//$NON-NLS-2$ >+ } >+ >+ return index; >+ } >+ >+ /** >+ * Returns the binding for the columnID. >+ * >+ * @param columnID >+ * @return binding, <code>null</code> if a binding doesn't exist because there is no editor. >+ */ >+ public Binding getBinding(Object columnID) { >+ int columnIndex = getValidColumnIndex(columnID); >+ >+ return bindings[columnIndex]; >+ } >+ >+ /** >+ * Retuns the editor for the columnID. >+ * >+ * @param columnID >+ * @return editor, <code>null</code> if an editor was not set >+ */ >+ public CellEditor getEditor(Object columnID) { >+ int columnIndex = getValidColumnIndex(columnID); >+ >+ return viewer.getCellEditors()[columnIndex]; >+ } >+ >+ /** >+ * Sets a <code>editor</code> as the editor for the provided >+ * <code>columnID</code>. >+ * >+ * @param editor >+ * if <code>null</code> removes the current editor >+ * @param columnID >+ * @param propertyType >+ * type of the attribute to be edited >+ * @param bindSpec >+ * can be <code>null</code> >+ * @return binding for the editor >+ */ >+ public Binding setEditor(CellEditor editor, Object columnID, >+ Class propertyType, BindSpec bindSpec) { >+ if (columnID == null) { >+ throw new IllegalArgumentException( >+ "Parameter " + columnID + " was null."); //$NON-NLS-1$ //$NON-NLS-2$ >+ } >+ if (propertyType == null) { >+ throw new IllegalArgumentException( >+ "Parameter " + propertyType + " was null."); //$NON-NLS-1$ //$NON-NLS-2$ >+ } >+ >+ int columnIndex = getValidColumnIndex(columnID); >+ viewer.getCellEditors()[columnIndex] = editor; >+ Binding existingBinding = bindings[columnIndex]; >+ >+ if (existingBinding != null) { >+ // Remove the existing editor. >+ existingBinding.dispose(); >+ bindings[columnIndex] = null; >+ } >+ >+ Binding result = null; >+ >+ if (editor != null) { >+ if (editor.getControl() == null) { >+ editor.create(viewer.getTable()); >+ } >+ >+ IObservable observable = context.createObservable(new Property( >+ editableElement, columnID, propertyType, Boolean.FALSE)); >+ >+ result = context.bind(editor, observable, bindSpec); >+ bindings[columnIndex] = result; >+ } >+ >+ return result; >+ } >+ >+ /** >+ * Observable for the element being edited. >+ * >+ * @since 3.2 >+ */ >+ private class EditableElement extends AbstractObservableValue { >+ private Object element; >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.internal.databinding.provisional.observable.value.AbstractObservableValue#setValue(java.lang.Object) >+ */ >+ public void setValue(Object value) { >+ final Object old = element; >+ element = value; >+ fireValueChange(new ValueDiff() { >+ public Object getNewValue() { >+ return element; >+ } >+ >+ public Object getOldValue() { >+ return old; >+ } >+ }); >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.internal.databinding.provisional.observable.value.AbstractObservableValue#doGetValue() >+ */ >+ protected Object doGetValue() { >+ return element; >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.internal.databinding.provisional.observable.value.IObservableValue#getValueType() >+ */ >+ public Object getValueType() { >+ return Object.class; >+ } >+ } >+ >+ /** >+ * <code>ICellModifier</code> implementation to be used in data binding >+ * that when requested retrieves the values from the model and sets the >+ * value in the model. The observables should not automatically update the >+ * model, the cell modifier handles this. >+ * >+ * @since 3.2 >+ */ >+ private class CellModifier implements ICellModifier { >+ public boolean canModify(Object element, String property) { >+ int columnIndex = getColumnIndex(property); >+ Assert.isTrue(columnIndex > -1); >+ >+ return bindings[columnIndex] != null; >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.Object, >+ * java.lang.String) >+ */ >+ public Object getValue(Object element, String property) { >+ if (editableElement.getValue() != element) { >+ editableElement.setValue(element); >+ } >+ >+ Binding binding = getBinding(property); >+ Assert.isNotNull(binding); >+ binding.updateTargetFromModel(); >+ >+ /* >+ * Something has to be returned so just pass the value of the >+ * editor, it's the correct value at this point. This will end up >+ * setting the value twice but the observable shouldn't fire 2 >+ * change events as the value didn't change on the second set. >+ */ >+ return getEditor(property).getValue(); >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Object, >+ * java.lang.String, java.lang.Object) >+ */ >+ public void modify(Object element, String property, Object value) { >+ element = ((Item) element).getData(); >+ Assert.isTrue(editableElement.getValue().equals(element)); >+ >+ Binding binding = getBinding(property); >+ Assert.isNotNull(binding); >+ binding.updateModelFromTarget(); >+ >+ editableElement.setValue(null); >+ } >+ } >+ >+} >Index: src/org/eclipse/jface/internal/databinding/provisional/viewers/TableViewerBindingManager.java >=================================================================== >RCS file: src/org/eclipse/jface/internal/databinding/provisional/viewers/TableViewerBindingManager.java >diff -N src/org/eclipse/jface/internal/databinding/provisional/viewers/TableViewerBindingManager.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/jface/internal/databinding/provisional/viewers/TableViewerBindingManager.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,64 @@ >+/******************************************************************************* >+ * Copyright (c) 2006 Brad Reynolds and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Brad Reynolds - initial API and implementation >+ ******************************************************************************/ >+ >+package org.eclipse.jface.internal.databinding.provisional.viewers; >+ >+import org.eclipse.jface.internal.databinding.provisional.Binding; >+import org.eclipse.jface.internal.databinding.provisional.description.TableModelDescription; >+ >+/** >+ * Manages the binding created by {@link TableViewerBindingManagerFactory} >+ * providing access to all artifacts of the binding. >+ * >+ * @since 3.2 >+ */ >+public class TableViewerBindingManager { >+ private final TableModelDescription description; >+ >+ private final Binding binding; >+ >+ private final TableViewerEditorManager editorManager; >+ >+ /** >+ * Constructs a new instance. >+ * >+ * @param description >+ * @param binding >+ * @param editorManager >+ */ >+ public TableViewerBindingManager(TableModelDescription description, >+ Binding binding, TableViewerEditorManager editorManager) { >+ this.description = description; >+ this.binding = binding; >+ this.editorManager = editorManager; >+ } >+ >+ /** >+ * @return table model description >+ */ >+ public TableModelDescription getTableModelDescription() { >+ return description; >+ } >+ >+ /** >+ * @return binding >+ */ >+ public Binding getBinding() { >+ return binding; >+ } >+ >+ /** >+ * @return table viewer editor manager >+ */ >+ public TableViewerEditorManager getTableViewerEditorManager() { >+ return editorManager; >+ } >+} >Index: src/org/eclipse/jface/internal/databinding/provisional/viewers/BooleanCellEditorObservableValue.java >=================================================================== >RCS file: src/org/eclipse/jface/internal/databinding/provisional/viewers/BooleanCellEditorObservableValue.java >diff -N src/org/eclipse/jface/internal/databinding/provisional/viewers/BooleanCellEditorObservableValue.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/jface/internal/databinding/provisional/viewers/BooleanCellEditorObservableValue.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,113 @@ >+/******************************************************************************* >+ * Copyright (c) 2006 Brad Reynolds and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Brad Reynolds - initial API and implementation >+ ******************************************************************************/ >+ >+package org.eclipse.jface.internal.databinding.provisional.viewers; >+ >+import org.eclipse.jface.internal.databinding.provisional.observable.Diffs; >+import org.eclipse.jface.internal.databinding.provisional.observable.IObservable; >+import org.eclipse.jface.internal.databinding.provisional.observable.value.AbstractObservableValue; >+import org.eclipse.jface.viewers.CellEditor; >+import org.eclipse.jface.viewers.ICellEditorListener; >+ >+/** >+ * {@link IObservable} implementation for a <code>CellEditor</code> that has >+ * no control and its value is of type boolean. >+ * >+ * @since 3.2 >+ */ >+public class BooleanCellEditorObservableValue extends AbstractObservableValue { >+ private final CellEditor editor; >+ >+ private CellEditorListener listener; >+ >+ /** >+ * Constructs a new instance. >+ * >+ * @param cellEditor >+ */ >+ public BooleanCellEditorObservableValue(final CellEditor cellEditor) { >+ if (cellEditor == null) { >+ throw new IllegalArgumentException( >+ "Parameter " + cellEditor + " was null."); //$NON-NLS-1$ //$NON-NLS-2$ >+ } >+ >+ this.editor = cellEditor; >+ >+ listener = new CellEditorListener(this); >+ editor.addListener(listener); >+ } >+ >+ public void dispose() { >+ super.dispose(); >+ >+ if (listener != null) { >+ editor.removeListener(listener); >+ listener = null; >+ } >+ } >+ >+ /** >+ * @see org.eclipse.jface.internal.databinding.provisional.observable.value.AbstractObservableValue#doGetValue() >+ */ >+ protected Object doGetValue() { >+ return editor.getValue(); >+ } >+ >+ /** >+ * @see org.eclipse.jface.internal.databinding.provisional.observable.value.IObservableValue#getValueType() >+ */ >+ public Object getValueType() { >+ return Boolean.class; >+ } >+ >+ /** >+ * @see org.eclipse.jface.internal.databinding.provisional.observable.value.AbstractObservableValue#setValue(java.lang.Object) >+ */ >+ public void setValue(Object value) { >+ Boolean oldValue = (Boolean) editor.getValue(); >+ Boolean newValue = (value == null) ? Boolean.FALSE : (Boolean) value; >+ >+ if (!newValue.equals(oldValue)) { >+ editor.setValue(newValue); // does not fire an event >+ fireValueChange(Diffs.createValueDiff(oldValue, newValue)); >+ } >+ } >+ >+ private static class CellEditorListener implements ICellEditorListener { >+ private final BooleanCellEditorObservableValue observable; >+ >+ private CellEditorListener(BooleanCellEditorObservableValue observable) { >+ this.observable = observable; >+ } >+ >+ public void applyEditorValue() { >+ /* >+ * When applyEditorValue() is invoked the value has changed. Because >+ * of the nature of a boolean we know the old is the opposite of the >+ * current value. >+ */ >+ boolean value = ((Boolean) observable.editor.getValue()) >+ .booleanValue(); >+ >+ observable.fireValueChange(Diffs.createValueDiff(Boolean >+ .valueOf(!value), Boolean.valueOf(value))); >+ } >+ >+ public void cancelEditor() { >+ // not needed >+ } >+ >+ public void editorValueChanged(boolean oldValidState, >+ boolean newValidState) { >+ // not needed >+ } >+ } >+} >#P org.eclipse.jface.tests.databinding >Index: src/org/eclipse/jface/tests/internal/databinding/provisional/viewers/ViewersObservableFactoryTest.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jface.tests.databinding/src/org/eclipse/jface/tests/internal/databinding/provisional/viewers/ViewersObservableFactoryTest.java,v >retrieving revision 1.1 >diff -u -r1.1 ViewersObservableFactoryTest.java >--- src/org/eclipse/jface/tests/internal/databinding/provisional/viewers/ViewersObservableFactoryTest.java 16 Jun 2006 03:05:07 -0000 1.1 >+++ src/org/eclipse/jface/tests/internal/databinding/provisional/viewers/ViewersObservableFactoryTest.java 25 Jun 2006 22:51:55 -0000 >@@ -1,5 +1,5 @@ > /******************************************************************************* >- * Copyright (c) 2006 Brad Reynolds. >+ * Copyright (c) 2006 Brad Reynolds and others. > * All rights reserved. This program and the accompanying materials > * are made available under the terms of the Eclipse Public License v1.0 > * which accompanies this distribution, and is available at >@@ -18,17 +18,19 @@ > import org.eclipse.jface.internal.databinding.provisional.observable.IObservable; > import org.eclipse.jface.internal.databinding.provisional.viewers.ViewersObservableFactory; > import org.eclipse.jface.internal.databinding.provisional.viewers.ViewersProperties; >+import org.eclipse.jface.viewers.CheckboxCellEditor; > import org.eclipse.jface.viewers.ISelection; > import org.eclipse.jface.viewers.ISelectionChangedListener; > import org.eclipse.jface.viewers.ISelectionProvider; > import org.eclipse.jface.viewers.ListViewer; > import org.eclipse.jface.viewers.TableViewer; >+import org.eclipse.jface.viewers.TextCellEditor; > import org.eclipse.swt.widgets.Shell; > > /** > * Tests for ViewerObservableFactory. > * >- * @since 1.1 >+ * @since 3.2 > */ > public class ViewersObservableFactoryTest extends TestCase { > private Shell shell; >@@ -72,6 +74,27 @@ > assertTrue(observable instanceof SelectionProviderSingleSelectionObservableValue); > } > >+ public void testGetObservableForTextCellEditor() throws Exception { >+ TextCellEditor editor = new TextCellEditor(shell); >+ IObservable observable = factory.createObservable(editor); >+ assertNotNull(observable); >+ } >+ >+ public void testThrowsIAEIfTextCellEditorControlIsNotCreated() throws Exception { >+ TextCellEditor editor = new TextCellEditor(); >+ try { >+ factory.createObservable(editor); >+ fail("exception should have been thrown"); >+ } catch (IllegalArgumentException e) { >+ } >+ } >+ >+ public void testGetObservableForCheckboxCellEditor() throws Exception { >+ CheckboxCellEditor editor = new CheckboxCellEditor(); >+ IObservable observable = factory.createObservable(editor); >+ assertNotNull(observable); >+ } >+ > /** > * Empty stub to satisfy the requirement that we have a type of ISelectionProvider that is not a viewer. > */ >Index: src/org/eclipse/jface/tests/databinding/BindingTestSuite.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jface.tests.databinding/src/org/eclipse/jface/tests/databinding/BindingTestSuite.java,v >retrieving revision 1.18 >diff -u -r1.18 BindingTestSuite.java >--- src/org/eclipse/jface/tests/databinding/BindingTestSuite.java 19 Jun 2006 14:26:51 -0000 1.18 >+++ src/org/eclipse/jface/tests/databinding/BindingTestSuite.java 25 Jun 2006 22:51:55 -0000 >@@ -8,6 +8,7 @@ > * Contributors: > * IBM Corporation - initial API and implementation > * Brad Reynolds - bug 137877 >+ * Brad Reynolds - bug 144260 > *******************************************************************************/ > package org.eclipse.jface.tests.databinding; > >@@ -32,6 +33,9 @@ > import org.eclipse.jface.tests.internal.databinding.internal.viewers.SVOCWLTest; > import org.eclipse.jface.tests.internal.databinding.internal.viewers.SelectionProviderSingleSelectionObservableValueTest; > import org.eclipse.jface.tests.internal.databinding.provisional.observable.AbstractObservableTest; >+import org.eclipse.jface.tests.internal.databinding.provisional.viewers.BooleanCellEditorObservableValueTest; >+import org.eclipse.jface.tests.internal.databinding.provisional.viewers.TableViewerBindingManagerFactoryTest; >+import org.eclipse.jface.tests.internal.databinding.provisional.viewers.TableViewerEditorManagerTest; > import org.eclipse.jface.tests.internal.databinding.provisional.viewers.ViewersObservableFactoryTest; > > public class BindingTestSuite extends TestSuite { >@@ -72,6 +76,10 @@ > addTestSuite(AbstractObservableTest.class); > addTestSuite(MocksTest.class); > addTestSuite(PersonTests.class); >+ addTestSuite(BooleanCellEditorObservableValueTest.class); >+ addTestSuite(TableViewerBindingManagerFactoryTest.class); >+ addTestSuite(TableViewerEditorManagerTest.class); >+ addTestSuite(ViewersObservableFactoryTest.class); > } > > /** >Index: src/org/eclipse/jface/tests/internal/databinding/provisional/viewers/TableViewerEditorManagerTest.java >=================================================================== >RCS file: src/org/eclipse/jface/tests/internal/databinding/provisional/viewers/TableViewerEditorManagerTest.java >diff -N src/org/eclipse/jface/tests/internal/databinding/provisional/viewers/TableViewerEditorManagerTest.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/jface/tests/internal/databinding/provisional/viewers/TableViewerEditorManagerTest.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,282 @@ >+/******************************************************************************* >+ * Copyright (c) 2006 Brad Reynolds and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Brad Reynolds - initial API and implementation >+ ******************************************************************************/ >+ >+package org.eclipse.jface.tests.internal.databinding.provisional.viewers; >+ >+import junit.framework.TestCase; >+ >+import org.eclipse.jface.examples.databinding.model.Adventure; >+import org.eclipse.jface.examples.databinding.model.Catalog; >+import org.eclipse.jface.examples.databinding.model.Category; >+import org.eclipse.jface.examples.databinding.model.Lodging; >+import org.eclipse.jface.examples.databinding.model.SampleData; >+import org.eclipse.jface.internal.databinding.provisional.Binding; >+import org.eclipse.jface.internal.databinding.provisional.DataBindingContext; >+import org.eclipse.jface.internal.databinding.provisional.description.Property; >+import org.eclipse.jface.internal.databinding.provisional.description.TableModelDescription; >+import org.eclipse.jface.internal.databinding.provisional.viewers.TableViewerEditorManager; >+import org.eclipse.jface.viewers.CellEditor; >+import org.eclipse.jface.viewers.CheckboxCellEditor; >+import org.eclipse.jface.viewers.ICellModifier; >+import org.eclipse.jface.viewers.TableViewer; >+import org.eclipse.jface.viewers.TextCellEditor; >+import org.eclipse.swt.SWT; >+import org.eclipse.swt.layout.RowLayout; >+import org.eclipse.swt.widgets.Shell; >+ >+/** >+ * @since 3.2 >+ */ >+public class TableViewerEditorManagerTest extends TestCase { >+ private Shell shell; >+ >+ private TableViewer viewer; >+ >+ private DataBindingContext context; >+ >+ private TableModelDescription description; >+ >+ private TableViewerEditorManager manager; >+ >+ private Catalog model; >+ >+ protected void setUp() throws Exception { >+ shell = new Shell(); >+ shell.setLayout(new RowLayout()); >+ >+ viewer = new TableViewer(shell, SWT.NONE); >+ context = SampleData.getDatabindingContext(shell); >+ model = SampleData.CATALOG_2005; >+ >+ description = new TableModelDescription(new Property(model, >+ "lodgings", Lodging.class, Boolean.TRUE), >+ new String[] { "name", "description" }); >+ >+ context.bind(viewer, description, null); >+ >+ manager = new TableViewerEditorManager(viewer, description.getColumnIDs(), context); >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see junit.framework.TestCase#tearDown() >+ */ >+ protected void tearDown() throws Exception { >+ if (shell != null && !shell.isDisposed()) { >+ shell.dispose(); >+ shell = null; >+ } >+ } >+ >+ public void testCreateControlIfItDoesntExist() throws Exception { >+ TextCellEditor editor = new TextCellEditor(); >+ >+ manager.setEditor(editor, "name", String.class, null); >+ assertNotNull(viewer.getCellEditors()[0]); >+ } >+ >+ public void testSetupViewerForEditing() throws Exception { >+ assertNotNull(viewer.getCellModifier()); >+ >+ assertNotNull(viewer.getColumnProperties()); >+ assertEquals(2, viewer.getColumnProperties().length); >+ assertNotNull(viewer.getColumnProperties()[0]); >+ assertEquals("name", viewer.getColumnProperties()[0]); >+ >+ assertNotNull(viewer.getCellEditors()); >+ assertEquals(2, viewer.getCellEditors().length); >+ assertNull(viewer.getCellEditors()[0]); >+ >+ TextCellEditor editor = new TextCellEditor(); >+ manager.setEditor(editor, "name", String.class, null); >+ >+ assertEquals(editor, viewer.getCellEditors()[0]); >+ } >+ >+ public void testSetValueInTextCellEditorOnEdit() throws Exception { >+ TextCellEditor editor = new TextCellEditor(); >+ manager.setEditor(editor, "name", String.class, null); >+ >+ viewer.editElement(model.getLodgings()[0], 0); >+ >+ Lodging lodging = model.getLodgings()[0]; >+ assertEquals(lodging.getName(), editor.getValue()); >+ } >+ >+ public void testUpdateModelFromTextCellEditorAfterEdit() throws Exception { >+ TextCellEditor editor = new TextCellEditor(); >+ manager.setEditor(editor, "name", String.class, null); >+ >+ Lodging lodging = model.getLodgings()[0]; >+ viewer.editElement(lodging, 0); >+ >+ assertTrue(editor.isActivated()); >+ >+ String newValue = lodging.getName() + " new"; >+ editor.setValue(newValue); >+ >+ editor.getControl().notifyListeners(SWT.FocusOut, null); >+ >+ assertFalse(editor.isActivated()); >+ assertEquals(newValue, lodging.getName()); >+ } >+ >+ public void testToggleCheckboxCellEditorValueOnEdit() throws Exception { >+ Category model = SampleData.WINTER_CATEGORY; >+ description = new TableModelDescription(new Property(model, >+ "adventures", Adventure.class, Boolean.TRUE), >+ new String[] { "petsAllowed" }); >+ >+ context.bind(viewer, description, null); >+ >+ manager = new TableViewerEditorManager(viewer, description.getColumnIDs(), context); >+ >+ CheckboxCellEditor editor = new CheckboxCellEditor(); >+ manager.setEditor(editor, "petsAllowed", boolean.class, null); >+ >+ Adventure adventure = model.getAdventures()[0]; >+ boolean old = adventure.isPetsAllowed(); >+ >+ viewer.editElement(adventure, 0); >+ >+ assertEquals(!old, adventure.isPetsAllowed()); >+ } >+ >+ public void testRemoveEditor() throws Exception { >+ TextCellEditor editor = new TextCellEditor(); >+ Binding binding = manager.setEditor(editor, "name", String.class, null); >+ assertNotNull(binding); >+ assertFalse(binding.isDisposed()); >+ >+ ICellModifier cellModifier = viewer.getCellModifier(); >+ Lodging lodging = model.getLodgings()[0]; >+ assertTrue(cellModifier.canModify(lodging, "name")); >+ >+ manager.setEditor(null, "name", String.class, null); >+ >+ assertTrue(binding.isDisposed()); >+ assertFalse(cellModifier.canModify(lodging, "name")); >+ } >+ >+ public void testSetEditorForExistingEditableColumn() throws Exception { >+ TextCellEditor firstEditor = new TextCellEditor(); >+ Binding firstBinding = manager.setEditor(firstEditor, "name", String.class, null); >+ assertFalse(firstBinding.isDisposed()); >+ >+ TextCellEditor secondEditor = new TextCellEditor(); >+ Binding secondBinding = manager.setEditor(secondEditor, "name", String.class, null); >+ assertTrue(firstBinding.isDisposed()); >+ assertFalse(secondBinding.isDisposed()); >+ >+ ICellModifier cellModifier = viewer.getCellModifier(); >+ Lodging lodging = model.getLodgings()[0]; >+ assertTrue(cellModifier.canModify(lodging, "name")); >+ } >+ >+ public void testSetEditorForInvalidColumnID() throws Exception { >+ try { >+ TextCellEditor editor = new TextCellEditor(); >+ manager.setEditor(editor, "bogus ID", String.class, null); >+ fail("exception should have been thrown"); >+ } catch (IllegalArgumentException e) { >+ } >+ } >+ >+ public void testSetNullColumnID() throws Exception { >+ try { >+ TextCellEditor editor = new TextCellEditor(); >+ manager.setEditor(editor, null, String.class, null); >+ } catch (IllegalArgumentException e) { >+ } >+ } >+ >+ public void testSetNullPropertyType() throws Exception { >+ try { >+ TextCellEditor editor = new TextCellEditor(); >+ manager.setEditor(editor, "name", null, null); >+ } catch (IllegalArgumentException e) { >+ } >+ } >+ >+ public void testCellModifierOnlyAllowsEditingForColumnsWithAnEditor() throws Exception { >+ ICellModifier modifier = viewer.getCellModifier(); >+ Lodging lodging = model.getLodgings()[0]; >+ >+ TextCellEditor editor = new TextCellEditor(); >+ manager.setEditor(editor, "name", String.class, null); >+ >+ assertTrue(modifier.canModify(lodging, "name")); >+ assertFalse(modifier.canModify(lodging, "description")); >+ } >+ >+ public void testConstructWithNullTableViewer() throws Exception { >+ try { >+ manager = new TableViewerEditorManager(null, description.getColumnIDs(), context); >+ fail("exception should have been thrown"); >+ } catch (IllegalArgumentException e) { >+ } >+ } >+ >+ public void testConstructWithNullTableModelDescription() throws Exception { >+ try { >+ manager = new TableViewerEditorManager(viewer, null, context); >+ fail("exception should have been thrown"); >+ } catch (IllegalArgumentException e) { >+ } >+ } >+ >+ public void testConstructWithNullDataBindingContext() throws Exception { >+ try { >+ manager = new TableViewerEditorManager(viewer, description.getColumnIDs(), null); >+ fail("exception should have been thrown"); >+ } catch (IllegalArgumentException e) { >+ } >+ } >+ >+ public void testGetEditor() throws Exception { >+ assertNull(manager.getEditor("name")); >+ >+ TextCellEditor editor = new TextCellEditor(); >+ manager.setEditor(editor, "name", String.class, null); >+ >+ CellEditor nameEditor = manager.getEditor("name"); >+ assertNotNull(nameEditor); >+ assertEquals(editor, nameEditor); >+ } >+ >+ public void testGetInvalidEditor() throws Exception { >+ try { >+ manager.getEditor("column ID that does not exist"); >+ fail("exception should have been thrown"); >+ } catch (IllegalArgumentException e) { >+ } >+ } >+ >+ public void testGetBinding() throws Exception { >+ assertNull(manager.getBinding("name")); >+ >+ TextCellEditor editor = new TextCellEditor(); >+ Binding binding = manager.setEditor(editor, "name", String.class, null); >+ >+ Binding nameBinding = manager.getBinding("name"); >+ assertNotNull(nameBinding); >+ assertEquals(binding, nameBinding); >+ } >+ >+ public void testGetInvalidBinding() throws Exception { >+ try { >+ manager.getBinding("column ID that does not exist"); >+ fail("exception should have been thrown"); >+ } catch (IllegalArgumentException e) { >+ } >+ } >+} >Index: src/org/eclipse/jface/tests/internal/databinding/provisional/viewers/BooleanCellEditorObservableValueTest.java >=================================================================== >RCS file: src/org/eclipse/jface/tests/internal/databinding/provisional/viewers/BooleanCellEditorObservableValueTest.java >diff -N src/org/eclipse/jface/tests/internal/databinding/provisional/viewers/BooleanCellEditorObservableValueTest.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/jface/tests/internal/databinding/provisional/viewers/BooleanCellEditorObservableValueTest.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,162 @@ >+/******************************************************************************* >+ * Copyright (c) 2006 Brad Reynolds and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Brad Reynolds - initial API and implementation >+ ******************************************************************************/ >+ >+package org.eclipse.jface.tests.internal.databinding.provisional.viewers; >+ >+import junit.framework.TestCase; >+ >+import org.eclipse.jface.internal.databinding.provisional.observable.value.IObservableValue; >+import org.eclipse.jface.internal.databinding.provisional.observable.value.IValueChangeListener; >+import org.eclipse.jface.internal.databinding.provisional.observable.value.ValueDiff; >+import org.eclipse.jface.internal.databinding.provisional.viewers.BooleanCellEditorObservableValue; >+import org.eclipse.jface.viewers.CheckboxCellEditor; >+ >+/** >+ * @since 3.2 >+ */ >+public class BooleanCellEditorObservableValueTest extends TestCase { >+ private BooleanCellEditorObservableValue observable; >+ >+ private MyCheckboxCellEditor cellEditor; >+ >+ protected void setUp() throws Exception { >+ cellEditor = new MyCheckboxCellEditor(); >+ cellEditor.setValue(Boolean.FALSE); >+ observable = new BooleanCellEditorObservableValue(cellEditor); >+ } >+ >+ private class MyCheckboxCellEditor extends CheckboxCellEditor { >+ protected void setValueValid(boolean valid) { >+ // TODO Auto-generated method stub >+ super.setValueValid(valid); >+ } >+ } >+ >+ public void testConstructionIllegalArgumentExceptions() throws Exception { >+ try { >+ new BooleanCellEditorObservableValue(null); >+ fail("Exception should have been thrown."); >+ } catch (IllegalArgumentException e) { >+ } >+ } >+ >+ public void testGetValueType() throws Exception { >+ assertEquals(Boolean.class, observable.getValueType()); >+ } >+ >+ public void testGetValue() throws Exception { >+ cellEditor.setValue(Boolean.TRUE); >+ assertEquals(Boolean.TRUE, observable.getValue()); >+ cellEditor.setValue(Boolean.FALSE); >+ assertEquals(Boolean.FALSE, observable.getValue()); >+ } >+ >+ public void testSetFalseWhenCellEditorValueIsNull() throws Exception { >+ cellEditor.setValueValid(false); >+ assertNull(cellEditor.getValue()); >+ observable.setValue(Boolean.FALSE); >+ Boolean editorValue = (Boolean) cellEditor.getValue(); >+ assertNotNull(editorValue); >+ assertEquals(Boolean.FALSE, editorValue); >+ } >+ >+ public void testObservableSetValueSame() throws Exception { >+ ValueChangeCounter listener = new ValueChangeCounter(); >+ observable.addValueChangeListener(listener); >+ assertEquals(0, listener.count); >+ assertEquals(Boolean.FALSE, observable.getValue()); >+ assertEquals(Boolean.FALSE, cellEditor.getValue()); >+ >+ observable.setValue(Boolean.FALSE); >+ >+ assertEquals(0, listener.count); >+ assertEquals(Boolean.FALSE, observable.getValue()); >+ assertEquals(Boolean.FALSE, cellEditor.getValue()); >+ } >+ >+ public void testObservableSetValueDifferent() throws Exception { >+ ValueChangeCounter listener = new ValueChangeCounter(); >+ observable.addValueChangeListener(listener); >+ assertEquals(0, listener.count); >+ assertEquals(Boolean.FALSE, observable.getValue()); >+ assertEquals(Boolean.FALSE, cellEditor.getValue()); >+ >+ observable.setValue(Boolean.TRUE); >+ >+ assertEquals(1, listener.count); >+ assertEquals(Boolean.TRUE, observable.getValue()); >+ assertEquals(Boolean.TRUE, cellEditor.getValue()); >+ >+ assertEquals(observable, listener.source); >+ assertEquals(Boolean.FALSE, listener.diff.getOldValue()); >+ assertEquals(Boolean.TRUE, listener.diff.getNewValue()); >+ } >+ >+ public void testCellEditorChangeValue() throws Exception { >+ ValueChangeCounter listener = new ValueChangeCounter(); >+ observable.addValueChangeListener(listener); >+ assertEquals(0, listener.count); >+ assertEquals(Boolean.FALSE, observable.getValue()); >+ assertEquals(Boolean.FALSE, cellEditor.getValue()); >+ >+ cellEditor.activate(); >+ >+ assertEquals(1, listener.count); >+ assertEquals(Boolean.TRUE, observable.getValue()); >+ assertEquals(Boolean.TRUE, cellEditor.getValue()); >+ >+ assertEquals(observable, listener.source); >+ assertEquals(Boolean.FALSE, listener.diff.getOldValue()); >+ assertEquals(Boolean.TRUE, listener.diff.getNewValue()); >+ } >+ >+ public void testCellEditorSetValueDifferent() throws Exception { >+ ValueChangeCounter listener = new ValueChangeCounter(); >+ observable.addValueChangeListener(listener); >+ assertEquals(0, listener.count); >+ assertEquals(Boolean.FALSE, observable.getValue()); >+ assertEquals(Boolean.FALSE, cellEditor.getValue()); >+ >+ cellEditor.setValue(Boolean.TRUE); >+ >+ assertEquals(0, listener.count); >+ assertEquals(Boolean.TRUE, observable.getValue()); >+ assertEquals(Boolean.TRUE, cellEditor.getValue()); >+ } >+ >+ public void testSetObservableNullValue() throws Exception { >+ ValueChangeCounter listener = new ValueChangeCounter(); >+ observable.addValueChangeListener(listener); >+ assertEquals(0, listener.count); >+ assertEquals(Boolean.FALSE, observable.getValue()); >+ assertEquals(Boolean.FALSE, cellEditor.getValue()); >+ >+ observable.setValue(null); >+ >+ assertEquals(0, listener.count); >+ assertEquals(Boolean.FALSE, observable.getValue()); >+ assertEquals(Boolean.FALSE, cellEditor.getValue()); >+ } >+ >+ private class ValueChangeCounter implements IValueChangeListener { >+ private int count; >+ >+ private IObservableValue source; >+ >+ private ValueDiff diff; >+ >+ public void handleValueChange(IObservableValue source, ValueDiff diff) { >+ count++; >+ this.source = source; >+ this.diff = diff; >+ } >+ } >+} >Index: src/org/eclipse/jface/tests/internal/databinding/provisional/viewers/TableViewerBindingManagerFactoryTest.java >=================================================================== >RCS file: src/org/eclipse/jface/tests/internal/databinding/provisional/viewers/TableViewerBindingManagerFactoryTest.java >diff -N src/org/eclipse/jface/tests/internal/databinding/provisional/viewers/TableViewerBindingManagerFactoryTest.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/jface/tests/internal/databinding/provisional/viewers/TableViewerBindingManagerFactoryTest.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,191 @@ >+/******************************************************************************* >+ * Copyright (c) 2006 Brad Reynolds and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Brad Reynolds - initial API and implementation >+ ******************************************************************************/ >+ >+package org.eclipse.jface.tests.internal.databinding.provisional.viewers; >+ >+import junit.framework.TestCase; >+ >+import org.eclipse.jface.examples.databinding.model.Catalog; >+import org.eclipse.jface.examples.databinding.model.Lodging; >+import org.eclipse.jface.examples.databinding.model.SampleData; >+import org.eclipse.jface.internal.databinding.provisional.Binding; >+import org.eclipse.jface.internal.databinding.provisional.DataBindingContext; >+import org.eclipse.jface.internal.databinding.provisional.description.Property; >+import org.eclipse.jface.internal.databinding.provisional.description.TableModelDescription; >+import org.eclipse.jface.internal.databinding.provisional.observable.list.WritableList; >+import org.eclipse.jface.internal.databinding.provisional.viewers.TableViewerBindingManager; >+import org.eclipse.jface.internal.databinding.provisional.viewers.TableViewerBindingManagerFactory; >+import org.eclipse.jface.internal.databinding.provisional.viewers.TableViewerEditorManager; >+import org.eclipse.jface.viewers.TableViewer; >+import org.eclipse.jface.viewers.TextCellEditor; >+import org.eclipse.swt.SWT; >+import org.eclipse.swt.layout.RowLayout; >+import org.eclipse.swt.widgets.Shell; >+import org.eclipse.swt.widgets.Table; >+import org.eclipse.swt.widgets.TableColumn; >+import org.eclipse.swt.widgets.TableItem; >+ >+/** >+ * @since 3.2 >+ * >+ */ >+public class TableViewerBindingManagerFactoryTest extends TestCase { >+ private Shell shell; >+ >+ private TableViewer viewer; >+ >+ private DataBindingContext context; >+ >+ private Catalog model; >+ >+ private TableViewerBindingManagerFactory factory; >+ >+ /* (non-Javadoc) >+ * @see junit.framework.TestCase#setUp() >+ */ >+ protected void setUp() throws Exception { >+ shell = new Shell(); >+ shell.setLayout(new RowLayout()); >+ >+ viewer = new TableViewer(shell, SWT.NONE); >+ context = SampleData.getDatabindingContext(shell); >+ model = SampleData.CATALOG_2005; >+ factory = new TableViewerBindingManagerFactory(); >+ } >+ >+ /* (non-Javadoc) >+ * @see junit.framework.TestCase#tearDown() >+ */ >+ protected void tearDown() throws Exception { >+ if (shell != null && !shell.isDisposed()) { >+ shell.dispose(); >+ shell = null; >+ } >+ } >+ >+ public void testAddAndGetFactoryColumnIDs() throws Exception { >+ TableViewerBindingManagerFactory factory = new TableViewerBindingManagerFactory(); >+ Object[] columnIDs = factory.getColumnIDs(); >+ assertNotNull(columnIDs); >+ assertEquals(0, columnIDs.length); >+ >+ factory.addColumn("name"); >+ columnIDs = factory.getColumnIDs(); >+ assertNotNull(columnIDs); >+ assertEquals(1, columnIDs.length); >+ assertEquals("name", columnIDs[0]); >+ >+ factory.addColumn("description"); >+ columnIDs = factory.getColumnIDs(); >+ assertEquals(2, columnIDs.length); >+ assertEquals("name", columnIDs[0]); >+ assertEquals("description", columnIDs[1]); >+ >+ assertEquals("name", factory.getColumnID(0)); >+ assertEquals("description", factory.getColumnID(1)); >+ } >+ >+ public void testTableModelDescriptionConstruction() throws Exception { >+ TableViewerBindingManagerFactory factory = new TableViewerBindingManagerFactory(); >+ factory.addColumn("name"); >+ factory.addColumn("description"); >+ >+ TableViewerBindingManager tvb = factory.bind(viewer, model, "lodgings", Lodging.class, context, null); >+ TableModelDescription description = tvb.getTableModelDescription(); >+ Object[] columnIDs = description.getColumnIDs(); >+ assertNotNull(columnIDs); >+ assertEquals(2, columnIDs.length); >+ assertEquals("name", columnIDs[0]); >+ assertEquals("description", columnIDs[1]); >+ >+ Property property = description.getCollectionProperty(); >+ assertEquals(model, property.getObject()); >+ assertEquals("lodgings", property.getPropertyID()); >+ assertEquals(Lodging.class, property.getPropertyType()); >+ } >+ >+ public void testBindProperty() throws Exception { >+ Table table = viewer.getTable(); >+ for (int i = 0; i < 2; i++) { >+ new TableColumn(table, SWT.NONE); >+ } >+ >+ factory.addColumn("name"); >+ factory.addColumn("description"); >+ >+ TableViewerBindingManager tvb = factory.bind(viewer, model, "lodgings", Lodging.class, context, null); >+ Binding binding = tvb.getBinding(); >+ assertNotNull(binding); >+ >+ //Assert the binding worked >+ TableItem item = table.getItem(0); >+ Lodging lodging = model.getLodgings()[0]; >+ assertEquals(lodging, item.getData()); >+ assertEquals(lodging.getName(), item.getText(0)); >+ assertEquals(lodging.getDescription(), item.getText(1)); >+ } >+ >+ public void testAddEditableColumn() throws Exception { >+ TextCellEditor nameEditor = new TextCellEditor(); >+ >+ factory.addEditableColumn("name", nameEditor, String.class, null); >+ factory.addColumn("description"); >+ >+ TableViewerBindingManager tvb = factory.bind(viewer, model, "lodgings", Lodging.class, context, null); >+ TableViewerEditorManager editorManager = tvb.getTableViewerEditorManager(); >+ assertNotNull(editorManager); >+ assertEquals(nameEditor, editorManager.getEditor("name")); >+ >+ assertNull(editorManager.getEditor("description")); >+ } >+ >+ public void testAddEditableColumnWithNullEditor() throws Exception { >+ try { >+ factory.addEditableColumn("name", null, String.class, null); >+ fail("exception should have been thrown"); >+ } catch (IllegalArgumentException e) { >+ } >+ } >+ >+ public void testAddEditableColumnWithNullPropertyType() throws Exception { >+ try { >+ factory.addEditableColumn("name", new TextCellEditor(), null, null); >+ fail("exception should have been thrown"); >+ } catch (IllegalArgumentException e) { >+ } >+ } >+ >+ public void testBindObservableList() throws Exception { >+ WritableList list = new WritableList(Lodging.class); >+ Lodging lodging = new Lodging(); >+ lodging.setName("lodging name"); >+ list.add(lodging); >+ >+ factory.addColumn("name"); >+ >+ TableViewerBindingManager tvb = factory.bind(viewer, list, context, null); >+ assertNotNull(tvb.getBinding()); >+ >+ //Assert the binding worked >+ Table table = viewer.getTable(); >+ TableItem item = table.getItem(0); >+ assertEquals(lodging, item.getData()); >+ assertEquals(lodging.getName(), item.getText(0)); >+ } >+ >+ public void testAddNullColumnID() throws Exception { >+ try { >+ factory.addColumn(null); >+ fail("exception should have been thrown"); >+ } catch (IllegalArgumentException e) { >+ } >+ } >+}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 144260
:
42873
|
43411
|
45272
|
45438
|
82061
|
82072