3

I have a Remote File Model which has fields as file name, file path, and connection IP,connection port etc for the remote directory.I want to show only the file path in a Text. I am using JFace Data binding for binding the model to SWT Text but I am able to bind only 1 field to it. Please help me to bind the Complete model to the Text and showing only one field. Also tell me if it is possible or not or there is some other way.

Baz
  • 36,440
  • 11
  • 68
  • 94
saurabh
  • 257
  • 1
  • 3
  • 12

1 Answers1

2

If I understand you correctly you want to show multiple model fields in one SWT Text widget? You can do it in the following way:

class FileModel {

    private String name;
    private String filePath;
    private String ip;
    // other fields, getters and setters

    public String getFileSummary() {
        return name + " : " + filePath + " : " + ip;
    }

    public void setFileSummary(String summary) {
        // ignore
    }

}

Then you can bind it like this:

FileModel model;
new DataBindingContext().bindValue(SWTObservables.observeText(text, SWT.Modify),
            BeansObservables.observeValue(model, "fileSummary"), new UpdateValueStrategy(), new UpdateValueStrategy());

The idea is that while specifying "fileSummary" field name to bind in your model, JFace will look for getter and setter for that field, so you don't actually need a field itself.

In getter you can provided required info and you could even parse some special format in setter and assign those to related fields, something like this:

public void setFileSummary(String summary) {
    // todo: implement in a smart way;)
    String[] parts = summary.split(" : ");
    name = parts[0];
    filePath = parts[1];
    ip = parts[2];
}
Alex K.
  • 3,294
  • 4
  • 29
  • 41
  • Thanks,it worked. Just I had to make the field "summary" as @XmlTransient since I was also writing the content of the Model in an Xml using Jaxb. – saurabh Jul 11 '14 at 05:01
  • Okay, now this method is now creating problem. As I am extending this class and also writing it to Xml using Jaxb, so this extra summary field comes there. To avoid that I used Xml Transient, but while writing it writes only the super class fields into it. Please help me in dealing with that. – saurabh Jul 20 '14 at 19:11
  • Here is the class I am using- class FileModel { private String name; private String filePath; //getters and setters } class RemoteFileModel extends FileModel { public String Ip; //getters and setters } and several other classes extending FileModel. Please help me how to deal with that or any other way to bind the model to the Text widget. – saurabh Jul 20 '14 at 19:18
  • Actually I am writing it to Xml, so I have to make the summary field as transient since I dont want to show that field in the Xml. And also I dont know how, only the contents of the super class is getting persisted and the contents of the sub class are not. – saurabh Jul 21 '14 at 06:12
  • IObservableValue target = WidgetProperties.text(SWT.Modify).observe(filePathText); IObservableValue model = PojoProperties.value(RemoteFileModel.class,"summary").observe((RemoteFileModel) inputFileDetails); //in case of remote file dbc.bindValue(target, model); I am doing the binding as above, So i have that filePathText in which I want to show the filePath but i want to save the completed model. Is it possible to do it this way because I didnt got any thing like this any where. – saurabh Jul 21 '14 at 06:14