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());
}
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;
}
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);
}
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();
}
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;
}
Aggregations