05cd697bfc6799c8540f4e6c77c79100b1379e79
[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
6 import org.eclipse.jface.viewers.TableViewer;
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.widgets.Composite;
9 import org.eclipse.swt.widgets.Control;
10 import org.eclipse.swt.widgets.Table;
11 import org.eclipse.swt.widgets.TableColumn;
12
13 import com.quantum.Messages;
14 import com.quantum.model.BookmarkCollection;
15 import com.quantum.model.JDBCDriver;
16
17
18 /**
19  * @author BC
20  */
21 public class JDBCDriverTableViewer implements PropertyChangeListener {
22
23         private TableViewer tableViewer;
24         
25         public JDBCDriverTableViewer(Composite container) {
26                 Table table = new Table(container, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER);
27         table.setHeaderVisible(true);
28         for (int i = 0, length = 4; i < length; i++) {
29             TableColumn column = new TableColumn(table, SWT.NONE);
30             column.setText(Messages.getString(getClass(), "driverColumn" + i));
31         }
32                 
33         for (int i = 0, length = 4; i < length; i++) {
34             table.getColumn(i).pack();
35         }
36                 
37                 this.tableViewer = new TableViewer(table);
38                 this.tableViewer.setContentProvider(new JDBCDriverContentProvider());
39                 this.tableViewer.setLabelProvider(new JDBCDriverLabelProvider());
40                 this.tableViewer.setColumnProperties(new String[] { "name", "className", "version", "jarFileName" });
41                 this.tableViewer.setInput(BookmarkCollection.getInstance());
42                 
43                 BookmarkCollection.getInstance().addPropertyChangeListener(this);
44                 registerWithAllDrivers();
45         }
46         /**
47          * 
48          */
49         private void registerWithAllDrivers() {
50                 JDBCDriver[] drivers = BookmarkCollection.getInstance().getJDBCDrivers();
51                 for (int i = 0, length = drivers == null ? 0 : drivers.length; i < length; i++) {
52                         drivers[i].addPropertyChangeListener(this);
53                 }
54         }
55         /**
56          * @return
57          */
58         public Control getControl() {
59                 return this.tableViewer.getControl();
60         }
61         /* (non-Javadoc)
62          * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
63          */
64         public void propertyChange(PropertyChangeEvent event) {
65                 if ("drivers".equals(event.getPropertyName())) {
66                         registerWithAllDrivers();
67                         this.tableViewer.refresh();
68                 } else if (event.getSource() instanceof JDBCDriver) {
69                         this.tableViewer.refresh(event.getSource());
70                 }
71         }
72         
73         public void dispose() {
74                 JDBCDriver[] drivers = BookmarkCollection.getInstance().getJDBCDrivers();
75                 for (int i = 0, length = drivers == null ? 0 : drivers.length; i < length; i++) {
76                         drivers[i].removePropertyChangeListener(this);
77                 }
78                 BookmarkCollection.getInstance().removePropertyChangeListener(this);
79         }
80 }