1 package net.sourceforge.phpdt.sql.view;
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;
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;
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;
55 public void setFocus() {
57 public static TableView getInstance() {
60 public void closeCurrent() {
61 TabItem item = tabs.getItem(tabs.getSelectionIndex());
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();
71 loadTable(bookmark, item, null, null, true, true);
73 loadTable(bookmark, item, null, null, true, true);
76 public void loadQuery(BookmarkNode bookmark, SQLResults results) {
77 loadTable(bookmark, null, null, results, true, false);
79 public void loadTable(BookmarkNode bookmark, String table) {
80 loadTable(bookmark, null, table, null, false, true);
82 public void loadTable(BookmarkNode bookmark, TabItem tabItem, String entity, SQLResults results, boolean query, boolean reload) {
84 if (tabItem == null) {
85 tabItem = new TabItem(tabs, SWT.NONE);
87 adapter = TableAdapter.createFromQuery(bookmark, results);
89 adapter = TableAdapter.createFromTable(bookmark, entity);
91 tabItem.setData(adapter);
93 adapter = (TableAdapter) tabItem.getData();
96 final TableAdapter ta = adapter;
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);
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);
109 final Action copyAction = new Action() {
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));
122 new Object[] { text.toString()},
123 new Transfer[] { TextTransfer.getInstance()});
126 final Action selectAllAction = new Action() {
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) {
139 public void widgetSelected(SelectionEvent e) {
140 refreshTableAction.run();
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) {
149 public void widgetSelected(SelectionEvent e) {
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) {
159 public void widgetSelected(SelectionEvent e) {
160 selectAllAction.run();
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$
168 toolItem = new ToolItem(toolbar, SWT.SEPARATOR);
170 final ToolItem fullMode = new ToolItem(toolbar, SWT.PUSH | SWT.CHECK);
172 final ToolItem previous = new ToolItem(toolbar, SWT.PUSH);
173 final ToolItem next = new ToolItem(toolbar, SWT.PUSH);
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) {
181 public void widgetSelected(SelectionEvent e) {
182 if (ta.getPageSize() == Integer.MAX_VALUE) {
189 for (int i = table.getColumnCount() - 1; i >= 0; i--) {
190 table.getColumn(i).dispose();
193 label.setText(ta.getStatusString());
194 previous.setEnabled(ta.hasPreviousPage());
195 next.setEnabled(ta.hasNextPage());
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) {
203 public void widgetSelected(SelectionEvent e) {
207 for (int i = table.getColumnCount() - 1; i >= 0; i--) {
208 table.getColumn(i).dispose();
211 label.setText(ta.getStatusString());
212 previous.setEnabled(ta.hasPreviousPage());
213 next.setEnabled(ta.hasNextPage());
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) {
221 public void widgetSelected(SelectionEvent e) {
225 for (int i = table.getColumnCount() - 1; i >= 0; i--) {
226 table.getColumn(i).dispose();
229 label.setText(ta.getStatusString());
230 previous.setEnabled(ta.hasPreviousPage());
231 next.setEnabled(ta.hasNextPage());
235 toolItem = new ToolItem(toolbar, SWT.SEPARATOR);
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) {
243 public void widgetSelected(SelectionEvent e) {
244 closeTableAction.run();
250 adapter.resetOffset();
253 adapter.loadTable(table);
254 String tableName = adapter.getTable();
255 if (tableName != null) {
256 tabItem.setText(tableName);
258 tabItem.setText(adapter.getQuery());
261 previous.setEnabled(adapter.hasPreviousPage());
262 next.setEnabled(adapter.hasNextPage());
263 label.setText(ta.getStatusString());
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);
272 gridData = new GridData();
273 gridData.horizontalAlignment = GridData.FILL;
274 label.setLayoutData(gridData);
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();
282 String data[] = new String[columnNames.length];
283 for (int i = 0; i < columns.length; i++) {
284 data[i] = ""; //$NON-NLS-1$
287 final TableRow emptyRow =
288 new TableRow(ta.getBookmark(), ta.getTable(), columnNames, data);
290 filter.addSelectionListener(new SelectionListener() {
291 public void widgetDefaultSelected(SelectionEvent e) {
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 =
299 getSite().getShell(),
306 final Action defaultEncodingAction = new Action() {
308 ta.setEncoding(TableAdapter.DEFAULT);
311 defaultEncodingAction.setText(Messages.getString("tableview.defaultEncoding")); //$NON-NLS-1$
312 final Action UTF8EncodingAction = new Action() {
314 ta.setEncoding(TableAdapter.UTF_8);
317 UTF8EncodingAction.setText(Messages.getString("tableview.UTF8Encoding")); //$NON-NLS-1$
318 final Action UTF16EncodingAction = new Action() {
320 ta.setEncoding(TableAdapter.UTF_16);
323 UTF16EncodingAction.setText(Messages.getString("tableview.UTF16Encoding")); //$NON-NLS-1$
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();
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);
341 for (int i = 0; i < columns.length; i++) {
342 data[i] = ""; //$NON-NLS-1$
346 new TableRow(ta.getBookmark(), ta.getTable(), columnNames, data);
347 Action updateAction = new Action() {
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 =
354 getSite().getShell(),
359 updateAction.setText(Messages.getString("tableview.update")); //$NON-NLS-1$
360 Action insertAction = new Action() {
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 =
367 getSite().getShell(),
372 insertAction.setText(Messages.getString("tableview.insert")); //$NON-NLS-1$
373 Action deleteAction = new Action() {
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 =
380 getSite().getShell(),
385 deleteAction.setText(Messages.getString("tableview.delete")); //$NON-NLS-1$
386 mgr.add(insertAction);
387 mgr.add(updateAction);
388 mgr.add(deleteAction);
390 mgr.add(defaultEncodingAction);
391 mgr.add(UTF8EncodingAction);
392 mgr.add(UTF16EncodingAction);
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);
404 tabItem.setControl(main);
405 tabs.setSelection(tabs.indexOf(tabItem));
408 public void createPartControl(Composite parent) {
410 this.parent = parent;
412 clip = new Clipboard(getSite().getShell().getDisplay());
413 tabs = new TabFolder(parent, SWT.NONE);
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);
428 public void selectionChanged(IWorkbenchPart part, ISelection selection) {