FilteredTree不会过滤任何项目

时间:2016-08-27 21:24:34

标签: java jface

目前我正在开发一个大型软件项目,我需要一些帮助。 我添加了一个FilteredTree,所以显示我自己创建的树。 以下代码用于初始化它:

PatternFilter patternFilter = new PatternFilter();

dataTree = new FilteredTree(comp1, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL, patternFilter, true);
dataTreeViewer = signalTree.getViewer();

dataTreeViewer.setContentProvider(getDataTreeContentProvider());
dataTreeViewer.setLabelProvider(getDataTreeLabelProvider());
dataTreeViewer.setInput(data);

输入是由单个节点表示的树,其中包含子节点,依此类推。 如果我在过滤器中填写一些单词,字面上没有任何反应。我可以编写奇怪的东西,但仍然会显示所有内容。

我还会为我的标签提供商添加一些代码。也许你看到了错误。这让我感到疯狂,因为我浪费时间和时间来处理这件小事。

public String getText(Object element) {
    if (element != null) {
        // MatTreeNode
        if (element instanceof MatTreeNode) {
            MatTreeNode node = (MatTreeNode) element;
            return node.getName();
        }

        // OtherData
        if (element instanceof OtherData) {
            OtherData data = (OtherData) element;
            return data.getName();
        }
    }

    return null;
}

在我的代码中,OtherData是一个不同的命名类。由于版权原因,我不允许发布此代码。

我希望有人能帮助我。

周末愉快!

祝你好运 LouBen3010

完整的ContentProvider:

public class RawdataTreeContentProvider extends TreeNodeContentProvider implements IContentProvider {

final static String[] EMPTY_ARRAY = {};

@Override
public Object[] getElements(Object inputElement) {
    return getChildren(inputElement);
}

@Override
public Object[] getChildren(Object parentElement) {
    // MatTreeNode
    if (parentElement instanceof MatTreeNode) {
        MatTreeNode parent = (MatTreeNode) parentElement;
        LinkedList<MatTreeNode> children = parent.getChildren();
        Iterator<MatTreeNode> iterator = children.iterator();

        MatTreeNode[] res = new MatTreeNode[children.size()];

        int i = 0;
        while (iterator.hasNext()) {
            MatTreeNode node = iterator.next();
            res[i] = node;
            i++;
        }

        return res;
    }

    // SimpleTreeNode
    if (parentElement instanceof SimpleTreeNode) {
        SimpleTreeNode treeNode = (SimpleTreeNode) parentElement;
        return treeNode.getChildren().toArray();
    }

    return EMPTY_ARRAY;
}

@Override
public Object getParent(Object element) {
    if (element instanceof MatTreeNode) {
        return ((MatTreeNode) element).getParent();
    }

    if (element instanceof SimpleTreeNode) {
        return ((SimpleTreeNode) element).getParent();
    }

    return null;
}

@Override
public boolean hasChildren(Object element) {
    return getChildren(element).length > 0;
}
}

完成LabelProvider:

public class RawdataTreeLabelProvider extends LabelProvider implements ILabelProvider {

private Map<ImageDescriptor, Image> imageCache = new HashMap<ImageDescriptor, Image>();

@Override
public String getText(Object element) {
    if (element != null) {
        // MatTreeNode
        if (element instanceof MatTreeNode) {
            MatTreeNode node = (MatTreeNode) element;
            return node.getName();
        }

        // SimpleTreeNode
        if (element instanceof SimpleTreeNode) {
            SimpleTreeNode signal = (SimpleTreeNode) element;
            return signal.getName();
        }
    }

    return null;
}

@Override
public Image getImage(Object element) {
    // SimpleTreeNode
    if (element instanceof SimpleTreeNode) {
        ImageDescriptor descriptor = null;

        SimpleTreeNode treeNode = (SimpleTreeNode) element;
        SignalData signalData = treeNode.getContent();

        // Chose image by signal type
        if (signalData.getType() == 0) {
            // Input
            descriptor = getImageDescriptor("login-16.png");
        }
        else if (signalData.getType() == 1) {
            // Output
            descriptor = getImageDescriptor("logout-16.png");
        }

        // Try to get the image from cache
        Image image = (Image) imageCache.get(descriptor);

        // If not in cache load the corresponding image
        if (image == null) {
            image = descriptor.createImage();
            imageCache.put(descriptor, image);
        }

        return image;
    }
    else {
        return super.getImage(element);
    }
}

/*
 * Custom methods
 */

public ImageDescriptor getImageDescriptor(String name) {
    ClassLoader loader = getClass().getClassLoader();
    URL url = loader.getResource(name);
    return ImageDescriptor.createFromURL(url);
}
}

0 个答案:

没有答案