Search in sources :

Example 1 with NavContentProvider

use of org.eclipse.nebula.widgets.nattable.examples.runner.NavContentProvider 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());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TreeViewer(org.eclipse.jface.viewers.TreeViewer) NavLabelProvider(org.eclipse.nebula.widgets.nattable.examples.runner.NavLabelProvider) DoubleClickEvent(org.eclipse.jface.viewers.DoubleClickEvent) StringTokenizer(java.util.StringTokenizer) GridLayout(org.eclipse.swt.layout.GridLayout) TreePath(org.eclipse.jface.viewers.TreePath) TreeSelection(org.eclipse.jface.viewers.TreeSelection) IDoubleClickListener(org.eclipse.jface.viewers.IDoubleClickListener) INatExample(org.eclipse.nebula.widgets.nattable.examples.INatExample) NavContentProvider(org.eclipse.nebula.widgets.nattable.examples.runner.NavContentProvider) PostConstruct(javax.annotation.PostConstruct)

Aggregations

LinkedHashSet (java.util.LinkedHashSet)1 StringTokenizer (java.util.StringTokenizer)1 PostConstruct (javax.annotation.PostConstruct)1 DoubleClickEvent (org.eclipse.jface.viewers.DoubleClickEvent)1 IDoubleClickListener (org.eclipse.jface.viewers.IDoubleClickListener)1 TreePath (org.eclipse.jface.viewers.TreePath)1 TreeSelection (org.eclipse.jface.viewers.TreeSelection)1 TreeViewer (org.eclipse.jface.viewers.TreeViewer)1 INatExample (org.eclipse.nebula.widgets.nattable.examples.INatExample)1 NavContentProvider (org.eclipse.nebula.widgets.nattable.examples.runner.NavContentProvider)1 NavLabelProvider (org.eclipse.nebula.widgets.nattable.examples.runner.NavLabelProvider)1 GridLayout (org.eclipse.swt.layout.GridLayout)1