简体   繁体   中英

How to read data from binding bean?

I am trying to make a simple editor using Eclipse RCP. Below is some code fragment for that.

I have this view for new File -

public class NewView extends ViewPart {
private DataBindingContext m_bindingContext;
public static final String ID = "com.app.Editor.newView";
SaveFileBean bean = new SaveFileBean();
private StyledText text;

public NewView() {
}

@Override
public void createPartControl(Composite parent) {

    text = new StyledText(parent, SWT.BORDER);
    m_bindingContext = initDataBindings();
}


@Override
public void setFocus() {

}
protected DataBindingContext initDataBindings() {
    DataBindingContext bindingContext = new DataBindingContext();
    //
    IObservableValue observeTextTextObserveWidget = WidgetProperties.text(SWT.Modify).observe(text);
    IObservableValue textBeanObserveValue = PojoProperties.value("text").observe(bean);
    bindingContext.bindValue(observeTextTextObserveWidget, textBeanObserveValue, null, null);
    //
    return bindingContext;
}
}

I have SaveFileBean to whom I am binding the data for saving a newly created file.

public class SaveFileBean {
private String text;
public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

}

When I click save button, SaveHandler Class gets called, And, I have wrote the class like this.

public class SaveHandler extends AbstractHandler implements IHandler {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
            .getShell();
    FileDialog dialog = new FileDialog(shell, SWT.SAVE);
    dialog.setFilterExtensions(new String[] { "*.txt", "*.*" });
    dialog.setFilterNames(new String[] { "Text File", "All Files" });
    String fileSelected = dialog.open();

    if (fileSelected != null) {
        //want to read data
    }
    return null;
}
}

Now, I want to read data what in entered inside the text field in NewView. How can I access the value from beans? Of course it doesn't work by creating a new instance inside the handler class. Because, it creates a empty object ans returns me null. :( Or, is there any problem in my approach?

Thanks!

In your handler you can get the active part (presumably your view) using:

IWorkbenchPart part = HandlerUtil.getActivePart(event);

if (part instanceof NewView) {
   NewView newView = (NewView)part;

   ... call a method you define on NewView to access your 'bean' 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM