Cleaned up / renamed.
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / view / propertysheetsupport / PropertySheetValueEditingSupport.java
1 package eu.etaxonomy.taxeditor.view.propertysheetsupport;
2
3 import java.util.TreeMap;
4
5 import org.eclipse.core.databinding.DataBindingContext;
6 import org.eclipse.core.databinding.UpdateValueStrategy;
7 import org.eclipse.core.databinding.observable.value.IObservableValue;
8 import org.eclipse.core.runtime.Assert;
9 import org.eclipse.jface.databinding.swt.SWTObservables;
10 import org.eclipse.jface.viewers.CellEditor;
11 import org.eclipse.jface.viewers.EditingSupport;
12 import org.eclipse.jface.viewers.TextCellEditor;
13 import org.eclipse.jface.viewers.ViewerCell;
14 import org.eclipse.swt.SWT;
15
16 import eu.etaxonomy.taxeditor.model.PropertySheetNode;
17 import eu.etaxonomy.taxeditor.view.PropertySheetViewer;
18
19 public class PropertySheetValueEditingSupport extends EditingSupport {
20
21 private TreeMap<String,CellEditor> cellEditors;
22 private PropertySheetViewer viewer;
23 private DataBindingContext bindingContext;
24
25 public PropertySheetValueEditingSupport(PropertySheetViewer viewer) {
26 super(viewer);
27 this.viewer = viewer;
28 this.bindingContext = viewer.getBindingContext();
29 this.cellEditors = new TreeMap<String, CellEditor>();
30 }
31
32 protected boolean canEdit(Object element) {
33 return ((PropertySheetNode) element).isEditable();
34 }
35
36 protected CellEditor getCellEditor(Object element) {
37 String elementString = element.toString();
38 if (cellEditors.get(elementString) == null)
39 cellEditors.put(elementString, new TextCellEditor(viewer.getTree()));
40 return cellEditors.get(elementString);
41 }
42
43 protected Object getValue(Object element) {
44 return (PropertySheetNode) element;
45 }
46
47 protected void setValue(Object element, Object value) {
48 ((PropertySheetNode) element).setPropertyValue(value.toString());
49 this.getViewer().update(element, null);
50 }
51
52 protected void initializeCellEditorValue(CellEditor cellEditor, ViewerCell cell) {
53 PropertySheetNode node = (PropertySheetNode) getValue(cell.getElement());
54
55 // an empty string instead of a null makes the field editable
56 cellEditor.setValue(node.getPropertyValue() == null ? "" : node.getPropertyValue());
57
58 // if node element is observing a value, bind it to its TreeItem
59 IObservableValue observeNodeValue = node.getObserveValue();
60 if (observeNodeValue != null) {
61
62 Assert.isNotNull(bindingContext,
63 "Editing support: PropertySheetViewer's binding context must be explicitly set.");
64
65 IObservableValue observeCellValue = SWTObservables.observeText(cellEditor.getControl(), SWT.Modify);
66 bindingContext.bindValue(observeNodeValue, observeCellValue,
67 new UpdateValueStrategy(UpdateValueStrategy.POLICY_CONVERT), null);
68
69 }
70 }
71 }