1 package net.sourceforge.phpdt.sql.view;
3 import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
5 import org.eclipse.jface.action.Action;
6 import org.eclipse.jface.action.IMenuListener;
7 import org.eclipse.jface.action.IMenuManager;
8 import org.eclipse.jface.action.MenuManager;
9 import org.eclipse.jface.viewers.ISelection;
10 import org.eclipse.jface.wizard.WizardDialog;
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.dnd.Clipboard;
13 import org.eclipse.swt.dnd.TextTransfer;
14 import org.eclipse.swt.dnd.Transfer;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.events.SelectionListener;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Label;
21 import org.eclipse.swt.widgets.Menu;
22 import org.eclipse.swt.widgets.TabFolder;
23 import org.eclipse.swt.widgets.TabItem;
24 import org.eclipse.swt.widgets.Table;
25 import org.eclipse.swt.widgets.TableColumn;
26 import org.eclipse.swt.widgets.TableItem;
27 import org.eclipse.swt.widgets.ToolBar;
28 import org.eclipse.swt.widgets.ToolItem;
29 import org.eclipse.ui.ISelectionListener;
30 import org.eclipse.ui.IWorkbenchPart;
31 import org.eclipse.ui.part.ViewPart;
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.tableview.TableAdapter;
38 import net.sourceforge.phpdt.sql.wizards.DeleteRowPage;
39 import net.sourceforge.phpdt.sql.wizards.InsertRowPage;
40 import net.sourceforge.phpdt.sql.wizards.PHPSelectRowPage;
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 import net.sourceforge.phpdt.sql.wizards.PHPSelectRowPage;
46 public class TableView extends ViewPart implements ISelectionListener {
47 public static String TABLEVIEW_ID = "net.sourceforge.phpdt.sql.view.tableview";
48 private Clipboard clip;
49 private RefreshTableAction refreshTableAction;
50 private CloseTableAction closeTableAction;
51 private static TableView instance = null;
52 private TabFolder tabs;
53 private Composite parent;
57 public void setFocus() {
59 public static TableView getInstance() {
62 public void closeCurrent() {
63 TabItem item = tabs.getItem(tabs.getSelectionIndex());
66 public void refreshCurrent() {
67 TabItem item = tabs.getItem(tabs.getSelectionIndex());
68 TableAdapter adapter = (TableAdapter) item.getData();
69 String table = adapter.getTable();
71 loadTable(item, null, null, true, true);
73 loadTable(item, null, null, true, true);
76 public void loadQuery(SQLResults results) {
77 loadTable(null, null, results, true, false);
79 public void loadTable(String table) {
80 loadTable(null, table, null, false, true);
82 public void loadTable(
89 if (tabItem == null) {
90 tabItem = new TabItem(tabs, SWT.NONE);
92 adapter = TableAdapter.createFromQuery(results);
94 adapter = TableAdapter.createFromTable(entity);
96 tabItem.setData(adapter);
98 adapter = (TableAdapter) tabItem.getData();
101 final TableAdapter ta = adapter;
103 Composite main = new Composite(tabs, SWT.NONE);
104 GridLayout layout = new GridLayout(1, false);
105 layout.horizontalSpacing = 0;
106 layout.verticalSpacing = 0;
107 main.setLayout(layout);
110 ToolBar toolbar = new ToolBar(main, SWT.HORIZONTAL);
111 final Table table = new Table(main, SWT.FULL_SELECTION | SWT.MULTI);
112 final Label label = new Label(main, SWT.NULL);
114 final Action copyAction = new Action() {
116 TableItem items[] = table.getSelection();
117 StringBuffer text = new StringBuffer();
118 for (int i = 0; i < items.length; i++) {
119 int columns = table.getColumnCount();
120 for (int col = 0; col < columns; col++) {
121 text.append(items[i].getText(col));
127 new Object[] { text.toString()},
128 new Transfer[] { TextTransfer.getInstance()});
131 final Action selectAllAction = new Action() {
138 ToolItem toolItem = new ToolItem(toolbar, SWT.PUSH);
139 toolItem.setImage(PHPEclipseSQLPlugin.getImage("refresh.gif")); //$NON-NLS-1$
140 toolItem.setToolTipText(Messages.getString("tableview.refresh")); //$NON-NLS-1$
141 toolItem.addSelectionListener(new SelectionListener() {
142 public void widgetDefaultSelected(SelectionEvent e) {
144 public void widgetSelected(SelectionEvent e) {
145 refreshTableAction.run();
148 toolItem = new ToolItem(toolbar, SWT.PUSH);
149 toolItem.setImage(PHPEclipseSQLPlugin.getImage("copy.gif")); //$NON-NLS-1$
150 toolItem.setToolTipText(Messages.getString("tableview.copy")); //$NON-NLS-1$
151 toolItem.addSelectionListener(new SelectionListener() {
152 public void widgetDefaultSelected(SelectionEvent e) {
154 public void widgetSelected(SelectionEvent e) {
158 toolItem = new ToolItem(toolbar, SWT.PUSH);
159 toolItem.setImage(PHPEclipseSQLPlugin.getImage("table.gif")); //$NON-NLS-1$
160 toolItem.setToolTipText(Messages.getString("tableview.selectAll")); //$NON-NLS-1$
161 toolItem.addSelectionListener(new SelectionListener() {
162 public void widgetDefaultSelected(SelectionEvent e) {
164 public void widgetSelected(SelectionEvent e) {
165 selectAllAction.run();
169 ToolItem filter = new ToolItem(toolbar, SWT.PUSH);
170 filter.setImage(PHPEclipseSQLPlugin.getImage("filter.gif")); //$NON-NLS-1$
171 filter.setToolTipText(Messages.getString("tableview.filterSort")); //$NON-NLS-1$
173 toolItem = new ToolItem(toolbar, SWT.SEPARATOR);
175 final ToolItem fullMode = new ToolItem(toolbar, SWT.PUSH | SWT.CHECK);
177 final ToolItem previous = new ToolItem(toolbar, SWT.PUSH);
178 final ToolItem next = new ToolItem(toolbar, SWT.PUSH);
180 fullMode.setImage(PHPEclipseSQLPlugin.getImage("fulldata.gif"));
181 fullMode.setToolTipText(Messages.getString("tableview.showAll")); //$NON-NLS-1$
182 fullMode.setSelection(false);
183 fullMode.addSelectionListener(new SelectionListener() {
184 public void widgetDefaultSelected(SelectionEvent e) {
186 public void widgetSelected(SelectionEvent e) {
187 if (ta.getPageSize() == Integer.MAX_VALUE) {
194 for (int i = table.getColumnCount() - 1; i >= 0; i--) {
195 table.getColumn(i).dispose();
198 label.setText(ta.getStatusString());
199 previous.setEnabled(ta.hasPreviousPage());
200 next.setEnabled(ta.hasNextPage());
203 previous.setImage(PHPEclipseSQLPlugin.getImage("previous.gif"));
204 previous.setToolTipText("Previous");
205 previous.addSelectionListener(new SelectionListener() {
206 public void widgetDefaultSelected(SelectionEvent e) {
208 public void widgetSelected(SelectionEvent e) {
212 for (int i = table.getColumnCount() - 1; i >= 0; i--) {
213 table.getColumn(i).dispose();
216 label.setText(ta.getStatusString());
217 previous.setEnabled(ta.hasPreviousPage());
218 next.setEnabled(ta.hasNextPage());
221 next.setImage(PHPEclipseSQLPlugin.getImage("next.gif"));
222 next.setToolTipText("Next");
223 next.addSelectionListener(new SelectionListener() {
224 public void widgetDefaultSelected(SelectionEvent e) {
226 public void widgetSelected(SelectionEvent e) {
230 for (int i = table.getColumnCount() - 1; i >= 0; i--) {
231 table.getColumn(i).dispose();
234 label.setText(ta.getStatusString());
235 previous.setEnabled(ta.hasPreviousPage());
236 next.setEnabled(ta.hasNextPage());
240 toolItem = new ToolItem(toolbar, SWT.SEPARATOR);
242 toolItem = new ToolItem(toolbar, SWT.PUSH);
243 toolItem.setImage(PHPEclipseSQLPlugin.getImage("close.gif")); //$NON-NLS-1$
244 toolItem.setToolTipText(Messages.getString("tableview.close")); //$NON-NLS-1$
245 toolItem.addSelectionListener(new SelectionListener() {
246 public void widgetDefaultSelected(SelectionEvent e) {
248 public void widgetSelected(SelectionEvent e) {
249 closeTableAction.run();
255 adapter.resetOffset();
258 adapter.loadTable(table);
259 String tableName = adapter.getTable();
260 if (tableName != null) {
261 tabItem.setText(tableName);
263 tabItem.setText(adapter.getQuery());
266 previous.setEnabled(adapter.hasPreviousPage());
267 next.setEnabled(adapter.hasNextPage());
268 label.setText(ta.getStatusString());
270 GridData gridData = new GridData();
271 gridData.horizontalAlignment = GridData.FILL;
272 gridData.verticalAlignment = GridData.FILL;
273 gridData.grabExcessHorizontalSpace = true;
274 gridData.grabExcessVerticalSpace = true;
275 table.setLayoutData(gridData);
277 gridData = new GridData();
278 gridData.horizontalAlignment = GridData.FILL;
279 label.setLayoutData(gridData);
281 // create empty table row
282 TableColumn[] columns = table.getColumns();
283 String columnNames[] = new String[columns.length];
284 for (int i = 0; i < columns.length; i++) {
285 columnNames[i] = columns[i].getText();
287 String data[] = new String[columnNames.length];
288 for (int i = 0; i < columns.length; i++) {
291 final TableRow emptyRow = new TableRow(ta.getTable(), columnNames, data);
293 filter.addSelectionListener(new SelectionListener() {
294 public void widgetDefaultSelected(SelectionEvent e) {
296 public void widgetSelected(SelectionEvent e) {
297 SortFilterPage page = new SortFilterPage("");
298 SQLRowWizard wizard = new SQLRowWizard();
299 wizard.init("Filter and Sort", page, emptyRow, ta);
300 WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard);
305 final Action defaultEncodingAction = new Action() {
307 ta.setEncoding(TableAdapter.DEFAULT);
310 defaultEncodingAction.setText(Messages.getString("tableview.defaultEncoding")); //$NON-NLS-1$
311 final Action UTF8EncodingAction = new Action() {
313 ta.setEncoding(TableAdapter.UTF_8);
316 UTF8EncodingAction.setText(Messages.getString("tableview.UTF8Encoding")); //$NON-NLS-1$
317 final Action UTF16EncodingAction = new Action() {
319 ta.setEncoding(TableAdapter.UTF_16);
322 UTF16EncodingAction.setText(Messages.getString("tableview.UTF16Encoding")); //$NON-NLS-1$
324 IMenuListener menuListener = new IMenuListener() {
325 public void menuAboutToShow(IMenuManager mgr) {
326 if (ta.getTable() != null) {
327 TableItem[] selection = table.getSelection();
328 TableColumn[] columns = table.getColumns();
329 String columnNames[] = new String[columns.length];
330 for (int i = 0; i < columns.length; i++) {
331 columnNames[i] = columns[i].getText();
333 String data[] = new String[columnNames.length];
334 if (selection != null && selection.length > 0) {
335 TableItem sel = selection[0];
336 for (int i = 0; i < columns.length; i++) {
337 data[i] = sel.getText(i);
340 for (int i = 0; i < columns.length; i++) {
344 final TableRow row = new TableRow(ta.getTable(), columnNames, data);
345 Action updateAction = new Action() {
347 UpdateRowPage page = new UpdateRowPage("");
348 SQLRowWizard wizard = new SQLRowWizard();
349 wizard.init("Update Row", page, row, ta);
350 WizardDialog dialog =
351 new WizardDialog(getSite().getShell(), wizard);
355 updateAction.setText(Messages.getString("tableview.update")); //$NON-NLS-1$
356 Action insertAction = new Action() {
358 InsertRowPage page = new InsertRowPage("");
359 SQLRowWizard wizard = new SQLRowWizard();
360 wizard.init("Insert Row", page, row, ta);
361 WizardDialog dialog =
362 new WizardDialog(getSite().getShell(), wizard);
366 insertAction.setText(Messages.getString("tableview.insert")); //$NON-NLS-1$
367 Action deleteAction = new Action() {
369 DeleteRowPage page = new DeleteRowPage("");
370 SQLRowWizard wizard = new SQLRowWizard();
371 wizard.init("Delete Row", page, row, ta);
372 WizardDialog dialog =
373 new WizardDialog(getSite().getShell(), wizard);
377 deleteAction.setText(Messages.getString("tableview.delete")); //$NON-NLS-1$
379 Action phpSelectAction = new Action() {
381 PHPSelectRowPage page = new PHPSelectRowPage("");
382 SQLRowWizard wizard = new SQLRowWizard();
383 wizard.init("PHP Select Generation", page, row, ta);
384 WizardDialog dialog =
385 new WizardDialog(getSite().getShell(), wizard);
389 phpSelectAction.setText(Messages.getString("tableview.phpselect")); //$NON-NLS-1$
391 mgr.add(phpSelectAction);
393 mgr.add(insertAction);
394 mgr.add(updateAction);
395 mgr.add(deleteAction);
399 mgr.add(defaultEncodingAction);
400 mgr.add(UTF8EncodingAction);
401 mgr.add(UTF16EncodingAction);
406 MenuManager manager = new MenuManager();
407 manager.setRemoveAllWhenShown(true);
408 Menu fTextContextMenu = manager.createContextMenu(table);
409 table.setMenu(fTextContextMenu);
410 table.setLinesVisible(true);
411 manager.addMenuListener(menuListener);
413 tabItem.setControl(main);
414 tabs.setSelection(tabs.indexOf(tabItem));
417 public void createPartControl(Composite parent) {
419 this.parent = parent;
421 clip = new Clipboard(getSite().getShell().getDisplay());
422 tabs = new TabFolder(parent, SWT.NONE);
424 public void initActions() {
425 refreshTableAction = new RefreshTableAction();
426 refreshTableAction.setText(Messages.getString("tableview.refresh"));
427 refreshTableAction.setImageDescriptor(
428 PHPEclipseSQLPlugin.getImageDescriptor("refresh.gif"));
429 refreshTableAction.init(this);
430 closeTableAction = new CloseTableAction();
431 closeTableAction.setText(Messages.getString("tableview.close"));
432 closeTableAction.setImageDescriptor(
433 PHPEclipseSQLPlugin.getImageDescriptor("close.gif"));
434 closeTableAction.init(this);
437 public void selectionChanged(IWorkbenchPart part, ISelection selection) {