11package de .vogella .plugin .adapter .views ;
22
3+ import java .util .ArrayList ;
4+ import java .util .List ;
5+
6+ import javax .annotation .PostConstruct ;
7+
8+ import org .eclipse .e4 .ui .workbench .modeling .ESelectionService ;
39import org .eclipse .jface .viewers .ArrayContentProvider ;
10+ import org .eclipse .jface .viewers .ISelectionChangedListener ;
411import org .eclipse .jface .viewers .ITableLabelProvider ;
512import org .eclipse .jface .viewers .LabelProvider ;
13+ import org .eclipse .jface .viewers .SelectionChangedEvent ;
614import org .eclipse .jface .viewers .TableViewer ;
715import org .eclipse .swt .SWT ;
816import org .eclipse .swt .graphics .Image ;
917import org .eclipse .swt .widgets .Composite ;
1018import org .eclipse .ui .ISharedImages ;
1119import org .eclipse .ui .PlatformUI ;
12- import org .eclipse .ui .part .ViewPart ;
1320
1421import de .vogella .plugin .adapter .model .Todo ;
1522
16- public class SampleView extends ViewPart {
17- public static final String ID = "de.vogella.plugin.adapter.views.SampleView" ;
23+ public class SampleView {
1824
1925 private TableViewer viewer ;
2026
21- class ViewLabelProvider extends LabelProvider implements
22- ITableLabelProvider {
27+ class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
2328 public String getColumnText (Object obj , int index ) {
2429 Todo todo = (Todo ) obj ;
2530 return todo .getSummary ();
@@ -30,43 +35,42 @@ public Image getColumnImage(Object obj, int index) {
3035 }
3136
3237 public Image getImage (Object obj ) {
33- return PlatformUI .getWorkbench ().getSharedImages ()
34- .getImage (ISharedImages .IMG_OBJ_ELEMENT );
38+ return PlatformUI .getWorkbench ().getSharedImages ().getImage (ISharedImages .IMG_OBJ_ELEMENT );
3539 }
3640 }
3741
3842 /**
3943 * This is a callback that will allow us to create the viewer and initialize
4044 * it.
4145 */
42- public void createPartControl ( Composite parent ) {
43- viewer = new TableViewer ( parent , SWT . MULTI | SWT . H_SCROLL
44- | SWT .V_SCROLL );
46+ @ PostConstruct
47+ public void createPartControl ( Composite parent , ESelectionService selectionService ) {
48+ viewer = new TableViewer ( parent , SWT . MULTI | SWT . H_SCROLL | SWT .V_SCROLL );
4549 viewer .setContentProvider (new ArrayContentProvider ());
4650 viewer .setLabelProvider (new ViewLabelProvider ());
47- getSite ().setSelectionProvider (viewer );
48- viewer .setInput (getElements ());
51+ viewer .addSelectionChangedListener (new ISelectionChangedListener () {
4952
50- }
53+ @ Override
54+ public void selectionChanged (SelectionChangedEvent event ) {
55+ selectionService .setSelection (event .getSelection ());
56+ }
57+ });
5158
52- /**
53- * Passing the focus request to the viewer's control.
54- */
55- public void setFocus () {
56- viewer .getControl ().setFocus ();
59+ viewer .setInput (getElements ());
5760 }
5861
5962 // Build up a simple data model
60- private Todo [] getElements () {
61- Todo [] todos = new Todo [ 2 ] ;
63+ private List < Todo > getElements () {
64+ List < Todo > todos = new ArrayList <>() ;
6265 Todo todo = new Todo ();
6366 todo .setSummary ("First Todo" );
6467 todo .setDescription ("A very good description" );
65- todos [ 0 ] = todo ;
68+ todos . add ( todo ) ;
6669 todo = new Todo ();
6770 todo .setSummary ("Second Todo" );
6871 todo .setDescription ("Second super description" );
69- todos [1 ] = todo ;
72+ todos .add (todo );
73+
7074 return todos ;
7175 }
7276}
0 commit comments