The following will demonstrate the usage of commands. Feel free to skip with chapter.
This chapter add the functionality to print the content of the domain model to the console. This way it is possible to verify that the changes on the JFace table are updating the model.
Create the command "de.vogella.jface.tableviewer.commands.Print" with the default handler "de.vogella.jface.tableviewer.commands.Print" (see Defining commands for details).
Implement the following coding:
package de.vogella.jface.tableviewer.commands;
import java.util.List;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import de.vogella.jface.tableviewer.model.ModelProvider;
import de.vogella.jface.tableviewer.model.Person;
public class Print extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
ListpersonList = ModelProvider.getInstance().getPersons();
for (Person p : personList) {
System.out.println(p);
}
return null;
}
}
Add your command to the menu.(see Defining commands for details).
After finishing this you will be able to print the current state of your domain model to the console via the menu. Validate that the changes you are doing in the UI are reflected the model.
This chapter shows how to add and delete Persons to and from the table.
You have to make the selection of the viewer available and will also provide an access method to the viewer of the view.
public void createPartControl(Composite parent) {
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
| SWT.V_SCROLL | SWT.FULL_SELECTION);
createColumns(viewer);
viewer.setContentProvider(new PersonContentProvider());
viewer.setLabelProvider(new PersonLabelProvider());
// Get the content for the viewer, setInput will call getElements in the
// contentProvider
viewer.setInput(ModelProvider.getInstance().getPersons());
// Make the selection available
getSite().setSelectionProvider(viewer);
}
public TableViewer getViewer() {
return viewer;
}
Create the command "de.vogella.jface.tableviewer.commands.AddPerson" with the default handler "de.vogella.jface.tableviewer.commands.AddPerson". Add the command to your menu. Again see Defining commands for details.
Create a dialog to maintain the data for the additional person. Create package "de.vogella.jface.tableviewer.dialogs" and the following class "AddPersonDialog".
package de.vogella.jface.tableviewer.dialogs;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import de.vogella.jface.tableviewer.model.Person;
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");
}
}
});
}
}
Implement the following code for the class "de.vogella.jface.tableviewer.commands.AddPerson":
package de.vogella.jface.tableviewer.commands;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import de.vogella.jface.tableviewer.View;
import de.vogella.jface.tableviewer.dialogs.AddPersonDialog;
import de.vogella.jface.tableviewer.model.ModelProvider;
public class AddPerson extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
ModelProvider persons = ModelProvider.getInstance();
AddPersonDialog dialog = new AddPersonDialog(window.getShell());
dialog.open();
if (dialog.getPerson() != null) {
persons.getPersons().add(dialog.getPerson());
// Updating the display in the view
IWorkbenchPage page = window.getActivePage();
View view = (View) page.findView(View.ID);
view.getViewer().refresh();
}
return null;
}
}
When you finished this successful you are able to add new persons in your application.
We will implement that a person can be deleted from the list after someone selected a person in the table, e.g. via a double click.
Create the command "de.vogella.jface.tableviewer.commands.DeletePerson" with the default handler "de.vogella.jface.tableviewer.commands.DeletePerson". Add the command to your menu.
Implement class "de.vogella.jface.tableviewer.commands.DeletePerson".
package de.vogella.jface.tableviewer.commands;
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import de.vogella.jface.tableviewer.View;
import de.vogella.jface.tableviewer.model.ModelProvider;
import de.vogella.jface.tableviewer.model.Person;
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) {
Listpersons = ModelProvider.getInstance().getPersons();
IStructuredSelection sel = (IStructuredSelection) selection;
for (Iteratoriterator = sel.iterator(); iterator.hasNext();) {
Person person = iterator.next();
persons.remove(person);
}
view.getViewer().refresh();
}
return null;
}
}
Please try to delete entries of the table by selection entries and executing "Delete Action".
This chapter shows how to copy the table data to the system clipboard via a command.
Create the command "de.vogella.jface.tableviewer.commands.CopyPersonClipboard" with the default handler "de.vogella.jface.tableviewer.commands.CopyPersonClipboard". Add the command to the menu.
package de.vogella.jface.tableviewer.commands;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import de.vogella.jface.tableviewer.View;
import de.vogella.jface.tableviewer.model.Person;
public class CopyPersonClipboard extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow();
IWorkbenchPage page = window.getActivePage();
IViewPart view = page.findView(View.ID);
Clipboard cb = new Clipboard(Display.getDefault());
ISelection selection = view.getSite().getSelectionProvider()
.getSelection();
ListpersonList = new ArrayList ();
if (selection != null && selection instanceof IStructuredSelection) {
IStructuredSelection sel = (IStructuredSelection) selection;
for (Iteratoriterator = sel.iterator(); iterator.hasNext();) {
Person person = iterator.next();
personList.add(person);
}
}
StringBuilder sb = new StringBuilder();
for (Person person : personList) {
sb.append(personToString(person));
}
TextTransfer textTransfer = TextTransfer.getInstance();
cb.setContents(new Object[] { sb.toString() },
new Transfer[] { textTransfer });
return null;
}
private String personToString(Person person) {
return person.getFirstName() + "\t" + person.getLastName() + "\t"
+ person.getGender() + "\t" + person.isMarried()
+ System.getProperty("line.separator");
}
}
Run your application, select a few persons, run your command and paste the result in a text editor, e.g. notepad.
0 comments:
Post a Comment