package com.quantum.view.tableview; import java.util.Vector; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IMenuListener; import org.eclipse.jface.action.MenuManager; import org.eclipse.jface.viewers.ISelection; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.TabFolder; import org.eclipse.swt.widgets.TabItem; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.ToolBar; import org.eclipse.ui.ISelectionListener; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.WorkbenchException; import org.eclipse.ui.part.ViewPart; import com.quantum.Messages; import com.quantum.QuantumPlugin; import com.quantum.extensions.ProcessServiceMembers; import com.quantum.model.Bookmark; import com.quantum.model.Entity; import com.quantum.model.NotConnectedException; import com.quantum.sql.SQLResults; import com.quantum.ui.dialog.ExceptionDisplayDialog; import com.quantum.view.LogProxy; /** * The Table View. Displays tables and Queries. */ public class TableView extends ViewPart implements ISelectionListener { private class DefaultEncodingAction extends Action { private final TableAdapter ta; private DefaultEncodingAction(TableAdapter ta) { super(); this.ta = ta; } public void run() { ta.setEncoding(TableAdapter.DEFAULT); } } private static TableView instance = null; private TabFolder tabs = null; private Composite parent; private Vector extensionVector; /** * Generic contructor */ public TableView() { } public void setFocus() { setQualifiedTitle(); } /** * Gets the instance of the TableView. This view can appear on multiple * perspectives, but tabs within the view are shared no matter which perspective * is open. * * @return the TableView instance. */ public static TableView getInstance() { return (TableView) QuantumPlugin.getDefault().getView("com.quantum.view.tableview.TableView"); } /** * Close the current tab, disposing of it */ public void closeCurrent() { if (tabs == null) return; if (tabs.getSelectionIndex() >= 0) { try { TabItem item = tabs.getItem(tabs.getSelectionIndex()); item.dispose(); } catch (Throwable e) { LogProxy.getInstance().addText(LogProxy.ERROR, "Error Closing Current: " + e.toString()); //$NON-NLS-1$ e.printStackTrace(); } } if (tabs.getItemCount() == 0) { setTitle(Messages.getString("tableview.QuantumTableViewName")); //$NON-NLS-1$ } } /** * Reload table or query data into the selected tab */ public void refreshCurrent() { TabItem item = tabs.getItem(tabs.getSelectionIndex()); TableAdapter adapter = (TableAdapter) item.getData(); Bookmark bookmark = adapter.getBookmark(); String table = adapter.getTable(); if (table == null) { loadTable(bookmark, item, null, null, true, true); } else { loadTable(bookmark, item, null, null, true, true); } String title = Messages.getString("tableview.QuantumTableViewName"); //$NON-NLS-1$ if (bookmark != null) title = bookmark.getName() + Messages.getString("tableview.ViewNameInitialDecoration") + title + Messages.getString("tableview.ViewNameFinalDecoration"); //$NON-NLS-1$ //$NON-NLS-2$ setTitle(title); } public void loadQuery(Bookmark bookmark, SQLResults results) { loadTable(bookmark, null, null, results, true, false); } public void loadTable(Entity entity) { loadTable(entity.getBookmark(), null, entity, null, false, true); } public void loadTable(Bookmark bookmark, TabItem tabItem, Entity entity, SQLResults results, boolean query, boolean reload) { try { TableAdapter adapter; // If no TabItem is given we have to create a new one, with the info of the table or view. if (tabItem == null) { tabItem = new TabItem(tabs, SWT.NONE); // Data is stored in a TableAdapter object if (query) { adapter = TableAdapter.createFromQuery(bookmark, results); } else { adapter = TableAdapter.createFromTable(entity); } // That is stored in the tabItem, so it won't get lost tabItem.setData(adapter); // This does not really belong here, but it'll fail if done before the creation of the // first TabItem, so it remains here till a better place found. // We set a function to change the Title of the window depending on the selected tab. tabs.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) { } public void widgetSelected(SelectionEvent e) { setQualifiedTitle(); } }); } else { // If there is already a TabItem, we take its TableAdapter object adapter = (TableAdapter) tabItem.getData(); } // We create a Composite widget (main) to display our data, with a GridLayout Composite main = new Composite(tabs, SWT.NONE); GridLayout layout = new GridLayout(1, false); layout.horizontalSpacing = 0; layout.verticalSpacing = 0; main.setLayout(layout); // load widgets, the order of loading them determines the appearance in screen ToolBar widgetToolBar = new ToolBar(main, SWT.HORIZONTAL); // We fill up our Composite widget, the main table display, etc. final Table table = new Table(main, SWT.FULL_SELECTION | SWT.MULTI); final Label label = new Label(main, SWT.NULL); TableViewToolBar toolBar = new TableViewToolBar(this, widgetToolBar, table, adapter, label); // load table if (reload) { adapter.resetOffset(); adapter.loadData(); } // Load the table data from the adapter into the widget adapter.loadTable(table); // Experimental, won't make it into 2.2 // final TableViewer viewer = adapter.addTableViewer(table); String tableName = adapter.getTable(); if (tableName != null) { tabItem.setText(bookmark.getName() + ":" + tableName); } else { tabItem.setText(bookmark.getName() + ":" + adapter.getQuery()); } toolBar.getPrevious().setEnabled(adapter.hasPreviousPage()); toolBar.getNext().setEnabled(adapter.hasNextPage()); label.setText(adapter.getStatusString()); GridData gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.verticalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; gridData.grabExcessVerticalSpace = true; table.setLayoutData(gridData); gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; label.setLayoutData(gridData); toolBar.setColumns(this, adapter, table); final TableAdapter ta = adapter; final Action defaultEncodingAction = new DefaultEncodingAction(ta); defaultEncodingAction.setText(Messages.getString("tableview.defaultEncoding")); //$NON-NLS-1$ final Action UTF8EncodingAction = new Action() { public void run() { ta.setEncoding(TableAdapter.UTF_8); } }; UTF8EncodingAction.setText(Messages.getString("tableview.UTF8Encoding")); //$NON-NLS-1$ final Action UTF16EncodingAction = new Action() { public void run() { ta.setEncoding(TableAdapter.UTF_16); } }; UTF16EncodingAction.setText(Messages.getString("tableview.UTF16Encoding")); //$NON-NLS-1$ IMenuListener menuListener = new TableViewMenuListener(this, table, UTF16EncodingAction, ta, defaultEncodingAction, UTF8EncodingAction, extensionVector); // final setup MenuManager manager = new MenuManager(); manager.setRemoveAllWhenShown(true); Menu fTextContextMenu = manager.createContextMenu(table); table.setMenu(fTextContextMenu); table.setLinesVisible(true); manager.addMenuListener(menuListener); tabItem.setControl(main); tabs.setSelection(tabs.indexOf(tabItem)); setQualifiedTitle(); } catch (NotConnectedException e) { e.printStackTrace(); handleException(e); } catch (Exception e) { e.printStackTrace(); } } protected void handleException(Exception e) { ExceptionDisplayDialog.openError(getSite().getShell(), null, null, e); } /** * @return */ /** * Sets the title of the window to the text of the selected tab */ private void setQualifiedTitle() { if (tabs.getSelectionIndex() < 0) return; TabItem item = tabs.getItem(tabs.getSelectionIndex()); String defTitle = Messages.getString("tableview.QuantumTableViewName"); //$NON-NLS-1$ String title = item.getText(); int ind = title.indexOf(Messages.getString("tableview.BookmarkSeparator")); //$NON-NLS-1$ if (ind > 0) defTitle = title.substring(0,ind) + Messages.getString("tableview.ViewNameInitialDecoration") //$NON-NLS-1$ + defTitle + Messages.getString("tableview.ViewNameFinalDecoration"); //$NON-NLS-1$ setTitle(defTitle); } public void createPartControl(Composite parent) { instance = this; this.parent = parent; initActions(); tabs = new TabFolder(parent, SWT.NONE); } public void initActions() { extensionVector = new Vector(); try { ProcessServiceMembers.process(this, extensionVector); } catch (WorkbenchException e) { e.printStackTrace(); } } public void selectionChanged(IWorkbenchPart part, ISelection selection) { } }