8eb4bf19f47f78a2c0afb9cd3ae524024af0194d
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / JDBCDriverTableViewer.java
1 package com.quantum.view;
2
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;
8 import java.util.List;
9
10 import com.quantum.ImageStore;
11 import com.quantum.Messages;
12 import com.quantum.adapters.AdapterFactory;
13 import com.quantum.model.BookmarkCollection;
14 import com.quantum.model.JDBCDriver;
15
16 import org.eclipse.jface.viewers.ILabelProviderListener;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.jface.viewers.ISelectionChangedListener;
19 import org.eclipse.jface.viewers.ISelectionProvider;
20 import org.eclipse.jface.viewers.IStructuredContentProvider;
21 import org.eclipse.jface.viewers.ITableLabelProvider;
22 import org.eclipse.jface.viewers.SelectionChangedEvent;
23 import org.eclipse.jface.viewers.TableViewer;
24 import org.eclipse.jface.viewers.Viewer;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.graphics.Image;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Table;
30 import org.eclipse.swt.widgets.TableColumn;
31
32
33 /**
34  * @author BC
35  */
36 public class JDBCDriverTableViewer implements PropertyChangeListener, ISelectionProvider {
37
38         public class LabelProviderImpl implements ITableLabelProvider {
39                 public Image getColumnImage(Object element, int columnIndex) {
40                         if (columnIndex == 0) {
41                                 return ImageStore.getImage(ImageStore.DRIVER);
42                         } else {
43                                 return null;
44                         }
45                 }
46                 public String getColumnText(Object element, int columnIndex) {
47                         String result = null;
48                         switch (columnIndex) {
49                         case 0: 
50                                 result = ((JDBCDriver) element).getName();
51                                 break;
52                         case 1: 
53                                 result = ((JDBCDriver) element).getClassName();
54                                 break;
55                         case 2: 
56                                 String type = ((JDBCDriver) element).getType();
57                                 result = AdapterFactory.getInstance().getAdapter(type).getDisplayName();
58                                 break;
59                         case 3: 
60                                 result = ((JDBCDriver) element).getVersion();
61                                 break;
62                         case 4: 
63                                 result = ((JDBCDriver) element).getJarFileName();
64                                 break;
65                         default:
66                         }
67                         return result == null ? "" : result;
68                 }
69                 public void addListener(ILabelProviderListener listener) {
70                 }
71                 public void dispose() {
72                 }
73                 public boolean isLabelProperty(Object element, String property) {
74                         return false;
75                 }
76                 public void removeListener(ILabelProviderListener listener) {
77                 }
78         }
79         
80         public class ContentProviderImpl implements IStructuredContentProvider {
81                 public void dispose() {
82                 }
83                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
84                 }
85                 public Object[] getElements(Object inputElement) {
86                         if (inputElement == BookmarkCollection.getInstance()) {
87                                 return BookmarkCollection.getInstance().getJDBCDrivers();
88                         } else {
89                                 return null;
90                         }
91                 }
92         }
93         
94         private TableViewer tableViewer;
95         private List listeners = Collections.synchronizedList(new ArrayList());
96         
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));
103         }
104                 
105         for (int i = 0, length = table.getColumnCount(); i < length; i++) {
106             table.getColumn(i).pack();
107         }
108                 
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());
114                 
115                 this.tableViewer.addSelectionChangedListener(new ISelectionChangedListener(){
116                         public void selectionChanged(SelectionChangedEvent event) {
117                                 fireSelectionChangedEvent(event.getSelection());
118                         }
119                 });
120                 
121                 BookmarkCollection.getInstance().addPropertyChangeListener(this);
122                 registerWithAllDrivers();
123         }
124         /**
125          * 
126          */
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);
132                 }
133         }
134         /**
135          * @return
136          */
137         public Control getControl() {
138                 return this.tableViewer.getControl();
139         }
140         public void propertyChange(PropertyChangeEvent event) {
141                 if ("drivers".equals(event.getPropertyName())) {
142                         registerWithAllDrivers();
143                         if (!this.tableViewer.getControl().isDisposed()) {
144                                 this.tableViewer.refresh();
145                         }
146                 } else if (event.getSource() instanceof JDBCDriver) {
147                         if (!this.tableViewer.getControl().isDisposed()) {
148                                 this.tableViewer.refresh(event.getSource());
149                         }
150                 }
151         }
152         
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);
157                 }
158                 BookmarkCollection.getInstance().removePropertyChangeListener(this);
159         }
160         public void addSelectionChangedListener(ISelectionChangedListener listener) {
161                 if (!this.listeners.contains(listener)) {
162                         this.listeners.add(listener);
163                 }
164         }
165         public ISelection getSelection() {
166                 return this.tableViewer == null ? null : this.tableViewer.getSelection();
167         }
168         public void removeSelectionChangedListener(ISelectionChangedListener listener) {
169                 this.listeners.remove(listener);
170         }
171         public void setSelection(ISelection selection) {
172         }
173         /**
174          * @param selection
175          */
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));
180                 }
181         }
182 }