过滤对话框

过滤搜索:

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.SelectionDialog;

public class FilterViewerDialog extends SelectionDialog
{
	private TreeViewer dataViewer;

	protected FilterViewerDialog(Shell parentShell)
	{
		super(parentShell);
	}

	protected Control createDialogArea(Composite parent)
	{
		Composite composite = (Composite) super.createDialogArea(parent);
		GridLayout layout = new GridLayout();
		layout.marginLeft = 10;
		layout.marginRight = 10;
		layout.marginTop = 10;
		layout.marginBottom = 10;
		GridData gd = new GridData(GridData.FILL_BOTH);
		composite.setLayout(layout);
		gd.widthHint = 300;
		composite.setLayoutData(gd);

		dataViewer = new TreeViewer(composite, SWT.BORDER | SWT.V_SCROLL);
		dataViewer.setContentProvider(new TreeViewerContentProvider());
		dataViewer.setLabelProvider(new LabelProvider());
		dataViewer.setInput(applyInputDatas());
		dataViewer.addFilter(oddFilter);
		dataViewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));

		return composite;
	}

	private List applyInputDatas()
	{
		List numbers = new ArrayList();
		for (int i = 0; i < 10; i++)
		{
			numbers.add(i);
		}

		return numbers;
	}

	ViewerFilter oddFilter = new ViewerFilter()
	{
		public boolean select(Viewer viewer, Object parentElement,
				Object element)
		{
			Integer temp = (Integer) element;
			int obj = temp.intValue();
			if (obj % 2 == 0)
				return true;

			return false;
		}
	};
	
	public static void main(String[] args)
	{
		FilterViewerDialog filter = new FilterViewerDialog(null);
		filter.open();
	}
}


import java.util.List;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;


public class TreeViewerContentProvider implements ITreeContentProvider
{

	public void dispose()
	{

	}

	public void inputChanged(Viewer viewer, Object oldInput, Object newInput)
	{

	}

	public Object[] getChildren(Object parentElement)
	{
		if(parentElement instanceof List)
		{
			return ((List) parentElement).toArray();
		}
		return new Object[0];
	}

	public Object getParent(Object element)
	{
		return null;
	}

	public boolean hasChildren(Object element)
	{
		if(element instanceof List)
		{
			return ((List) element).size() > 0;
		}
		return false;
	}

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

}







 Viewer提供一套机制ViewerFilter,给Viewer展示过滤

你可能感兴趣的:(eclipse,UI)