1 package net.sourceforge.phpdt.sql.view;
3 import net.sourceforge.phpdt.sql.Messages;
4 import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
5 import net.sourceforge.phpdt.sql.actions.CloseTableAction;
6 import net.sourceforge.phpdt.sql.actions.RefreshTableAction;
7 import net.sourceforge.phpdt.sql.sql.SQLResults;
8 import net.sourceforge.phpdt.sql.sql.TableRow;
9 import net.sourceforge.phpdt.sql.view.bookmark.BookmarkNode;
10 import net.sourceforge.phpdt.sql.view.tableview.TableAdapter;
11 import net.sourceforge.phpdt.sql.wizards.DeleteRowPage;
12 import net.sourceforge.phpdt.sql.wizards.InsertRowPage;
13 import net.sourceforge.phpdt.sql.wizards.PHPDeleteRowPage;
14 import net.sourceforge.phpdt.sql.wizards.PHPInsertRowPage;
15 import net.sourceforge.phpdt.sql.wizards.PHPSelectRowPage;
16 import net.sourceforge.phpdt.sql.wizards.PHPUpdateRowPage;
17 import net.sourceforge.phpdt.sql.wizards.SQLRowWizard;
18 import net.sourceforge.phpdt.sql.wizards.SortFilterPage;
19 import net.sourceforge.phpdt.sql.wizards.UpdateRowPage;
21 import org.eclipse.jface.action.Action;
22 import org.eclipse.jface.action.IMenuListener;
23 import org.eclipse.jface.action.IMenuManager;
24 import org.eclipse.jface.action.MenuManager;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.wizard.WizardDialog;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.dnd.Clipboard;
29 import org.eclipse.swt.dnd.TextTransfer;
30 import org.eclipse.swt.dnd.Transfer;
31 import org.eclipse.swt.events.SelectionEvent;
32 import org.eclipse.swt.events.SelectionListener;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Label;
37 import org.eclipse.swt.widgets.Menu;
38 import org.eclipse.swt.widgets.TabFolder;
39 import org.eclipse.swt.widgets.TabItem;
40 import org.eclipse.swt.widgets.Table;
41 import org.eclipse.swt.widgets.TableColumn;
42 import org.eclipse.swt.widgets.TableItem;
43 import org.eclipse.swt.widgets.ToolBar;
44 import org.eclipse.swt.widgets.ToolItem;
45 import org.eclipse.ui.ISelectionListener;
46 import org.eclipse.ui.IWorkbenchPart;
47 import org.eclipse.ui.part.ViewPart;
49 public class TableView extends ViewPart implements ISelectionListener {
50 private Clipboard clip;
51 private RefreshTableAction refreshTableAction;
52 private CloseTableAction closeTableAction;
53 private static TableView instance = null;
54 private TabFolder tabs;
55 private Composite parent;
59 public void setFocus() {
61 public static TableView getInstance() {
64 public void closeCurrent() {
65 TabItem item = tabs.getItem(tabs.getSelectionIndex());
68 public void refreshCurrent() {
69 TabItem item = tabs.getItem(tabs.getSelectionIndex());
70 TableAdapter adapter = (TableAdapter) item.getData();
71 BookmarkView bookmarkView = BookmarkView.getInstance();
72 BookmarkNode bookmark = bookmarkView.getCurrentBookmark();
73 String table = adapter.getTable();
75 loadTable(bookmark, item, null, null, true, true);
77 loadTable(bookmark, item, null, null, true, true);
80 public void loadQuery(BookmarkNode bookmark, SQLResults results) {
81 loadTable(bookmark, null, null, results, true, false);
83 public void loadTable(BookmarkNode bookmark, String table) {
84 loadTable(bookmark, null, table, null, false, true);
86 public void loadTable(BookmarkNode bookmark, TabItem tabItem, String entity, SQLResults results, boolean query, boolean reload) {
88 if (tabItem == null) {
89 tabItem = new TabItem(tabs, SWT.NONE);
91 adapter = TableAdapter.createFromQuery(bookmark, results);
93 adapter = TableAdapter.createFromTable(bookmark, entity);
95 tabItem.setData(adapter);
97 adapter = (TableAdapter) tabItem.getData();
100 final TableAdapter ta = adapter;
102 Composite main = new Composite(tabs, SWT.NONE);
103 GridLayout layout = new GridLayout(1, false);
104 layout.horizontalSpacing = 0;
105 layout.verticalSpacing = 0;
106 main.setLayout(layout);
109 ToolBar toolbar = new ToolBar(main, SWT.HORIZONTAL);
110 final Table table = new Table(main, SWT.FULL_SELECTION | SWT.MULTI);
111 final Label label = new Label(main, SWT.NULL);
113 final Action copyAction = new Action() {
115 TableItem items[] = table.getSelection();
116 StringBuffer text = new StringBuffer();
117 for (int i = 0; i < items.length; i++) {
118 int columns = table.getColumnCount();
119 for (int col = 0; col < columns; col++) {
120 text.append(items[i].getText(col));
125 clip.setContents(new Object[] { text.toString()}, new Transfer[] { TextTransfer.getInstance()});
128 final Action selectAllAction = new Action() {
135 ToolItem toolItem = new ToolItem(toolbar, SWT.PUSH);
136 toolItem.setImage(PHPEclipseSQLPlugin.getImage("refresh.gif")); //$NON-NLS-1$
137 toolItem.setToolTipText(Messages.getString("tableview.refresh")); //$NON-NLS-1$
138 toolItem.addSelectionListener(new SelectionListener() {
139 public void widgetDefaultSelected(SelectionEvent e) {
141 public void widgetSelected(SelectionEvent e) {
142 refreshTableAction.run();
145 toolItem = new ToolItem(toolbar, SWT.PUSH);
146 toolItem.setImage(PHPEclipseSQLPlugin.getImage("copy.gif")); //$NON-NLS-1$
147 toolItem.setToolTipText(Messages.getString("tableview.copy")); //$NON-NLS-1$
148 toolItem.addSelectionListener(new SelectionListener() {
149 public void widgetDefaultSelected(SelectionEvent e) {
151 public void widgetSelected(SelectionEvent e) {
155 toolItem = new ToolItem(toolbar, SWT.PUSH);
156 toolItem.setImage(PHPEclipseSQLPlugin.getImage("table.gif")); //$NON-NLS-1$
157 toolItem.setToolTipText(Messages.getString("tableview.selectAll")); //$NON-NLS-1$
158 toolItem.addSelectionListener(new SelectionListener() {
159 public void widgetDefaultSelected(SelectionEvent e) {
161 public void widgetSelected(SelectionEvent e) {
162 selectAllAction.run();
166 ToolItem filter = new ToolItem(toolbar, SWT.PUSH);
167 filter.setImage(PHPEclipseSQLPlugin.getImage("filter.gif")); //$NON-NLS-1$
168 filter.setToolTipText(Messages.getString("tableview.filterSort")); //$NON-NLS-1$
170 toolItem = new ToolItem(toolbar, SWT.SEPARATOR);
172 final ToolItem fullMode = new ToolItem(toolbar, SWT.PUSH | SWT.CHECK);
174 final ToolItem previous = new ToolItem(toolbar, SWT.PUSH);
175 final ToolItem next = new ToolItem(toolbar, SWT.PUSH);
177 fullMode.setImage(PHPEclipseSQLPlugin.getImage("fulldata.gif")); //$NON-NLS-1$
178 fullMode.setToolTipText(Messages.getString("tableview.showAll")); //$NON-NLS-1$
179 fullMode.setSelection(false);
180 fullMode.addSelectionListener(new SelectionListener() {
181 public void widgetDefaultSelected(SelectionEvent e) {
183 public void widgetSelected(SelectionEvent e) {
184 if (ta.getPageSize() == Integer.MAX_VALUE) {
191 for (int i = table.getColumnCount() - 1; i >= 0; i--) {
192 table.getColumn(i).dispose();
195 label.setText(ta.getStatusString());
196 previous.setEnabled(ta.hasPreviousPage());
197 next.setEnabled(ta.hasNextPage());
200 previous.setImage(PHPEclipseSQLPlugin.getImage("previous.gif")); //$NON-NLS-1$
201 previous.setToolTipText("Previous"); //$NON-NLS-1$
202 previous.addSelectionListener(new SelectionListener() {
203 public void widgetDefaultSelected(SelectionEvent e) {
205 public void widgetSelected(SelectionEvent e) {
209 for (int i = table.getColumnCount() - 1; i >= 0; i--) {
210 table.getColumn(i).dispose();
213 label.setText(ta.getStatusString());
214 previous.setEnabled(ta.hasPreviousPage());
215 next.setEnabled(ta.hasNextPage());
218 next.setImage(PHPEclipseSQLPlugin.getImage("next.gif")); //$NON-NLS-1$
219 next.setToolTipText("Next"); //$NON-NLS-1$
220 next.addSelectionListener(new SelectionListener() {
221 public void widgetDefaultSelected(SelectionEvent e) {
223 public void widgetSelected(SelectionEvent e) {
227 for (int i = table.getColumnCount() - 1; i >= 0; i--) {
228 table.getColumn(i).dispose();
231 label.setText(ta.getStatusString());
232 previous.setEnabled(ta.hasPreviousPage());
233 next.setEnabled(ta.hasNextPage());
237 toolItem = new ToolItem(toolbar, SWT.SEPARATOR);
239 toolItem = new ToolItem(toolbar, SWT.PUSH);
240 toolItem.setImage(PHPEclipseSQLPlugin.getImage("close.gif")); //$NON-NLS-1$
241 toolItem.setToolTipText(Messages.getString("tableview.close")); //$NON-NLS-1$
242 toolItem.addSelectionListener(new SelectionListener() {
243 public void widgetDefaultSelected(SelectionEvent e) {
245 public void widgetSelected(SelectionEvent e) {
246 closeTableAction.run();
252 adapter.resetOffset();
255 adapter.loadTable(table);
256 String tableName = adapter.getTable();
257 if (tableName != null) {
258 tabItem.setText(tableName);
260 tabItem.setText(adapter.getQuery());
263 previous.setEnabled(adapter.hasPreviousPage());
264 next.setEnabled(adapter.hasNextPage());
265 label.setText(ta.getStatusString());
267 GridData gridData = new GridData();
268 gridData.horizontalAlignment = GridData.FILL;
269 gridData.verticalAlignment = GridData.FILL;
270 gridData.grabExcessHorizontalSpace = true;
271 gridData.grabExcessVerticalSpace = true;
272 table.setLayoutData(gridData);
274 gridData = new GridData();
275 gridData.horizontalAlignment = GridData.FILL;
276 label.setLayoutData(gridData);
278 // create empty table row
279 TableColumn[] columns = table.getColumns();
280 String columnNames[] = new String[columns.length];
281 for (int i = 0; i < columns.length; i++) {
282 columnNames[i] = columns[i].getText();
284 String data[] = new String[columnNames.length];
285 for (int i = 0; i < columns.length; i++) {
286 data[i] = ""; //$NON-NLS-1$
289 final TableRow emptyRow = new TableRow(ta.getBookmark(), ta.getTable(), columnNames, data);
291 filter.addSelectionListener(new SelectionListener() {
292 public void widgetDefaultSelected(SelectionEvent e) {
294 public void widgetSelected(SelectionEvent e) {
295 SortFilterPage page = new SortFilterPage(""); //$NON-NLS-1$
296 SQLRowWizard wizard = new SQLRowWizard();
297 wizard.init(Messages.getString("TableView.FilterAndSort"), page, emptyRow, ta); //$NON-NLS-1$
298 WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard);
303 final Action defaultEncodingAction = new Action() {
305 ta.setEncoding(TableAdapter.DEFAULT);
308 defaultEncodingAction.setText(Messages.getString("tableview.defaultEncoding")); //$NON-NLS-1$
309 final Action UTF8EncodingAction = new Action() {
311 ta.setEncoding(TableAdapter.UTF_8);
314 UTF8EncodingAction.setText(Messages.getString("tableview.UTF8Encoding")); //$NON-NLS-1$
315 final Action UTF16EncodingAction = new Action() {
317 ta.setEncoding(TableAdapter.UTF_16);
320 UTF16EncodingAction.setText(Messages.getString("tableview.UTF16Encoding")); //$NON-NLS-1$
322 IMenuListener menuListener = new IMenuListener() {
323 public void menuAboutToShow(IMenuManager mgr) {
324 if (ta.getTable() != null) {
325 TableItem[] selection = table.getSelection();
326 TableColumn[] columns = table.getColumns();
327 String columnNames[] = new String[columns.length];
328 for (int i = 0; i < columns.length; i++) {
329 columnNames[i] = columns[i].getText();
331 String data[] = new String[columnNames.length];
332 if (selection != null && selection.length > 0) {
333 TableItem sel = selection[0];
334 for (int i = 0; i < columns.length; i++) {
335 data[i] = sel.getText(i);
338 for (int i = 0; i < columns.length; i++) {
339 data[i] = ""; //$NON-NLS-1$
342 final TableRow row = new TableRow(ta.getBookmark(), ta.getTable(), columnNames, data);
343 Action phpSelectAction = new Action() {
345 PHPSelectRowPage page = new PHPSelectRowPage(""); //$NON-NLS-1$
346 SQLRowWizard wizard = new SQLRowWizard();
347 wizard.init(Messages.getString("TableView.UpdateRow"), page, row, ta); //$NON-NLS-1$
348 WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard);
352 phpSelectAction.setText(Messages.getString("tableview.phpselect")); //$NON-NLS-1$
354 Action phpUpdateAction = new Action() {
356 PHPUpdateRowPage page = new PHPUpdateRowPage(""); //$NON-NLS-1$
357 SQLRowWizard wizard = new SQLRowWizard();
358 wizard.init(Messages.getString("TableView.UpdateRow"), page, row, ta); //$NON-NLS-1$
359 WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard);
363 phpUpdateAction.setText(Messages.getString("tableview.phpupdate")); //$NON-NLS-1$
364 Action phpInsertAction = new Action() {
366 PHPInsertRowPage page = new PHPInsertRowPage(""); //$NON-NLS-1$
367 SQLRowWizard wizard = new SQLRowWizard();
368 wizard.init(Messages.getString("TableView.InsertRow"), page, row, ta); //$NON-NLS-1$
369 WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard);
373 phpInsertAction.setText(Messages.getString("tableview.phpinsert")); //$NON-NLS-1$
374 Action phpDeleteAction = new Action() {
376 PHPDeleteRowPage page = new PHPDeleteRowPage(""); //$NON-NLS-1$
377 SQLRowWizard wizard = new SQLRowWizard();
378 wizard.init(Messages.getString("TableView.DeleteRow"), page, row, ta); //$NON-NLS-1$
379 WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard);
383 phpDeleteAction.setText(Messages.getString("tableview.phpdelete")); //$NON-NLS-1$
385 Action updateAction = new Action() {
387 UpdateRowPage page = new UpdateRowPage(""); //$NON-NLS-1$
388 SQLRowWizard wizard = new SQLRowWizard();
389 wizard.init(Messages.getString("TableView.UpdateRow"), page, row, ta); //$NON-NLS-1$
390 WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard);
394 updateAction.setText(Messages.getString("tableview.update")); //$NON-NLS-1$
395 Action insertAction = new Action() {
397 InsertRowPage page = new InsertRowPage(""); //$NON-NLS-1$
398 SQLRowWizard wizard = new SQLRowWizard();
399 wizard.init(Messages.getString("TableView.InsertRow"), page, row, ta); //$NON-NLS-1$
400 WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard);
404 insertAction.setText(Messages.getString("tableview.insert")); //$NON-NLS-1$
405 Action deleteAction = new Action() {
407 DeleteRowPage page = new DeleteRowPage(""); //$NON-NLS-1$
408 SQLRowWizard wizard = new SQLRowWizard();
409 wizard.init(Messages.getString("TableView.DeleteRow"), page, row, ta); //$NON-NLS-1$
410 WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard);
414 deleteAction.setText(Messages.getString("tableview.delete")); //$NON-NLS-1$
415 mgr.add(phpSelectAction);
416 mgr.add(phpInsertAction);
417 mgr.add(phpUpdateAction);
418 mgr.add(phpDeleteAction);
420 mgr.add(insertAction);
421 mgr.add(updateAction);
422 mgr.add(deleteAction);
424 mgr.add(defaultEncodingAction);
425 mgr.add(UTF8EncodingAction);
426 mgr.add(UTF16EncodingAction);
431 MenuManager manager = new MenuManager();
432 manager.setRemoveAllWhenShown(true);
433 Menu fTextContextMenu = manager.createContextMenu(table);
434 table.setMenu(fTextContextMenu);
435 table.setLinesVisible(true);
436 manager.addMenuListener(menuListener);
438 tabItem.setControl(main);
439 tabs.setSelection(tabs.indexOf(tabItem));
442 public void createPartControl(Composite parent) {
444 this.parent = parent;
446 clip = new Clipboard(getSite().getShell().getDisplay());
447 tabs = new TabFolder(parent, SWT.NONE);
449 public void initActions() {
450 refreshTableAction = new RefreshTableAction();
451 refreshTableAction.setText(Messages.getString("tableview.refresh")); //$NON-NLS-1$
452 refreshTableAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("refresh.gif")); //$NON-NLS-1$
453 refreshTableAction.init(this);
454 closeTableAction = new CloseTableAction();
455 closeTableAction.setText(Messages.getString("tableview.close")); //$NON-NLS-1$
456 closeTableAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("close.gif")); //$NON-NLS-1$
457 closeTableAction.init(this);
460 public void selectionChanged(IWorkbenchPart part, ISelection selection) {