1 package net.sourceforge.phpdt.sql.view;
3 import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
4 import net.sourceforge.phpdt.sql.actions.CloseTableAction;
5 import net.sourceforge.phpdt.sql.actions.RefreshTableAction;
6 import net.sourceforge.phpdt.sql.sql.SQLResults;
7 import net.sourceforge.phpdt.sql.sql.TableRow;
8 import net.sourceforge.phpdt.sql.view.tableview.TableAdapter;
9 import net.sourceforge.phpdt.sql.wizards.DeleteRowPage;
10 import net.sourceforge.phpdt.sql.wizards.InsertRowPage;
11 import net.sourceforge.phpdt.sql.wizards.PHPDeleteRowPage;
12 import net.sourceforge.phpdt.sql.wizards.PHPInsertRowPage;
13 import net.sourceforge.phpdt.sql.wizards.PHPSelectRowPage;
14 import net.sourceforge.phpdt.sql.wizards.PHPUpdateRowPage;
15 import net.sourceforge.phpdt.sql.wizards.SQLRowWizard;
16 import net.sourceforge.phpdt.sql.wizards.SortFilterPage;
17 import net.sourceforge.phpdt.sql.wizards.UpdateRowPage;
19 import org.eclipse.jface.action.Action;
20 import org.eclipse.jface.action.IMenuListener;
21 import org.eclipse.jface.action.IMenuManager;
22 import org.eclipse.jface.action.MenuManager;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.jface.wizard.WizardDialog;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.dnd.Clipboard;
27 import org.eclipse.swt.dnd.TextTransfer;
28 import org.eclipse.swt.dnd.Transfer;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.events.SelectionListener;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Menu;
36 import org.eclipse.swt.widgets.TabFolder;
37 import org.eclipse.swt.widgets.TabItem;
38 import org.eclipse.swt.widgets.Table;
39 import org.eclipse.swt.widgets.TableColumn;
40 import org.eclipse.swt.widgets.TableItem;
41 import org.eclipse.swt.widgets.ToolBar;
42 import org.eclipse.swt.widgets.ToolItem;
43 import org.eclipse.ui.ISelectionListener;
44 import org.eclipse.ui.IWorkbenchPart;
45 import org.eclipse.ui.part.ViewPart;
47 public class TableView extends ViewPart implements ISelectionListener {
48 public static String TABLEVIEW_ID =
49 "net.sourceforge.phpdt.sql.view.tableview";
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 String table = adapter.getTable();
73 loadTable(item, null, null, true, true);
75 loadTable(item, null, null, true, true);
78 public void loadQuery(SQLResults results) {
79 loadTable(null, null, results, true, false);
81 public void loadTable(String table) {
82 loadTable(null, table, null, false, true);
84 public void loadTable(
91 if (tabItem == null) {
92 tabItem = new TabItem(tabs, SWT.NONE);
94 adapter = TableAdapter.createFromQuery(results);
96 adapter = TableAdapter.createFromTable(entity);
98 tabItem.setData(adapter);
100 adapter = (TableAdapter) tabItem.getData();
103 final TableAdapter ta = adapter;
105 Composite main = new Composite(tabs, SWT.NONE);
106 GridLayout layout = new GridLayout(1, false);
107 layout.horizontalSpacing = 0;
108 layout.verticalSpacing = 0;
109 main.setLayout(layout);
112 ToolBar toolbar = new ToolBar(main, SWT.HORIZONTAL);
113 final Table table = new Table(main, SWT.FULL_SELECTION | SWT.MULTI);
114 final Label label = new Label(main, SWT.NULL);
116 final Action copyAction = new Action() {
118 TableItem items[] = table.getSelection();
119 StringBuffer text = new StringBuffer();
120 for (int i = 0; i < items.length; i++) {
121 int columns = table.getColumnCount();
122 for (int col = 0; col < columns; col++) {
123 text.append(items[i].getText(col));
129 new Object[] { text.toString()},
130 new Transfer[] { TextTransfer.getInstance()});
133 final Action selectAllAction = new Action() {
140 ToolItem toolItem = new ToolItem(toolbar, SWT.PUSH);
141 toolItem.setImage(PHPEclipseSQLPlugin.getImage("refresh.gif")); //$NON-NLS-1$
142 toolItem.setToolTipText(Messages.getString("tableview.refresh")); //$NON-NLS-1$
143 toolItem.addSelectionListener(new SelectionListener() {
144 public void widgetDefaultSelected(SelectionEvent e) {
146 public void widgetSelected(SelectionEvent e) {
147 refreshTableAction.run();
150 toolItem = new ToolItem(toolbar, SWT.PUSH);
151 toolItem.setImage(PHPEclipseSQLPlugin.getImage("copy.gif")); //$NON-NLS-1$
152 toolItem.setToolTipText(Messages.getString("tableview.copy")); //$NON-NLS-1$
153 toolItem.addSelectionListener(new SelectionListener() {
154 public void widgetDefaultSelected(SelectionEvent e) {
156 public void widgetSelected(SelectionEvent e) {
160 toolItem = new ToolItem(toolbar, SWT.PUSH);
161 toolItem.setImage(PHPEclipseSQLPlugin.getImage("table.gif")); //$NON-NLS-1$
162 toolItem.setToolTipText(Messages.getString("tableview.selectAll")); //$NON-NLS-1$
163 toolItem.addSelectionListener(new SelectionListener() {
164 public void widgetDefaultSelected(SelectionEvent e) {
166 public void widgetSelected(SelectionEvent e) {
167 selectAllAction.run();
171 ToolItem filter = new ToolItem(toolbar, SWT.PUSH);
172 filter.setImage(PHPEclipseSQLPlugin.getImage("filter.gif")); //$NON-NLS-1$
173 filter.setToolTipText(Messages.getString("tableview.filterSort")); //$NON-NLS-1$
175 toolItem = new ToolItem(toolbar, SWT.SEPARATOR);
177 final ToolItem fullMode = new ToolItem(toolbar, SWT.PUSH | SWT.CHECK);
179 final ToolItem previous = new ToolItem(toolbar, SWT.PUSH);
180 final ToolItem next = new ToolItem(toolbar, SWT.PUSH);
182 fullMode.setImage(PHPEclipseSQLPlugin.getImage("fulldata.gif"));
183 fullMode.setToolTipText(Messages.getString("tableview.showAll")); //$NON-NLS-1$
184 fullMode.setSelection(false);
185 fullMode.addSelectionListener(new SelectionListener() {
186 public void widgetDefaultSelected(SelectionEvent e) {
188 public void widgetSelected(SelectionEvent e) {
189 if (ta.getPageSize() == Integer.MAX_VALUE) {
196 for (int i = table.getColumnCount() - 1; i >= 0; i--) {
197 table.getColumn(i).dispose();
200 label.setText(ta.getStatusString());
201 previous.setEnabled(ta.hasPreviousPage());
202 next.setEnabled(ta.hasNextPage());
205 previous.setImage(PHPEclipseSQLPlugin.getImage("previous.gif"));
206 previous.setToolTipText("Previous");
207 previous.addSelectionListener(new SelectionListener() {
208 public void widgetDefaultSelected(SelectionEvent e) {
210 public void widgetSelected(SelectionEvent e) {
214 for (int i = table.getColumnCount() - 1; i >= 0; i--) {
215 table.getColumn(i).dispose();
218 label.setText(ta.getStatusString());
219 previous.setEnabled(ta.hasPreviousPage());
220 next.setEnabled(ta.hasNextPage());
223 next.setImage(PHPEclipseSQLPlugin.getImage("next.gif"));
224 next.setToolTipText("Next");
225 next.addSelectionListener(new SelectionListener() {
226 public void widgetDefaultSelected(SelectionEvent e) {
228 public void widgetSelected(SelectionEvent e) {
232 for (int i = table.getColumnCount() - 1; i >= 0; i--) {
233 table.getColumn(i).dispose();
236 label.setText(ta.getStatusString());
237 previous.setEnabled(ta.hasPreviousPage());
238 next.setEnabled(ta.hasNextPage());
242 toolItem = new ToolItem(toolbar, SWT.SEPARATOR);
244 toolItem = new ToolItem(toolbar, SWT.PUSH);
245 toolItem.setImage(PHPEclipseSQLPlugin.getImage("close.gif")); //$NON-NLS-1$
246 toolItem.setToolTipText(Messages.getString("tableview.close")); //$NON-NLS-1$
247 toolItem.addSelectionListener(new SelectionListener() {
248 public void widgetDefaultSelected(SelectionEvent e) {
250 public void widgetSelected(SelectionEvent e) {
251 closeTableAction.run();
257 adapter.resetOffset();
260 adapter.loadTable(table);
261 String tableName = adapter.getTable();
262 if (tableName != null) {
263 tabItem.setText(tableName);
265 tabItem.setText(adapter.getQuery());
268 previous.setEnabled(adapter.hasPreviousPage());
269 next.setEnabled(adapter.hasNextPage());
270 label.setText(ta.getStatusString());
272 GridData gridData = new GridData();
273 gridData.horizontalAlignment = GridData.FILL;
274 gridData.verticalAlignment = GridData.FILL;
275 gridData.grabExcessHorizontalSpace = true;
276 gridData.grabExcessVerticalSpace = true;
277 table.setLayoutData(gridData);
279 gridData = new GridData();
280 gridData.horizontalAlignment = GridData.FILL;
281 label.setLayoutData(gridData);
283 // create empty table row
284 TableColumn[] columns = table.getColumns();
285 String columnNames[] = new String[columns.length];
286 for (int i = 0; i < columns.length; i++) {
287 columnNames[i] = columns[i].getText();
289 String data[] = new String[columnNames.length];
290 for (int i = 0; i < columns.length; i++) {
293 final TableRow emptyRow = new TableRow(ta.getTable(), columnNames, data);
295 filter.addSelectionListener(new SelectionListener() {
296 public void widgetDefaultSelected(SelectionEvent e) {
298 public void widgetSelected(SelectionEvent e) {
299 SortFilterPage page = new SortFilterPage("");
300 SQLRowWizard wizard = new SQLRowWizard();
301 wizard.init("Filter and Sort", page, emptyRow, ta);
302 WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard);
307 final Action defaultEncodingAction = new Action() {
309 ta.setEncoding(TableAdapter.DEFAULT);
312 defaultEncodingAction.setText(Messages.getString("tableview.defaultEncoding")); //$NON-NLS-1$
313 final Action UTF8EncodingAction = new Action() {
315 ta.setEncoding(TableAdapter.UTF_8);
318 UTF8EncodingAction.setText(Messages.getString("tableview.UTF8Encoding")); //$NON-NLS-1$
319 final Action UTF16EncodingAction = new Action() {
321 ta.setEncoding(TableAdapter.UTF_16);
324 UTF16EncodingAction.setText(Messages.getString("tableview.UTF16Encoding")); //$NON-NLS-1$
326 IMenuListener menuListener = new IMenuListener() {
327 public void menuAboutToShow(IMenuManager mgr) {
328 if (ta.getTable() != null) {
329 TableItem[] selection = table.getSelection();
330 TableColumn[] columns = table.getColumns();
331 String columnNames[] = new String[columns.length];
332 for (int i = 0; i < columns.length; i++) {
333 columnNames[i] = columns[i].getText();
335 String data[] = new String[columnNames.length];
336 if (selection != null && selection.length > 0) {
337 TableItem sel = selection[0];
338 for (int i = 0; i < columns.length; i++) {
339 data[i] = sel.getText(i);
342 for (int i = 0; i < columns.length; i++) {
346 final TableRow row = new TableRow(ta.getTable(), columnNames, data);
347 Action updateAction = new Action() {
349 UpdateRowPage page = new UpdateRowPage("");
350 SQLRowWizard wizard = new SQLRowWizard();
351 wizard.init("Update Row", page, row, ta);
352 WizardDialog dialog =
353 new WizardDialog(getSite().getShell(), wizard);
357 updateAction.setText(Messages.getString("tableview.update")); //$NON-NLS-1$
358 Action insertAction = new Action() {
360 InsertRowPage page = new InsertRowPage("");
361 SQLRowWizard wizard = new SQLRowWizard();
362 wizard.init("Insert Row", page, row, ta);
363 WizardDialog dialog =
364 new WizardDialog(getSite().getShell(), wizard);
368 insertAction.setText(Messages.getString("tableview.insert")); //$NON-NLS-1$
369 Action deleteAction = new Action() {
371 DeleteRowPage page = new DeleteRowPage("");
372 SQLRowWizard wizard = new SQLRowWizard();
373 wizard.init("Delete Row", page, row, ta);
374 WizardDialog dialog =
375 new WizardDialog(getSite().getShell(), wizard);
379 deleteAction.setText(Messages.getString("tableview.delete")); //$NON-NLS-1$
381 Action phpSelectAction = new Action() {
383 PHPSelectRowPage page = new PHPSelectRowPage("");
384 SQLRowWizard wizard = new SQLRowWizard();
385 wizard.init("PHP Select Generator", page, row, ta);
386 WizardDialog dialog =
387 new WizardDialog(getSite().getShell(), wizard);
391 phpSelectAction.setText(Messages.getString("tableview.phpselect")); //$NON-NLS-1$
393 Action phpUpdateAction = new Action() {
395 PHPUpdateRowPage page = new PHPUpdateRowPage("");
396 SQLRowWizard wizard = new SQLRowWizard();
397 wizard.init("PHP Update Generator", page, row, ta);
398 WizardDialog dialog =
399 new WizardDialog(getSite().getShell(), wizard);
403 phpUpdateAction.setText(Messages.getString("tableview.phpupdate")); //$NON-NLS-1$
405 Action phpInsertAction = new Action() {
407 PHPInsertRowPage page = new PHPInsertRowPage("");
408 SQLRowWizard wizard = new SQLRowWizard();
409 wizard.init("PHP Insert Generator", page, row, ta);
410 WizardDialog dialog =
411 new WizardDialog(getSite().getShell(), wizard);
415 phpInsertAction.setText(Messages.getString("tableview.phpinsert")); //$NON-NLS-1$
417 Action phpDeleteAction = new Action() {
419 PHPDeleteRowPage page = new PHPDeleteRowPage("");
420 SQLRowWizard wizard = new SQLRowWizard();
421 wizard.init("PHP Delete Generator", page, row, ta);
422 WizardDialog dialog =
423 new WizardDialog(getSite().getShell(), wizard);
427 phpDeleteAction.setText(Messages.getString("tableview.phpdelete")); //$NON-NLS-1$
429 mgr.add(phpSelectAction);
430 mgr.add(phpUpdateAction);
431 mgr.add(phpInsertAction);
432 mgr.add(phpDeleteAction);
434 mgr.add(insertAction);
435 mgr.add(updateAction);
436 mgr.add(deleteAction);
439 mgr.add(defaultEncodingAction);
440 mgr.add(UTF8EncodingAction);
441 mgr.add(UTF16EncodingAction);
446 MenuManager manager = new MenuManager();
447 manager.setRemoveAllWhenShown(true);
448 Menu fTextContextMenu = manager.createContextMenu(table);
449 table.setMenu(fTextContextMenu);
450 table.setLinesVisible(true);
451 manager.addMenuListener(menuListener);
453 tabItem.setControl(main);
454 tabs.setSelection(tabs.indexOf(tabItem));
457 public void createPartControl(Composite parent) {
459 this.parent = parent;
461 clip = new Clipboard(getSite().getShell().getDisplay());
462 tabs = new TabFolder(parent, SWT.NONE);
464 public void initActions() {
465 refreshTableAction = new RefreshTableAction();
466 refreshTableAction.setText(Messages.getString("tableview.refresh"));
467 refreshTableAction.setImageDescriptor(
468 PHPEclipseSQLPlugin.getImageDescriptor("refresh.gif"));
469 refreshTableAction.init(this);
470 closeTableAction = new CloseTableAction();
471 closeTableAction.setText(Messages.getString("tableview.close"));
472 closeTableAction.setImageDescriptor(
473 PHPEclipseSQLPlugin.getImageDescriptor("close.gif"));
474 closeTableAction.init(this);
477 public void selectionChanged(IWorkbenchPart part, ISelection selection) {