复选框树查看器未显示在复合材料上
Checkbox tree viewer is not displaying on composite
我实现了我的自定义复选框树查看器,它显示了简单的组织层次结构。组织有一名(或)多名经理,且每位经理手下有一名(或)多名下属。
Organization
Manager1
reportee1
reportee2
Manager2
reportee1
reportee2
reportee3
....
....
reporteeN
但是当我尝试将树查看器添加到合成中时,它没有显示。我正在添加代码供您参考。请让我知道我在哪里做错了。
Employee.java
package com.sample;
public class Employee {
private String name;
private boolean inOffice;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isInOffice() {
return inOffice;
}
public void setInOffice(boolean inOffice) {
this.inOffice = inOffice;
}
}
Manager.java
package com.sample;
import java.util.ArrayList;
import java.util.List;
public class Manager {
private String name;
private final List<Employee> employees = new ArrayList<> ();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Employee> getEmployees() {
return employees;
}
}
Organization.java
package com.sample;
import java.util.ArrayList;
import java.util.List;
public class Organization {
private String name;
private List<Manager> managers = new ArrayList<>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Manager> getManagers() {
return managers;
}
}
DataUtil.java
package com.sample;
public class DataUtil {
public static Organization getOrganization() {
Organization oranization = new Organization();
oranization.setName("XYZ Corporation");
Manager manager1 = new Manager();
manager1.setName("Mukund Dixit");
Manager manager2 = new Manager();
manager2.setName("Anand Bandaru");
Manager manager3 = new Manager();
manager3.setName("Shreyas Desai");
Manager manager4 = new Manager();
manager4.setName("VadiRaj");
Employee emp1 = new Employee();
emp1.setInOffice(true);
emp1.setName("Keerthi Shetty");
Employee emp2 = new Employee();
emp2.setInOffice(true);
emp2.setName("Karthik");
Employee emp3 = new Employee();
emp3.setInOffice(true);
emp3.setName("Brijesh");
Employee emp4 = new Employee();
emp4.setInOffice(true);
emp4.setName("Deepank Bansal");
Employee emp5 = new Employee();
emp5.setInOffice(true);
emp5.setName("Reshmi George");
Employee emp6 = new Employee();
emp6.setInOffice(true);
emp6.setName("Janaki Sriram");
Employee emp7 = new Employee();
emp7.setInOffice(false);
emp7.setName("Aravind Phaneendra");
Employee emp8 = new Employee();
emp8.setInOffice(true);
emp8.setName("Nagendra");
Employee emp9 = new Employee();
emp9.setInOffice(true);
emp9.setName("Maruthi");
Employee emp10 = new Employee();
emp10.setInOffice(true);
emp10.setName("Piyush");
Employee emp11 = new Employee();
emp11.setInOffice(true);
emp11.setName("Shanmugham");
Employee emp12 = new Employee();
emp12.setInOffice(false);
emp12.setName("Phalgun Garimella");
Employee emp13 = new Employee();
emp13.setInOffice(false);
emp13.setName("Sankalp");
Employee emp14 = new Employee();
emp14.setInOffice(false);
emp14.setName("Arpan");
Employee emp15 = new Employee();
emp15.setInOffice(false);
emp15.setName("Sandesh");
Employee emp16 = new Employee();
emp16.setInOffice(false);
emp16.setName("Senthil");
manager1.getEmployees().add(emp1);
manager1.getEmployees().add(emp2);
manager1.getEmployees().add(emp3);
manager1.getEmployees().add(emp4);
manager2.getEmployees().add(emp5);
manager2.getEmployees().add(emp6);
manager2.getEmployees().add(emp7);
manager2.getEmployees().add(emp8);
manager3.getEmployees().add(emp9);
manager3.getEmployees().add(emp10);
manager3.getEmployees().add(emp11);
manager4.getEmployees().add(emp12);
manager4.getEmployees().add(emp13);
manager4.getEmployees().add(emp14);
manager4.getEmployees().add(emp15);
manager4.getEmployees().add(emp16);
oranization.getManagers().add(manager1);
oranization.getManagers().add(manager2);
oranization.getManagers().add(manager3);
oranization.getManagers().add(manager4);
return oranization;
}
}
MyCheckBoxLabelProvider.java
package com.sample;
import org.eclipse.jface.viewers.CellLabelProvider;
import org.eclipse.jface.viewers.ViewerCell;
public class MyCheckBoxLabelProvider extends CellLabelProvider {
@Override
public void update(ViewerCell cell) {
Object element = cell.getElement();
if (element instanceof Organization) {
cell.setText("Org: ");
} else if (element instanceof Manager) {
cell.setText("M: ");
} else {
cell.setText("Emp: ");
}
}
}
MyCheckBoxTreeContentProvider.java
package com.sample;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
public class MyCheckBoxTreeContentProvider implements ITreeContentProvider {
public MyCheckBoxTreeContentProvider() {
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
// TODO Auto-generated method stub
}
@Override
public Object[] getChildren(Object obj) {
if (obj instanceof Organization) {
return ((Organization) obj).getManagers().toArray();
}
if (obj instanceof Manager) {
return ((Manager) obj).getEmployees().toArray();
}
return new Object[0];
}
@Override
public Object[] getElements(Object arg0) {
Object[] roots = new Object[1];
roots[0] = DataUtil.getOrganization();
return roots;
}
@Override
public Object getParent(Object arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean hasChildren(Object obj) {
if (obj instanceof Organization || obj instanceof Manager) {
return true;
}
return false;
}
}
MyCheckBoxTreeSorter.java
package com.sample;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
public class MyCheckBoxTreeSorter extends ViewerSorter {
@Override
public int compare(Viewer viewer, Object left, Object right) {
if (left instanceof Manager && right instanceof Manager) {
return ((Manager) left).getName().compareTo(((Manager) right).getName());
}
if (left instanceof Manager && right instanceof Manager) {
return ((Employee) left).getName().compareTo(((Employee) right).getName());
}
return 0;
}
}
MyCheckBoxTreeViewer.java
package com.sample;
import java.util.List;
import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.ICheckStateProvider;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.widgets.Widget;
public class MyCheckBoxTreeViewer extends CheckboxTreeViewer implements ICheckStateListener, ICheckStateProvider {
public MyCheckBoxTreeViewer(Composite parent, int style) {
super(parent, style);
ColumnViewerToolTipSupport.enableFor(this);
initializeTree();
}
private void initializeTree() {
setCheckStateProvider(this);
addCheckStateListener(this);
}
@Override
public boolean isChecked(Object obj) {
if (obj instanceof Manager) {
Manager manager = (Manager) obj;
List<Employee> employees = manager.getEmployees();
for (Employee emp : employees) {
if (!emp.isInOffice()) {
return false;
}
}
}
return true;
}
@Override
public boolean isGrayed(Object obj) {
int totalEmpsInOffice = 0;
if (obj instanceof Manager) {
Manager manager = (Manager) obj;
List<Employee> employees = manager.getEmployees();
for (Employee emp : employees) {
if (emp.isInOffice())
totalEmpsInOffice++;
}
if (totalEmpsInOffice == employees.size()) {
return true;
}
}
return false;
}
@Override
public void checkStateChanged(CheckStateChangedEvent event) {
Object element = event.getElement();
Widget item = findItem(element);
if (!(item instanceof TreeItem)) {
System.out.println("Given item is not an instance of tree item");
return;
}
TreeItem treeItem = (TreeItem) item;
Object obj = treeItem.getData();
if (obj instanceof Manager) {
System.out.println("State is changed for " + ((Manager) obj).getName());
}
if (obj instanceof Employee) {
System.out.println("State is changed for " + ((Employee) obj).getName());
}
}
}
TestTreeViewer.java
package com.sample;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class TestTreeViewer {
private static int shellWidth = 600;
private static int shellHeight = 600;
private static int compositeWidth = 500;
private static int compositeHeight = 250;
private static void addWidgetsToShell(Display display, Shell shell) {
Composite composite = new Composite(shell, SWT.BORDER);
composite.setBackground(new Color(display, 102, 153, 153));
composite.setBounds(10, 10, compositeWidth, compositeHeight);
MyCheckBoxTreeContentProvider contentProvider = new MyCheckBoxTreeContentProvider();
MyCheckBoxLabelProvider labelProvider = new MyCheckBoxLabelProvider();
MyCheckBoxTreeSorter sorter = new MyCheckBoxTreeSorter();
MyCheckBoxTreeViewer treeViewer = new MyCheckBoxTreeViewer(shell, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
GridDataFactory.fillDefaults().grab(true, true).applyTo(treeViewer.getTree());
treeViewer.setContentProvider(contentProvider);
treeViewer.setLabelProvider(labelProvider);
treeViewer.setSorter(sorter);
//treeViewer.setInput("root");
treeViewer.setAutoExpandLevel(2);
}
public static void main(String[] args) {
/* Instantiate Display object, it represents SWT session */
Display display = new Display();
/*
* Define Shell, it represent a window, You can add more than one shell
* to Display
*/
Shell shell = new Shell(display);
shell.setSize(shellWidth, shellHeight);
addWidgetsToShell(display, shell);
/* Open shell window */
shell.open();
/*
* Run the event dispatching loop until an exit condition occurs, which
* is typically when the main shell window is closed by the user.
*/
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
/* Dispose the display */
display.dispose();
}
}
我调试了,但是找不到problem/solution。
这里有几个问题。
您没有调用查看器 setInput
方法 - 在您调用此方法之前什么都不会发生。
复选框的父项是 Shell 而不是您创建的 Composite。您正在使用 GridData
作为复选框布局数据,但尚未为 Shell 或 Composite 设置布局。如果您使用布局数据,则必须设置布局。
您正在混合 setBounds
和布局 - 不要这样做,它不起作用。
您必须在调用 setInput
之前调用 setAutoExpandLevel
。
所以对于 Shell 做这样的事情:
Shell shell = new Shell(display);
shell.setSize(shellWidth, shellHeight);
shell.setLayout(new GridLayout());
对于复合:
Composite composite = new Composite(shell, SWT.BORDER);
composite.setBackground(new Color(shell.getDisplay(), 102, 153, 153));
composite.setLayout(new GridLayout());
GridDataFactory.fillDefaults().hint(compositeWidth, compositeHeight).applyTo(composite);
对于复选框:
MyCheckBoxTreeViewer treeViewer = new MyCheckBoxTreeViewer(composite, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
GridDataFactory.fillDefaults().grab(true, true).applyTo(treeViewer.getTree());
treeViewer.setContentProvider(contentProvider);
treeViewer.setLabelProvider(labelProvider);
treeViewer.setSorter(sorter);
treeViewer.setAutoExpandLevel(2);
treeViewer.setInput("root");
我实现了我的自定义复选框树查看器,它显示了简单的组织层次结构。组织有一名(或)多名经理,且每位经理手下有一名(或)多名下属。
Organization
Manager1
reportee1
reportee2
Manager2
reportee1
reportee2
reportee3
....
....
reporteeN
但是当我尝试将树查看器添加到合成中时,它没有显示。我正在添加代码供您参考。请让我知道我在哪里做错了。
Employee.java
package com.sample;
public class Employee {
private String name;
private boolean inOffice;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isInOffice() {
return inOffice;
}
public void setInOffice(boolean inOffice) {
this.inOffice = inOffice;
}
}
Manager.java
package com.sample;
import java.util.ArrayList;
import java.util.List;
public class Manager {
private String name;
private final List<Employee> employees = new ArrayList<> ();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Employee> getEmployees() {
return employees;
}
}
Organization.java
package com.sample;
import java.util.ArrayList;
import java.util.List;
public class Organization {
private String name;
private List<Manager> managers = new ArrayList<>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Manager> getManagers() {
return managers;
}
}
DataUtil.java
package com.sample;
public class DataUtil {
public static Organization getOrganization() {
Organization oranization = new Organization();
oranization.setName("XYZ Corporation");
Manager manager1 = new Manager();
manager1.setName("Mukund Dixit");
Manager manager2 = new Manager();
manager2.setName("Anand Bandaru");
Manager manager3 = new Manager();
manager3.setName("Shreyas Desai");
Manager manager4 = new Manager();
manager4.setName("VadiRaj");
Employee emp1 = new Employee();
emp1.setInOffice(true);
emp1.setName("Keerthi Shetty");
Employee emp2 = new Employee();
emp2.setInOffice(true);
emp2.setName("Karthik");
Employee emp3 = new Employee();
emp3.setInOffice(true);
emp3.setName("Brijesh");
Employee emp4 = new Employee();
emp4.setInOffice(true);
emp4.setName("Deepank Bansal");
Employee emp5 = new Employee();
emp5.setInOffice(true);
emp5.setName("Reshmi George");
Employee emp6 = new Employee();
emp6.setInOffice(true);
emp6.setName("Janaki Sriram");
Employee emp7 = new Employee();
emp7.setInOffice(false);
emp7.setName("Aravind Phaneendra");
Employee emp8 = new Employee();
emp8.setInOffice(true);
emp8.setName("Nagendra");
Employee emp9 = new Employee();
emp9.setInOffice(true);
emp9.setName("Maruthi");
Employee emp10 = new Employee();
emp10.setInOffice(true);
emp10.setName("Piyush");
Employee emp11 = new Employee();
emp11.setInOffice(true);
emp11.setName("Shanmugham");
Employee emp12 = new Employee();
emp12.setInOffice(false);
emp12.setName("Phalgun Garimella");
Employee emp13 = new Employee();
emp13.setInOffice(false);
emp13.setName("Sankalp");
Employee emp14 = new Employee();
emp14.setInOffice(false);
emp14.setName("Arpan");
Employee emp15 = new Employee();
emp15.setInOffice(false);
emp15.setName("Sandesh");
Employee emp16 = new Employee();
emp16.setInOffice(false);
emp16.setName("Senthil");
manager1.getEmployees().add(emp1);
manager1.getEmployees().add(emp2);
manager1.getEmployees().add(emp3);
manager1.getEmployees().add(emp4);
manager2.getEmployees().add(emp5);
manager2.getEmployees().add(emp6);
manager2.getEmployees().add(emp7);
manager2.getEmployees().add(emp8);
manager3.getEmployees().add(emp9);
manager3.getEmployees().add(emp10);
manager3.getEmployees().add(emp11);
manager4.getEmployees().add(emp12);
manager4.getEmployees().add(emp13);
manager4.getEmployees().add(emp14);
manager4.getEmployees().add(emp15);
manager4.getEmployees().add(emp16);
oranization.getManagers().add(manager1);
oranization.getManagers().add(manager2);
oranization.getManagers().add(manager3);
oranization.getManagers().add(manager4);
return oranization;
}
}
MyCheckBoxLabelProvider.java
package com.sample;
import org.eclipse.jface.viewers.CellLabelProvider;
import org.eclipse.jface.viewers.ViewerCell;
public class MyCheckBoxLabelProvider extends CellLabelProvider {
@Override
public void update(ViewerCell cell) {
Object element = cell.getElement();
if (element instanceof Organization) {
cell.setText("Org: ");
} else if (element instanceof Manager) {
cell.setText("M: ");
} else {
cell.setText("Emp: ");
}
}
}
MyCheckBoxTreeContentProvider.java
package com.sample;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
public class MyCheckBoxTreeContentProvider implements ITreeContentProvider {
public MyCheckBoxTreeContentProvider() {
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
// TODO Auto-generated method stub
}
@Override
public Object[] getChildren(Object obj) {
if (obj instanceof Organization) {
return ((Organization) obj).getManagers().toArray();
}
if (obj instanceof Manager) {
return ((Manager) obj).getEmployees().toArray();
}
return new Object[0];
}
@Override
public Object[] getElements(Object arg0) {
Object[] roots = new Object[1];
roots[0] = DataUtil.getOrganization();
return roots;
}
@Override
public Object getParent(Object arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean hasChildren(Object obj) {
if (obj instanceof Organization || obj instanceof Manager) {
return true;
}
return false;
}
}
MyCheckBoxTreeSorter.java
package com.sample;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
public class MyCheckBoxTreeSorter extends ViewerSorter {
@Override
public int compare(Viewer viewer, Object left, Object right) {
if (left instanceof Manager && right instanceof Manager) {
return ((Manager) left).getName().compareTo(((Manager) right).getName());
}
if (left instanceof Manager && right instanceof Manager) {
return ((Employee) left).getName().compareTo(((Employee) right).getName());
}
return 0;
}
}
MyCheckBoxTreeViewer.java
package com.sample;
import java.util.List;
import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.ICheckStateProvider;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.widgets.Widget;
public class MyCheckBoxTreeViewer extends CheckboxTreeViewer implements ICheckStateListener, ICheckStateProvider {
public MyCheckBoxTreeViewer(Composite parent, int style) {
super(parent, style);
ColumnViewerToolTipSupport.enableFor(this);
initializeTree();
}
private void initializeTree() {
setCheckStateProvider(this);
addCheckStateListener(this);
}
@Override
public boolean isChecked(Object obj) {
if (obj instanceof Manager) {
Manager manager = (Manager) obj;
List<Employee> employees = manager.getEmployees();
for (Employee emp : employees) {
if (!emp.isInOffice()) {
return false;
}
}
}
return true;
}
@Override
public boolean isGrayed(Object obj) {
int totalEmpsInOffice = 0;
if (obj instanceof Manager) {
Manager manager = (Manager) obj;
List<Employee> employees = manager.getEmployees();
for (Employee emp : employees) {
if (emp.isInOffice())
totalEmpsInOffice++;
}
if (totalEmpsInOffice == employees.size()) {
return true;
}
}
return false;
}
@Override
public void checkStateChanged(CheckStateChangedEvent event) {
Object element = event.getElement();
Widget item = findItem(element);
if (!(item instanceof TreeItem)) {
System.out.println("Given item is not an instance of tree item");
return;
}
TreeItem treeItem = (TreeItem) item;
Object obj = treeItem.getData();
if (obj instanceof Manager) {
System.out.println("State is changed for " + ((Manager) obj).getName());
}
if (obj instanceof Employee) {
System.out.println("State is changed for " + ((Employee) obj).getName());
}
}
}
TestTreeViewer.java
package com.sample;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class TestTreeViewer {
private static int shellWidth = 600;
private static int shellHeight = 600;
private static int compositeWidth = 500;
private static int compositeHeight = 250;
private static void addWidgetsToShell(Display display, Shell shell) {
Composite composite = new Composite(shell, SWT.BORDER);
composite.setBackground(new Color(display, 102, 153, 153));
composite.setBounds(10, 10, compositeWidth, compositeHeight);
MyCheckBoxTreeContentProvider contentProvider = new MyCheckBoxTreeContentProvider();
MyCheckBoxLabelProvider labelProvider = new MyCheckBoxLabelProvider();
MyCheckBoxTreeSorter sorter = new MyCheckBoxTreeSorter();
MyCheckBoxTreeViewer treeViewer = new MyCheckBoxTreeViewer(shell, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
GridDataFactory.fillDefaults().grab(true, true).applyTo(treeViewer.getTree());
treeViewer.setContentProvider(contentProvider);
treeViewer.setLabelProvider(labelProvider);
treeViewer.setSorter(sorter);
//treeViewer.setInput("root");
treeViewer.setAutoExpandLevel(2);
}
public static void main(String[] args) {
/* Instantiate Display object, it represents SWT session */
Display display = new Display();
/*
* Define Shell, it represent a window, You can add more than one shell
* to Display
*/
Shell shell = new Shell(display);
shell.setSize(shellWidth, shellHeight);
addWidgetsToShell(display, shell);
/* Open shell window */
shell.open();
/*
* Run the event dispatching loop until an exit condition occurs, which
* is typically when the main shell window is closed by the user.
*/
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
/* Dispose the display */
display.dispose();
}
}
我调试了,但是找不到problem/solution。
这里有几个问题。
您没有调用查看器 setInput
方法 - 在您调用此方法之前什么都不会发生。
复选框的父项是 Shell 而不是您创建的 Composite。您正在使用 GridData
作为复选框布局数据,但尚未为 Shell 或 Composite 设置布局。如果您使用布局数据,则必须设置布局。
您正在混合 setBounds
和布局 - 不要这样做,它不起作用。
您必须在调用 setInput
之前调用 setAutoExpandLevel
。
所以对于 Shell 做这样的事情:
Shell shell = new Shell(display);
shell.setSize(shellWidth, shellHeight);
shell.setLayout(new GridLayout());
对于复合:
Composite composite = new Composite(shell, SWT.BORDER);
composite.setBackground(new Color(shell.getDisplay(), 102, 153, 153));
composite.setLayout(new GridLayout());
GridDataFactory.fillDefaults().hint(compositeWidth, compositeHeight).applyTo(composite);
对于复选框:
MyCheckBoxTreeViewer treeViewer = new MyCheckBoxTreeViewer(composite, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
GridDataFactory.fillDefaults().grab(true, true).applyTo(treeViewer.getTree());
treeViewer.setContentProvider(contentProvider);
treeViewer.setLabelProvider(labelProvider);
treeViewer.setSorter(sorter);
treeViewer.setAutoExpandLevel(2);
treeViewer.setInput("root");