1 package com.quantum.view;
3 import java.beans.PropertyChangeEvent;
4 import java.beans.PropertyChangeListener;
5 import java.util.ArrayList;
6 import java.util.Collections;
7 import java.util.Iterator;
10 import org.eclipse.jface.viewers.ILabelProviderListener;
11 import org.eclipse.jface.viewers.ISelection;
12 import org.eclipse.jface.viewers.ISelectionChangedListener;
13 import org.eclipse.jface.viewers.ISelectionProvider;
14 import org.eclipse.jface.viewers.IStructuredContentProvider;
15 import org.eclipse.jface.viewers.ITableLabelProvider;
16 import org.eclipse.jface.viewers.SelectionChangedEvent;
17 import org.eclipse.jface.viewers.TableViewer;
18 import org.eclipse.jface.viewers.Viewer;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Table;
24 import org.eclipse.swt.widgets.TableColumn;
26 import com.quantum.Messages;
27 import com.quantum.QuantumPlugin;
28 import com.quantum.adapters.AdapterFactory;
29 import com.quantum.model.BookmarkCollection;
30 import com.quantum.model.JDBCDriver;
36 public class JDBCDriverTableViewer implements PropertyChangeListener, ISelectionProvider {
38 public class LabelProviderImpl implements ITableLabelProvider {
39 public Image getColumnImage(Object element, int columnIndex) {
40 if (columnIndex == 0) {
41 return QuantumPlugin.getImage("driver.gif");
46 public String getColumnText(Object element, int columnIndex) {
48 switch (columnIndex) {
50 result = ((JDBCDriver) element).getName();
53 result = ((JDBCDriver) element).getClassName();
56 String type = ((JDBCDriver) element).getType();
57 result = AdapterFactory.getInstance().getAdapter(type).getDisplayName();
60 result = ((JDBCDriver) element).getVersion();
63 result = ((JDBCDriver) element).getJarFileName();
67 return result == null ? "" : result;
69 public void addListener(ILabelProviderListener listener) {
71 public void dispose() {
73 public boolean isLabelProperty(Object element, String property) {
76 public void removeListener(ILabelProviderListener listener) {
80 public class ContentProviderImpl implements IStructuredContentProvider {
81 public void dispose() {
83 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
85 public Object[] getElements(Object inputElement) {
86 if (inputElement == BookmarkCollection.getInstance()) {
87 return BookmarkCollection.getInstance().getJDBCDrivers();
94 private TableViewer tableViewer;
95 private List listeners = Collections.synchronizedList(new ArrayList());
97 public JDBCDriverTableViewer(Composite container) {
98 Table table = new Table(container, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER);
99 table.setHeaderVisible(true);
100 for (int i = 0, length = 5; i < length; i++) {
101 TableColumn column = new TableColumn(table, SWT.NONE);
102 column.setText(Messages.getString(getClass(), "driverColumn" + i));
105 for (int i = 0, length = table.getColumnCount(); i < length; i++) {
106 table.getColumn(i).pack();
109 this.tableViewer = new TableViewer(table);
110 this.tableViewer.setContentProvider(new ContentProviderImpl());
111 this.tableViewer.setLabelProvider(new LabelProviderImpl());
112 this.tableViewer.setColumnProperties(new String[] { "name", "className", "driverType", "version", "jarFileName" });
113 this.tableViewer.setInput(BookmarkCollection.getInstance());
115 this.tableViewer.addSelectionChangedListener(new ISelectionChangedListener(){
116 public void selectionChanged(SelectionChangedEvent event) {
117 fireSelectionChangedEvent(event.getSelection());
121 BookmarkCollection.getInstance().addPropertyChangeListener(this);
122 registerWithAllDrivers();
127 private void registerWithAllDrivers() {
128 JDBCDriver[] drivers = BookmarkCollection.getInstance().getJDBCDrivers();
129 for (int i = 0, length = drivers == null ? 0 : drivers.length; i < length; i++) {
130 drivers[i].removePropertyChangeListener(this);
131 drivers[i].addPropertyChangeListener(this);
137 public Control getControl() {
138 return this.tableViewer.getControl();
140 public void propertyChange(PropertyChangeEvent event) {
141 if ("drivers".equals(event.getPropertyName())) {
142 registerWithAllDrivers();
143 if (!this.tableViewer.getControl().isDisposed()) {
144 this.tableViewer.refresh();
146 } else if (event.getSource() instanceof JDBCDriver) {
147 if (!this.tableViewer.getControl().isDisposed()) {
148 this.tableViewer.refresh(event.getSource());
153 public void dispose() {
154 JDBCDriver[] drivers = BookmarkCollection.getInstance().getJDBCDrivers();
155 for (int i = 0, length = drivers == null ? 0 : drivers.length; i < length; i++) {
156 drivers[i].removePropertyChangeListener(this);
158 BookmarkCollection.getInstance().removePropertyChangeListener(this);
160 public void addSelectionChangedListener(ISelectionChangedListener listener) {
161 if (!this.listeners.contains(listener)) {
162 this.listeners.add(listener);
165 public ISelection getSelection() {
166 return this.tableViewer == null ? null : this.tableViewer.getSelection();
168 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
169 this.listeners.remove(listener);
171 public void setSelection(ISelection selection) {
176 protected void fireSelectionChangedEvent(ISelection selection) {
177 for (Iterator i = this.listeners.iterator(); selection != null && i.hasNext();) {
178 ISelectionChangedListener listener = (ISelectionChangedListener) i.next();
179 listener.selectionChanged(new SelectionChangedEvent(this, selection));