0

When I create my TableViewer in my dialog class. I am adding a ListChangeListener. This listener listens to a ObservableList in my Data Model Class.

This is my createTableViewer method in the dialog class.

 private void createTableViewer(Composite parent) {
  viewer = new AplotDataTableViewer(parent, SWT.BORDER|SWT.V_SCROLL|SWT.FULL_SELECTION);
  IObservableList iob = AplotDataModel.getInstance().getObservableList();
  viewer.setInput(iob);
  iob.addListChangeListener(new IListChangeListener() {
     @Override
     public void handleListChange(ListChangeEvent event) {
        updateTableViewer();   
     }
  });
}

So what is happening. When a user Closes the dialog using the Window Close Button (Red X). That is disposing all the widgets and closing the window. When the Dialog is opened back. The ListChangeListener is looking to the updateTableViewer, but the widgets in the update is already disposed.

Right now there are 2 ways to close the dialog. 1. Red X - maybe doing a Window.close() 2. My close button on the form.

@Override
protected void createButtonsForButtonBar(Composite parent) {
  createButton(parent, IDialogConstants.OK_ID, "Close Aplot",
        true);
}

@Override
protected void okPressed() {
   getShell().setVisible(false);
}

Which is using okPressed and hiding the shell.

What I would like is to have both methods of closing the dialog the same.

  1. Is it possible to add a listener to the Shell and in the handleEvent method. Have a call to the okPressed method?

      getShell().addListener(SWT.Close, new Listener() {
        @Override
        public void handleEvent(Event e) {
           okPressed();
        }
      });
    
  2. Instead of SWT.Close should I be using Window.Close?

  3. Should I be using Close_ID instead of ok_ID

    @Override protected void createButtonsForButtonBar(Composite parent) { createButton(parent, IDialogConstants.CLOSE_ID, "Close Aplot", true); }

    @Override protected void closePressed() { getShell().setVisible(false); }

  4. Is there a way to get my active ListChangeListener and remove it?

    protected void closePressed() { AplotDataModel.getInstance().getObservableList().removeListChangeListener(this); }

  5. I am not sure how to get active listeners?

I want to Close the Dialog either using the Windows Close Button (Red X) or the Close Button on the form. If possible I wish both actions would use the same code to remove the active Listener from my IObservableList and close the dialog.

4

1 回答 1

3

您是否尝试过DisposeListener在窗口中添加一个?然后 dispose 侦听器可以取消注册您在其控件上设置的任何事件侦听器。无论窗口如何关闭,无论是从红色 X 还是通过调用方法,都会发生这种shell.close()情况okPressed()

例如:

private void createTableViewer(Composite parent) {
  viewer = new AplotDataTableViewer(parent, SWT.BORDER|SWT.V_SCROLL|SWT.FULL_SELECTION);
  final IObservableList iob = AplotDataModel.getInstance().getObservableList();
  viewer.setInput(iob);

  final IListChangeListener listener = new IListChangeListener() {
     @Override
     public void handleListChange(ListChangeEvent event) {
        updateTableViewer();   
     }
  };

  iob.addListChangeListener(listener);

  getShell().addDisposeListener(
    new DisposeListener() {
        @Override public void widgetDisposed(DisposeEvent e) {
            iob.removeListChangeListener(listener);
        }
    });
}
于 2013-06-05T04:15:11.403 回答