Search in sources :

Example 1 with SQLHistoryElement

use of net.sourceforge.sqlexplorer.history.SQLHistoryElement in project tdq-studio-se by Talend.

the class SQLHistoryView method createPartControl.

/*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
     */
public void createPartControl(final Composite parent) {
    // MOD gdbu 2011-4-29 bug : 20960
    SQLHistory tem_history = SQLExplorerPlugin.getDefault().getSQLHistory();
    final SQLHistory history = tem_history == null ? new SQLHistory() : tem_history;
    // ~20960
    history.sort(1, SWT.DOWN);
    // $NON-NLS-1$
    PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, SQLExplorerPlugin.PLUGIN_ID + ".SQLHistoryView");
    history.addListener(this);
    Composite composite = new Composite(parent, SWT.NULL);
    GridLayout layout = new GridLayout();
    layout.numColumns = 1;
    layout.marginLeft = 0;
    layout.horizontalSpacing = 0;
    layout.verticalSpacing = 2;
    layout.marginWidth = 0;
    layout.marginHeight = 0;
    composite.setLayout(layout);
    composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    // add search box
    _searchBox = new Text(composite, SWT.BORDER);
    _searchBox.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
    // $NON-NLS-1$
    _searchBox.setText(Messages.getString("SQLHistoryView.SearchText"));
    _searchBox.selectAll();
    SQLHistorySearchListener searchListener = new SQLHistorySearchListener(history);
    _searchBox.addModifyListener(searchListener);
    _searchBox.addMouseListener(new MouseAdapter() {

        public void mouseDown(MouseEvent e) {
            Text searchbox = (Text) e.widget;
            if (searchbox.getText() != null && searchbox.getText().equals(Messages.getString("SQLHistoryView.SearchText"))) {
                searchbox.setText("");
            }
        }
    });
    _tableViewer = new TableViewer(composite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.FULL_SELECTION | SWT.MULTI | SWT.VIRTUAL);
    getSite().setSelectionProvider(_tableViewer);
    _table = _tableViewer.getTable();
    _table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    _table.setHeaderVisible(true);
    _table.setLinesVisible(true);
    _table.setItemCount(history.getEntryCount());
    _tableViewer.setLabelProvider(new SQLHistoryLabelProvider());
    _tableViewer.setContentProvider(new IStructuredContentProvider() {

        public void dispose() {
        }

        public Object[] getElements(Object inputElement) {
            return SQLExplorerPlugin.getDefault().getSQLHistory().toArray();
        }

        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        }
    });
    _tableViewer.setInput(history);
    // create listener for sorting
    Listener sortListener = new Listener() {

        public void handleEvent(Event e) {
            // determine new sort column and direction
            TableColumn sortColumn = _table.getSortColumn();
            TableColumn currentColumn = (TableColumn) e.widget;
            int dir = _table.getSortDirection();
            if (sortColumn == currentColumn) {
                dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
            } else {
                _table.setSortColumn(currentColumn);
                dir = SWT.UP;
            }
            sortColumn = _table.getSortColumn();
            TableColumn[] cols = _table.getColumns();
            for (int i = 0; i < cols.length; i++) {
                if (cols[i] == sortColumn) {
                    history.sort(i, dir);
                    break;
                }
            }
            // update data displayed in table
            _table.setSortDirection(dir);
            _tableViewer.refresh();
        }
    };
    String[] columnLabels = new String[] { Messages.getString("SQLHistoryView.Column.SQL"), Messages.getString("SQLHistoryView.Column.Time"), Messages.getString("SQLHistoryView.Column.Connection"), Messages.getString("SQLHistoryView.Column.Executions") };
    _tableViewer.setColumnProperties(columnLabels);
    // add all column headers to our table
    for (int i = 0; i < columnLabels.length; i++) {
        // add column header
        TableColumn column = new TableColumn(_table, SWT.LEFT);
        column.setText(columnLabels[i]);
        column.setMoveable(false);
        column.setResizable(true);
        column.addListener(SWT.Selection, sortListener);
    }
    _tableViewer.refresh();
    // add sizing weights to the different columns
    TableLayout tableLayout = new TableLayout();
    tableLayout.addColumnData(new ColumnWeightData(7, 150));
    tableLayout.addColumnData(new ColumnWeightData(2, 120));
    tableLayout.addColumnData(new ColumnWeightData(1, 50));
    tableLayout.addColumnData(new ColumnWeightData(1, 50));
    _table.setLayout(tableLayout);
    _table.layout();
    // redraw table if view is resized
    parent.addControlListener(new ControlAdapter() {

        public void controlResized(ControlEvent e) {
            super.controlResized(e);
            // reset weights in case of view resizing
            TableLayout tableLayout = new TableLayout();
            tableLayout.addColumnData(new ColumnWeightData(7, 150));
            tableLayout.addColumnData(new ColumnWeightData(2, 120));
            tableLayout.addColumnData(new ColumnWeightData(1, 50));
            tableLayout.addColumnData(new ColumnWeightData(1, 50));
            _table.setLayout(tableLayout);
        }
    });
    // create action bar
    final IToolBarManager toolBarMgr = getViewSite().getActionBars().getToolBarManager();
    final SQLHistoryActionGroup actionGroup = new SQLHistoryActionGroup(this, history, _tableViewer, toolBarMgr);
    _tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {

        public void selectionChanged(SelectionChangedEvent event) {
            actionGroup.refresh();
            toolBarMgr.update(true);
        }
    });
    // add context menus
    final MenuManager menuMgr = new MenuManager("#HistoryPopupMenu");
    menuMgr.setRemoveAllWhenShown(true);
    Menu historyContextMenu = menuMgr.createContextMenu(_table);
    _table.setMenu(historyContextMenu);
    menuMgr.addMenuListener(new IMenuListener() {

        public void menuAboutToShow(IMenuManager manager) {
            toolBarMgr.markDirty();
            actionGroup.fillContextMenu(manager);
        }
    });
    // also add action as default when an entry is doubleclicked.
    final OpenInEditorAction openInEditorAction = new OpenInEditorAction();
    openInEditorAction.setTableViewer(_tableViewer);
    openInEditorAction.setView(this);
    _tableViewer.addDoubleClickListener(new IDoubleClickListener() {

        public void doubleClick(DoubleClickEvent event) {
            openInEditorAction.run();
        }
    });
    // add remove action on delete key
    final RemoveFromHistoryAction removeFromHistoryAction = new RemoveFromHistoryAction();
    removeFromHistoryAction.setTableViewer(_tableViewer);
    _table.addKeyListener(new KeyAdapter() {

        public void keyReleased(KeyEvent e) {
            // delete entry
            if (e.keyCode == SWT.DEL) {
                removeFromHistoryAction.run();
            }
        }
    });
    // Set multi-line tooltip
    final Display display = parent.getDisplay();
    _tipShell = new Shell(parent.getShell(), SWT.ON_TOP);
    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 2;
    gridLayout.marginWidth = 2;
    gridLayout.marginHeight = 2;
    _tipShell.setLayout(gridLayout);
    _tipShell.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
    _tipLabelText = new Label(_tipShell, SWT.WRAP | SWT.LEFT);
    _tipLabelText.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
    _tipLabelText.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
    GridData gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
    _tipLabelText.setLayoutData(gridData);
    _table.addMouseListener(new MouseAdapter() {

        public void mouseDown(MouseEvent e) {
            if (_tipShell.isVisible()) {
                _tipShell.setVisible(false);
                _tipWidget = null;
            }
        }
    });
    _table.addMouseTrackListener(new MouseTrackAdapter() {

        public void mouseExit(MouseEvent e) {
            if (_tipShell.isVisible())
                _tipShell.setVisible(false);
            _tipWidget = null;
        }

        /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.MouseTrackListener#mouseHover(org.eclipse.swt.events.MouseEvent)
             */
        public void mouseHover(MouseEvent event) {
            Point pt = new Point(event.x, event.y);
            Widget widget = event.widget;
            TableItem tableItem = null;
            if (widget instanceof Table) {
                Table table = (Table) widget;
                widget = table.getItem(pt);
            }
            if (widget instanceof TableItem) {
                tableItem = (TableItem) widget;
            }
            if (widget == null) {
                _tipShell.setVisible(false);
                _tipWidget = null;
                return;
            }
            if (widget == _tipWidget)
                return;
            _tipWidget = widget;
            _tipPosition = _table.toDisplay(pt);
            SQLHistoryElement sqlString = (SQLHistoryElement) tableItem.getData();
            String text = TextUtil.getWrappedText(sqlString.getRawSQLString());
            if (text == null || text.equals("")) {
                _tipWidget = null;
                return;
            }
            // Set off the table tooltip as we provide our own
            _table.setToolTipText("");
            _tipLabelText.setText(text);
            _tipShell.pack();
            setHoverLocation(_tipShell, _tipPosition, _tipLabelText.getBounds().height);
            _tipShell.setVisible(true);
        }
    });
    _tableViewer.setSelection(null);
    composite.layout();
    parent.layout();
}
Also used : ColumnWeightData(org.eclipse.jface.viewers.ColumnWeightData) IDoubleClickListener(org.eclipse.jface.viewers.IDoubleClickListener) IMenuListener(org.eclipse.jface.action.IMenuListener) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) SQLHistoryChangedListener(net.sourceforge.sqlexplorer.history.SQLHistoryChangedListener) Listener(org.eclipse.swt.widgets.Listener) SQLHistorySearchListener(net.sourceforge.sqlexplorer.history.SQLHistorySearchListener) ControlAdapter(org.eclipse.swt.events.ControlAdapter) KeyAdapter(org.eclipse.swt.events.KeyAdapter) TableItem(org.eclipse.swt.widgets.TableItem) Label(org.eclipse.swt.widgets.Label) MouseTrackAdapter(org.eclipse.swt.events.MouseTrackAdapter) Widget(org.eclipse.swt.widgets.Widget) SQLHistoryLabelProvider(net.sourceforge.sqlexplorer.history.SQLHistoryLabelProvider) RemoveFromHistoryAction(net.sourceforge.sqlexplorer.history.actions.RemoveFromHistoryAction) TableViewer(org.eclipse.jface.viewers.TableViewer) Viewer(org.eclipse.jface.viewers.Viewer) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) SQLHistory(net.sourceforge.sqlexplorer.history.SQLHistory) KeyEvent(org.eclipse.swt.events.KeyEvent) GridLayout(org.eclipse.swt.layout.GridLayout) Shell(org.eclipse.swt.widgets.Shell) SQLHistoryElement(net.sourceforge.sqlexplorer.history.SQLHistoryElement) IDoubleClickListener(org.eclipse.jface.viewers.IDoubleClickListener) OpenInEditorAction(net.sourceforge.sqlexplorer.history.actions.OpenInEditorAction) Menu(org.eclipse.swt.widgets.Menu) TableLayout(org.eclipse.jface.viewers.TableLayout) MouseEvent(org.eclipse.swt.events.MouseEvent) Table(org.eclipse.swt.widgets.Table) Composite(org.eclipse.swt.widgets.Composite) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) MouseAdapter(org.eclipse.swt.events.MouseAdapter) Text(org.eclipse.swt.widgets.Text) DoubleClickEvent(org.eclipse.jface.viewers.DoubleClickEvent) Point(org.eclipse.swt.graphics.Point) SQLHistorySearchListener(net.sourceforge.sqlexplorer.history.SQLHistorySearchListener) TableColumn(org.eclipse.swt.widgets.TableColumn) Point(org.eclipse.swt.graphics.Point) IMenuListener(org.eclipse.jface.action.IMenuListener) IToolBarManager(org.eclipse.jface.action.IToolBarManager) GridData(org.eclipse.swt.layout.GridData) IStructuredContentProvider(org.eclipse.jface.viewers.IStructuredContentProvider) MenuManager(org.eclipse.jface.action.MenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) KeyEvent(org.eclipse.swt.events.KeyEvent) MouseEvent(org.eclipse.swt.events.MouseEvent) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) Event(org.eclipse.swt.widgets.Event) ControlEvent(org.eclipse.swt.events.ControlEvent) DoubleClickEvent(org.eclipse.jface.viewers.DoubleClickEvent) IMenuManager(org.eclipse.jface.action.IMenuManager) ControlEvent(org.eclipse.swt.events.ControlEvent) SQLHistoryActionGroup(net.sourceforge.sqlexplorer.history.actions.SQLHistoryActionGroup) TableViewer(org.eclipse.jface.viewers.TableViewer) Display(org.eclipse.swt.widgets.Display)

Example 2 with SQLHistoryElement

use of net.sourceforge.sqlexplorer.history.SQLHistoryElement in project tdq-studio-se by Talend.

the class AppendToEditorAction method run.

public void run() {
    try {
        TableItem[] ti = _table.getSelection();
        if (ti == null || ti.length == 0) {
            return;
        }
        String queryDelimiter = SQLExplorerPlugin.getDefault().getPluginPreferences().getString(IConstants.SQL_QRY_DELIMITER);
        StringBuffer copiedText = new StringBuffer();
        for (int i = 0; i < ti.length; i++) {
            SQLHistoryElement el = (SQLHistoryElement) ti[i].getData();
            copiedText.append(el.getRawSQLString());
            copiedText.append(queryDelimiter);
            if (ti.length > 1) {
                copiedText.append("\n");
            }
        }
        IWorkbenchPage page = SQLExplorerPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
        if (page == null) {
            return;
        }
        SQLEditor editorPart = (SQLEditor) page.getActiveEditor();
        editorPart.setText(editorPart.getSQLToBeExecuted() + "\n" + copiedText.toString());
    } catch (Throwable e) {
        SQLExplorerPlugin.error("Error creating sql editor", e);
    }
}
Also used : SQLEditor(net.sourceforge.sqlexplorer.plugin.editors.SQLEditor) SQLHistoryElement(net.sourceforge.sqlexplorer.history.SQLHistoryElement) TableItem(org.eclipse.swt.widgets.TableItem) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage)

Example 3 with SQLHistoryElement

use of net.sourceforge.sqlexplorer.history.SQLHistoryElement in project tdq-studio-se by Talend.

the class RemoveFromHistoryAction method run.

public void run() {
    try {
        TableItem[] selections = _table.getSelection();
        if (selections != null && selections.length != 0) {
            for (int i = 0; i < selections.length; i++) {
                SQLHistoryElement el = (SQLHistoryElement) selections[i].getData();
                if (el != null) {
                    _history.remove(el);
                }
            }
        }
        _table.deselectAll();
        setEnabled(false);
    } catch (Throwable e) {
        SQLExplorerPlugin.error("Error removing item from clipboard", e);
    }
}
Also used : SQLHistoryElement(net.sourceforge.sqlexplorer.history.SQLHistoryElement) TableItem(org.eclipse.swt.widgets.TableItem)

Example 4 with SQLHistoryElement

use of net.sourceforge.sqlexplorer.history.SQLHistoryElement in project tdq-studio-se by Talend.

the class OpenInEditorAction method run.

public void run() {
    try {
        TableItem[] ti = _table.getSelection();
        if (ti == null || ti.length == 0) {
            return;
        }
        String queryDelimiter = SQLExplorerPlugin.getDefault().getPluginPreferences().getString(IConstants.SQL_QRY_DELIMITER);
        StringBuffer copiedText = new StringBuffer();
        for (int i = 0; i < ti.length; i++) {
            SQLHistoryElement el = (SQLHistoryElement) ti[i].getData();
            copiedText.append(el.getRawSQLString());
            if (ti.length > 0) {
                copiedText.append(queryDelimiter);
                copiedText.append("\n");
            }
        }
        SQLHistoryElement sqlHistoryElement = (SQLHistoryElement) ti[0].getData();
        User user = sqlHistoryElement.getUser();
        Alias alias;
        if (user != null)
            alias = user.getAlias();
        else {
            alias = sqlHistoryElement.getAlias();
            if (alias != null)
                user = alias.getDefaultUser();
            if (user == null) {
                ConnectionsView view = SQLExplorerPlugin.getDefault().getConnectionsView();
                if (view != null)
                    user = view.getDefaultUser();
            }
        }
        if (user != null && !user.hasAuthenticated()) {
            boolean okToOpen = MessageDialog.openConfirm(_table.getShell(), Messages.getString("SQLHistoryView.OpenInEditor.Confirm.Title"), Messages.getString("SQLHistoryView.OpenInEditor.Confirm.Message.Prefix") + " " + user.getDescription() + Messages.getString("SQLHistoryView.OpenInEditor.Confirm.Message.Postfix"));
            if (okToOpen) {
                OpenPasswordConnectDialogAction openDlgAction = new OpenPasswordConnectDialogAction(alias, user, false);
                openDlgAction.run();
            }
        }
        SQLEditorInput input = new SQLEditorInput("SQL Editor (" + SQLExplorerPlugin.getDefault().getEditorSerialNo() + ").sql");
        input.setUser(user);
        IWorkbenchPage page = SQLExplorerPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
        if (page == null)
            return;
        SQLEditor editorPart = (SQLEditor) page.openEditor(input, SQLEditor.class.getName());
        editorPart.setText(copiedText.toString());
    } catch (Throwable e) {
        SQLExplorerPlugin.error("Error creating sql editor", e);
    }
}
Also used : SQLEditor(net.sourceforge.sqlexplorer.plugin.editors.SQLEditor) User(net.sourceforge.sqlexplorer.dbproduct.User) ConnectionsView(net.sourceforge.sqlexplorer.connections.ConnectionsView) TableItem(org.eclipse.swt.widgets.TableItem) OpenPasswordConnectDialogAction(net.sourceforge.sqlexplorer.plugin.actions.OpenPasswordConnectDialogAction) SQLEditorInput(net.sourceforge.sqlexplorer.plugin.editors.SQLEditorInput) SQLHistoryElement(net.sourceforge.sqlexplorer.history.SQLHistoryElement) Alias(net.sourceforge.sqlexplorer.dbproduct.Alias) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage)

Example 5 with SQLHistoryElement

use of net.sourceforge.sqlexplorer.history.SQLHistoryElement in project tdq-studio-se by Talend.

the class CopyStatementAction method run.

public void run() {
    try {
        TableItem[] ti = _table.getSelection();
        if (ti == null || ti.length == 0) {
            return;
        }
        String queryDelimiter = SQLExplorerPlugin.getDefault().getPluginPreferences().getString(IConstants.SQL_QRY_DELIMITER);
        StringBuffer copiedText = new StringBuffer();
        for (int i = 0; i < ti.length; i++) {
            SQLHistoryElement el = (SQLHistoryElement) ti[i].getData();
            copiedText.append(el.getRawSQLString());
            if (ti.length > 0) {
                copiedText.append(queryDelimiter);
                copiedText.append("\n");
            }
        }
        Clipboard cb = new Clipboard(Display.getCurrent());
        TextTransfer textTransfer = TextTransfer.getInstance();
        cb.setContents(new Object[] { copiedText.toString() }, new Transfer[] { textTransfer });
    } catch (Throwable e) {
        SQLExplorerPlugin.error("Error copying to clipboard", e);
    }
}
Also used : SQLHistoryElement(net.sourceforge.sqlexplorer.history.SQLHistoryElement) TableItem(org.eclipse.swt.widgets.TableItem) Clipboard(org.eclipse.swt.dnd.Clipboard) TextTransfer(org.eclipse.swt.dnd.TextTransfer)

Aggregations

SQLHistoryElement (net.sourceforge.sqlexplorer.history.SQLHistoryElement)5 TableItem (org.eclipse.swt.widgets.TableItem)5 SQLEditor (net.sourceforge.sqlexplorer.plugin.editors.SQLEditor)2 ConnectionsView (net.sourceforge.sqlexplorer.connections.ConnectionsView)1 Alias (net.sourceforge.sqlexplorer.dbproduct.Alias)1 User (net.sourceforge.sqlexplorer.dbproduct.User)1 SQLHistory (net.sourceforge.sqlexplorer.history.SQLHistory)1 SQLHistoryChangedListener (net.sourceforge.sqlexplorer.history.SQLHistoryChangedListener)1 SQLHistoryLabelProvider (net.sourceforge.sqlexplorer.history.SQLHistoryLabelProvider)1 SQLHistorySearchListener (net.sourceforge.sqlexplorer.history.SQLHistorySearchListener)1 OpenInEditorAction (net.sourceforge.sqlexplorer.history.actions.OpenInEditorAction)1 RemoveFromHistoryAction (net.sourceforge.sqlexplorer.history.actions.RemoveFromHistoryAction)1 SQLHistoryActionGroup (net.sourceforge.sqlexplorer.history.actions.SQLHistoryActionGroup)1 OpenPasswordConnectDialogAction (net.sourceforge.sqlexplorer.plugin.actions.OpenPasswordConnectDialogAction)1 SQLEditorInput (net.sourceforge.sqlexplorer.plugin.editors.SQLEditorInput)1 IMenuListener (org.eclipse.jface.action.IMenuListener)1 IMenuManager (org.eclipse.jface.action.IMenuManager)1 IToolBarManager (org.eclipse.jface.action.IToolBarManager)1 MenuManager (org.eclipse.jface.action.MenuManager)1 ColumnWeightData (org.eclipse.jface.viewers.ColumnWeightData)1