开发者

SWT - Question about Dialog best practice in retrieving result / detecting aborted user input

开发者 https://www.devze.com 2023-04-03 07:23 出处:网络
I want to build a database application with SWT / JFace as UI library. I\'ve been used to code SWT by hand and wanted to dig into databinding with JFace since I stumbled across it the very first time

I want to build a database application with SWT / JFace as UI library. I've been used to code SWT by hand and wanted to dig into databinding with JFace since I stumbled across it the very first time I used Window Builder Eclipse plugin.

Currently my work focuses on the "add record" dialog and I'm interested in best practice. I bound an empty model object to the textfields and the model object properties get updated as soon as the user does the input.

Furthermore I changed the method signature of the open() method to return the model object after the dialog disposes. Truncated:

public class AddRecordDialog extends Dialog {
    private MyItem item = new MyItem();

    ...

    public MyItem open() {
        createContents();
        shell.open();
        shell.layout();
        Display display = getParent().getDisplay();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        return item;
    }
}

However, if the user cancels his input (either by clicked the "Cancel" button or by closing the dialog) this way of handling the result object only gives me the model object with the default attributes from the empty default constructor without parameters.

This brings up the question of the best practice of how to determine what action the user has taken. Some solutions in my mind:

  • Should an extra flag be introduced? Like an extra boolean value like initialised like cancelCli开发者_运维问答cked = false and altering the variable on cancel button's selection listener?

  • Should the buttons be given IDs like in the MessageBox class and introducing a switch-statement in the class that brings up the dialog? Adding an extra method for returning a eventually valid model object must be introduced.

  • Might there be any other way of informing the dialog caller of canceled input?

You might think, oh, he worries about tiny issues and not real programming problems, but since this is my first application where I want to stick strictly to MVC and best practices in a Java / SWT environment I do care.

Thanks in advance for reading this relatively loong question ;)


If you really want to stick to MVC, I suggest changing the model a bit :)

class MyItemBuilder {
  private bool isCanceled = false;
  private MyItem item;
  // includes getters/setters of MyItem, which delegate to item

  public bool cancel() {
    isCanceled = true;
  }

  public MyItem build() {
    if (isCanceled) 
      return null;
    else
      return item;
  }
}

public class AddRecordDialog extends Dialog {
  private MyItemBuilder builder = new MyItemBuilder();
  ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号