package com.quantum.ui.dialog; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.viewers.ILabelProviderListener; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.ITableLabelProvider; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.Viewer; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Shell; /** * @author BC */ public class SimpleSelectionDialog extends Dialog { class LabelProvider implements ITableLabelProvider { public Image getColumnImage(Object element, int columnIndex) { return SimpleSelectionDialog.this.image; } public String getColumnText(Object element, int columnIndex) { return (String) element; } public void addListener(ILabelProviderListener listener) { } public void dispose() { } public boolean isLabelProperty(Object element, String property) { return false; } public void removeListener(ILabelProviderListener listener) { } } class ContentProvider implements IStructuredContentProvider { public Object[] getElements(Object inputElement) { return SimpleSelectionDialog.this.objects; } public void dispose() { } public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { } } private final String title; private TableViewer viewer; private final Image image; private final String[] objects; private String selectedElement = null; /** * @param parentShell */ protected SimpleSelectionDialog(Shell parentShell, String title, String[] objects, Image image) { super(parentShell); this.title = title; this.objects = objects; this.image = image; } protected void configureShell(Shell shell) { super.configureShell(shell); shell.setText(title); GridLayout layout = new GridLayout(); shell.setLayout(layout); } protected Control createDialogArea(Composite parent) { Composite composite = new Composite(parent, 0); GridLayout layout = new GridLayout(); composite.setLayout(layout); layout.numColumns = 1; layout.verticalSpacing = 1; this.viewer = new TableViewer(composite); GridData full = new GridData(GridData.FILL_HORIZONTAL); full.widthHint = 200; full.heightHint = 50; this.viewer.getControl().setLayoutData(full); this.viewer.setLabelProvider(new LabelProvider()); this.viewer.setContentProvider(new ContentProvider()); this.viewer.setInput(this); return composite; } protected void okPressed() { IStructuredSelection selection = (IStructuredSelection) this.viewer.getSelection(); this.selectedElement = (String) selection.getFirstElement(); super.okPressed(); } /** * @return Returns the selectedElement. */ public String getSelectedElement() { return this.selectedElement; } /** * @param selectedElement The selectedElement to set. */ public void setSelectedElement(String selectedElement) { this.selectedElement = selectedElement; } }