Search in sources :

Example 1 with CloseSQLResultTab

use of net.sourceforge.sqlexplorer.sqlpanel.actions.CloseSQLResultTab in project tdq-studio-se by Talend.

the class SqlResultsView method addSQLExecution.

/**
 * Add a new (SQL) Execution Tab
 *
 * @param AbstractSQLExecution
 */
public void addSQLExecution(AbstractSQLExecution sqlExecution) {
    if (_tabFolder == null || _tabFolder.isDisposed()) {
        clearParent();
        // create tab folder for different sessions
        _tabFolder = new TabFolder(_parent, SWT.NULL);
        _parent.layout();
        _parent.redraw();
    }
    // create tab
    _lastTabNumber = _lastTabNumber + 1;
    final TabItem tabItem = new TabItem(_tabFolder, SWT.NULL);
    // set tab text & tooltip
    String labelText = "" + _lastTabNumber;
    tabItem.setText(labelText);
    tabItem.setData("tabLabel", labelText);
    tabItem.setToolTipText(TextUtil.getWrappedText(sqlExecution.getSqlStatement()));
    // create composite for our result
    Composite composite = new Composite(_tabFolder, 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.TOP, true, false));
    tabItem.setControl(composite);
    tabItem.setData(sqlExecution);
    tabItem.addDisposeListener(new DisposeListener() {

        public void widgetDisposed(final DisposeEvent e) {
            BusyIndicator.showWhile(Display.getCurrent(), new Runnable() {

                public void run() {
                    // stop all sql execution if still running
                    TabItem tabItem = (TabItem) e.getSource();
                    AbstractSQLExecution sqlExecution = (AbstractSQLExecution) tabItem.getData();
                    sqlExecution.stop();
                    tabItem.setData(null);
                    if (_tabFolder != null && !_tabFolder.isDisposed()) {
                        if (_tabFolder.getItemCount() == 0) {
                            // this is last tab..
                            clearParent();
                            setDefaultMessage();
                        }
                    } else if (_tabFolder.isDisposed()) {
                        clearParent();
                        setDefaultMessage();
                    }
                }
            });
        }
    });
    // add sql statement, first create temp label to calculate correct size
    String sqlStatement = sqlExecution.getSqlStatement();
    int labelHeight = 60;
    int labelStyle = SWT.WRAP | SWT.MULTI;
    Text tmpLabel = new Text(composite, labelStyle);
    tmpLabel.setText(TextUtil.removeLineBreaks(sqlExecution.getSqlStatement()));
    tmpLabel.setLayoutData(new FillLayout());
    int parentWidth = _parent.getClientArea().width;
    Point idealSize = tmpLabel.computeSize(parentWidth - 30, SWT.DEFAULT);
    if (idealSize.y <= 60) {
        // we don't need a scroll bar. minimize
        labelHeight = idealSize.y;
    } else {
        // we need a scroll bar
        labelStyle = SWT.WRAP | SWT.MULTI | SWT.V_SCROLL;
    }
    tmpLabel.dispose();
    // now create real label
    // create spanned cell for table data
    Composite headerComposite = new Composite(composite, SWT.FILL);
    headerComposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
    GridLayout hLayout = new GridLayout();
    hLayout.numColumns = 2;
    hLayout.marginLeft = 0;
    hLayout.horizontalSpacing = 0;
    hLayout.verticalSpacing = 0;
    hLayout.marginWidth = 0;
    hLayout.marginHeight = 0;
    headerComposite.setLayout(hLayout);
    Text label = new Text(headerComposite, labelStyle);
    label.setEditable(false);
    label.setBackground(_parent.getBackground());
    label.setText(TextUtil.removeLineBreaks(sqlStatement));
    label.setToolTipText(TextUtil.getWrappedText(sqlStatement));
    GridData labelGridData = new GridData(SWT.FILL, SWT.TOP, true, false);
    labelGridData.heightHint = labelHeight;
    label.setLayoutData(labelGridData);
    // add action bar
    ToolBarManager toolBarMgr = new ToolBarManager(SWT.FLAT);
    toolBarMgr.createControl(headerComposite);
    toolBarMgr.add(new CloseSQLResultTab(tabItem));
    toolBarMgr.update(true);
    GridData gid = new GridData();
    gid.horizontalAlignment = SWT.RIGHT;
    gid.verticalAlignment = SWT.TOP;
    toolBarMgr.getControl().setLayoutData(gid);
    // add detail composite to show progress bar and results
    Composite detailComposite = new Composite(composite, SWT.FILL);
    detailComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    sqlExecution.setComposite(detailComposite);
    sqlExecution.setParentTab(tabItem);
    sqlExecution.startExecution();
    // set new tab as the active one
    _tabFolder.setSelection(_tabFolder.getItemCount() - 1);
    // refresh view
    composite.layout();
    _tabFolder.layout();
    _tabFolder.redraw();
    // bring this view to top of the view stack
    getSite().getPage().bringToTop(this);
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) Composite(org.eclipse.swt.widgets.Composite) TabFolder(org.eclipse.swt.widgets.TabFolder) Text(org.eclipse.swt.widgets.Text) FillLayout(org.eclipse.swt.layout.FillLayout) Point(org.eclipse.swt.graphics.Point) DisposeEvent(org.eclipse.swt.events.DisposeEvent) Point(org.eclipse.swt.graphics.Point) ToolBarManager(org.eclipse.jface.action.ToolBarManager) TabItem(org.eclipse.swt.widgets.TabItem) GridLayout(org.eclipse.swt.layout.GridLayout) AbstractSQLExecution(net.sourceforge.sqlexplorer.sqlpanel.AbstractSQLExecution) CloseSQLResultTab(net.sourceforge.sqlexplorer.sqlpanel.actions.CloseSQLResultTab) GridData(org.eclipse.swt.layout.GridData)

Aggregations

AbstractSQLExecution (net.sourceforge.sqlexplorer.sqlpanel.AbstractSQLExecution)1 CloseSQLResultTab (net.sourceforge.sqlexplorer.sqlpanel.actions.CloseSQLResultTab)1 ToolBarManager (org.eclipse.jface.action.ToolBarManager)1 DisposeEvent (org.eclipse.swt.events.DisposeEvent)1 DisposeListener (org.eclipse.swt.events.DisposeListener)1 Point (org.eclipse.swt.graphics.Point)1 FillLayout (org.eclipse.swt.layout.FillLayout)1 GridData (org.eclipse.swt.layout.GridData)1 GridLayout (org.eclipse.swt.layout.GridLayout)1 Composite (org.eclipse.swt.widgets.Composite)1 TabFolder (org.eclipse.swt.widgets.TabFolder)1 TabItem (org.eclipse.swt.widgets.TabItem)1 Text (org.eclipse.swt.widgets.Text)1