preparing new release
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / tableview / ResultSetViewer.java
1 package com.quantum.view.tableview;
2
3 import java.beans.PropertyChangeEvent;
4 import java.beans.PropertyChangeListener;
5
6 import com.quantum.ImageStore;
7 import com.quantum.Messages;
8 import com.quantum.sql.SQLResultSetResults;
9 import com.quantum.sql.Scrollable;
10
11 import org.eclipse.jface.action.IMenuListener;
12 import org.eclipse.jface.action.IMenuManager;
13 import org.eclipse.jface.action.IStatusLineManager;
14 import org.eclipse.jface.action.MenuManager;
15 import org.eclipse.jface.viewers.ILabelProviderListener;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.jface.viewers.IStructuredContentProvider;
18 import org.eclipse.jface.viewers.ITableLabelProvider;
19 import org.eclipse.jface.viewers.TableViewer;
20 import org.eclipse.jface.viewers.Viewer;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Menu;
27 import org.eclipse.swt.widgets.TabItem;
28 import org.eclipse.swt.widgets.Table;
29 import org.eclipse.swt.widgets.TableColumn;
30
31 /**
32  * @author BC
33  */
34 class ResultSetViewer implements PropertyChangeListener {
35         
36         class LabelProviderImpl implements ITableLabelProvider {
37                 public Image getColumnImage(Object element, int columnIndex) {
38                         return null;
39                 }
40                 public String getColumnText(Object element, int columnIndex) {
41                         Object value = null;
42                         if (element instanceof SQLResultSetResults.Row) {
43                                 value = ((SQLResultSetResults.Row) element).get(columnIndex+1);
44                         }
45                         return value == null ? "" : value.toString();
46                 }
47                 public void addListener(ILabelProviderListener listener) {
48                 }
49                 public void dispose() {
50                 }
51                 public boolean isLabelProperty(Object element, String property) {
52                         return false;
53                 }
54                 public void removeListener(ILabelProviderListener listener) {
55                 }
56         }
57         
58         class ContentProviderImpl implements IStructuredContentProvider {
59                 public Object[] getElements(Object inputElement) {
60                         if (inputElement instanceof SQLResultSetResults) {
61                                 return ((SQLResultSetResults) inputElement).getRows();
62                         } else {
63                                 return null;
64                         }
65                 }
66
67                 public void dispose() {
68                 }
69
70                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
71                 }
72                 
73         }
74         
75
76         private final SQLResultSetResults resultSet;
77         private TableViewer tableViewer;
78         private TabItem tabItem;
79         private final TableView tableView;
80         
81         public ResultSetViewer(TableView tableView, SQLResultSetResults resultSet) {
82                 this.tableView = tableView;
83                 this.resultSet = resultSet;
84                 
85                 createControl();
86                 
87                 this.resultSet.addPropertyChangeListener(this);
88         }
89         
90         protected TabItem getTabItem() {
91                 return this.tabItem;
92         }
93         
94         public Table getTable() {
95                 return this.tableViewer.getTable();
96         }
97         
98         protected void createControl() {
99         this.tabItem = new TabItem(this.tableView.tabs, SWT.NONE);
100         
101         int index = this.tableView.tabs.getItems().length;      
102         Composite composite = new Composite(this.tableView.tabs, SWT.NONE);
103                 GridLayout layout = new GridLayout();
104                 layout.marginWidth = 5;
105                 layout.marginHeight = 5;
106                 composite.setLayout(layout);
107                 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
108
109                 if (this.resultSet.getEntity() == null) {
110                         this.tabItem.setImage(ImageStore.getImage(ImageStore.SCRIPT));
111                         this.tabItem.setText(this.resultSet.getBookmark().getName());
112                         this.tabItem.setToolTipText(this.resultSet.getQuery());
113                 } else if (this.resultSet.isMetaData()) {
114                         this.tabItem.setImage(ImageStore.getImage(ImageStore.TABLE_DETAILS));
115                         this.tabItem.setText(this.resultSet.getBookmark().getName() + ":" + 
116                                         this.resultSet.getEntity().getQualifiedName());
117                         tabItem.setToolTipText(this.resultSet.getEntity().getQualifiedName());
118                 } else {
119                         this.tabItem.setImage(ImageStore.getImage(ImageStore.TABLE));
120                         this.tabItem.setText(this.resultSet.getBookmark().getName() + ":" + 
121                                         this.resultSet.getEntity().getQualifiedName());
122                         this.tabItem.setToolTipText(this.resultSet.getEntity().getQualifiedName());
123                 }
124                 
125                 createTable(composite);
126                 this.tabItem.setControl(composite);
127                 initializePopUpMenu();
128
129                 this.tableView.tabs.setSelection(index-1);
130         }
131
132
133         /**
134          * @param tabItem
135          * @param composite
136          */
137         private void createTable(Composite composite) {
138                 final Table table = new Table(composite,  SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER);
139         table.setLayout(new GridLayout());
140         table.setLayoutData(new GridData(GridData.FILL_BOTH));
141
142                 addColumnsToTable(table);
143                 this.tableViewer = new TableViewer(table);
144                 this.tableViewer.setLabelProvider(new LabelProviderImpl());
145                 this.tableViewer.setContentProvider(new ContentProviderImpl());
146                 this.tableViewer.setInput(this.resultSet);
147
148                 packColumns(table);
149         }
150
151         /**
152          * @param table
153          */
154         private void packColumns(final Table table) {
155                 for (int i = 0; i < table.getColumnCount(); i++) {
156                         table.getColumn(i).pack();
157                 }
158         }
159
160         /**
161          * @param table
162          * @return
163          */
164         private int addColumnsToTable(final Table table) {
165                 table.setHeaderVisible(true);
166                 table.setLinesVisible(true);
167                 int columnCount = this.resultSet.getColumnCount();
168                 for (int i = 0; i < columnCount; i++) {
169                         TableColumn column = new TableColumn(table, SWT.NONE);
170                         column.setText(this.resultSet.getColumnName(i+1).toString());
171                 }
172                 return columnCount;
173         }
174
175         public void propertyChange(PropertyChangeEvent event) {
176                 if ("rows".equals(event.getPropertyName())) {
177                         this.tableViewer.refresh();
178                 } else if ("columns".equals(event.getPropertyName())) {
179                         Table table = this.tableViewer.getTable();
180                         TableColumn[] columns = table.getColumns();
181                         for (int i = 0, length = columns == null ? 0 : columns.length; i < length; i++) {
182                                 columns[i].dispose();
183                         }
184                         addColumnsToTable(table);
185                         this.tableViewer.setInput(this.resultSet);
186                         packColumns(table);
187                         table.layout();
188                 }
189                 updateStatusLine();
190         }
191         
192         public void dispose() {
193                 this.resultSet.removePropertyChangeListener(this);
194                 this.tabItem.dispose();
195         }
196
197         protected SQLResultSetResults getResultSet() {
198                 return this.resultSet;
199         }
200         
201         private void initializePopUpMenu() {
202         MenuManager manager = new MenuManager();
203         manager.setRemoveAllWhenShown(true);
204         manager.addMenuListener(new IMenuListener() {
205             public void menuAboutToShow(IMenuManager menuManager) {
206                 ResultSetViewer.this.tableView.actionGroup.fillContextMenu(menuManager);
207             }
208         });
209         Menu contextMenu = manager.createContextMenu(this.tableViewer.getControl());
210         this.tableViewer.getControl().setMenu(contextMenu);
211         // register the menu to the site so that we can allow 
212         // actions to be plugged in
213         this.tableView.getSite().registerContextMenu(manager, this.tableView);
214         }
215
216         /**
217          * 
218          */
219         protected void updateStatusLine() {
220                 
221                 if (this == this.tableView.getSelectedResultSetViewer()) {
222                         IStatusLineManager statusLine = this.tableView.getViewSite().getActionBars().getStatusLineManager();
223                         if (this.resultSet != null && this.resultSet instanceof Scrollable) {
224                                 Scrollable scrollable = (Scrollable) this.resultSet;
225                                 int start = scrollable.getStart();
226                                 int end = scrollable.getEnd();
227                                 int last = scrollable.getLast();
228                                 
229                                 statusLine.setMessage(
230                                                 Messages.getString(getClass(), "position", 
231                                                                 new String[] { 
232                                                                         String.valueOf(start), 
233                                                                         String.valueOf(end),
234                                                                         last < 0 
235                                                                                 ? Messages.getString(getClass(), "unknown")
236                                                                                 : String.valueOf(last)}));
237                         } else {
238                                 statusLine.setMessage("");
239                         }
240                 }
241         }
242         
243         protected ISelection getSelection() {
244                 return this.tableViewer.getSelection();
245         }
246 }