use of org.eclipse.nebula.widgets.nattable.examples.runner.NavLabelProvider in project nebula.widgets.nattable by eclipse.
the class NavigationPart method postConstruct.
@PostConstruct
public void postConstruct(Composite parent) {
final TreeViewer navTreeViewer = new TreeViewer(parent);
final NavContentProvider contentProvider = new NavContentProvider() {
@Override
public Object[] getElements(Object inputElement) {
Set<String> topLevelElements = new LinkedHashSet<>();
String[] examplePaths = (String[]) inputElement;
for (final String examplePath : examplePaths) {
String parentPath = "";
String absolutePath = "";
// remove the package name for the tree structure
String path = examplePath;
if (examplePath.startsWith(INatExample.TUTORIAL_EXAMPLES_PREFIX)) {
path = examplePath.replace(INatExample.BASE_PATH, "");
} else if (examplePath.startsWith(INatExample.CLASSIC_EXAMPLES_PREFIX)) {
path = examplePath.replace(INatExample.CLASSIC_BASE_PATH, "");
} else if (examplePath.startsWith(E4_EXAMPLES_PREFIX)) {
path = examplePath.replace(E4_BASE_PATH, "");
}
final StringTokenizer tok = new StringTokenizer(path, "/");
while (tok.hasMoreTokens()) {
final String pathElement = tok.nextToken();
if (parentPath.length() == 0) {
topLevelElements.add("/" + pathElement);
}
absolutePath += "/" + pathElement;
final Collection<String> children = getChildren(parentPath);
children.add(absolutePath);
parentPath = absolutePath;
}
}
return topLevelElements.toArray();
}
};
navTreeViewer.setContentProvider(contentProvider);
navTreeViewer.setLabelProvider(new NavLabelProvider(contentProvider) {
@Override
public String getText(Object element) {
String str = (String) element;
if (!contentProvider.hasChildren(element)) {
INatExample example = getExample(str);
return example.getName();
}
int lastSlashIndex = str.lastIndexOf('/');
if (lastSlashIndex < 0) {
return format(str);
} else {
return format(str.substring(lastSlashIndex + 1));
}
}
});
navTreeViewer.setInput(getExamplePaths());
navTreeViewer.addDoubleClickListener(new IDoubleClickListener() {
@Override
public void doubleClick(DoubleClickEvent event) {
TreeSelection selection = (TreeSelection) event.getSelection();
for (TreePath path : selection.getPaths()) {
// check for item - if node expand/collapse, if child open
if (contentProvider.hasChildren(path.getLastSegment().toString())) {
boolean expanded = navTreeViewer.getExpandedState(path);
navTreeViewer.setExpandedState(path, !expanded);
} else {
openExampleInTab(path.getLastSegment().toString());
}
}
}
});
GridLayout layout = new GridLayout();
layout.marginWidth = 0;
layout.marginHeight = 0;
parent.setLayout(layout);
GridDataFactory.fillDefaults().grab(true, true).applyTo(navTreeViewer.getControl());
}
Aggregations