1 package com.quantum.properties;
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;
10 import java.util.Vector;
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;
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;
43 public class SchemaPropertyPage extends PropertyPage {
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();
52 public void dispose() {
55 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
59 class LabelProviderImpl implements ITableLabelProvider {
61 public Image getColumnImage(Object element, int columnIndex) {
62 if (columnIndex == 0) {
63 return QuantumPlugin.getImage("schema.gif");
69 public String getColumnText(Object element, int columnIndex) {
70 if (columnIndex == 0) {
71 return ((Schema) element).getDisplayName();
77 public void addListener(ILabelProviderListener listener) {
80 public void dispose() {
83 public boolean isLabelProperty(Object element, String property) {
84 return "displayName".equals(property);
87 public void removeListener(ILabelProviderListener listener) {
91 private Set schemas = Collections.synchronizedSet(new HashSet());
92 private TableViewer schemaTable;
93 private Button addButton;
94 private Button removeButton;
96 private ConnectionUtil connectionUtil = new ConnectionUtil();
98 protected Control createContents(Composite parent) {
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);
107 this.schemaTable = new TableViewer(composite,
108 SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER);
110 layout = new GridLayout();
111 layout.marginWidth = 5;
112 layout.marginHeight = 5;
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);
124 createButtonArea(composite);
130 private void createButtonArea(Composite composite) {
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);
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;
147 this.addButton.setLayoutData(data);
148 this.addButton.addSelectionListener(new SelectionAdapter() {
149 public void widgetSelected(SelectionEvent event) {
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;
161 this.removeButton.setLayoutData(data);
162 this.removeButton.addSelectionListener(new SelectionAdapter() {
163 public void widgetSelected(SelectionEvent event) {
164 removeSchema(SchemaPropertyPage.this.schemaTable.getSelection());
168 this.schemaTable.addSelectionChangedListener(new ISelectionChangedListener() {
169 public void selectionChanged(SelectionChangedEvent event) {
170 SchemaPropertyPage.this.removeButton.setEnabled(
171 !event.getSelection().isEmpty());
176 private void addSchema() {
177 Bookmark bookmark = getBookmark();
178 boolean isAlreadyConnected = bookmark.isConnected();
180 if (!isAlreadyConnected) {
181 boolean confirmed = MessageDialog.openConfirm(getShell(), "Connect Required",
182 "We must connect to the database to retrieve schemas.");
184 this.connectionUtil.connect(bookmark, getShell());
189 if (bookmark.isConnected()) {
190 Vector schemas = SQLHelper.getSchemas(bookmark.getConnection());
191 AddSchemaDialog dialog = new AddSchemaDialog(getShell(), schemas);
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));
202 if (!isAlreadyConnected) {
203 bookmark.disconnect();
206 } catch (ConnectionException e) {
207 ExceptionDisplayDialog.openError(getShell(), null, null, e);
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);
220 private Bookmark getBookmark() {
222 ((BookmarkHolder) getElement()).getBookmark();
227 * @see org.eclipse.jface.preference.PreferencePage#performApply()
229 public boolean performOk() {
230 getBookmark().setSchemas((Schema[]) this.schemas.toArray(
231 new Schema[this.schemas.size()]));
235 * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
237 protected void performDefaults() {
238 super.performDefaults();
239 Bookmark bookmark = getBookmark();
241 this.schemas.clear();
242 Schema[] schemas = bookmark.getSchemas();
243 for (int i = 0, length = (schemas == null) ? 0 : schemas.length;
246 if (!schemas[i].isDefault()) {
247 this.schemas.add(schemas[i]);
253 private void refreshTable() {
254 this.schemaTable.refresh();