misc
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / ui / dialog / SimpleSelectionDialog.java
1 package com.quantum.ui.dialog;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Iterator;
6 import java.util.List;
7
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;
27
28
29 /**
30  * @author BC
31  */
32 public class SimpleSelectionDialog extends Dialog {
33         
34         class LabelProvider implements ITableLabelProvider {
35
36                 public Image getColumnImage(Object element, int columnIndex) {
37                         return SimpleSelectionDialog.this.image;
38                 }
39
40                 public String getColumnText(Object element, int columnIndex) {
41                         return element.toString();
42                 }
43
44                 public void addListener(ILabelProviderListener listener) {
45                 }
46
47                 public void dispose() {
48                 }
49
50                 public boolean isLabelProperty(Object element, String property) {
51                         return false;
52                 }
53
54                 public void removeListener(ILabelProviderListener listener) {
55                 }
56         }
57         
58         class ContentProvider implements IStructuredContentProvider {
59
60                 public Object[] getElements(Object inputElement) {
61                         return SimpleSelectionDialog.this.objects;
62                 }
63
64                 public void dispose() {
65                 }
66
67                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
68                 }
69                 
70         }
71
72         private final String title;
73         private TableViewer viewer;
74         private final Image image;
75         private final Object[] objects;
76         
77         private List selection = Collections.synchronizedList(new ArrayList());
78         private final boolean multipleSelection;
79
80         /**
81          * @param parentShell
82          */
83         public SimpleSelectionDialog(Shell parentShell, String title, 
84                         Object[] objects, Image image) {
85                 this(parentShell, title, objects, image, false);
86         }
87         public SimpleSelectionDialog(Shell parentShell, String title, 
88                         Object[] objects, Image image, boolean multipleSelection) {
89                 super(parentShell);
90                 this.title = title;
91                 this.objects = objects;
92                 this.image = image;
93                 this.multipleSelection = multipleSelection;
94         }
95
96     protected void configureShell(Shell shell) {
97         super.configureShell(shell);
98         shell.setText(title);
99         GridLayout layout = new GridLayout();
100         shell.setLayout(layout);
101     }
102         
103     protected Control createDialogArea(Composite parent) {
104
105         Composite composite = new Composite(parent, 0);
106         GridLayout layout = new GridLayout();
107         composite.setLayout(layout);
108         layout.numColumns = 1;
109         layout.verticalSpacing = 1;
110         
111         int style = SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL;
112         if (this.multipleSelection) {
113                 style |= SWT.MULTI;
114         }
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);
120         
121         this.viewer.setLabelProvider(new LabelProvider());
122         this.viewer.setContentProvider(new ContentProvider());
123
124         this.viewer.setInput(this);
125         
126         this.viewer.addSelectionChangedListener(new ISelectionChangedListener() {
127
128                         public void selectionChanged(SelectionChangedEvent event) {
129                                 Button okButton = getButton(IDialogConstants.OK_ID);
130                                 okButton.setEnabled(!event.getSelection().isEmpty());
131                         }
132                 
133         });
134
135         return composite;
136     }
137     
138         protected void createButtonsForButtonBar(Composite parent) {
139                 super.createButtonsForButtonBar(parent);
140                 Button okButton = getButton(IDialogConstants.OK_ID);
141                 okButton.setEnabled(false);
142         }
143         
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());
149                 }
150         super.okPressed();
151         }
152         /**
153          * @return Returns the selectedElement.
154          */
155         public IStructuredSelection getSelection() {
156                 return new StructuredSelection(this.selection);
157         }
158 }