new sql.gif
[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 com.quantum.model.Displayable;
9
10 import org.eclipse.jface.dialogs.Dialog;
11 import org.eclipse.jface.dialogs.IDialogConstants;
12 import org.eclipse.jface.viewers.ILabelProviderListener;
13 import org.eclipse.jface.viewers.ISelectionChangedListener;
14 import org.eclipse.jface.viewers.IStructuredContentProvider;
15 import org.eclipse.jface.viewers.IStructuredSelection;
16 import org.eclipse.jface.viewers.ITableLabelProvider;
17 import org.eclipse.jface.viewers.SelectionChangedEvent;
18 import org.eclipse.jface.viewers.StructuredSelection;
19 import org.eclipse.jface.viewers.TableViewer;
20 import org.eclipse.jface.viewers.Viewer;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Shell;
29
30
31 /**
32  * @author BC
33  */
34 public class SimpleSelectionDialog extends Dialog {
35         
36         class LabelProvider implements ITableLabelProvider {
37
38                 public Image getColumnImage(Object element, int columnIndex) {
39                         return SimpleSelectionDialog.this.image;
40                 }
41
42                 public String getColumnText(Object element, int columnIndex) {
43                         if (element instanceof Displayable) {
44                                 return ((Displayable) element).getDisplayName();
45                         } else {
46                                 return element.toString();
47                         }
48                 }
49
50                 public void addListener(ILabelProviderListener listener) {
51                 }
52
53                 public void dispose() {
54                 }
55
56                 public boolean isLabelProperty(Object element, String property) {
57                         return false;
58                 }
59
60                 public void removeListener(ILabelProviderListener listener) {
61                 }
62         }
63         
64         class ContentProvider implements IStructuredContentProvider {
65
66                 public Object[] getElements(Object inputElement) {
67                         return SimpleSelectionDialog.this.objects;
68                 }
69
70                 public void dispose() {
71                 }
72
73                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
74                 }
75                 
76         }
77
78         private final String title;
79         private TableViewer viewer;
80         private final Image image;
81         private final Object[] objects;
82         
83         private List selection = Collections.synchronizedList(new ArrayList());
84         private final boolean multipleSelection;
85
86         /**
87          * @param parentShell
88          */
89         public SimpleSelectionDialog(Shell parentShell, String title, 
90                         Object[] objects, Image image) {
91                 this(parentShell, title, objects, image, false);
92         }
93         public SimpleSelectionDialog(Shell parentShell, String title, 
94                         Object[] objects, Image image, boolean multipleSelection) {
95                 super(parentShell);
96                 this.title = title;
97                 this.objects = objects;
98                 this.image = image;
99                 this.multipleSelection = multipleSelection;
100         }
101
102     protected void configureShell(Shell shell) {
103         super.configureShell(shell);
104         shell.setText(title);
105         GridLayout layout = new GridLayout();
106         shell.setLayout(layout);
107     }
108         
109     protected Control createDialogArea(Composite parent) {
110
111         Composite composite = new Composite(parent, 0);
112         GridLayout layout = new GridLayout();
113         composite.setLayout(layout);
114         layout.numColumns = 1;
115         layout.verticalSpacing = 1;
116         
117         int style = SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL;
118         if (this.multipleSelection) {
119                 style |= SWT.MULTI;
120         }
121         this.viewer = new TableViewer(composite, style);
122         GridData full = new GridData(GridData.FILL_BOTH);
123         full.widthHint = 300;
124         full.heightHint = 100;
125         this.viewer.getControl().setLayoutData(full);
126         
127         this.viewer.setLabelProvider(new LabelProvider());
128         this.viewer.setContentProvider(new ContentProvider());
129
130         this.viewer.setInput(this);
131         
132         this.viewer.addSelectionChangedListener(new ISelectionChangedListener() {
133
134                         public void selectionChanged(SelectionChangedEvent event) {
135                                 Button okButton = getButton(IDialogConstants.OK_ID);
136                                 okButton.setEnabled(!event.getSelection().isEmpty());
137                         }
138                 
139         });
140
141         return composite;
142     }
143     
144         protected void createButtonsForButtonBar(Composite parent) {
145                 super.createButtonsForButtonBar(parent);
146                 Button okButton = getButton(IDialogConstants.OK_ID);
147                 okButton.setEnabled(false);
148         }
149         
150         protected void okPressed() {
151         IStructuredSelection selection = (IStructuredSelection) this.viewer.getSelection();
152         this.selection.clear();
153         for (Iterator i = selection.iterator(); i.hasNext();) {
154                         this.selection.add(i.next());
155                 }
156         super.okPressed();
157         }
158         /**
159          * @return Returns the selectedElement.
160          */
161         public IStructuredSelection getSelection() {
162                 return new StructuredSelection(this.selection);
163         }
164 }