私を正しい軌道に乗せ、初期コードを提供してくれたBazに感謝します。しかし、これらすべてを取り入れたので、RCPビュー自体で使用するクリーナーが必要でした。また、コンボボックスの一般的なインスタンスが将来的には便利になるので、すべての定型的なものを新しいクラスにラップしました。次のように使用できます。
List<Person> persons = new ArrayList<Person>();
persons.add(new Person("Baz",26));
persons.add(new Person("Glen",27));
persons.add(new Person("Jimmy",18));
TypedComboBox<Person> box = new TypedComboBox<Person>(parent);
box.addSelectionListener(new TypedComboBoxSelectionListener<Person>() {
@Override
public void selectionChanged(TypedComboBox<Person> typedComboBox,
Person newSelection) {
System.out.println(newSelection);
}
});
box.setLabelProvider(new TypedComboBoxLabelProvider<Person>() {
@Override
public String getSelectedLabel(Person element) {
return element.getName();
}
@Override
public String getListLabel(Person element) {
return element.getName() + " | " + element.getAge();
}
});
box.setContent(persons);
box.selectFirstItem();
さまざまなオブジェクトを含む複数の選択ボックスが必要になるビューの場合、ビューのコードの本体にボイラープレートコードをキャストしたり再現したりすることはないので、型付きボックスを使用する方がはるかに満足です。
toSting()メソッドを使用するだけの場合は、ラベルプロバイダーを設定する必要はありません。それ以外の場合は、選択したアイテム用と他のすべてのアイテム用の2つのラベルを提供します。
誰かが私と同じ問題でこの質問に出くわした場合に備えて、ここに私のコードがあります、フィードバックをいただければ幸いです。
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class TypedComboBox<T> {
private ComboViewer viewer;
private TypedComboBoxLabelProvider<T> labelProvider;
private List<T> content;
private List<TypedComboBoxSelectionListener<T>> selectionListeners;
private T currentSelection;
public TypedComboBox(Composite parent) {
this.viewer = new ComboViewer(parent, SWT.READ_ONLY);
this.viewer.setContentProvider(ArrayContentProvider.getInstance());
viewer.setLabelProvider(new LabelProvider() {
@Override
public String getText(Object element) {
T typedElement = getTypedObject(element);
if (labelProvider != null && typedElement != null) {
if (typedElement == currentSelection) {
return labelProvider.getSelectedLabel(typedElement);
} else {
return labelProvider.getListLabel(typedElement);
}
} else {
return element.toString();
}
}
});
viewer.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
IStructuredSelection selection = (IStructuredSelection) event
.getSelection();
T typedSelection = getTypedObject(selection.getFirstElement());
if (typedSelection != null) {
currentSelection = typedSelection;
viewer.refresh();
notifySelectionListeners(typedSelection);
}
}
});
this.content = new ArrayList<T>();
this.selectionListeners = new ArrayList<TypedComboBoxSelectionListener<T>>();
}
public void setLabelProvider(TypedComboBoxLabelProvider<T> labelProvider) {
this.labelProvider = labelProvider;
}
public void setContent(List<T> content) {
this.content = content;
this.viewer.setInput(content.toArray());
}
public T getSelection() {
return currentSelection;
}
public void setSelection(T selection) {
if (content.contains(selection)) {
viewer.setSelection(new StructuredSelection(selection), true);
}
}
public void selectFirstItem() {
if (content.size()>0) {
setSelection(content.get(0));
}
}
public void addSelectionListener(TypedComboBoxSelectionListener<T> listener) {
this.selectionListeners.add(listener);
}
public void removeSelectionListener(
TypedComboBoxSelectionListener<T> listener) {
this.selectionListeners.remove(listener);
}
private T getTypedObject(Object o) {
if (content.contains(o)) {
return content.get(content.indexOf(o));
} else {
return null;
}
}
private void notifySelectionListeners(T newSelection) {
for (TypedComboBoxSelectionListener<T> listener : selectionListeners) {
listener.selectionChanged(this, newSelection);
}
}
そして、ラベルプロバイダーインターフェイス。
public interface TypedComboBoxLabelProvider<T> {
public String getSelectedLabel(T element);
public String getListLabel(T element);
}
そして選択リスナー:
public interface TypedComboBoxSelectionListener<T> {
public void selectionChanged(TypedComboBox<T> typedComboBox, T newSelection);
}