fixed parser bug;
[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 org.eclipse.jface.action.Action;
4 import org.eclipse.jface.action.IMenuListener;
5 import org.eclipse.jface.action.IMenuManager;
6 import org.eclipse.jface.action.MenuManager;
7 import org.eclipse.jface.viewers.ISelection;
8 import org.eclipse.jface.wizard.WizardDialog;
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.dnd.Clipboard;
11 import org.eclipse.swt.dnd.TextTransfer;
12 import org.eclipse.swt.dnd.Transfer;
13 import org.eclipse.swt.events.SelectionEvent;
14 import org.eclipse.swt.events.SelectionListener;
15 import org.eclipse.swt.layout.GridData;
16 import org.eclipse.swt.layout.GridLayout;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Label;
19 import org.eclipse.swt.widgets.Menu;
20 import org.eclipse.swt.widgets.TabFolder;
21 import org.eclipse.swt.widgets.TabItem;
22 import org.eclipse.swt.widgets.Table;
23 import org.eclipse.swt.widgets.TableColumn;
24 import org.eclipse.swt.widgets.TableItem;
25 import org.eclipse.swt.widgets.ToolBar;
26 import org.eclipse.swt.widgets.ToolItem;
27 import org.eclipse.ui.ISelectionListener;
28 import org.eclipse.ui.IWorkbenchPart;
29 import org.eclipse.ui.part.ViewPart;
30
31 import net.sourceforge.phpdt.sql.Messages;
32 import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
33 import net.sourceforge.phpdt.sql.actions.CloseTableAction;
34 import net.sourceforge.phpdt.sql.actions.RefreshTableAction;
35 import net.sourceforge.phpdt.sql.sql.SQLResults;
36 import net.sourceforge.phpdt.sql.sql.TableRow;
37 import net.sourceforge.phpdt.sql.view.bookmark.BookmarkNode;
38 import net.sourceforge.phpdt.sql.view.tableview.TableAdapter;
39 import net.sourceforge.phpdt.sql.wizards.DeleteRowPage;
40 import net.sourceforge.phpdt.sql.wizards.InsertRowPage;
41 import net.sourceforge.phpdt.sql.wizards.SQLRowWizard;
42 import net.sourceforge.phpdt.sql.wizards.SortFilterPage;
43 import net.sourceforge.phpdt.sql.wizards.UpdateRowPage;
44
45 public class TableView extends ViewPart implements ISelectionListener {
46         private Clipboard clip;
47         private RefreshTableAction refreshTableAction;
48         private CloseTableAction closeTableAction;
49         private static TableView instance = null;
50         private TabFolder tabs;
51         private Composite parent;
52         public TableView() {
53                 super();
54         }
55         public void setFocus() {
56         }
57         public static TableView getInstance() {
58                 return instance;
59         }
60         public void closeCurrent() {
61                 TabItem item = tabs.getItem(tabs.getSelectionIndex());
62                 item.dispose();
63         }
64         public void refreshCurrent() {
65                 TabItem item = tabs.getItem(tabs.getSelectionIndex());
66                 TableAdapter adapter = (TableAdapter) item.getData();
67                 BookmarkView bookmarkView = BookmarkView.getInstance();
68                 BookmarkNode bookmark = bookmarkView.getCurrentBookmark();
69                 String table = adapter.getTable();
70                 if (table == null) {
71                         loadTable(bookmark, item, null, null, true, true);
72                 } else {
73                         loadTable(bookmark, item, null, null, true, true);
74                 }
75         }
76         public void loadQuery(BookmarkNode bookmark, SQLResults results) {
77                 loadTable(bookmark, null, null, results, true, false);
78         }
79         public void loadTable(BookmarkNode bookmark, String table) {
80                 loadTable(bookmark, null, table, null, false, true);
81         }
82         public void loadTable(BookmarkNode bookmark, TabItem tabItem, String entity, SQLResults results, boolean query, boolean reload) {
83                 TableAdapter adapter;
84                 if (tabItem == null) {
85                         tabItem = new TabItem(tabs, SWT.NONE);
86                         if (query) {
87                                 adapter = TableAdapter.createFromQuery(bookmark, results);
88                         } else {
89                                 adapter = TableAdapter.createFromTable(bookmark, entity);
90                         }
91                         tabItem.setData(adapter);
92                 } else {
93                         adapter = (TableAdapter) tabItem.getData();
94                 }
95
96                 final TableAdapter ta = adapter;
97                 
98                 Composite main = new Composite(tabs, SWT.NONE);
99                 GridLayout layout = new GridLayout(1, false);
100                 layout.horizontalSpacing = 0;
101                 layout.verticalSpacing = 0;
102                 main.setLayout(layout);
103
104                 // load up main
105                 ToolBar toolbar = new ToolBar(main, SWT.HORIZONTAL);
106                 final Table table = new Table(main, SWT.FULL_SELECTION | SWT.MULTI);
107                 final Label label = new Label(main, SWT.NULL);
108
109                 final Action copyAction = new Action() {
110                         public void run() {
111                                 TableItem items[] = table.getSelection();
112                                 StringBuffer text = new StringBuffer();
113                                 for (int i = 0; i < items.length; i++) {
114                                         int columns = table.getColumnCount();
115                                         for (int col = 0; col < columns; col++) {
116                                                 text.append(items[i].getText(col));
117                                                 text.append('\t');
118                                         }
119                                         text.append('\n');
120                                 }
121                                 clip.setContents(
122                                         new Object[] { text.toString()},
123                                         new Transfer[] { TextTransfer.getInstance()});
124                         }
125                 };
126                 final Action selectAllAction = new Action() {
127                         public void run() {
128                                 table.selectAll();
129                         }
130                 };
131
132                 // load toobar
133                 ToolItem toolItem = new ToolItem(toolbar, SWT.PUSH);
134                 toolItem.setImage(PHPEclipseSQLPlugin.getImage("refresh.gif")); //$NON-NLS-1$
135                 toolItem.setToolTipText(Messages.getString("tableview.refresh")); //$NON-NLS-1$
136                 toolItem.addSelectionListener(new SelectionListener() {
137                         public void widgetDefaultSelected(SelectionEvent e) {
138                         }
139                         public void widgetSelected(SelectionEvent e) {
140                                 refreshTableAction.run();
141                         }
142                 });
143                 toolItem = new ToolItem(toolbar, SWT.PUSH);
144                 toolItem.setImage(PHPEclipseSQLPlugin.getImage("copy.gif")); //$NON-NLS-1$
145                 toolItem.setToolTipText(Messages.getString("tableview.copy")); //$NON-NLS-1$
146                 toolItem.addSelectionListener(new SelectionListener() {
147                         public void widgetDefaultSelected(SelectionEvent e) {
148                         }
149                         public void widgetSelected(SelectionEvent e) {
150                                 copyAction.run();
151                         }
152                 });
153                 toolItem = new ToolItem(toolbar, SWT.PUSH);
154                 toolItem.setImage(PHPEclipseSQLPlugin.getImage("table.gif")); //$NON-NLS-1$
155                 toolItem.setToolTipText(Messages.getString("tableview.selectAll")); //$NON-NLS-1$
156                 toolItem.addSelectionListener(new SelectionListener() {
157                         public void widgetDefaultSelected(SelectionEvent e) {
158                         }
159                         public void widgetSelected(SelectionEvent e) {
160                                 selectAllAction.run();
161                         }
162                 });
163
164                 ToolItem filter = new ToolItem(toolbar, SWT.PUSH);
165                 filter.setImage(PHPEclipseSQLPlugin.getImage("filter.gif")); //$NON-NLS-1$
166                 filter.setToolTipText(Messages.getString("tableview.filterSort")); //$NON-NLS-1$
167
168                 toolItem = new ToolItem(toolbar, SWT.SEPARATOR);
169
170                 final ToolItem fullMode = new ToolItem(toolbar, SWT.PUSH | SWT.CHECK);
171
172                 final ToolItem previous = new ToolItem(toolbar, SWT.PUSH);
173                 final ToolItem next = new ToolItem(toolbar, SWT.PUSH);
174
175                 fullMode.setImage(PHPEclipseSQLPlugin.getImage("fulldata.gif")); //$NON-NLS-1$
176                 fullMode.setToolTipText(Messages.getString("tableview.showAll")); //$NON-NLS-1$
177                 fullMode.setSelection(false);
178                 fullMode.addSelectionListener(new SelectionListener() {
179                         public void widgetDefaultSelected(SelectionEvent e) {
180                         }
181                         public void widgetSelected(SelectionEvent e) {
182                                 if (ta.getPageSize() == Integer.MAX_VALUE) {
183                                         ta.resetMode();
184                                 } else {
185                                         ta.fullMode();
186                                 }
187                                 ta.loadData();
188                                 table.removeAll();
189                                 for (int i = table.getColumnCount() - 1; i >= 0; i--) {
190                                         table.getColumn(i).dispose();
191                                 }
192                                 ta.loadTable(table);
193                                 label.setText(ta.getStatusString());
194                                 previous.setEnabled(ta.hasPreviousPage());
195                                 next.setEnabled(ta.hasNextPage());
196                         }
197                 });
198                 previous.setImage(PHPEclipseSQLPlugin.getImage("previous.gif")); //$NON-NLS-1$
199                 previous.setToolTipText("Previous"); //$NON-NLS-1$
200                 previous.addSelectionListener(new SelectionListener() {
201                         public void widgetDefaultSelected(SelectionEvent e) {
202                         }
203                         public void widgetSelected(SelectionEvent e) {
204                                 ta.previousPage();
205                                 ta.loadData();
206                                 table.removeAll();
207                                 for (int i = table.getColumnCount() - 1; i >= 0; i--) {
208                                         table.getColumn(i).dispose();
209                                 }
210                                 ta.loadTable(table);
211                                 label.setText(ta.getStatusString());
212                                 previous.setEnabled(ta.hasPreviousPage());
213                                 next.setEnabled(ta.hasNextPage());
214                         }
215                 });
216                 next.setImage(PHPEclipseSQLPlugin.getImage("next.gif")); //$NON-NLS-1$
217                 next.setToolTipText("Next"); //$NON-NLS-1$
218                 next.addSelectionListener(new SelectionListener() {
219                         public void widgetDefaultSelected(SelectionEvent e) {
220                         }
221                         public void widgetSelected(SelectionEvent e) {
222                                 ta.nextPage();
223                                 ta.loadData();
224                                 table.removeAll();
225                                 for (int i = table.getColumnCount() - 1; i >= 0; i--) {
226                                         table.getColumn(i).dispose();
227                                 }
228                                 ta.loadTable(table);
229                                 label.setText(ta.getStatusString());
230                                 previous.setEnabled(ta.hasPreviousPage());
231                                 next.setEnabled(ta.hasNextPage());
232                         }
233                 });
234
235                 toolItem = new ToolItem(toolbar, SWT.SEPARATOR);
236
237                 toolItem = new ToolItem(toolbar, SWT.PUSH);
238                 toolItem.setImage(PHPEclipseSQLPlugin.getImage("close.gif")); //$NON-NLS-1$
239                 toolItem.setToolTipText(Messages.getString("tableview.close")); //$NON-NLS-1$
240                 toolItem.addSelectionListener(new SelectionListener() {
241                                                                                   public void widgetDefaultSelected(SelectionEvent e) {
242                                                                                   }
243                                                                                   public void widgetSelected(SelectionEvent e) {
244                                                                                           closeTableAction.run();
245                                                                                   }
246                                                                           });
247
248                 // load table
249                 if (reload) {
250                         adapter.resetOffset();
251                         adapter.loadData();
252                 }
253                 adapter.loadTable(table);
254                 String tableName = adapter.getTable();
255                 if (tableName != null) {
256                         tabItem.setText(tableName);
257                 } else {
258                         tabItem.setText(adapter.getQuery());
259                 }
260
261                 previous.setEnabled(adapter.hasPreviousPage());
262                 next.setEnabled(adapter.hasNextPage());
263                 label.setText(ta.getStatusString());
264                 
265                 GridData gridData = new GridData();
266                 gridData.horizontalAlignment = GridData.FILL;
267                 gridData.verticalAlignment = GridData.FILL;
268                 gridData.grabExcessHorizontalSpace = true;
269                 gridData.grabExcessVerticalSpace = true;
270                 table.setLayoutData(gridData);
271
272                 gridData = new GridData();
273                 gridData.horizontalAlignment = GridData.FILL;
274                 label.setLayoutData(gridData);
275
276                 // create empty table row
277                 TableColumn[] columns = table.getColumns();
278                 String columnNames[] = new String[columns.length];
279                 for (int i = 0; i < columns.length; i++) {
280                         columnNames[i] = columns[i].getText();
281                 }
282                 String data[] = new String[columnNames.length];
283                 for (int i = 0; i < columns.length; i++) {
284                         data[i] = ""; //$NON-NLS-1$
285                 }
286                 
287                 final TableRow emptyRow =
288                         new TableRow(ta.getBookmark(), ta.getTable(), columnNames, data);
289                 
290                 filter.addSelectionListener(new SelectionListener() {
291                         public void widgetDefaultSelected(SelectionEvent e) {
292                         }
293                         public void widgetSelected(SelectionEvent e) {
294                                         SortFilterPage page = new SortFilterPage(""); //$NON-NLS-1$
295                                         SQLRowWizard wizard = new SQLRowWizard();
296                                         wizard.init(Messages.getString("TableView.FilterAndSort"), page, emptyRow, ta); //$NON-NLS-1$
297                                         WizardDialog dialog =
298                                                 new WizardDialog(
299                                                         getSite().getShell(),
300                                                         wizard);
301                                         dialog.open();
302                         }
303                 });
304
305
306                 final Action defaultEncodingAction = new Action() {
307                         public void run() {
308                                 ta.setEncoding(TableAdapter.DEFAULT);
309                         }
310                 };
311                 defaultEncodingAction.setText(Messages.getString("tableview.defaultEncoding")); //$NON-NLS-1$
312                 final Action UTF8EncodingAction = new Action() {
313                         public void run() {
314                                 ta.setEncoding(TableAdapter.UTF_8);
315                         }
316                 };
317                 UTF8EncodingAction.setText(Messages.getString("tableview.UTF8Encoding")); //$NON-NLS-1$
318                 final Action UTF16EncodingAction = new Action() {
319                         public void run() {
320                                 ta.setEncoding(TableAdapter.UTF_16);
321                         }
322                 };
323                 UTF16EncodingAction.setText(Messages.getString("tableview.UTF16Encoding")); //$NON-NLS-1$
324
325                 IMenuListener menuListener = new IMenuListener() {
326                         public void menuAboutToShow(IMenuManager mgr) {
327                                 if (ta.getTable() != null) {
328                                         TableItem[] selection = table.getSelection();
329                                         TableColumn[] columns = table.getColumns();
330                                         String columnNames[] = new String[columns.length];
331                                         for (int i = 0; i < columns.length; i++) {
332                                                 columnNames[i] = columns[i].getText();
333                                         }
334                                         String data[] = new String[columnNames.length];
335                                         if (selection != null && selection.length > 0) {
336                                                 TableItem sel = selection[0];
337                                                 for (int i = 0; i < columns.length; i++) {
338                                                         data[i] = sel.getText(i);
339                                                 }
340                                         } else {
341                                                 for (int i = 0; i < columns.length; i++) {
342                                                         data[i] = ""; //$NON-NLS-1$
343                                                 }
344                                         }
345                                         final TableRow row =
346                                                 new TableRow(ta.getBookmark(), ta.getTable(), columnNames, data);
347                                                 Action updateAction = new Action() {
348                                                         public void run() {
349                                                                 UpdateRowPage page = new UpdateRowPage(""); //$NON-NLS-1$
350                                                                 SQLRowWizard wizard = new SQLRowWizard();
351                                                                 wizard.init(Messages.getString("TableView.UpdateRow"), page, row, ta); //$NON-NLS-1$
352                                                                 WizardDialog dialog =
353                                                                         new WizardDialog(
354                                                                                 getSite().getShell(),
355                                                                                 wizard);
356                                                                 dialog.open();
357                                                         }
358                                                 };
359                                                 updateAction.setText(Messages.getString("tableview.update")); //$NON-NLS-1$
360                                                 Action insertAction = new Action() {
361                                                         public void run() {
362                                                                 InsertRowPage page = new InsertRowPage(""); //$NON-NLS-1$
363                                                                 SQLRowWizard wizard = new SQLRowWizard();
364                                                                 wizard.init(Messages.getString("TableView.InsertRow"), page, row, ta); //$NON-NLS-1$
365                                                                 WizardDialog dialog =
366                                                                         new WizardDialog(
367                                                                                 getSite().getShell(),
368                                                                                 wizard);
369                                                                 dialog.open();
370                                                         }
371                                                 };
372                                                 insertAction.setText(Messages.getString("tableview.insert")); //$NON-NLS-1$
373                                                 Action deleteAction = new Action() {
374                                                         public void run() {
375                                                                 DeleteRowPage page = new DeleteRowPage(""); //$NON-NLS-1$
376                                                                 SQLRowWizard wizard = new SQLRowWizard();
377                                                                 wizard.init(Messages.getString("TableView.DeleteRow"), page, row, ta); //$NON-NLS-1$
378                                                                 WizardDialog dialog =
379                                                                         new WizardDialog(
380                                                                                 getSite().getShell(),
381                                                                                 wizard);
382                                                                 dialog.open();
383                                                         }
384                                                 };
385                                                 deleteAction.setText(Messages.getString("tableview.delete")); //$NON-NLS-1$
386                                                 mgr.add(insertAction);
387                                                 mgr.add(updateAction);
388                                                 mgr.add(deleteAction);
389                                         }
390                                         mgr.add(defaultEncodingAction);
391                                         mgr.add(UTF8EncodingAction);
392                                         mgr.add(UTF16EncodingAction);
393                                 }
394                 };
395
396                 // final setup
397                 MenuManager manager = new MenuManager();
398                 manager.setRemoveAllWhenShown(true);
399                 Menu fTextContextMenu = manager.createContextMenu(table);
400                 table.setMenu(fTextContextMenu);
401                 table.setLinesVisible(true);
402                 manager.addMenuListener(menuListener);
403                 
404                 tabItem.setControl(main);
405                 tabs.setSelection(tabs.indexOf(tabItem));
406         }
407
408         public void createPartControl(Composite parent) {
409                 instance = this;
410                 this.parent = parent;
411                 initActions();
412                 clip = new Clipboard(getSite().getShell().getDisplay());
413                 tabs = new TabFolder(parent, SWT.NONE);
414         }
415         public void initActions() {
416                 refreshTableAction = new RefreshTableAction();
417                 refreshTableAction.setText(Messages.getString("tableview.refresh")); //$NON-NLS-1$
418                 refreshTableAction.setImageDescriptor(
419                         PHPEclipseSQLPlugin.getImageDescriptor("refresh.gif")); //$NON-NLS-1$
420                 refreshTableAction.init(this);
421                 closeTableAction = new CloseTableAction();
422                 closeTableAction.setText(Messages.getString("tableview.close")); //$NON-NLS-1$
423                 closeTableAction.setImageDescriptor(
424                         PHPEclipseSQLPlugin.getImageDescriptor("close.gif")); //$NON-NLS-1$
425                 closeTableAction.init(this);
426         }
427
428         public void selectionChanged(IWorkbenchPart part, ISelection selection) {
429         }
430 }