remove name clash with JDT editor category
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / view / TableView.java
1 package net.sourceforge.phpdt.sql.view;
2
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;
20
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;
48
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;
56   public TableView() {
57     super();
58   }
59   public void setFocus() {
60   }
61   public static TableView getInstance() {
62     return instance;
63   }
64   public void closeCurrent() {
65     TabItem item = tabs.getItem(tabs.getSelectionIndex());
66     item.dispose();
67   }
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();
74     if (table == null) {
75       loadTable(bookmark, item, null, null, true, true);
76     } else {
77       loadTable(bookmark, item, null, null, true, true);
78     }
79   }
80   public void loadQuery(BookmarkNode bookmark, SQLResults results) {
81     loadTable(bookmark, null, null, results, true, false);
82   }
83   public void loadTable(BookmarkNode bookmark, String table) {
84     loadTable(bookmark, null, table, null, false, true);
85   }
86   public void loadTable(BookmarkNode bookmark, TabItem tabItem, String entity, SQLResults results, boolean query, boolean reload) {
87     TableAdapter adapter;
88     if (tabItem == null) {
89       tabItem = new TabItem(tabs, SWT.NONE);
90       if (query) {
91         adapter = TableAdapter.createFromQuery(bookmark, results);
92       } else {
93         adapter = TableAdapter.createFromTable(bookmark, entity);
94       }
95       tabItem.setData(adapter);
96     } else {
97       adapter = (TableAdapter) tabItem.getData();
98     }
99
100     final TableAdapter ta = adapter;
101
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);
107
108     // load up main
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);
112
113     final Action copyAction = new Action() {
114       public void run() {
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));
121             text.append('\t');
122           }
123           text.append('\n');
124         }
125         clip.setContents(new Object[] { text.toString()}, new Transfer[] { TextTransfer.getInstance()});
126       }
127     };
128     final Action selectAllAction = new Action() {
129       public void run() {
130         table.selectAll();
131       }
132     };
133
134     // load toobar
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) {
140       }
141       public void widgetSelected(SelectionEvent e) {
142         refreshTableAction.run();
143       }
144     });
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) {
150       }
151       public void widgetSelected(SelectionEvent e) {
152         copyAction.run();
153       }
154     });
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) {
160       }
161       public void widgetSelected(SelectionEvent e) {
162         selectAllAction.run();
163       }
164     });
165
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$
169
170     toolItem = new ToolItem(toolbar, SWT.SEPARATOR);
171
172     final ToolItem fullMode = new ToolItem(toolbar, SWT.PUSH | SWT.CHECK);
173
174     final ToolItem previous = new ToolItem(toolbar, SWT.PUSH);
175     final ToolItem next = new ToolItem(toolbar, SWT.PUSH);
176
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) {
182       }
183       public void widgetSelected(SelectionEvent e) {
184         if (ta.getPageSize() == Integer.MAX_VALUE) {
185           ta.resetMode();
186         } else {
187           ta.fullMode();
188         }
189         ta.loadData();
190         table.removeAll();
191         for (int i = table.getColumnCount() - 1; i >= 0; i--) {
192           table.getColumn(i).dispose();
193         }
194         ta.loadTable(table);
195         label.setText(ta.getStatusString());
196         previous.setEnabled(ta.hasPreviousPage());
197         next.setEnabled(ta.hasNextPage());
198       }
199     });
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) {
204       }
205       public void widgetSelected(SelectionEvent e) {
206         ta.previousPage();
207         ta.loadData();
208         table.removeAll();
209         for (int i = table.getColumnCount() - 1; i >= 0; i--) {
210           table.getColumn(i).dispose();
211         }
212         ta.loadTable(table);
213         label.setText(ta.getStatusString());
214         previous.setEnabled(ta.hasPreviousPage());
215         next.setEnabled(ta.hasNextPage());
216       }
217     });
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) {
222       }
223       public void widgetSelected(SelectionEvent e) {
224         ta.nextPage();
225         ta.loadData();
226         table.removeAll();
227         for (int i = table.getColumnCount() - 1; i >= 0; i--) {
228           table.getColumn(i).dispose();
229         }
230         ta.loadTable(table);
231         label.setText(ta.getStatusString());
232         previous.setEnabled(ta.hasPreviousPage());
233         next.setEnabled(ta.hasNextPage());
234       }
235     });
236
237     toolItem = new ToolItem(toolbar, SWT.SEPARATOR);
238
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) {
244       }
245       public void widgetSelected(SelectionEvent e) {
246         closeTableAction.run();
247       }
248     });
249
250     // load table
251     if (reload) {
252       adapter.resetOffset();
253       adapter.loadData();
254     }
255     adapter.loadTable(table);
256     String tableName = adapter.getTable();
257     if (tableName != null) {
258       tabItem.setText(tableName);
259     } else {
260       tabItem.setText(adapter.getQuery());
261     }
262
263     previous.setEnabled(adapter.hasPreviousPage());
264     next.setEnabled(adapter.hasNextPage());
265     label.setText(ta.getStatusString());
266
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);
273
274     gridData = new GridData();
275     gridData.horizontalAlignment = GridData.FILL;
276     label.setLayoutData(gridData);
277
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();
283     }
284     String data[] = new String[columnNames.length];
285     for (int i = 0; i < columns.length; i++) {
286       data[i] = ""; //$NON-NLS-1$
287     }
288
289     final TableRow emptyRow = new TableRow(ta.getBookmark(), ta.getTable(), columnNames, data);
290
291     filter.addSelectionListener(new SelectionListener() {
292       public void widgetDefaultSelected(SelectionEvent e) {
293       }
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);
299         dialog.open();
300       }
301     });
302
303     final Action defaultEncodingAction = new Action() {
304       public void run() {
305         ta.setEncoding(TableAdapter.DEFAULT);
306       }
307     };
308     defaultEncodingAction.setText(Messages.getString("tableview.defaultEncoding")); //$NON-NLS-1$
309     final Action UTF8EncodingAction = new Action() {
310       public void run() {
311         ta.setEncoding(TableAdapter.UTF_8);
312       }
313     };
314     UTF8EncodingAction.setText(Messages.getString("tableview.UTF8Encoding")); //$NON-NLS-1$
315     final Action UTF16EncodingAction = new Action() {
316       public void run() {
317         ta.setEncoding(TableAdapter.UTF_16);
318       }
319     };
320     UTF16EncodingAction.setText(Messages.getString("tableview.UTF16Encoding")); //$NON-NLS-1$
321
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();
330           }
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);
336             }
337           } else {
338             for (int i = 0; i < columns.length; i++) {
339               data[i] = ""; //$NON-NLS-1$
340             }
341           }
342           final TableRow row = new TableRow(ta.getBookmark(), ta.getTable(), columnNames, data);
343           Action phpSelectAction = new Action() {
344             public void run() {
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);
349               dialog.open();
350             }
351           };
352           phpSelectAction.setText(Messages.getString("tableview.phpselect")); //$NON-NLS-1$
353
354           Action phpUpdateAction = new Action() {
355             public void run() {
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);
360               dialog.open();
361             }
362           };
363           phpUpdateAction.setText(Messages.getString("tableview.phpupdate")); //$NON-NLS-1$
364           Action phpInsertAction = new Action() {
365             public void run() {
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);
370               dialog.open();
371             }
372           };
373           phpInsertAction.setText(Messages.getString("tableview.phpinsert")); //$NON-NLS-1$
374           Action phpDeleteAction = new Action() {
375             public void run() {
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);
380               dialog.open();
381             }
382           };
383           phpDeleteAction.setText(Messages.getString("tableview.phpdelete")); //$NON-NLS-1$
384
385           Action updateAction = new Action() {
386             public void run() {
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);
391               dialog.open();
392             }
393           };
394           updateAction.setText(Messages.getString("tableview.update")); //$NON-NLS-1$
395           Action insertAction = new Action() {
396             public void run() {
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);
401               dialog.open();
402             }
403           };
404           insertAction.setText(Messages.getString("tableview.insert")); //$NON-NLS-1$
405           Action deleteAction = new Action() {
406             public void run() {
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);
411               dialog.open();
412             }
413           };
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);
419
420           mgr.add(insertAction);
421           mgr.add(updateAction);
422           mgr.add(deleteAction);
423         }
424         mgr.add(defaultEncodingAction);
425         mgr.add(UTF8EncodingAction);
426         mgr.add(UTF16EncodingAction);
427       }
428     };
429
430     // final setup
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);
437
438     tabItem.setControl(main);
439     tabs.setSelection(tabs.indexOf(tabItem));
440   }
441
442   public void createPartControl(Composite parent) {
443     instance = this;
444     this.parent = parent;
445     initActions();
446     clip = new Clipboard(getSite().getShell().getDisplay());
447     tabs = new TabFolder(parent, SWT.NONE);
448   }
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);
458   }
459
460   public void selectionChanged(IWorkbenchPart part, ISelection selection) {
461   }
462 }