Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » CheckedTreeSelectionDialog and ICheckStateProvider issue(cannot make the root element as "checked" when the dialog is opened)
CheckedTreeSelectionDialog and ICheckStateProvider issue [message #647465] Thu, 06 January 2011 15:00
Carusyte Missing name is currently offline Carusyte Missing name
Messages: 27
Registered: July 2009
Junior Member
I am making a dialog that mimics the DirectoryDialog functionality but limits the user selection within a certain folder (which the setFilterPath(String) method in DirectoryDialog cannot realize) and provides multiple selection capability. Besides, I want to display a certain directory as "checked" when the dialog is opened, and I am trying to realize this by injecting ICheckStateProvider in the viewer created by CheckedTreeSelectionDialog (overriding the createViewer() method).
Here comes the problem: while I am 100% assured that the isChecked(Object) returns true when this method is invoked with the root element as its parameter, the element in the viewer still appears as unchecked, and when I test with other elements they are displayed correctly. That's weird, is this a bug somehow? I can't google any results related to this issue elsewhere.
For convenience, these are the classes involved (you can safely omit those Chinese characters):
public class CheckedTreeDirectorySelectionDialog extends
		CheckedTreeSelectionDialog {

	// private ICheckStateListener parentCheckStateOverrideListener;
	private String[] selectedFiles;

	public CheckedTreeDirectorySelectionDialog(Shell shell) {
		super(shell, new DirectoryTreeLabelProvider(),
				new DirectoryTreeContentProvider());
	}

	/**
	 * 设置根目录。传入的参数必须为{@link java.io.File}或者{@link java.lang.String}
	 * 。参数类型为String时,必须为有效的文件路径。
	 * 
	 * @param input
	 */
	@Override
	public void setInput(Object input) {
		if (input instanceof File)
			super.setInput(new File[] { (File) input });
		else if (input instanceof String)
			super.setInput(new File[] { new File((String) input) });
	}

	/**
	 * 若文件路径匹配,则设置树中的该元素为勾选状态。
	 * 
	 * @param selFiles
	 */
	public void setSelectedFiles(String[] selFiles) {
		if (selFiles == null)
			throw new IllegalArgumentException("argument cannot be null");
		if (selFiles.length == 0) {
			return;
		}
		selectedFiles = selFiles;
		Arrays.sort(selectedFiles);
	}

	private class CheckStateProvider implements ICheckStateProvider {

		@Override
		public boolean isChecked(Object element) {
			if (selectedFiles == null || selectedFiles.length < 1)
				return false;
			File file = (File) element;
			return Arrays.binarySearch(selectedFiles, file.getPath()) >= 0;
		}

		@Override
		public boolean isGrayed(Object element) {
			// 暂不使用该状态
			return false;
		}

	}

	@Override
	protected CheckboxTreeViewer createTreeViewer(Composite parent) {
		CheckboxTreeViewer viewer = super.createTreeViewer(parent);
		viewer.setCheckStateProvider(new CheckStateProvider());
		return viewer;
	}

}


public class DirectoryTreeLabelProvider extends LabelProvider {
	private Image IMG_FOLDER;

	public DirectoryTreeLabelProvider() {
		IMG_FOLDER = Activator.getImageDescriptor("icons/tool/folder16.gif")
				.createImage();
	}

	@Override
	public Image getImage(Object element) {
		return IMG_FOLDER;
	}

	@Override
	public String getText(Object element) {
		File file = (File) element;
		return file.getName().isEmpty() ? file.getPath() : file.getName();
	}

	@Override
	public void dispose() {
		IMG_FOLDER.dispose();
		super.dispose();
	}
}


public class DirectoryTreeContentProvider implements ITreeContentProvider {

	private FileFilter fileFilter = new DirectoryFilter();

	@Override
	public void dispose() {
		// do nothing
	}

	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		// do nothing
	}

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

	@Override
	public Object[] getChildren(Object parentElement) {
		File parentFile = (File) parentElement;
		return parentFile.listFiles(fileFilter);
	}

	@Override
	public Object getParent(Object element) {
		File file = (File) element;
		return file.getParentFile();
	}

	@Override
	public boolean hasChildren(Object element) {
		File file = (File) element;
		File[] subFiles = file.listFiles(fileFilter);
		return subFiles != null && subFiles.length > 0;
	}

	private class DirectoryFilter implements FileFilter {
		@Override
		public boolean accept(File pathname) {
			return pathname.isDirectory();
		}
	}

}


Previous Topic:How to set the table header font to bold in a JFace table ?
Next Topic:TitleAreaDialog using databinding validation to disable the OK button
Goto Forum:
  


Current Time: Thu Sep 18 05:52:25 GMT 2014

Powered by FUDForum. Page generated in 0.05717 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software