patch from http://www.cowbarn.org/phpeclipse/
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / SchemaSelectionControl.java
1 package com.quantum.view;
2
3 import java.beans.PropertyChangeListener;
4 import java.beans.PropertyChangeSupport;
5 import java.sql.SQLException;
6 import java.util.ArrayList;
7 import java.util.Arrays;
8 import java.util.Collection;
9 import java.util.Collections;
10 import java.util.HashSet;
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.Set;
14
15 import com.quantum.ImageStore;
16 import com.quantum.Messages;
17 import com.quantum.model.Bookmark;
18 import com.quantum.model.ConnectionException;
19 import com.quantum.model.NotConnectedException;
20 import com.quantum.model.Schema;
21 import com.quantum.ui.dialog.ExceptionDisplayDialog;
22 import com.quantum.ui.dialog.SimpleSelectionDialog;
23 import com.quantum.util.connection.ConnectionUtil;
24
25 import org.eclipse.jface.dialogs.MessageDialog;
26 import org.eclipse.jface.viewers.ILabelProviderListener;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.ISelectionChangedListener;
29 import org.eclipse.jface.viewers.IStructuredContentProvider;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.jface.viewers.ITableLabelProvider;
32 import org.eclipse.jface.viewers.SelectionChangedEvent;
33 import org.eclipse.jface.viewers.TableViewer;
34 import org.eclipse.jface.viewers.Viewer;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.events.SelectionAdapter;
37 import org.eclipse.swt.events.SelectionEvent;
38 import org.eclipse.swt.graphics.Image;
39 import org.eclipse.swt.layout.GridData;
40 import org.eclipse.swt.layout.GridLayout;
41 import org.eclipse.swt.widgets.Button;
42 import org.eclipse.swt.widgets.Composite;
43
44
45 /**
46  * @author BC
47  */
48 public class SchemaSelectionControl extends Composite {
49         
50     class ContentProviderImpl implements IStructuredContentProvider {
51         public Object[] getElements(Object inputElement) {
52             List list = new ArrayList((Collection) inputElement);
53             Collections.sort(list);
54             return list.toArray();
55         }
56
57         public void dispose() {
58         }
59
60         public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
61         }
62     }
63     
64     class LabelProviderImpl implements ITableLabelProvider {
65
66         public Image getColumnImage(Object element, int columnIndex) {
67             if (columnIndex == 0) {
68                 return ImageStore.getImage(ImageStore.SCHEMA);
69             } else {
70                 return null;
71             }
72         }
73
74         public String getColumnText(Object element, int columnIndex) {
75             if (columnIndex == 0) {
76                 return ((Schema) element).getDisplayName();
77             } else {
78                 return null;
79             }
80         }
81
82         public void addListener(ILabelProviderListener listener) {
83         }
84
85         public void dispose() {
86         }
87
88         public boolean isLabelProperty(Object element, String property) {
89             return "displayName".equals(property);
90         }
91
92         public void removeListener(ILabelProviderListener listener) {
93         }
94     }
95     
96         
97     private final Bookmark bookmarkForConnection;
98     private ConnectionUtil connectionUtil = new ConnectionUtil();
99         private Set schemas = Collections.synchronizedSet(new HashSet());
100         private TableViewer schemaTable;
101         private Button useAllSchemasButton;
102         
103         private int schemaRule;
104         
105         private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
106         private Button useUsernameAsSchemaButton;
107         private Button useSelectedSchemasButton;
108         private Button removeButton;
109         private Button addButton;
110
111         /**
112          * @param parent
113          * @param style
114          */
115         public SchemaSelectionControl(Composite parent, Bookmark bookmarkForConnection) {
116                 super(parent, SWT.NONE);
117                 this.bookmarkForConnection = bookmarkForConnection;
118                 
119                 Schema[] schemas = bookmarkForConnection.getSchemaSelections();
120                 this.schemas.addAll(Arrays.asList(schemas));
121                 this.schemaRule = this.bookmarkForConnection.getSchemaRule();
122                 createContents();
123         }
124
125         protected void createContents() {
126
127         GridLayout layout = new GridLayout();
128         layout.numColumns = 1;
129         setLayout(layout);
130         GridData data = new GridData(GridData.FILL_BOTH);
131         setLayoutData(data);
132
133         this.useAllSchemasButton = new Button(this, SWT.RADIO);
134         this.useAllSchemasButton.setText(Messages.getString(getClass(), "useAllSchemas")); //$NON-NLS-1$
135                 this.useAllSchemasButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
136                 this.useAllSchemasButton.addSelectionListener(new SelectionAdapter() {
137                         public void widgetSelected(SelectionEvent event) {
138                                 setSchemaRule(Bookmark.SCHEMA_RULE_USE_ALL);
139                                 updateControls();
140                         }
141                 });
142                 
143         this.useUsernameAsSchemaButton = new Button(this, SWT.RADIO);
144         this.useUsernameAsSchemaButton.setText(Messages.getString(getClass(), "useUsernameAsSchema")); //$NON-NLS-1$
145         this.useUsernameAsSchemaButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
146                 this.useUsernameAsSchemaButton.addSelectionListener(new SelectionAdapter() {
147                         public void widgetSelected(SelectionEvent event) {
148                                 setSchemaRule(Bookmark.SCHEMA_RULE_USE_DEFAULT);
149                                 updateControls();
150                         }
151                 });
152                 
153         this.useSelectedSchemasButton = new Button(this, SWT.RADIO);
154         this.useSelectedSchemasButton.setText(Messages.getString(getClass(), "useSelectedSchemas")); //$NON-NLS-1$
155         this.useSelectedSchemasButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
156                 this.useSelectedSchemasButton.addSelectionListener(new SelectionAdapter() {
157                         public void widgetSelected(SelectionEvent event) {
158                                 setSchemaRule(Bookmark.SCHEMA_RULE_USE_SELECTED);
159                                 updateControls();
160                         }
161                 });
162                 
163                 Composite composite = new Composite(this, SWT.NONE);
164         layout = new GridLayout();
165         layout.numColumns = 2;
166         composite.setLayout(layout);
167         data = new GridData(GridData.FILL_BOTH);
168         composite.setLayoutData(data);
169                 
170         this.schemaTable = new TableViewer(composite, 
171                                             SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER);
172                 layout = new GridLayout();
173         layout.marginWidth = 5;
174         layout.marginHeight = 5;
175         
176         this.schemaTable.getTable().setLayout(layout);
177         data = new GridData(GridData.FILL_BOTH);
178         this.schemaTable.getTable().setLayoutData(data);
179         this.schemaTable.setLabelProvider(new LabelProviderImpl());
180         this.schemaTable.setContentProvider(new ContentProviderImpl());
181         this.schemaTable.setInput(this.schemas);
182         
183         createButtonArea(composite);
184         
185         updateControls();
186     }
187         
188         private void updateControls() {
189                 this.useAllSchemasButton.setSelection(this.schemaRule == Bookmark.SCHEMA_RULE_USE_ALL);
190                 this.useUsernameAsSchemaButton.setSelection(this.schemaRule == Bookmark.SCHEMA_RULE_USE_DEFAULT);
191                 
192                 boolean enabled = (this.schemaRule != Bookmark.SCHEMA_RULE_USE_ALL
193                                 && this.schemaRule != Bookmark.SCHEMA_RULE_USE_DEFAULT);
194
195                 this.useSelectedSchemasButton.setSelection(enabled);
196         this.schemaTable.getControl().setEnabled(enabled);
197         
198         this.addButton.setEnabled(enabled);
199         this.removeButton.setEnabled(
200                         enabled && !this.schemaTable.getSelection().isEmpty());
201         }
202
203     private void createButtonArea(Composite composite) {
204         Composite buttonArea = new Composite(composite, SWT.NONE);
205         GridLayout layout = new GridLayout();
206         layout.numColumns = 1;
207         buttonArea.setLayout(layout);
208         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING);
209         buttonArea.setLayoutData(data);
210         
211         this.addButton = new Button(buttonArea, SWT.NULL);
212         this.addButton.setText("Add");
213         data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
214         this.addButton.setLayoutData(data);
215         this.addButton.addSelectionListener(new SelectionAdapter() {
216             public void widgetSelected(SelectionEvent event) {
217                 addSchema();
218             }
219         });
220         
221         this.removeButton = new Button(buttonArea, SWT.NULL);
222         this.removeButton.setText("Remove");
223         this.removeButton.setEnabled(false);
224         data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
225         this.removeButton.setLayoutData(data);
226         this.removeButton.addSelectionListener(new SelectionAdapter() {
227             public void widgetSelected(SelectionEvent event) {
228                 removeSchema(SchemaSelectionControl.this.schemaTable.getSelection());
229             }
230         });
231         
232         schemaTable.addSelectionChangedListener(new ISelectionChangedListener() {
233             public void selectionChanged(SelectionChangedEvent event) {
234                 updateControls();
235             }
236         });
237     }
238     private void addSchema() {
239         Bookmark bookmark = getBookmark();
240         boolean isAlreadyConnected = bookmark.isConnected();
241         
242         if (!isAlreadyConnected) {
243             boolean confirmed = MessageDialog.openConfirm(getShell(), 
244                         Messages.getString(getClass(), "connectTitle"),
245                         Messages.getString(getClass(), "connectMessage"));
246             if (confirmed) {
247                 this.connectionUtil.connect(bookmark, getShell());
248             }
249         }
250         
251         try {
252             if (bookmark.isConnected()) {
253                 List schemaList = getAllUnselectedSchemas(bookmark);
254                 SimpleSelectionDialog dialog = new SimpleSelectionDialog(
255                                 getShell(), Messages.getString(getClass(), "addSchemaDialog"), 
256                                                 schemaList.toArray(), 
257                                                 ImageStore.getImage(ImageStore.SCHEMA), true);
258                 int result = dialog.open();
259                 if (result == SimpleSelectionDialog.OK 
260                                 && !dialog.getSelection().isEmpty()) {
261                     for (Iterator i = dialog.getSelection().iterator(); i.hasNext();) {
262                         this.schemas.add(i.next());
263                     }
264                     
265                         refreshTable();
266                         this.propertyChangeSupport.firePropertyChange("schemas", null, getSchemas());
267                 }
268     
269                 if (!isAlreadyConnected) {
270                     bookmark.disconnect();
271                 }
272             }
273         } catch (ConnectionException e) {
274             ExceptionDisplayDialog.openError(getShell(), null, null, e);
275         } catch (SQLException e) {
276             ExceptionDisplayDialog.openError(getShell(), null, null, e);
277                 }
278     }
279     
280     /**
281          * @param bookmark
282          * @return
283          * @throws NotConnectedException
284          * @throws SQLException
285          */
286         private List getAllUnselectedSchemas(Bookmark bookmark) 
287                         throws NotConnectedException, SQLException {
288                 Schema[] schemas = bookmark.getDatabase().getSchemas();
289                 List schemaList = new ArrayList(Arrays.asList(schemas));
290                 schemaList.removeAll(this.schemas);
291                 Collections.sort(schemaList);
292                 return schemaList;
293         }
294
295         private void removeSchema(ISelection selection) {
296         IStructuredSelection structuredSelection = (IStructuredSelection) selection;
297         for (Iterator i = structuredSelection.iterator(); i.hasNext();) {
298             Schema element = (Schema) i.next();
299             this.schemas.remove(element);
300         }
301         refreshTable();
302         this.propertyChangeSupport.firePropertyChange("schemas", null, getSchemas());
303     }
304
305     private Bookmark getBookmark() {
306         return this.bookmarkForConnection;
307     }
308
309     private void refreshTable() {
310         this.schemaTable.refresh();
311     }
312     
313     public Schema[] getSchemas() {
314         return (Schema[]) this.schemas.toArray(new Schema[this.schemas.size()]);
315     }
316
317     public void setSchemas(Schema[] schemas) {
318         this.schemas.clear();
319         this.schemas.addAll(Arrays.asList(schemas));
320                 updateControls();
321         refreshTable();
322         this.propertyChangeSupport.firePropertyChange("schemas", null, getSchemas());
323     }
324         public void addPropertyChangeListener(PropertyChangeListener arg0) {
325                 this.propertyChangeSupport.addPropertyChangeListener(arg0);
326         }
327         public void removePropertyChangeListener(PropertyChangeListener arg0) {
328                 this.propertyChangeSupport.removePropertyChangeListener(arg0);
329         }
330         public int getSchemaRule() {
331                 return this.schemaRule;
332         }
333         public void setSchemaRule(int schemaRule) {
334                 if (schemaRule != this.schemaRule) {
335                         int original = this.schemaRule;
336                         this.schemaRule = schemaRule;
337                         updateControls();
338                         refreshTable();
339                         this.propertyChangeSupport.firePropertyChange(
340                                         "schemaRule", original, schemaRule);
341                 }
342         }
343 }