added button to start tests.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / properties / SchemaPropertyPage.java
1 package com.quantum.properties;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashSet;
7 import java.util.Iterator;
8 import java.util.List;
9 import java.util.Set;
10 import java.util.Vector;
11
12 import com.quantum.QuantumPlugin;
13 import com.quantum.model.Bookmark;
14 import com.quantum.model.BookmarkHolder;
15 import com.quantum.model.ConnectionException;
16 import com.quantum.model.Schema;
17 import com.quantum.sql.SQLHelper;
18 import com.quantum.ui.dialog.ExceptionDisplayDialog;
19 import com.quantum.util.connection.ConnectionUtil;
20 import com.quantum.view.bookmark.AddSchemaDialog;
21
22 import org.eclipse.jface.dialogs.MessageDialog;
23 import org.eclipse.jface.viewers.ILabelProviderListener;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.ISelectionChangedListener;
26 import org.eclipse.jface.viewers.IStructuredContentProvider;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.jface.viewers.ITableLabelProvider;
29 import org.eclipse.jface.viewers.SelectionChangedEvent;
30 import org.eclipse.jface.viewers.TableViewer;
31 import org.eclipse.jface.viewers.Viewer;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.events.SelectionAdapter;
34 import org.eclipse.swt.events.SelectionEvent;
35 import org.eclipse.swt.graphics.Image;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Button;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Control;
41 import org.eclipse.ui.dialogs.PropertyPage;
42
43 public class SchemaPropertyPage extends PropertyPage {
44     
45     class ContentProviderImpl implements IStructuredContentProvider {
46         public Object[] getElements(Object inputElement) {
47             List list = new ArrayList((Collection) inputElement);
48             Collections.sort(list);
49             return list.toArray();
50         }
51
52         public void dispose() {
53         }
54
55         public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
56         }
57     }
58     
59     class LabelProviderImpl implements ITableLabelProvider {
60
61         public Image getColumnImage(Object element, int columnIndex) {
62             if (columnIndex == 0) {
63                 return QuantumPlugin.getImage("schema.gif");
64             } else {
65                 return null;
66             }
67         }
68
69         public String getColumnText(Object element, int columnIndex) {
70             if (columnIndex == 0) {
71                 return ((Schema) element).getDisplayName();
72             } else {
73                 return null;
74             }
75         }
76
77         public void addListener(ILabelProviderListener listener) {
78         }
79
80         public void dispose() {
81         }
82
83         public boolean isLabelProperty(Object element, String property) {
84             return "displayName".equals(property);
85         }
86
87         public void removeListener(ILabelProviderListener listener) {
88         }
89     }
90     
91     private Set schemas = Collections.synchronizedSet(new HashSet());
92     private TableViewer schemaTable;
93     private Button addButton;
94     private Button removeButton;
95     
96     private ConnectionUtil connectionUtil = new ConnectionUtil();
97     
98     protected Control createContents(Composite parent) {
99
100         Composite composite = new Composite(parent, SWT.NONE);
101         GridLayout layout = new GridLayout();
102         layout.numColumns = 2;
103         composite.setLayout(layout);
104         GridData data = new GridData();
105         composite.setLayoutData(data);
106
107         this.schemaTable = new TableViewer(composite, 
108             SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER);
109             
110         layout = new GridLayout();
111         layout.marginWidth = 5;
112         layout.marginHeight = 5;
113         
114         this.schemaTable.getTable().setLayout(layout);
115         data = new GridData();
116         data.heightHint = 200;
117         data.widthHint = 200;
118 //        data.verticalSpan = 2;
119         this.schemaTable.getTable().setLayoutData(data);
120         this.schemaTable.setLabelProvider(new LabelProviderImpl());
121         this.schemaTable.setContentProvider(new ContentProviderImpl());
122         this.schemaTable.setInput(this.schemas);
123         
124         createButtonArea(composite);
125         
126         performDefaults();
127         return composite;
128     }
129
130     private void createButtonArea(Composite composite) {
131         GridLayout layout;
132         GridData data;
133         Composite buttonArea = new Composite(composite, SWT.NONE);
134         layout = new GridLayout();
135         layout.numColumns = 1;
136         buttonArea.setLayout(layout);
137         data = new GridData();
138         data.verticalAlignment = GridData.VERTICAL_ALIGN_BEGINNING;
139         buttonArea.setLayoutData(data);
140         
141         this.addButton = new Button(buttonArea, SWT.NULL);
142         this.addButton.setText("Add");
143         data = new GridData();
144         data.horizontalAlignment = GridData.FILL_HORIZONTAL;
145         data.verticalAlignment = GridData.VERTICAL_ALIGN_BEGINNING;
146         data.widthHint = 60;
147         this.addButton.setLayoutData(data);
148         this.addButton.addSelectionListener(new SelectionAdapter() {
149             public void widgetSelected(SelectionEvent event) {
150                 addSchema();
151             }
152         });
153         
154         this.removeButton = new Button(buttonArea, SWT.NULL);
155         this.removeButton.setText("Remove");
156         this.removeButton.setEnabled(false);
157         data = new GridData();
158         data.horizontalAlignment = GridData.FILL_HORIZONTAL;
159         data.verticalAlignment = GridData.VERTICAL_ALIGN_BEGINNING;
160         data.widthHint = 60;
161         this.removeButton.setLayoutData(data);
162         this.removeButton.addSelectionListener(new SelectionAdapter() {
163             public void widgetSelected(SelectionEvent event) {
164                 removeSchema(SchemaPropertyPage.this.schemaTable.getSelection());
165             }
166         });
167         
168         this.schemaTable.addSelectionChangedListener(new ISelectionChangedListener() {
169             public void selectionChanged(SelectionChangedEvent event) {
170                 SchemaPropertyPage.this.removeButton.setEnabled(
171                     !event.getSelection().isEmpty());
172             }
173         });
174     }
175     
176     private void addSchema() {
177         Bookmark bookmark = getBookmark();
178         boolean isAlreadyConnected = bookmark.isConnected();
179         
180         if (!isAlreadyConnected) {
181             boolean confirmed = MessageDialog.openConfirm(getShell(), "Connect Required", 
182                 "We must connect to the database to retrieve schemas.");
183             if (confirmed) {
184                 this.connectionUtil.connect(bookmark, getShell());
185             }
186         }
187         
188         try {
189             if (bookmark.isConnected()) {
190                 Vector schemas = SQLHelper.getSchemas(bookmark.getConnection());
191                 AddSchemaDialog dialog = new AddSchemaDialog(getShell(), schemas);
192                 dialog.open();
193                 if (dialog.getSelectedSchemas() != null) {
194                     Collection temp = dialog.getSelectedSchemas();
195                     for (Iterator i = temp.iterator(); i.hasNext();) {
196                         String name = (String) i.next();
197                         this.schemas.add(new Schema(name));
198                     }
199                     refreshTable();
200                 }
201     
202                 if (!isAlreadyConnected) {
203                     bookmark.disconnect();
204                 }
205             }
206         } catch (ConnectionException e) {
207             ExceptionDisplayDialog.openError(getShell(), null, null, e);
208         }
209     }
210     
211     private void removeSchema(ISelection selection) {
212         IStructuredSelection structuredSelection = (IStructuredSelection) selection;
213         for (Iterator i = structuredSelection.iterator(); i.hasNext();) {
214             Schema element = (Schema) i.next();
215             this.schemas.remove(element);
216         }
217         refreshTable();
218     }
219
220     private Bookmark getBookmark() {
221         Bookmark bookmark =
222             ((BookmarkHolder) getElement()).getBookmark();
223         return bookmark;
224     }
225
226     /**
227      * @see org.eclipse.jface.preference.PreferencePage#performApply()
228      */
229     public boolean performOk() {
230         getBookmark().setSchemas((Schema[]) this.schemas.toArray(
231             new Schema[this.schemas.size()]));
232         return true;
233     }
234     /* (non-Javadoc)
235      * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
236      */
237     protected void performDefaults() {
238         super.performDefaults();
239         Bookmark bookmark = getBookmark();
240
241         this.schemas.clear();
242         Schema[] schemas = bookmark.getSchemas();
243         for (int i = 0, length = (schemas == null) ? 0 : schemas.length;
244             i < length;
245             i++) {
246             if (!schemas[i].isDefault()) {
247                 this.schemas.add(schemas[i]);
248             }
249         }
250         refreshTable();
251     }
252
253     private void refreshTable() {
254         this.schemaTable.refresh();
255     }
256 }