Update athen how to correctly write your own view authored by Markus Krug's avatar Markus Krug
...@@ -51,8 +51,131 @@ All the code does, is to create a new widget (the corefViewComposite), and set i ...@@ -51,8 +51,131 @@ All the code does, is to create a new widget (the corefViewComposite), and set i
corefViewComposite.setInput(this); corefViewComposite.setInput(this);
``` ```
So the view can store this reference of the controller and communicate with it. So the view can store this reference of the controller and communicate with it. The next step is to actually write the UI Element. In this tutorial i am only going to address this using eclipse SWT, but it should be equally possible using AWT,Swing or JavaFX.
```java
public class CorefViewComposite extends Composite {
//a reference to the controller
private CorefView part;
//sub composites
private CorefViewButtonComposite corefViewButtonComposite;
private CorefViewCheckBoxOptionComposite corefViewCheckboxComposite;
private CorefViewTableComposite corefViewTableComposite;
public CorefViewComposite(Composite parent, int style) {
super(parent, style);
}
public void setInput(CorefView part){
this.part = part;
initLayout();
}
private void initLayout() {
this.setLayout(new GridLayout(1, true));
//the first composite with push buttons
corefViewButtonComposite = new CorefViewButtonComposite(this, SWT.BORDER);
corefViewButtonComposite.setInput(part);
corefViewCheckboxComposite = new CorefViewCheckBoxOptionComposite(this, SWT.NONE);
corefViewCheckboxComposite.setInput(part);
corefViewTableComposite = new CorefViewTableComposite(this, SWT.BORDER);
corefViewTableComposite.setInput(part);
GridData gd = new GridData(GridData.GRAB_VERTICAL | GridData.GRAB_HORIZONTAL | GridData.FILL_BOTH);
corefViewTableComposite.setLayoutData(gd);
}
public void refresh(List<AnnotationFS> annoList, List<AnnotationFS> overrides) {
corefViewTableComposite.refresh(annoList,overrides);
}
```
The snippet shows the implementation of the UI element, let us elaborate it a bit more in detail. The first line indicates, that we are a UI element, since we extend Composite
```java
public class CorefViewComposite extends Composite {
```
Then we got the fields that are stored in this view. As you can see, there is a reference to the controller object. The next three variables present new Composites (aka UI parts) themselfes. Separating them in this way keeps the code clean, as well as it helps you to have an easy understanding of the layout in which those elements are arranged (it is very easy to order three elements, managing all at once is extremely difficult sometimes)
```java
private CorefView part;
//sub composites
private CorefViewButtonComposite corefViewButtonComposite;
private CorefViewCheckBoxOptionComposite corefViewCheckboxComposite;
private CorefViewTableComposite corefViewTableComposite;
```
The next part shows the constructor. We only call the constructor of the superclass. This does not render anything visible on the screen
```java
public CorefViewComposite(Composite parent, int style) {
super(parent, style);
}
```
Now comes the method that starts to actually display something. In this method we get access to the controller and start creating our layout
```java
public void setInput(CorefView part){
this.part = part;
initLayout();
}
```
I usually split the method setInput into 3 parts. The first saves the arguments, the second creates the layout, and the third part is another method call that attaches handler of all sorts to the UI elements in this class.
```java
public void setInput(CorefView part){
this.part = part;
initLayout();
//not used in this class
initHandler();
}
```
This is followed by the actual implementation of the initLayout-method. Thisattaches a layout to ourselfes and creates the three contained Composites, as well as sets their input in exactly the same way.
```java
private void initLayout() {
this.setLayout(new GridLayout(1, true));
//the first composite with push buttons
corefViewButtonComposite = new CorefViewButtonComposite(this, SWT.BORDER);
corefViewButtonComposite.setInput(part);
corefViewCheckboxComposite = new CorefViewCheckBoxOptionComposite(this, SWT.NONE);
corefViewCheckboxComposite.setInput(part);
corefViewTableComposite = new CorefViewTableComposite(this, SWT.BORDER);
corefViewTableComposite.setInput(part);
GridData gd = new GridData(GridData.GRAB_VERTICAL | GridData.GRAB_HORIZONTAL | GridData.FILL_BOTH);
corefViewTableComposite.setLayoutData(gd);
}
```
Last but not least, a refresh method is created, which is invoked by the controller, once the model is changed and the view needs to reflect those changes
```java
public void refresh(List<AnnotationFS> annoList, List<AnnotationFS> overrides) {
corefViewTableComposite.refresh(annoList,overrides);
}
```