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