package com.yc.treecheck;
import java.io.File;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
import com.swtdesigner.SWTResourceManager;
/**
* 主要用于绘制菜单是显示的内容,如图标、标题等
* @company 源辰
* @author navy
*/
public class TreeLabelProvider2 extends LabelProvider implements ILabelProvider {
private static final String PACKAGE = "src";
private static final String JAVA_FILE = ".java";
private static final String XML_FILE = ".xml";
private static final String PROP_FILE = ".properties";
private Image javaFileImage = SWTResourceManager.getImage(TreeLabelProvider2.class, "/imgs/class.png");
private Image fileImage = SWTResourceManager.getImage(TreeLabelProvider2.class, "/imgs/file.png");
private Image folderFileImage = SWTResourceManager.getImage(TreeLabelProvider2.class, "/imgs/srcfolder.png");
private Image packageFolderFileImage = SWTResourceManager.getImage(TreeLabelProvider2.class, "/imgs/package.png");
@Override
public Image getImage(Object element) {
if (element instanceof File) {
File file = (File) element;
String fileName = file.getName();
if (fileName.equals(PACKAGE)) {
return packageFolderFileImage;
}
if (file.isDirectory()) {
return folderFileImage;
}
if (fileName.endsWith(JAVA_FILE)) {
return javaFileImage;
}
if (fileName.endsWith(XML_FILE)) {
return fileImage;
}
if (fileName.endsWith(PROP_FILE)) {
return fileImage;
}
}
return fileImage;
}
@Override
public String getText(Object element) {
File file = (File) element;
return file.getName();
}
@Override
public void dispose() {
javaFileImage.dispose();
packageFolderFileImage.dispose();
folderFileImage.dispose();
fileImage.dispose();
super.dispose();
}
}
package com.yc.treecheck;
import java.io.File;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
/**
* 树形结构:提供了获取下级元素和获取元素的方法
* @company 源辰
* @author navy
*/
public class TreeContentProvider2 implements ITreeContentProvider {
public Object[] getElements(Object inputElement) {
if (inputElement instanceof File[]) {
File[] fileList = (File[]) inputElement;
if (fileList == null || fileList.length <= 0) {
return new Object[0];
}
return fileList;
}
return new Object[0];
}
public Object[] getChildren(Object parentElement) {
if (parentElement instanceof File) {
File file = (File) parentElement;
File[] list = file.listFiles();
if (list == null || list.length <= 0) {
return new Object[0];
}
return list;
}
return new Object[0];
}
public boolean hasChildren(Object inputElement) {
if (inputElement instanceof File) {
File file = (File) inputElement;
File[] list = file.listFiles();
if (list == null || list.length <= 0) {
return false;
}
return true;
}
return false;
}
public Object getParent(Object element) {
return null;
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
public void dispose() {
}
}
package com.yc.treecheck;
import java.io.File;
import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import com.swtdesigner.SWTResourceManager;
import com.yc.login.Login;
public class TreeDialog2 {
protected Shell shell;
private CheckboxTreeViewer treeViewer;
private Tree tree;
/**
* Launch the application.
*
* @param args
*/
public static void main(String[] args) {
try {
TreeDialog2 window = new TreeDialog2();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("带复选框的树");
shell.setLayout(new FillLayout());
shell.setImage(SWTResourceManager.getImage(Login.class, "/images/yc.ico"));
SashForm sashForm = new SashForm(shell, SWT.NONE);
Composite composite = new Composite(sashForm, SWT.NONE);
composite.setLayout(new FillLayout());
treeViewer = new CheckboxTreeViewer(composite, SWT.BORDER | SWT.CHECK | SWT.MULTI);
treeViewer.setLabelProvider(new TreeLabelProvider2());
treeViewer.setContentProvider(new TreeContentProvider2());
tree = treeViewer.getTree();
treeViewer.setInput(new File("E:\\workspace\\SWT").listFiles());
Composite composite_1 = new Composite(sashForm, SWT.NONE);
composite_1.setLayout(new FillLayout(SWT.HORIZONTAL));
sashForm.setWeights(new int[] { 2, 3 });
treeViewer.addCheckStateListener(new ICheckStateListener() {
public void checkStateChanged(CheckStateChangedEvent arg0) { // 当状态改变是触发
CheckboxTreeViewer checkboxTreeViewer = (CheckboxTreeViewer)arg0.getSource();
boolean checked = arg0.getChecked(); // 当前节点的选中状态
checkboxTreeViewer.setSubtreeChecked(arg0.getElement(), checked); // 设置当前节点子节点的状态与当前节点一致
// TODO 当所有子节点都已经选中时,父节点也要选中
// 获取指定的选中的节点 -> 不能直接用arg0.getElement()这个获取到的是我们自定义的Item而不是TreeItem
TreeItem ti = (TreeItem)checkboxTreeViewer.testFindItem(arg0.getElement());
tree.setSelection(ti); // 选中这个节点
ti.setChecked(checked);
checkObject(ti);
}
});
}
private void checkObject(TreeItem ti) {
TreeItem parent = ti.getParentItem(); // 获取这个节点的节点,通过父节点获取这个父节点下的所有子节点,然后判断是否已经被选中
if(parent == null){
return;
}
TreeItem[] items = parent.getItems(); // 获取当前节点的所有兄弟节点
int checkItems = 0; // 记录被选中的节点的数量
for (TreeItem treeItem : items) {
if(treeItem.getChecked() && !treeItem.getGrayed()){ //Grayed:变灰的
checkItems = checkItems + 1;
}
}
if(checkItems == 0){
parent.setChecked(false);
parent.setGrayed(false);
} else if(checkItems == items.length){
parent.setGrayed(false);
parent.setChecked(true);
} else if(checkItems != items.length){
parent.setChecked(true);
parent.setGrayed(true);
}
checkObject(parent);
}
}
运行效果: