Last active
December 22, 2015 19:39
-
-
Save hendrikstill/6520893 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import javax.annotation.PostConstruct; | |
import org.eclipse.jface.viewers.ITreeContentProvider; | |
import org.eclipse.jface.viewers.LabelProvider; | |
import org.eclipse.jface.viewers.TreeViewer; | |
import org.eclipse.jface.viewers.Viewer; | |
import org.eclipse.jface.viewers.ViewerFilter; | |
import org.eclipse.swt.widgets.Composite; | |
public class GenericTreeViewerPartWithSuper { | |
@PostConstruct | |
public void postConstruct(Composite parent) { | |
MyTreeViewer<Person,PersonRootSubclass> treeViewer = new MyTreeViewer<Person,PersonRootSubclass>(parent); | |
treeViewer.setContentProvider(new MyTreeContentProvider()); | |
treeViewer.setLabelProvider(new MyLabelProvider()); | |
treeViewer.setInput(new PersonRootSubclass()); | |
} | |
class MyTreeContentProvider implements ITreeContentProvider<Person,PersonRoot>{ | |
@Override | |
public void dispose() { | |
} | |
@Override | |
public void inputChanged(Viewer<? extends PersonRoot> viewer, PersonRoot oldInput, PersonRoot newInput) { | |
MyTreeViewer<Person,? extends PersonRoot> myTreeViewer = (MyTreeViewer<Person,? extends PersonRoot>) viewer; | |
myTreeViewer.doSomethingDependingOnInput(newInput); //Compile error here | |
//The doSomethingDependingOnInput method needs an input of the type I. This is in the current implementation PersonRoot or a subset of this. | |
//Cause of the wildcard we can't use this method | |
} | |
@Override | |
public Person[] getElements(PersonRoot inputElement) { | |
Person[] personArray = new Person[1]; | |
List<Person> persons = new ArrayList<>(); | |
persons.add(inputElement.getRootPerson()); | |
return persons.toArray(personArray); | |
} | |
@Override | |
public Person[] getChildren(Person parentElement) { | |
Person[] personArray = new Person[parentElement.getChildren().size()]; | |
List<Person> persons = new ArrayList<>(); | |
persons.addAll(parentElement.getChildren()); | |
return persons.toArray(personArray); | |
} | |
@Override | |
public Person getParent(Person element) { | |
return null; | |
} | |
@Override | |
public boolean hasChildren(Person element) { | |
return (((Person)element).getChildren() != null); | |
} | |
} | |
class MyLabelProvider extends LabelProvider<Person>{ | |
@Override | |
public String getText(Person element) { | |
return element.getName(); | |
} | |
} | |
class MyTreeViewer<E,I extends PersonRoot> extends TreeViewer<E,I>{ | |
public MyTreeViewer(Composite parent) { | |
super(parent); | |
} | |
public void doSomethingDependingOnInput(I personRoot){ | |
personRoot.setDisplayed(true); | |
} | |
} | |
class Person{ | |
private String name; | |
private List<Person> children; | |
public Person(String name) { | |
super(); | |
this.name = name; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public List<Person> getChildren() { | |
return this.children; | |
} | |
public void addChildren(Person children) { | |
if (this.children == null) { | |
this.children = new ArrayList<>(); | |
} | |
this.children.add(children); | |
} | |
} | |
class PersonRoot { | |
private Person root; | |
private boolean displayed; | |
public PersonRoot() { | |
Person tom = new Person("Tom"); | |
Person marcus = new Person("Marcus"); | |
tom.addChildren(marcus); | |
root = tom; | |
} | |
public Person getRootPerson(){ | |
return root; | |
} | |
public void setDisplayed(boolean displayed) { | |
this.displayed = displayed; | |
} | |
} | |
class PersonRootSubclass extends PersonRoot{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment