本人最近学习TableView, 研究为什么TableView可以简化用户界面设计,先贴几张效果截图,在填代码
效果截图:
1、 代码结构:
2、运行开始效果图;
3.添加Baby
4.分别编辑Baby属性:
5. 设置防止重复添加功能
代码:(按照代码结构依次向下)
package swt.tebleviewer.babyManager;
import java.util.Date;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Item;
import swt.tebleviewer.babyManager.bean.Baby;
import swt.tebleviewer.babyManager.util.Constant;
public class BabyCellModifier implements ICellModifier {
private Viewer viewer;
public BabyCellModifier(Viewer viewer) {
this.viewer = viewer;
}
@Override
public boolean canModify(Object element, String property) {
return true;
}
@Override
public Object getValue(Object element, String property) {
Baby baby = (Baby) element;
if (Constant.NAME.equals(property)) {
return baby.getName();
} else if (Constant.MALE.equals(property)) {
return baby.isMale();
} else if (Constant.AGE.equals(property)) {
return baby.getAgeRange();
} else if (Constant.SHIRT_COLOR.equals(property)) {
return baby.getShirtColor();
} else if (Constant.PARENT_NAME.equals(property)) {
return baby.getPname();
} else if (Constant.PARENT_PHONE.equals(property)) {
return baby.getPnumber();
} else if (Constant.DATE.equals(property)) {
return baby.getDate();
} else {
return null;
}
}
@Override
public void modify(Object element, String property, Object value) {
if (element instanceof Item) {
element = ((Item) element).getData();
}
Baby baby = (Baby) element;
if (Constant.NAME.equals(property)) {
baby.setName((String) value);
} else if (Constant.MALE.equals(property)) {
baby.setMale(((Boolean) value).booleanValue());
} else if (Constant.AGE.equals(property)) {
baby.setAgeRange((Integer) value);
} else if (Constant.SHIRT_COLOR.equals(property)) {
baby.setShirtColor((RGB) value);
} else if (Constant.PARENT_NAME.equals(property)) {
baby.setPname((String) value);
} else if (Constant.PARENT_PHONE.equals(property)) {
baby.setPnumber((String) value);
} else if (Constant.DATE.equals(property)) {
baby.setDate((Date) value);
}
// Force the viewer to refresh
viewer.refresh();
}
}
package swt.tebleviewer.babyManager;
import java.util.Set;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;
public class BabyContentProvider implements IStructuredContentProvider{
@Override
public Object[] getElements(Object inputElement) {
if(inputElement instanceof Set) {
return ((Set)inputElement).toArray();
}
return new Object[0];
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
// TODO Auto-generated method stub
}
}
package swt.tebleviewer.babyManager;
import java.util.Set;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;
public class BabyContentProvider implements IStructuredContentProvider{
@Override
public Object[] getElements(Object inputElement) {
if(inputElement instanceof Set) {
return ((Set)inputElement).toArray();
}
return new Object[0];
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
// TODO Auto-generated method stub
}
}
package swt.tebleviewer.babyManager;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxCellEditor;
import org.eclipse.jface.viewers.ColorCellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import swt.tebleviewer.babyManager.bean.Baby;
import swt.tebleviewer.babyManager.util.Constant;
public class BabyManager extends ApplicationWindow {
private static Set<Baby> babys = null;
private Shell shell;
public BabyManager() {
super(null);
babys = new HashSet<Baby>();
}
public void run() {
// Don't return from open() until window closes
setBlockOnOpen(true);
// Open the main window
open();
// Dispose the display
Display.getCurrent().dispose();
}
@Override
/*
* * configure the shell
*/
protected void configureShell(Shell shell) {
super.configureShell(shell);
this.shell = shell;
InputStream in = BabyManager.class
.getResourceAsStream("PersonEditor.gif");
shell.setImage(new Image(Display.getCurrent(), in));
shell.setText("Baby Manager");
shell.setSize(800, 800);
}
@Override
/*
* * Creates the main window's contents
*
* @param parent the main window
*
* @return Control
*/
protected Control createContents(Composite parent) {
final Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(1, false));
// button to add a baby
Button addBaby = new Button(composite, SWT.PUSH);
addBaby.setText("Add Baby");
// Add the TableViewer
final TableViewer tv = new TableViewer(composite, SWT.MULTI
| SWT.FULL_SELECTION | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
// Set up the table
Table table = tv.getTable();
table.setLayoutData(new GridData(GridData.FILL_BOTH));
new TableColumn(table, SWT.CENTER).setText(Constant.NAME);
new TableColumn(table, SWT.CENTER).setText(Constant.MALE);
new TableColumn(table, SWT.CENTER).setText(Constant.AGE);
new TableColumn(table, SWT.CENTER).setText(Constant.SHIRT_COLOR);
new TableColumn(table, SWT.CENTER).setText(Constant.PARENT_NAME);
new TableColumn(table, SWT.CENTER).setText(Constant.PARENT_PHONE);
new TableColumn(table, SWT.CENTER).setText(Constant.DATE);
for (int i = 0, n = table.getColumnCount(); i < n; i++) {
table.getColumn(i).pack();
}
table.setHeaderVisible(true);
table.setLinesVisible(true);
// add Listener to addBaby Button
addBaby.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Baby baby = new Baby("baobao", true, Integer.valueOf("0"),
new RGB(255, 0, 0), "father", "1585858888", new Date());
if (babys.contains(baby)) {
MessageBox messageBox = new MessageBox(shell,
SWT.ICON_QUESTION | SWT.YES | SWT.NO);
messageBox.setMessage("The Baby already exist!");
messageBox.setText("Errors");
messageBox.open();
}
babys.add(baby);
tv.refresh();
}
});
// Create the cell editors
CellEditor[] editors = new CellEditor[7];
editors[0] = new TextCellEditor(table);
editors[1] = new CheckboxCellEditor(table);
editors[2] = new ComboBoxCellEditor(table, Constant.INSTANCES,
SWT.READ_ONLY);
editors[3] = new ColorCellEditor(table);
editors[4] = new TextCellEditor(table);
editors[5] = new TextCellEditor(table);
// init the table
tv.setContentProvider(new BabyContentProvider());
tv.setLabelProvider(new BabyLabelProvider());
tv.setInput(babys);
tv.setColumnProperties(Constant.PROPS);
tv.setCellEditors(editors);
tv.setCellModifier(new BabyCellModifier(tv));
return composite;
}
}
package swt.tebleviewer.babyManager;
public class Runner {
public static void main(String[] args) {
new BabyManager().run();
}
}
PersonEditor.gif(use it to set the shell)
package swt.tebleviewer.babyManager.bean;
import java.util.Date;
import org.eclipse.swt.graphics.RGB;
public class Baby {
private String name;
private boolean male;
private Integer ageRange;
private RGB shirtColor;
private String pname;
private String pnumber;
private Date date;
public Baby(String name, boolean male, Integer ageRange, RGB shirtColor,
String pname, String pnumber, Date date) {
super();
this.name = name;
this.male = male;
this.ageRange = ageRange;
this.shirtColor = shirtColor;
this.pname = pname;
this.pnumber = pnumber;
this.date = date;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isMale() {
return male;
}
public void setMale(boolean male) {
this.male = male;
}
public Integer getAgeRange() {
return ageRange;
}
public void setAgeRange(Integer ageRange) {
this.ageRange = ageRange;
}
public RGB getShirtColor() {
return shirtColor;
}
public void setShirtColor(RGB shirtColor) {
this.shirtColor = shirtColor;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public String getPnumber() {
return pnumber;
}
public void setPnumber(String pnumber) {
this.pnumber = pnumber;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Baby) {
Baby b = (Baby) obj;
return this.name.equals(b.getName()) && this.male == b.isMale()
&& this.ageRange.equals(b.getAgeRange())
&& this.getShirtColor().equals(b.getShirtColor())
&& this.pname.equals(b.getPname())
&& this.pnumber.equals(b.getPnumber());
}
return super.equals(obj);
}
@Override
public int hashCode() {
return this.name.hashCode() * 12 + this.pname.hashCode() * 7
+ this.pnumber.hashCode();
}
@Override
public String toString() {
return this.name + ", his father or mather is " + this.pname;
}
}
package swt.tebleviewer.babyManager.service;
public interface BabyDao {
//if necessary, to add
}
package swt.tebleviewer.babyManager.service;
public class BabyDaoImple implements BabyDao {
//if necessary add
}
package swt.tebleviewer.babyManager.util;
public class Constant {
/**
* Table column names/properties
*/
public static final String NAME = " Name ";
public static final String MALE = " Male? ";
public static final String AGE = "Age Range";
public static final String SHIRT_COLOR = " Shirt Color ";
public static final String PARENT_NAME = "Parent Name";
public static final String PARENT_PHONE = " Parent Phone ";
public static final String DATE = " Date ";
public static final String[] PROPS = { NAME, MALE, AGE, SHIRT_COLOR,
PARENT_NAME, PARENT_PHONE, DATE };
/**
* baby age range
*/
public static final String NONE = "";
public static final String BABY = "0 - 3";
public static final String TODDLER = "4 - 7";
public static final String CHILD = "8 - 12";
public static final String TEENAGER = "13 - 19";
public static final String ADULT = "20 - ?";
public static final String[] INSTANCES = { NONE, BABY, TODDLER, CHILD,
TEENAGER, ADULT };
}
代码完……
good luck.^_^