问题:
我想问一下如何为“添加人”和“为表查看器(视图)删除人”创建右键单击上下文菜单?
这是我的AddPersonDialog
班
public class AddPersonDialog extends TitleAreaDialog {
private Text text1;
private Text text2;
private Person person;
private Button button1;
private Combo combo1;
public Person getPerson() {
return person;
}
public AddPersonDialog(Shell parentShell) {
super(parentShell);
}
@Override
protected Control createContents(Composite parent) {
Control contents = super.createContents(parent);
setTitle("Add a new Person");
setMessage("Please enter the data of the new person",
IMessageProvider.INFORMATION);
return contents;
}
@Override
protected Control createDialogArea(Composite parent) {
GridLayout layout = new GridLayout();
layout.numColumns = 2;
parent.setLayout(layout);
Label label1 = new Label(parent, SWT.NONE);
label1.setText("First Name");
text1 = new Text(parent, SWT.BORDER);
Label label2 = new Label(parent, SWT.NONE);
label2.setText("Last Name");
text2 = new Text(parent, SWT.BORDER);
Label label3 = new Label(parent, SWT.NONE);
label3.setText("Gender");
GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
gd.horizontalSpan = 2;
combo1 = new Combo(parent, SWT.READ_ONLY);
combo1.add("male");
combo1.add("female");
button1 = new Button(parent, SWT.CHECK);
button1.setText("Is married?");
button1.setLayoutData(gd);
return parent;
}
@Override
protected void createButtonsForButtonBar(Composite parent)
{
((GridLayout) parent.getLayout()).numColumns++;
Button button = new Button(parent, SWT.PUSH);
button.setText("OK");
button.setFont(JFaceResources.getDialogFont());
button.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
if (text1.getText().length() != 0
&& text2.getText().length() != 0
&& combo1.getItem(combo1.getSelectionIndex()).length() != 0)
{
person = new Person(text1.getText(), text2.getText(),
combo1.getItem(combo1.getSelectionIndex()), button1
.getSelection());
close();
} else
{
setErrorMessage("Please enter all data");
}
}
});
}
}
DeletePerson
类
public class DeletePerson extends AbstractHandler {
@SuppressWarnings("unchecked")
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
IWorkbenchPage page = window.getActivePage();
View view = (View) page.findView(View.ID);
ISelection selection = view.getSite().getSelectionProvider()
.getSelection();
if (selection != null && selection instanceof IStructuredSelection) {
List<Person> persons = ModelProvider.INSTANCE.getPersons();
IStructuredSelection sel = (IStructuredSelection) selection;
for (Iterator<Person> iterator = sel.iterator(); iterator.hasNext();) {
Person person = iterator.next();
persons.remove(person);
}
view.getViewer().refresh();
}
return null;
}
}
这是我的tableviewer View
类
public class View extends ViewPart {
public static final String ID = "perry.tableviewer.view";
private MyViewerComparator comparator;
private TableViewer viewer;
private PersonFilter filter;
// We use icons
private static Image CHECKED1 = new Image(null, View.class.getResourceAsStream("/checkedpics/checked.gif"));
private static Image UNCHECKED1 = new Image(null, View.class.getResourceAsStream("/checkedpics/unchecked.gif"));
private Text searchText;
private static Color colorYellow = Display.getCurrent().getSystemColor(
SWT.COLOR_YELLOW);
private Button btnNewButton;
public void createPartControl(Composite parent)
{
parent.setLayout(new GridLayout(1, false));
btnNewButton = new Button(parent, SWT.PUSH);
btnNewButton.addSelectionListener(new SelectionAdapter()
{
@Override
public void widgetSelected(SelectionEvent e) {
ModelProvider persons = ModelProvider.INSTANCE;
AddPersonDialog dialog = new AddPersonDialog(getViewSite()
.getShell());
dialog.open();
if (dialog.getPerson() != null) {
persons.getPersons().add(dialog.getPerson());
View part = (View) getViewSite().getPage()
.findView(View.ID);
// Updating the display in the view
part.refresh();
}
}
});
btnNewButton.setText("Add");
Button btnDelete = new Button(parent, SWT.PUSH);
//btnDelete.addSelectionListener(listener);
btnDelete.setText("Delete");
GridLayout layout = new GridLayout(2, false);
parent.setLayout(layout);
Label searchLabel = new Label(parent, SWT.NONE);
searchLabel.setText("Search: ");
searchText = new Text(parent, SWT.BORDER | SWT.SEARCH);
searchText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
| GridData.HORIZONTAL_ALIGN_FILL));
createViewer(parent);
// Set the sorter for the table
comparator = new MyViewerComparator();
viewer.setComparator(comparator);
// New to support the search
searchText.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent ke) {
filter.setSearchText(searchText.getText());
viewer.refresh();
}
});
filter = new PersonFilter();
viewer.addFilter(filter);
getSite().setSelectionProvider(viewer);
//createContextMenu(viewer);
}
private void createViewer(Composite parent) {
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
| SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
createColumns(parent, viewer);
final Table table = viewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
viewer.setContentProvider(new ArrayContentProvider());
// Get the content for the viewer, setInput will call getElements in the
// contentProvider
viewer.setInput(ModelProvider.INSTANCE.getPersons());
// make the selection available to other views
getSite().setSelectionProvider(viewer);
// Layout the viewer
GridData gridData = new GridData();
gridData.verticalAlignment = GridData.FILL;
gridData.horizontalSpan = 2;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
viewer.getControl().setLayoutData(gridData);
}
public TableViewer getViewer() {
return viewer;
}
// This will create the columns for the table
private void createColumns(final Composite parent, final TableViewer viewer) {
String[] titles = { "First name", "Last name", "Gender", "Married" };
int[] bounds = { 100, 100, 100, 100 };
// First column is for the first name
TableViewerColumn col = createTableViewerColumn(titles[0], bounds[0], 0);
col.setLabelProvider(new StyledCellLabelProvider() {
@Override
public void update(ViewerCell cell) {
String search = searchText.getText();
Person person = (Person) cell.getElement();
String cellText = person.getFirstName();
cell.setText(cellText);
if (search != null && search.length() > 0) {
int intRangesCorrectSize[] = SearchUtil
.getSearchTermOccurrences(search, cellText);
List<StyleRange> styleRange = new ArrayList<StyleRange>();
for (int i = 0; i < intRangesCorrectSize.length / 2; i++) {
int start = intRangesCorrectSize[i];
int length = intRangesCorrectSize[++i];
StyleRange myStyledRange = new StyleRange(start,
length, null, colorYellow);
styleRange.add(myStyledRange);
}
cell.setStyleRanges(styleRange
.toArray(new StyleRange[styleRange.size()]));
} else {
cell.setStyleRanges(null);
}
super.update(cell);
}
});
col.setEditingSupport(new FirstNameEditingSupport(viewer));
// Second column is for the last name
col = createTableViewerColumn(titles[1], bounds[1], 1);
col.setLabelProvider(new CellLabelProvider() {
@Override
public void update(ViewerCell cell) {
cell.setText(((Person) cell.getElement()).getLastName());
}
});
col.setEditingSupport(new LastNameEditingSupport(viewer));
// now the gender
col = createTableViewerColumn(titles[2], bounds[2], 2);
col.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
Person p = (Person) element;
return p.getGender();
}
});
col.setEditingSupport(new GenderEditingSupport(viewer));
// now the status married
col = createTableViewerColumn(titles[3], bounds[3], 3);
col.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
return null;
}
@Override
public Image getImage(Object element) {
if (((Person) element).isMarried()) {
return CHECKED1;
} else {
return UNCHECKED1;
}
}
});
col.setEditingSupport(new MarriedEditingSupport(viewer));
}
private TableViewerColumn createTableViewerColumn(String title, int bound,
final int colNumber) {
final TableViewerColumn viewerColumn = new TableViewerColumn(viewer,
SWT.NONE);
final TableColumn column = viewerColumn.getColumn();
column.setText(title);
column.setWidth(bound);
column.setResizable(true);
column.setMoveable(true);
column.addSelectionListener(getSelectionAdapter(column, colNumber));
return viewerColumn;
}
private SelectionAdapter getSelectionAdapter(final TableColumn column,
final int index) {
SelectionAdapter selectionAdapter = new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
comparator.setColumn(index);
int dir = comparator.getDirection();
viewer.getTable().setSortDirection(dir);
viewer.refresh();
}
};
return selectionAdapter;
}
/**
* Passing the focus request to the viewer's control.
*/
public void setFocus() {
viewer.getControl().setFocus();
btnNewButton.setFocus();
}
public void refresh() {
viewer.refresh();
}
}
谢谢。