use of org.eclipse.elk.core.data.LayoutCategoryData in project elk by eclipse.
the class AlgorithmSelectionDialog method createSelectionTree.
/**
* Create the dialog area that displays the selection tree and filter text.
*
* @param parent the parent composite
* @return the control for the selection area
*/
private Control createSelectionTree(final Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
// create filter text
final Text filterText = new Text(composite, SWT.BORDER);
filterText.setText(Messages.getString("elk.ui.59"));
filterText.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
filterText.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
// create tree viewer
final TreeViewer treeViewer = new TreeViewer(composite, SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER);
final AlgorithmContentProvider contentProvider = new AlgorithmContentProvider();
treeViewer.setContentProvider(contentProvider);
treeViewer.setLabelProvider(new LabelProvider() {
@Override
public String getText(final Object element) {
if (element instanceof LayoutAlgorithmData) {
LayoutAlgorithmData algoData = (LayoutAlgorithmData) element;
String bundleName = algoData.getBundleName();
if (bundleName == null) {
return algoData.getName();
} else {
return algoData.getName() + " (" + bundleName + ")";
}
} else if (element instanceof LayoutCategoryData) {
LayoutCategoryData typeData = (LayoutCategoryData) element;
if (typeData.getName() == null) {
return "Other";
} else {
return typeData.getName();
}
}
return super.getText(element);
}
});
treeViewer.setComparator(new ViewerComparator() {
public int category(final Object element) {
if (element instanceof LayoutCategoryData) {
LayoutCategoryData typeData = (LayoutCategoryData) element;
// the "Other" layout type has empty identifier and is put to the bottom
return typeData.getId().length() == 0 ? 1 : 0;
}
return super.category(element);
}
});
treeViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
treeViewer.setInput(LayoutMetaDataService.getInstance());
treeViewer.expandAll();
treeViewer.addDoubleClickListener(new IDoubleClickListener() {
public void doubleClick(final DoubleClickEvent event) {
okPressed();
}
});
// set up a filter on the tree viewer using the filter text
final Maybe<Boolean> filterChanged = new Maybe<Boolean>(Boolean.FALSE);
final Maybe<Boolean> filterLeft = new Maybe<Boolean>(Boolean.FALSE);
filterText.addModifyListener(new ModifyListener() {
public void modifyText(final ModifyEvent e) {
if (!filterChanged.get()) {
filterChanged.set(Boolean.TRUE);
filterText.setForeground(null);
int pos = filterText.getCaretPosition();
String newText = filterText.getText(pos - 1, pos - 1);
filterText.setText(newText);
filterText.setSelection(pos);
} else {
contentProvider.updateFilter(filterText.getText());
treeViewer.refresh();
treeViewer.expandAll();
ILayoutMetaData selected = contentProvider.getBestFilterMatch();
if (selected != null) {
treeViewer.setSelection(new StructuredSelection(selected));
}
}
}
});
filterText.addFocusListener(new FocusListener() {
public void focusGained(final FocusEvent e) {
if (filterLeft.get() && !filterChanged.get()) {
filterChanged.set(Boolean.TRUE);
filterText.setForeground(null);
filterText.setText("");
}
}
public void focusLost(final FocusEvent e) {
filterLeft.set(Boolean.TRUE);
}
});
treeViewer.addFilter(new ViewerFilter() {
public boolean select(final Viewer viewer, final Object parentElement, final Object element) {
return contentProvider.applyFilter(element);
}
});
// add a selection listener to the tree so that the selected element is displayed
treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(final SelectionChangedEvent event) {
IStructuredSelection selection = (IStructuredSelection) event.getSelection();
Object element = selection.getFirstElement();
if (element instanceof ILayoutMetaData) {
updateValue((ILayoutMetaData) element);
}
}
});
composite.setLayout(new GridLayout());
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
gridData.minimumWidth = SELECTION_WIDTH;
composite.setLayoutData(gridData);
// register all selection listeners that have been stored in a list
selectionProvider = treeViewer;
for (ISelectionChangedListener listener : selectionListeners) {
selectionProvider.addSelectionChangedListener(listener);
}
return composite;
}
use of org.eclipse.elk.core.data.LayoutCategoryData in project elk by eclipse.
the class AlgorithmContentProvider method applyFilter.
/**
* Apply the current filter to the given element.
*
* @param element an element from the content
* @return true if the filter admits the element
*/
public boolean applyFilter(final Object element) {
Boolean result = filterMap.get(element);
if (result == null) {
if (filterValue != null && filterValue.length() > 0) {
if (element instanceof LayoutCategoryData) {
LayoutCategoryData typeData = (LayoutCategoryData) element;
result = !typeData.getLayouters().isEmpty();
if (result) {
String typeName = typeData.getName().toLowerCase();
result = typeName.contains(filterValue);
if (result) {
for (LayoutAlgorithmData layouterData : typeData.getLayouters()) {
filterMap.put(layouterData, Boolean.TRUE);
}
if (bestFilterMatch == null || typeName.startsWith(filterValue) && !bestFilterMatch.getName().toLowerCase().startsWith(filterValue)) {
bestFilterMatch = typeData;
}
} else {
boolean hasFilteredChild = false;
for (LayoutAlgorithmData layouterData : typeData.getLayouters()) {
hasFilteredChild |= applyFilter(layouterData);
}
result = hasFilteredChild;
}
}
} else if (element instanceof LayoutAlgorithmData) {
LayoutAlgorithmData layouterData = (LayoutAlgorithmData) element;
String layouterName = layouterData.getName().toLowerCase();
if (layouterName.contains(filterValue)) {
result = Boolean.TRUE;
if (bestFilterMatch == null || layouterName.startsWith(filterValue) && !bestFilterMatch.getName().toLowerCase().startsWith(filterValue)) {
bestFilterMatch = layouterData;
}
} else {
String bundle = layouterData.getBundleName();
result = bundle != null && bundle.toLowerCase().contains(filterValue);
}
}
} else if (element instanceof LayoutCategoryData) {
result = !((LayoutCategoryData) element).getLayouters().isEmpty();
} else {
result = Boolean.TRUE;
}
filterMap.put(element, result);
}
return result;
}
Aggregations