package net.sourceforge.phpdt.sql.view; import net.sourceforge.phpdt.sql.Messages; import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin; import net.sourceforge.phpdt.sql.actions.CloseTableAction; import net.sourceforge.phpdt.sql.actions.RefreshTableAction; import net.sourceforge.phpdt.sql.sql.SQLResults; import net.sourceforge.phpdt.sql.sql.TableRow; import net.sourceforge.phpdt.sql.view.bookmark.BookmarkNode; import net.sourceforge.phpdt.sql.view.tableview.TableAdapter; import net.sourceforge.phpdt.sql.wizards.DeleteRowPage; import net.sourceforge.phpdt.sql.wizards.InsertRowPage; import net.sourceforge.phpdt.sql.wizards.PHPDeleteRowPage; import net.sourceforge.phpdt.sql.wizards.PHPInsertRowPage; import net.sourceforge.phpdt.sql.wizards.PHPSelectRowPage; import net.sourceforge.phpdt.sql.wizards.PHPUpdateRowPage; import net.sourceforge.phpdt.sql.wizards.SQLRowWizard; import net.sourceforge.phpdt.sql.wizards.SortFilterPage; import net.sourceforge.phpdt.sql.wizards.UpdateRowPage; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IMenuListener; import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.MenuManager; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.wizard.WizardDialog; import org.eclipse.swt.SWT; import org.eclipse.swt.dnd.Clipboard; import org.eclipse.swt.dnd.TextTransfer; import org.eclipse.swt.dnd.Transfer; 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.TableColumn; import org.eclipse.swt.widgets.TableItem; import org.eclipse.swt.widgets.ToolBar; import org.eclipse.swt.widgets.ToolItem; import org.eclipse.ui.ISelectionListener; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.part.ViewPart; public class TableView extends ViewPart implements ISelectionListener { private Clipboard clip; private RefreshTableAction refreshTableAction; private CloseTableAction closeTableAction; private static TableView instance = null; private TabFolder tabs; private Composite parent; public TableView() { super(); } public void setFocus() { } public static TableView getInstance() { return instance; } public void closeCurrent() { TabItem item = tabs.getItem(tabs.getSelectionIndex()); item.dispose(); } public void refreshCurrent() { TabItem item = tabs.getItem(tabs.getSelectionIndex()); TableAdapter adapter = (TableAdapter) item.getData(); BookmarkView bookmarkView = BookmarkView.getInstance(); BookmarkNode bookmark = bookmarkView.getCurrentBookmark(); String table = adapter.getTable(); if (table == null) { loadTable(bookmark, item, null, null, true, true); } else { loadTable(bookmark, item, null, null, true, true); } } public void loadQuery(BookmarkNode bookmark, SQLResults results) { loadTable(bookmark, null, null, results, true, false); } public void loadTable(BookmarkNode bookmark, String table) { loadTable(bookmark, null, table, null, false, true); } public void loadTable(BookmarkNode bookmark, TabItem tabItem, String entity, SQLResults results, boolean query, boolean reload) { TableAdapter adapter; if (tabItem == null) { tabItem = new TabItem(tabs, SWT.NONE); if (query) { adapter = TableAdapter.createFromQuery(bookmark, results); } else { adapter = TableAdapter.createFromTable(bookmark, entity); } tabItem.setData(adapter); } else { adapter = (TableAdapter) tabItem.getData(); } final TableAdapter ta = adapter; Composite main = new Composite(tabs, SWT.NONE); GridLayout layout = new GridLayout(1, false); layout.horizontalSpacing = 0; layout.verticalSpacing = 0; main.setLayout(layout); // load up main ToolBar toolbar = new ToolBar(main, SWT.HORIZONTAL); final Table table = new Table(main, SWT.FULL_SELECTION | SWT.MULTI); final Label label = new Label(main, SWT.NULL); final Action copyAction = new Action() { public void run() { TableItem items[] = table.getSelection(); StringBuffer text = new StringBuffer(); for (int i = 0; i < items.length; i++) { int columns = table.getColumnCount(); for (int col = 0; col < columns; col++) { text.append(items[i].getText(col)); text.append('\t'); } text.append('\n'); } clip.setContents(new Object[] { text.toString()}, new Transfer[] { TextTransfer.getInstance()}); } }; final Action selectAllAction = new Action() { public void run() { table.selectAll(); } }; // load toobar ToolItem toolItem = new ToolItem(toolbar, SWT.PUSH); toolItem.setImage(PHPEclipseSQLPlugin.getImage("refresh.gif")); //$NON-NLS-1$ toolItem.setToolTipText(Messages.getString("tableview.refresh")); //$NON-NLS-1$ toolItem.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) { } public void widgetSelected(SelectionEvent e) { refreshTableAction.run(); } }); toolItem = new ToolItem(toolbar, SWT.PUSH); toolItem.setImage(PHPEclipseSQLPlugin.getImage("copy.gif")); //$NON-NLS-1$ toolItem.setToolTipText(Messages.getString("tableview.copy")); //$NON-NLS-1$ toolItem.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) { } public void widgetSelected(SelectionEvent e) { copyAction.run(); } }); toolItem = new ToolItem(toolbar, SWT.PUSH); toolItem.setImage(PHPEclipseSQLPlugin.getImage("table.gif")); //$NON-NLS-1$ toolItem.setToolTipText(Messages.getString("tableview.selectAll")); //$NON-NLS-1$ toolItem.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) { } public void widgetSelected(SelectionEvent e) { selectAllAction.run(); } }); ToolItem filter = new ToolItem(toolbar, SWT.PUSH); filter.setImage(PHPEclipseSQLPlugin.getImage("filter.gif")); //$NON-NLS-1$ filter.setToolTipText(Messages.getString("tableview.filterSort")); //$NON-NLS-1$ toolItem = new ToolItem(toolbar, SWT.SEPARATOR); final ToolItem fullMode = new ToolItem(toolbar, SWT.PUSH | SWT.CHECK); final ToolItem previous = new ToolItem(toolbar, SWT.PUSH); final ToolItem next = new ToolItem(toolbar, SWT.PUSH); fullMode.setImage(PHPEclipseSQLPlugin.getImage("fulldata.gif")); //$NON-NLS-1$ fullMode.setToolTipText(Messages.getString("tableview.showAll")); //$NON-NLS-1$ fullMode.setSelection(false); fullMode.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) { } public void widgetSelected(SelectionEvent e) { if (ta.getPageSize() == Integer.MAX_VALUE) { ta.resetMode(); } else { ta.fullMode(); } ta.loadData(); table.removeAll(); for (int i = table.getColumnCount() - 1; i >= 0; i--) { table.getColumn(i).dispose(); } ta.loadTable(table); label.setText(ta.getStatusString()); previous.setEnabled(ta.hasPreviousPage()); next.setEnabled(ta.hasNextPage()); } }); previous.setImage(PHPEclipseSQLPlugin.getImage("previous.gif")); //$NON-NLS-1$ previous.setToolTipText("Previous"); //$NON-NLS-1$ previous.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) { } public void widgetSelected(SelectionEvent e) { ta.previousPage(); ta.loadData(); table.removeAll(); for (int i = table.getColumnCount() - 1; i >= 0; i--) { table.getColumn(i).dispose(); } ta.loadTable(table); label.setText(ta.getStatusString()); previous.setEnabled(ta.hasPreviousPage()); next.setEnabled(ta.hasNextPage()); } }); next.setImage(PHPEclipseSQLPlugin.getImage("next.gif")); //$NON-NLS-1$ next.setToolTipText("Next"); //$NON-NLS-1$ next.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) { } public void widgetSelected(SelectionEvent e) { ta.nextPage(); ta.loadData(); table.removeAll(); for (int i = table.getColumnCount() - 1; i >= 0; i--) { table.getColumn(i).dispose(); } ta.loadTable(table); label.setText(ta.getStatusString()); previous.setEnabled(ta.hasPreviousPage()); next.setEnabled(ta.hasNextPage()); } }); toolItem = new ToolItem(toolbar, SWT.SEPARATOR); toolItem = new ToolItem(toolbar, SWT.PUSH); toolItem.setImage(PHPEclipseSQLPlugin.getImage("close.gif")); //$NON-NLS-1$ toolItem.setToolTipText(Messages.getString("tableview.close")); //$NON-NLS-1$ toolItem.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) { } public void widgetSelected(SelectionEvent e) { closeTableAction.run(); } }); // load table if (reload) { adapter.resetOffset(); adapter.loadData(); } adapter.loadTable(table); String tableName = adapter.getTable(); if (tableName != null) { tabItem.setText(tableName); } else { tabItem.setText(adapter.getQuery()); } previous.setEnabled(adapter.hasPreviousPage()); next.setEnabled(adapter.hasNextPage()); label.setText(ta.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); // create empty table row TableColumn[] columns = table.getColumns(); String columnNames[] = new String[columns.length]; for (int i = 0; i < columns.length; i++) { columnNames[i] = columns[i].getText(); } String data[] = new String[columnNames.length]; for (int i = 0; i < columns.length; i++) { data[i] = ""; //$NON-NLS-1$ } final TableRow emptyRow = new TableRow(ta.getBookmark(), ta.getTable(), columnNames, data); filter.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) { } public void widgetSelected(SelectionEvent e) { SortFilterPage page = new SortFilterPage(""); //$NON-NLS-1$ SQLRowWizard wizard = new SQLRowWizard(); wizard.init(Messages.getString("TableView.FilterAndSort"), page, emptyRow, ta); //$NON-NLS-1$ WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard); dialog.open(); } }); final Action defaultEncodingAction = new Action() { public void run() { ta.setEncoding(TableAdapter.DEFAULT); } }; 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 IMenuListener() { public void menuAboutToShow(IMenuManager mgr) { if (ta.getTable() != null) { TableItem[] selection = table.getSelection(); TableColumn[] columns = table.getColumns(); String columnNames[] = new String[columns.length]; for (int i = 0; i < columns.length; i++) { columnNames[i] = columns[i].getText(); } String data[] = new String[columnNames.length]; if (selection != null && selection.length > 0) { TableItem sel = selection[0]; for (int i = 0; i < columns.length; i++) { data[i] = sel.getText(i); } } else { for (int i = 0; i < columns.length; i++) { data[i] = ""; //$NON-NLS-1$ } } final TableRow row = new TableRow(ta.getBookmark(), ta.getTable(), columnNames, data); Action phpSelectAction = new Action() { public void run() { PHPSelectRowPage page = new PHPSelectRowPage(""); //$NON-NLS-1$ SQLRowWizard wizard = new SQLRowWizard(); wizard.init(Messages.getString("TableView.UpdateRow"), page, row, ta); //$NON-NLS-1$ WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard); dialog.open(); } }; phpSelectAction.setText(Messages.getString("tableview.phpselect")); //$NON-NLS-1$ Action phpUpdateAction = new Action() { public void run() { PHPUpdateRowPage page = new PHPUpdateRowPage(""); //$NON-NLS-1$ SQLRowWizard wizard = new SQLRowWizard(); wizard.init(Messages.getString("TableView.UpdateRow"), page, row, ta); //$NON-NLS-1$ WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard); dialog.open(); } }; phpUpdateAction.setText(Messages.getString("tableview.phpupdate")); //$NON-NLS-1$ Action phpInsertAction = new Action() { public void run() { PHPInsertRowPage page = new PHPInsertRowPage(""); //$NON-NLS-1$ SQLRowWizard wizard = new SQLRowWizard(); wizard.init(Messages.getString("TableView.InsertRow"), page, row, ta); //$NON-NLS-1$ WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard); dialog.open(); } }; phpInsertAction.setText(Messages.getString("tableview.phpinsert")); //$NON-NLS-1$ Action phpDeleteAction = new Action() { public void run() { PHPDeleteRowPage page = new PHPDeleteRowPage(""); //$NON-NLS-1$ SQLRowWizard wizard = new SQLRowWizard(); wizard.init(Messages.getString("TableView.DeleteRow"), page, row, ta); //$NON-NLS-1$ WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard); dialog.open(); } }; phpDeleteAction.setText(Messages.getString("tableview.phpdelete")); //$NON-NLS-1$ Action updateAction = new Action() { public void run() { UpdateRowPage page = new UpdateRowPage(""); //$NON-NLS-1$ SQLRowWizard wizard = new SQLRowWizard(); wizard.init(Messages.getString("TableView.UpdateRow"), page, row, ta); //$NON-NLS-1$ WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard); dialog.open(); } }; updateAction.setText(Messages.getString("tableview.update")); //$NON-NLS-1$ Action insertAction = new Action() { public void run() { InsertRowPage page = new InsertRowPage(""); //$NON-NLS-1$ SQLRowWizard wizard = new SQLRowWizard(); wizard.init(Messages.getString("TableView.InsertRow"), page, row, ta); //$NON-NLS-1$ WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard); dialog.open(); } }; insertAction.setText(Messages.getString("tableview.insert")); //$NON-NLS-1$ Action deleteAction = new Action() { public void run() { DeleteRowPage page = new DeleteRowPage(""); //$NON-NLS-1$ SQLRowWizard wizard = new SQLRowWizard(); wizard.init(Messages.getString("TableView.DeleteRow"), page, row, ta); //$NON-NLS-1$ WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard); dialog.open(); } }; deleteAction.setText(Messages.getString("tableview.delete")); //$NON-NLS-1$ mgr.add(phpSelectAction); mgr.add(phpInsertAction); mgr.add(phpUpdateAction); mgr.add(phpDeleteAction); mgr.add(insertAction); mgr.add(updateAction); mgr.add(deleteAction); } mgr.add(defaultEncodingAction); mgr.add(UTF8EncodingAction); mgr.add(UTF16EncodingAction); } }; // 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)); } public void createPartControl(Composite parent) { instance = this; this.parent = parent; initActions(); clip = new Clipboard(getSite().getShell().getDisplay()); tabs = new TabFolder(parent, SWT.NONE); } public void initActions() { refreshTableAction = new RefreshTableAction(); refreshTableAction.setText(Messages.getString("tableview.refresh")); //$NON-NLS-1$ refreshTableAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("refresh.gif")); //$NON-NLS-1$ refreshTableAction.init(this); closeTableAction = new CloseTableAction(); closeTableAction.setText(Messages.getString("tableview.close")); //$NON-NLS-1$ closeTableAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("close.gif")); //$NON-NLS-1$ closeTableAction.init(this); } public void selectionChanged(IWorkbenchPart part, ISelection selection) { } }