EditingDomain editingDomain = contactsManager.getEditingDomain();
// TreeView
TreeView<Object> treeView = new TreeView<>();
treeView.setRoot(new AdapterFactoryTreeItem(contactsManager.getRootGroup(), treeView, contactsManager.getAdapterFactory()));
AdapterFactoryTreeCellFactory treeCellFactory = new AdapterFactoryTreeCellFactory(contactsManager.getAdapterFactory());
// add edit support
treeCellFactory.addCellEditHandler(new EAttributeCellEditHandler(ContactsPackage.eINSTANCE.getGroup_Name(), editingDomain));
// adds drag support
treeCellFactory.addCellCreationListener(new CellDragAdapter());
// adds drop support
EditingDomainCellDropAdapter dropAdapter = new EditingDomainCellDropAdapter(editingDomain);
dropAdapter.setFeedbackHandler(new CustomFeedbackHandler());
treeCellFactory.addCellCreationListener(dropAdapter);
treeView.setCellFactory(treeCellFactory);
parent.setCenter(treeView);
treeView.setShowRoot(false);
treeView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Object>() {
public void changed(ObservableValue<? extends Object> arg0, Object arg1, Object arg2) {
if (arg2 instanceof AdapterFactoryTreeItem) {
Object value = ((AdapterFactoryTreeItem) arg2).getValue();
if (value instanceof Contact)
application.getContext().set(Object.class, value);
}
}
});
treeView.getSelectionModel().getSelectedItems().addListener(new ListChangeListener<Object>() {
@Override
public void onChanged(Change<?> change) {
ArrayList<Object> selection = new ArrayList<Object>();
for (Object item : change.getList()) {
if (item instanceof AdapterFactoryTreeItem) {
Object value = ((AdapterFactoryTreeItem) item).getValue();
selection.add(value);
}
}
application.getContext().set(List.class, selection);
}
});
// add the context menu
ContextMenuProvider contextMenuProvider = new ContextMenuProvider(contactsManager);
treeCellFactory.addCellUpdateListener(contextMenuProvider);
treeView.setEditable(true);
}