Search in sources :

Example 1 with LayoutCategoryData

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;
}
Also used : ModifyListener(org.eclipse.swt.events.ModifyListener) ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) TreeViewer(org.eclipse.jface.viewers.TreeViewer) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Viewer(org.eclipse.jface.viewers.Viewer) TreeViewer(org.eclipse.jface.viewers.TreeViewer) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) FocusEvent(org.eclipse.swt.events.FocusEvent) ModifyEvent(org.eclipse.swt.events.ModifyEvent) GridLayout(org.eclipse.swt.layout.GridLayout) IDoubleClickListener(org.eclipse.jface.viewers.IDoubleClickListener) LayoutCategoryData(org.eclipse.elk.core.data.LayoutCategoryData) Maybe(org.eclipse.elk.core.util.Maybe) Composite(org.eclipse.swt.widgets.Composite) ViewerComparator(org.eclipse.jface.viewers.ViewerComparator) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) Text(org.eclipse.swt.widgets.Text) DoubleClickEvent(org.eclipse.jface.viewers.DoubleClickEvent) LayoutAlgorithmData(org.eclipse.elk.core.data.LayoutAlgorithmData) GridData(org.eclipse.swt.layout.GridData) ILayoutMetaData(org.eclipse.elk.core.data.ILayoutMetaData) LabelProvider(org.eclipse.jface.viewers.LabelProvider) FocusListener(org.eclipse.swt.events.FocusListener)

Example 2 with LayoutCategoryData

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;
}
Also used : LayoutAlgorithmData(org.eclipse.elk.core.data.LayoutAlgorithmData) LayoutCategoryData(org.eclipse.elk.core.data.LayoutCategoryData)

Aggregations

LayoutAlgorithmData (org.eclipse.elk.core.data.LayoutAlgorithmData)2 LayoutCategoryData (org.eclipse.elk.core.data.LayoutCategoryData)2 ILayoutMetaData (org.eclipse.elk.core.data.ILayoutMetaData)1 Maybe (org.eclipse.elk.core.util.Maybe)1 DoubleClickEvent (org.eclipse.jface.viewers.DoubleClickEvent)1 IDoubleClickListener (org.eclipse.jface.viewers.IDoubleClickListener)1 ISelectionChangedListener (org.eclipse.jface.viewers.ISelectionChangedListener)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 LabelProvider (org.eclipse.jface.viewers.LabelProvider)1 SelectionChangedEvent (org.eclipse.jface.viewers.SelectionChangedEvent)1 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)1 TreeViewer (org.eclipse.jface.viewers.TreeViewer)1 Viewer (org.eclipse.jface.viewers.Viewer)1 ViewerComparator (org.eclipse.jface.viewers.ViewerComparator)1 ViewerFilter (org.eclipse.jface.viewers.ViewerFilter)1 FocusEvent (org.eclipse.swt.events.FocusEvent)1 FocusListener (org.eclipse.swt.events.FocusListener)1 ModifyEvent (org.eclipse.swt.events.ModifyEvent)1 ModifyListener (org.eclipse.swt.events.ModifyListener)1 GridData (org.eclipse.swt.layout.GridData)1