0

I have Part containing a TreeViewer with Nodes and each Node has a name property. I have a text field to edit the name of the currently selected node. I have bound the current selection and this text field as below:

IViewerObservableValue observedElementSingleSelection = ViewersObservables
            .observeSingleSelection(treeViewer);

IObservableValue detailValue = PojoProperties.value(Node.PROPERTY_NAME, String.class)
            .observeDetail(observedElementSingleSelection);
ISWTObservableValue observableNameText = WidgetProperties.text(SWT.Modify)
            .observe(nameText);
dataBindingContext.bindValue(observableNameText, detailValue);

Now I would like to mark the part with this treeViewer as dirty whenever the user edits the name of any Node.

When this Part opens the Nodes are retrieved from the database and the tree is created. If I add a ModifyListener to the text field to mark the Part dirty then on initial loading the Part is marked dirty, which I do not want. Is there any way to mark the Part dirty only when the user edits the name of any Node but not at the time of initial loading?

I have tried to set UpdateStrategyso that the data binding is one-way i.e. only from view to model, but then on selection the Node name does not appear in the text field.

I have tried with KeyListener, but then I have to filter all non-printable keys to mark the Part as dirty. Is there any better solution?

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
Jehan Zeb
  • 155
  • 12
  • I suggest you to first load the model and setup bindings/listeners/etc. afterwards. Then it should not get dirty. – Alex K. Jul 13 '15 at 09:30

1 Answers1

0

You could add the ModifyListener after the load has been done, or remove the modify listener while the load is being done, or set a flag during the load that the modify listener can test and not set the part to be dirty.

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • My flow in `@PostConstruct` method is like this: 1. Create TreeViewer and populate it with Nodes. 2. Create text field. 3. Setup the databinding. 4. Add the modify listener. But the data binding actually works on selection of a node. So when the Part is opened the root node is by default selected and the name of the root node is shown in the text field. This marks the Part as dirty. By the way is there a way to set MDirtyable to true directly with data binding? – Jehan Zeb Jul 13 '15 at 09:42
  • You may be able to use an `IChangeListener` on the model observable value. – greg-449 Jul 13 '15 at 10:20