1 package com.quantum.view.tableview;
3 import java.beans.PropertyChangeEvent;
4 import java.beans.PropertyChangeListener;
6 import com.quantum.ImageStore;
7 import com.quantum.Messages;
8 import com.quantum.sql.SQLResultSetResults;
9 import com.quantum.sql.Scrollable;
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;
34 class ResultSetViewer implements PropertyChangeListener {
36 class LabelProviderImpl implements ITableLabelProvider {
37 public Image getColumnImage(Object element, int columnIndex) {
40 public String getColumnText(Object element, int columnIndex) {
42 if (element instanceof SQLResultSetResults.Row) {
43 value = ((SQLResultSetResults.Row) element).get(columnIndex+1);
45 return value == null ? "" : value.toString();
47 public void addListener(ILabelProviderListener listener) {
49 public void dispose() {
51 public boolean isLabelProperty(Object element, String property) {
54 public void removeListener(ILabelProviderListener listener) {
58 class ContentProviderImpl implements IStructuredContentProvider {
59 public Object[] getElements(Object inputElement) {
60 if (inputElement instanceof SQLResultSetResults) {
61 return ((SQLResultSetResults) inputElement).getRows();
67 public void dispose() {
70 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
76 private final SQLResultSetResults resultSet;
77 private TableViewer tableViewer;
78 private TabItem tabItem;
79 private final TableView tableView;
81 public ResultSetViewer(TableView tableView, SQLResultSetResults resultSet) {
82 this.tableView = tableView;
83 this.resultSet = resultSet;
87 this.resultSet.addPropertyChangeListener(this);
90 protected TabItem getTabItem() {
94 public Table getTable() {
95 return this.tableViewer.getTable();
98 protected void createControl() {
99 this.tabItem = new TabItem(this.tableView.tabs, SWT.NONE);
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));
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());
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());
125 createTable(composite);
126 this.tabItem.setControl(composite);
127 initializePopUpMenu();
129 this.tableView.tabs.setSelection(index-1);
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));
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);
154 private void packColumns(final Table table) {
155 for (int i = 0; i < table.getColumnCount(); i++) {
156 table.getColumn(i).pack();
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());
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();
184 addColumnsToTable(table);
185 this.tableViewer.setInput(this.resultSet);
192 public void dispose() {
193 this.resultSet.removePropertyChangeListener(this);
194 this.tabItem.dispose();
197 protected SQLResultSetResults getResultSet() {
198 return this.resultSet;
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);
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);
219 protected void updateStatusLine() {
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();
229 statusLine.setMessage(
230 Messages.getString(getClass(), "position",
232 String.valueOf(start),
235 ? Messages.getString(getClass(), "unknown")
236 : String.valueOf(last)}));
238 statusLine.setMessage("");
243 protected ISelection getSelection() {
244 return this.tableViewer.getSelection();