1 package com.quantum.ui.dialog;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Iterator;
8 import org.eclipse.jface.dialogs.Dialog;
9 import org.eclipse.jface.dialogs.IDialogConstants;
10 import org.eclipse.jface.viewers.ILabelProviderListener;
11 import org.eclipse.jface.viewers.ISelectionChangedListener;
12 import org.eclipse.jface.viewers.IStructuredContentProvider;
13 import org.eclipse.jface.viewers.IStructuredSelection;
14 import org.eclipse.jface.viewers.ITableLabelProvider;
15 import org.eclipse.jface.viewers.SelectionChangedEvent;
16 import org.eclipse.jface.viewers.StructuredSelection;
17 import org.eclipse.jface.viewers.TableViewer;
18 import org.eclipse.jface.viewers.Viewer;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.swt.widgets.Shell;
32 public class SimpleSelectionDialog extends Dialog {
34 class LabelProvider implements ITableLabelProvider {
36 public Image getColumnImage(Object element, int columnIndex) {
37 return SimpleSelectionDialog.this.image;
40 public String getColumnText(Object element, int columnIndex) {
41 return element.toString();
44 public void addListener(ILabelProviderListener listener) {
47 public void dispose() {
50 public boolean isLabelProperty(Object element, String property) {
54 public void removeListener(ILabelProviderListener listener) {
58 class ContentProvider implements IStructuredContentProvider {
60 public Object[] getElements(Object inputElement) {
61 return SimpleSelectionDialog.this.objects;
64 public void dispose() {
67 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
72 private final String title;
73 private TableViewer viewer;
74 private final Image image;
75 private final Object[] objects;
77 private List selection = Collections.synchronizedList(new ArrayList());
78 private final boolean multipleSelection;
83 public SimpleSelectionDialog(Shell parentShell, String title,
84 Object[] objects, Image image) {
85 this(parentShell, title, objects, image, false);
87 public SimpleSelectionDialog(Shell parentShell, String title,
88 Object[] objects, Image image, boolean multipleSelection) {
91 this.objects = objects;
93 this.multipleSelection = multipleSelection;
96 protected void configureShell(Shell shell) {
97 super.configureShell(shell);
99 GridLayout layout = new GridLayout();
100 shell.setLayout(layout);
103 protected Control createDialogArea(Composite parent) {
105 Composite composite = new Composite(parent, 0);
106 GridLayout layout = new GridLayout();
107 composite.setLayout(layout);
108 layout.numColumns = 1;
109 layout.verticalSpacing = 1;
111 int style = SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL;
112 if (this.multipleSelection) {
115 this.viewer = new TableViewer(composite, style);
116 GridData full = new GridData(GridData.FILL_HORIZONTAL);
117 full.widthHint = 200;
118 full.heightHint = 50;
119 this.viewer.getControl().setLayoutData(full);
121 this.viewer.setLabelProvider(new LabelProvider());
122 this.viewer.setContentProvider(new ContentProvider());
124 this.viewer.setInput(this);
126 this.viewer.addSelectionChangedListener(new ISelectionChangedListener() {
128 public void selectionChanged(SelectionChangedEvent event) {
129 Button okButton = getButton(IDialogConstants.OK_ID);
130 okButton.setEnabled(!event.getSelection().isEmpty());
138 protected void createButtonsForButtonBar(Composite parent) {
139 super.createButtonsForButtonBar(parent);
140 Button okButton = getButton(IDialogConstants.OK_ID);
141 okButton.setEnabled(false);
144 protected void okPressed() {
145 IStructuredSelection selection = (IStructuredSelection) this.viewer.getSelection();
146 this.selection.clear();
147 for (Iterator i = selection.iterator(); i.hasNext();) {
148 this.selection.add(i.next());
153 * @return Returns the selectedElement.
155 public IStructuredSelection getSelection() {
156 return new StructuredSelection(this.selection);