Search code examples
valuechangelistenereclipse-scoutdirty-data

Scout Eclipse check for dirty fields


When you try to close Swing application and you change same value in field, application ask you if you want to save changes.

My first question is why RAP application doesn't ask same question ?

Second and more important question is how to force validation of fields change and how to manipulate this.

for example :

I have table with rows and some fields below table. If I click on row some value is entered in fields. I can change this value and click button save.

I would like to force change validation on row click. So if changes ware applied and if I click on row, application should warn me that some changes are not saved. But I should be able to manipulate what is change and what is not. For example if table on row click fill some data if fields this is not change, but if I entered value is same fields this is change.

I discovered method

checkSaveNeeded();

But it does nothing. (if I change values or not)

I see that every field has mathod

@Override
public final void checkSaveNeeded() {
    if (isInitialized()) {
      try {
        propertySupport.setPropertyBool(PROP_SAVE_NEEDED, m_touched || execIsSaveNeeded());
      }
      catch (ProcessingException e) {
      SERVICES.getService(IExceptionHandlerService.class).handleException(e);
      }
    }
  }

so I should manipulate changes throw m_touched ?

How is this handled in Scout ?


ADD

I am looking for function that check for dirty fields and pops up message dialog, same as when closing form, and way to set fields dirty or not.

I look here and here, but all it describes is how values is stored for popup messages and dot how to fire this messages (validation).


Solution

  • My first question is why RAP application doesn't ask same question ?

    I am not sure to know which message box you mean but it should be the case.


    There are several questions about unsaved changes and form lifecycle in the Eclipse Scout Forum. I think you can find them with Google.

    I have also taken the time to start to document it in the Eclipse Wiki:

    I think you should implement execIsSaveNeeded() in the corresponding field. The default implementation in AbstractTableField uses the state of the rows, but you can imagine the logic you want.

    @Order(10.0)
    public class MyTableField extends AbstractTableField<MyTableField.Table> {
    
      @Override
      protected boolean execIsSaveNeeded() throws ProcessingException {
        boolean result;
        //some logic that computes if the table field contains modification (result = true) or not (result = false)
        return result;
      }
    
      //...
    

    I hope this helps.


    I am looking for function that check for dirty fields and pops up message dialog, same as when closing form.

    Are you speaking from this message box that appears when the user clicks on Cancel in the form?

    Do you want to save the changes

    There is no specific function that you can call for that. But you can check the beginning of the AbstractForm.doCancel() function. It is exactly what you are looking for.

    I have rewritten it like this:

    // ensure all fields have the right save-needed-state
    checkSaveNeeded();
    // find any fields that needs save
    AbstractCollectingFieldVisitor<IFormField> collector = new AbstractCollectingFieldVisitor<IFormField>() {
      @Override
      public boolean visitField(IFormField field, int level, int fieldIndex) {
        if (field instanceof IValueField && field.isSaveNeeded()) {
          collect(field);
        }
        return true;
      }
    };
    SomeTestForm.this.visitFields(collector);
    
    MessageBox.showOkMessage("DEBUG", "You have " + collector.getCollectionCount() + " fields containing a change in your form", null);
    

    I have changed the Visitor to collect all value fields with unchanged changes. But you can stick to the original visitField(..) implementation. You cannot use P_AbstractCollectingFieldVisitor because it is private, but you can have a similar field visitor somewhere else.

    I am looking for a way to set fields dirty or not.

    As I told you: execIsSaveNeeded() in each field for some custom logic. You can also call touch() / markSaved() on a field to indicate that it contains modifications or not. But unless you cannot do otherwise, I do not think that this is the correct approach for you.