1 package com.quantum.view.widget;
3 import org.eclipse.jface.viewers.ContentViewer;
4 import org.eclipse.jface.viewers.ILabelProvider;
5 import org.eclipse.jface.viewers.ISelection;
6 import org.eclipse.jface.viewers.IStructuredContentProvider;
7 import org.eclipse.jface.viewers.IStructuredSelection;
8 import org.eclipse.jface.viewers.SelectionChangedEvent;
9 import org.eclipse.jface.viewers.StructuredSelection;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.events.SelectionAdapter;
12 import org.eclipse.swt.events.SelectionEvent;
13 import org.eclipse.swt.widgets.Combo;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.swt.widgets.Control;
20 * <p>In Eclipse version 3.0, a standard JFace ComboViewer class exists.
24 public class ComboViewer extends ContentViewer {
28 private Object[] objects = new Object[0];
30 public ComboViewer(Composite parent) {
31 this.combo = new Combo(parent, SWT.SIMPLE | SWT.DROP_DOWN | SWT.READ_ONLY);
32 this.combo.addSelectionListener(new SelectionAdapter() {
33 public void widgetSelected(SelectionEvent event) {
34 ComboViewer.this.fireSelectionChanged(new SelectionChangedEvent(
35 ComboViewer.this, getSelection()) );
40 public Control getControl() {
44 public ISelection getSelection() {
45 int index = this.combo.getSelectionIndex();
47 return (this.objects == null || index >= this.objects.length || index < 0)
48 ? new StructuredSelection()
49 : new StructuredSelection(this.objects[index]);
52 protected void inputChanged(Object input, Object oldInput) {
53 super.inputChanged(input, oldInput);
56 this.objects = new Object[0];
58 this.objects = ((IStructuredContentProvider) getContentProvider()).getElements(input);
59 for (int i = 0, length = this.objects == null ? 0 : this.objects.length; i < length; i++) {
60 String label = ((ILabelProvider) getLabelProvider()).getText(this.objects[i]);
61 this.combo.add(label);
65 public void refresh() {
68 public void setSelection(ISelection selection, boolean reveal) {
69 if (selection != null && selection instanceof IStructuredSelection) {
70 Object object = ((IStructuredSelection) selection).getFirstElement();
71 for (int i = 0, length = this.objects == null ? 0 : this.objects.length;
72 object != null && i < length; i++) {
73 if (this.objects[i] == object) {