Search in sources :

Example 1 with INatExample

use of org.eclipse.nebula.widgets.nattable.examples.INatExample 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)

Example 2 with INatExample

use of org.eclipse.nebula.widgets.nattable.examples.INatExample in project nebula.widgets.nattable by eclipse.

the class NavigationPart method getExample.

public INatExample getExample(String examplePath) {
    INatExample example = null;
    String path = examplePath;
    ClassLoader loader = exampleClassLoader;
    if (examplePath.startsWith("/" + INatExample.TUTORIAL_EXAMPLES_PREFIX)) {
        path = examplePath.replace("/" + INatExample.TUTORIAL_EXAMPLES_PREFIX, INatExample.BASE_PATH + "/");
    } else if (examplePath.startsWith("/" + INatExample.CLASSIC_EXAMPLES_PREFIX)) {
        path = examplePath.replace("/" + INatExample.CLASSIC_EXAMPLES_PREFIX, INatExample.CLASSIC_BASE_PATH + "/");
    } else if (examplePath.startsWith("/" + E4_EXAMPLES_PREFIX)) {
        path = examplePath.replace("/" + E4_EXAMPLES_PREFIX, E4_BASE_PATH + "/");
        loader = this.getClass().getClassLoader();
    }
    if (path.startsWith("/"))
        path = path.substring(1);
    Class<? extends INatExample> exampleClass = getExampleClass(path, loader);
    if (exampleClass != null) {
        try {
            example = exampleClass.newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    return example;
}
Also used : INatExample(org.eclipse.nebula.widgets.nattable.examples.INatExample) IOException(java.io.IOException)

Example 3 with INatExample

use of org.eclipse.nebula.widgets.nattable.examples.INatExample in project nebula.widgets.nattable by eclipse.

the class NavigationPart method openExampleInTab.

private void openExampleInTab(final String examplePath) {
    final INatExample example = getExample(examplePath);
    if (example == null) {
        return;
    }
    MPartStack stack = (MPartStack) modelService.find("org.eclipse.nebula.widgets.nattable.examples.e4.partstack.0", app);
    MPart part = null;
    if (examplePath.startsWith("/" + E4_EXAMPLES_PREFIX)) {
        part = (MPart) modelService.find(example.getClass().getName(), app);
        if (part == null) {
            part = partService.createPart(example.getClass().getName());
            part.getTags().add(LifecycleManager.CLOSE_ON_SHUTDOWN_TAG);
            part.getTags().add(EPartService.REMOVE_ON_HIDE_TAG);
            stack.getChildren().add(part);
        }
    } else {
        part = partService.createPart("org.eclipse.nebula.widgets.nattable.examples.e4.partdescriptor.natexample");
        part.getTags().add(LifecycleManager.CLOSE_ON_SHUTDOWN_TAG);
        part.getTags().add(EPartService.REMOVE_ON_HIDE_TAG);
        part.getTransientData().put("example", example);
        part.getTransientData().put("examplePath", examplePath);
        stack.getChildren().add(part);
    }
    part.setLabel(example.getName());
    partService.showPart(part, PartState.ACTIVATE);
}
Also used : MPart(org.eclipse.e4.ui.model.application.ui.basic.MPart) MPartStack(org.eclipse.e4.ui.model.application.ui.basic.MPartStack) INatExample(org.eclipse.nebula.widgets.nattable.examples.INatExample)

Example 4 with INatExample

use of org.eclipse.nebula.widgets.nattable.examples.INatExample in project nebula.widgets.nattable by eclipse.

the class TabbedNatExampleRunner method run.

public static void run(int shellWidth, int shellHeight, final String... examplePaths) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
    // Setup
    final Display display = Display.getDefault();
    final Shell shell = new Shell(display, SWT.SHELL_TRIM);
    shell.setLayout(new GridLayout(2, false));
    shell.setSize(shellWidth, shellHeight);
    shell.setText("NatTable Examples Application");
    Image nebula16 = new Image(display, TabbedNatExampleRunner.class.getResourceAsStream("nebula_logo_16.png"));
    Image nebula32 = new Image(display, TabbedNatExampleRunner.class.getResourceAsStream("nebula_logo_32.png"));
    Image nebula64 = new Image(display, TabbedNatExampleRunner.class.getResourceAsStream("nebula_logo_64.png"));
    shell.setImages(new Image[] { nebula16, nebula32, nebula64 });
    // Nav tree
    final TreeViewer navTreeViewer = new TreeViewer(shell);
    GridData gridData = new GridData(GridData.FILL_VERTICAL);
    gridData.widthHint = 300;
    navTreeViewer.getControl().setLayoutData(gridData);
    final NavContentProvider contentProvider = new NavContentProvider();
    navTreeViewer.setContentProvider(contentProvider);
    navTreeViewer.setLabelProvider(new NavLabelProvider(contentProvider));
    navTreeViewer.setInput(examplePaths);
    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());
                }
            }
        }
    });
    tabFolder = new CTabFolder(shell, SWT.BORDER);
    tabFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    for (INatExample example : exampleControlMap.keySet()) {
        // Stop
        example.onStop();
        Control exampleControl = exampleControlMap.get(example);
        exampleControl.dispose();
    }
    tabFolder.dispose();
    shell.dispose();
    display.dispose();
}
Also used : CTabFolder(org.eclipse.swt.custom.CTabFolder) TreeViewer(org.eclipse.jface.viewers.TreeViewer) DoubleClickEvent(org.eclipse.jface.viewers.DoubleClickEvent) Image(org.eclipse.swt.graphics.Image) Shell(org.eclipse.swt.widgets.Shell) GridLayout(org.eclipse.swt.layout.GridLayout) Control(org.eclipse.swt.widgets.Control) TreePath(org.eclipse.jface.viewers.TreePath) TreeSelection(org.eclipse.jface.viewers.TreeSelection) IDoubleClickListener(org.eclipse.jface.viewers.IDoubleClickListener) GridData(org.eclipse.swt.layout.GridData) INatExample(org.eclipse.nebula.widgets.nattable.examples.INatExample) Display(org.eclipse.swt.widgets.Display)

Example 5 with INatExample

use of org.eclipse.nebula.widgets.nattable.examples.INatExample in project nebula.widgets.nattable by eclipse.

the class TabbedNatExampleRunner method getExample.

public static INatExample getExample(String examplePath) {
    INatExample example = examplePathMap.get(examplePath);
    if (example == null) {
        String path = examplePath;
        if (examplePath.startsWith("/" + INatExample.TUTORIAL_EXAMPLES_PREFIX)) {
            path = examplePath.replace("/" + INatExample.TUTORIAL_EXAMPLES_PREFIX, INatExample.BASE_PATH + "/");
        } else if (examplePath.startsWith("/" + INatExample.CLASSIC_EXAMPLES_PREFIX)) {
            path = examplePath.replace("/" + INatExample.CLASSIC_EXAMPLES_PREFIX, INatExample.CLASSIC_BASE_PATH + "/");
        }
        if (path.startsWith("/"))
            path = path.substring(1);
        Class<? extends INatExample> exampleClass = getExampleClass(path);
        if (exampleClass != null) {
            try {
                example = exampleClass.newInstance();
                examplePathMap.put(examplePath, example);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
    return example;
}
Also used : INatExample(org.eclipse.nebula.widgets.nattable.examples.INatExample) IOException(java.io.IOException)

Aggregations

INatExample (org.eclipse.nebula.widgets.nattable.examples.INatExample)7 GridLayout (org.eclipse.swt.layout.GridLayout)3 IOException (java.io.IOException)2 DoubleClickEvent (org.eclipse.jface.viewers.DoubleClickEvent)2 IDoubleClickListener (org.eclipse.jface.viewers.IDoubleClickListener)2 TreePath (org.eclipse.jface.viewers.TreePath)2 TreeSelection (org.eclipse.jface.viewers.TreeSelection)2 TreeViewer (org.eclipse.jface.viewers.TreeViewer)2 GridData (org.eclipse.swt.layout.GridData)2 Control (org.eclipse.swt.widgets.Control)2 LinkedHashSet (java.util.LinkedHashSet)1 StringTokenizer (java.util.StringTokenizer)1 PostConstruct (javax.annotation.PostConstruct)1 MPart (org.eclipse.e4.ui.model.application.ui.basic.MPart)1 MPartStack (org.eclipse.e4.ui.model.application.ui.basic.MPartStack)1 NavContentProvider (org.eclipse.nebula.widgets.nattable.examples.runner.NavContentProvider)1 NavLabelProvider (org.eclipse.nebula.widgets.nattable.examples.runner.NavLabelProvider)1 CTabFolder (org.eclipse.swt.custom.CTabFolder)1 CTabItem (org.eclipse.swt.custom.CTabItem)1 DisposeEvent (org.eclipse.swt.events.DisposeEvent)1