Java tutorial
/******************************************************************************* * Copyright (C) 2015 - Amit Kumar Mondal <admin@amitinside.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *******************************************************************************/ package com.amitinside.e4.rcp.todo.parts; import java.util.List; import javax.annotation.PostConstruct; import javax.inject.Inject; import org.eclipse.core.commands.ParameterizedCommand; import org.eclipse.e4.core.commands.ECommandService; import org.eclipse.e4.core.commands.EHandlerService; import org.eclipse.e4.core.contexts.IEclipseContext; import org.eclipse.e4.core.di.annotations.Optional; import org.eclipse.e4.core.services.nls.ILocaleChangeService; import org.eclipse.e4.core.services.nls.Translation; import org.eclipse.e4.ui.di.Focus; import org.eclipse.e4.ui.di.UIEventTopic; import org.eclipse.e4.ui.workbench.modeling.ESelectionService; import org.eclipse.jface.viewers.ArrayContentProvider; import org.eclipse.jface.viewers.ComboViewer; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import com.amitinside.e4.rcp.todo.events.MyEventConstants; import com.amitinside.e4.rcp.todo.i18n.Messages; import com.amitinside.e4.rcp.todo.model.ITodoService; import com.amitinside.e4.rcp.todo.model.Todo; public class TodoDeletionPart { @Inject private ITodoService model; @Inject ESelectionService selectionService; @Inject @Translation Messages messages; @Inject ILocaleChangeService localeChangeService; @Inject EHandlerService handlerService; @Inject ECommandService commandService; @Inject IEclipseContext ctx; private ComboViewer viewer; @PostConstruct public void createControls(Composite parent) { parent.setLayout(new GridLayout(2, false)); viewer = new ComboViewer(parent, SWT.READ_ONLY); viewer.setLabelProvider(new LabelProvider() { @Override public String getText(Object element) { Todo todo = (Todo) element; return todo.getSummary(); } }); viewer.setContentProvider(ArrayContentProvider.getInstance()); List<Todo> todos = model.getTodos(); updateViewer(todos); Button button = new Button(parent, SWT.PUSH); button.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { ISelection selection = viewer.getSelection(); IStructuredSelection sel = (IStructuredSelection) selection; if (sel.size() > 0) { selectionService.setSelection(sel.getFirstElement()); // assure that "com.example.e4.rcp.todo.remove" is the ID // of the command which deletes todos in your application model ParameterizedCommand cmd = commandService.createCommand("com.example.e4.rcp.todo.remove", null); handlerService.executeHandler(cmd, ctx); } } }); button.setText(messages.TodoDeletionPart_0); } private void updateViewer(List<Todo> todos) { viewer.setInput(todos); if (todos.size() > 0) { viewer.setSelection(new StructuredSelection(todos.get(0))); } } @Inject @Optional private void getNotified(@UIEventTopic(MyEventConstants.TOPIC_TODO_ALLTOPICS) Todo todo) { updateViewer(model.getTodos()); } @Focus public void focus() { viewer.getControl().setFocus(); } }