Search in sources :

Example 71 with DBSObject

use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by serge-rider.

the class SQLToolTaskWizardPageSettings method createControl.

@Override
public void createControl(Composite parent) {
    initializeDialogUnits(parent);
    Composite composite = UIUtils.createComposite(parent, 1);
    composite.setLayoutData(new GridData(GridData.FILL_BOTH));
    SashForm previewSplitter = new SashForm(composite, SWT.VERTICAL);
    previewSplitter.setLayoutData(new GridData(GridData.FILL_BOTH));
    SashForm settingsPanel = new SashForm(previewSplitter, SWT.HORIZONTAL);
    Group objectsPanel;
    {
        objectsPanel = UIUtils.createControlGroup(settingsPanel, TasksSQLUIMessages.sql_tool_task_wizard_page_settings_group_label_objects, 2, GridData.FILL_BOTH, 0);
        objectsViewer = new TableViewer(objectsPanel, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
        objectsViewer.setContentProvider(new ListContentProvider());
        objectsViewer.setLabelProvider(new ColumnLabelProvider() {

            @Override
            public String getText(Object element) {
                return DBUtils.getObjectFullName((DBPNamedObject) element, DBPEvaluationContext.UI);
            }

            @Override
            public Image getImage(Object element) {
                return DBeaverIcons.getImage(DBValueFormatting.getObjectImage((DBPObject) element));
            }
        });
        GridData gd = new GridData(GridData.FILL_BOTH);
        gd.heightHint = 150;
        gd.widthHint = 200;
        final Table objectTable = objectsViewer.getTable();
        objectTable.setLayoutData(gd);
        ToolBar buttonsToolbar = new ToolBar(objectsPanel, SWT.VERTICAL);
        buttonsToolbar.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
        UIUtils.createToolItem(buttonsToolbar, TasksSQLUIMessages.sql_tool_task_wizard_page_settings_tool_item_text_add_string, UIIcon.ROW_ADD, new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                DBNProject projectNode = DBWorkbench.getPlatform().getNavigatorModel().getRoot().getProjectNode(sqlWizard.getProject());
                SQLToolTaskObjectSelectorDialog objectListDialog = new SQLToolTaskObjectSelectorDialog(getShell(), projectNode, (TaskTypeDescriptor) sqlWizard.getTaskType());
                if (objectListDialog.open() == IDialogConstants.OK_ID) {
                    for (DBSObject object : objectListDialog.getSelectedObjects()) {
                        if (!selectedObjects.contains(object)) {
                            selectedObjects.add(object);
                        }
                    }
                    refreshObjects();
                    updatePageCompletion();
                }
            }
        });
        ToolItem deleteItem = UIUtils.createToolItem(buttonsToolbar, TasksSQLUIMessages.sql_tool_task_wizard_page_settings_tool_item_text_remove_string, UIIcon.ROW_DELETE, new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                ISelection selection = objectsViewer.getSelection();
                if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
                    for (Object element : ((IStructuredSelection) selection).toArray()) {
                        if (element instanceof DBSObject) {
                            selectedObjects.remove(element);
                        }
                    }
                    refreshObjects();
                    updatePageCompletion();
                }
            }
        });
        UIUtils.createToolBarSeparator(buttonsToolbar, SWT.HORIZONTAL);
        ToolItem[] moveButtons = new ToolItem[2];
        moveButtons[0] = UIUtils.createToolItem(buttonsToolbar, TasksSQLUIMessages.sql_tool_task_wizard_page_settings_tool_item_text_move_script_up, UIIcon.ARROW_UP, new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                int selectionIndex = objectTable.getSelectionIndex();
                if (selectionIndex > 0) {
                    DBSObject prevScript = selectedObjects.get(selectionIndex - 1);
                    selectedObjects.set(selectionIndex - 1, selectedObjects.get(selectionIndex));
                    selectedObjects.set(selectionIndex, prevScript);
                    refreshObjects();
                }
                moveButtons[0].setEnabled(selectionIndex > 1);
                moveButtons[1].setEnabled(selectionIndex < objectTable.getItemCount() - 1);
            }
        });
        moveButtons[1] = UIUtils.createToolItem(buttonsToolbar, TasksSQLUIMessages.sql_tool_task_wizard_page_settings_tool_item_text_move_script_down, UIIcon.ARROW_DOWN, new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                int selectionIndex = objectTable.getSelectionIndex();
                if (selectionIndex < objectTable.getItemCount() - 1) {
                    DBSObject nextScript = selectedObjects.get(selectionIndex + 1);
                    selectedObjects.set(selectionIndex + 1, selectedObjects.get(selectionIndex));
                    selectedObjects.set(selectionIndex, nextScript);
                    refreshObjects();
                }
                moveButtons[0].setEnabled(selectionIndex > 0);
                moveButtons[1].setEnabled(selectionIndex < objectTable.getItemCount() - 2);
            }
        });
        objectsViewer.addSelectionChangedListener(event -> {
            int selectionIndex = objectTable.getSelectionIndex();
            deleteItem.setEnabled(selectionIndex >= 0);
            moveButtons[0].setEnabled(selectionIndex > 0);
            moveButtons[1].setEnabled(selectionIndex < objectTable.getItemCount() - 1);
        });
        deleteItem.setEnabled(false);
        moveButtons[0].setEnabled(false);
        moveButtons[1].setEnabled(false);
    }
    {
        Group optionsPanel = UIUtils.createControlGroup(settingsPanel, TasksSQLUIMessages.sql_tool_task_wizard_page_settings_group_label_settings, 1, GridData.FILL_BOTH, 0);
        taskOptionsViewer = new PropertyTreeViewer(optionsPanel, SWT.BORDER);
        taskOptionsViewer.addPropertyChangeListener(event -> updateScriptPreview());
    }
    Composite previewPanel = UIUtils.createComposite(previewSplitter, 1);
    previewPanel.setLayout(new FillLayout());
    serviceSQL = DBWorkbench.getService(UIServiceSQL.class);
    if (serviceSQL != null) {
        try {
            sqlPreviewPanel = serviceSQL.createSQLPanel(UIUtils.getActiveWorkbenchWindow().getActivePage().getActivePart().getSite(), previewPanel, this, TasksSQLUIMessages.sql_tool_task_wizard_page_settings_sql_panel_name, true, "");
        } catch (DBException e) {
            DBWorkbench.getPlatformUI().showError(TasksSQLUIMessages.sql_tool_task_wizard_page_settings_title_sql_preview_error, TasksSQLUIMessages.sql_tool_task_wizard_page_settings_message_sql_preview_panel, e);
        }
    }
    Composite controlsPanel = UIUtils.createComposite(composite, 2);
    UIUtils.createDialogButton(controlsPanel, TasksSQLUIMessages.sql_tool_task_wizard_page_settings_dialog_button_label_copy, new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            String text = serviceSQL.getSQLPanelText(sqlPreviewPanel);
            if (!CommonUtils.isEmpty(text)) {
                UIUtils.setClipboardContents(getShell().getDisplay(), TextTransfer.getInstance(), text);
            }
        }
    });
    getWizard().createTaskSaveButtons(controlsPanel, true, 1);
    loadSettings();
    if (taskOptionsViewer.getTree().getItemCount() == 0) {
        settingsPanel.setMaximizedControl(objectsPanel);
    }
    setControl(composite);
}
Also used : DBWorkbench(org.jkiss.dbeaver.runtime.DBWorkbench) ActiveWizardPage(org.jkiss.dbeaver.ui.dialogs.ActiveWizardPage) ListContentProvider(org.jkiss.dbeaver.ui.controls.ListContentProvider) TableViewer(org.eclipse.jface.viewers.TableViewer) SashForm(org.eclipse.swt.custom.SashForm) Image(org.eclipse.swt.graphics.Image) IDialogConstants(org.eclipse.jface.dialogs.IDialogConstants) DBCException(org.jkiss.dbeaver.model.exec.DBCException) Nullable(org.jkiss.code.Nullable) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) TaskTypeDescriptor(org.jkiss.dbeaver.registry.task.TaskTypeDescriptor) UIServiceSQL(org.jkiss.dbeaver.runtime.ui.UIServiceSQL) ArrayList(java.util.ArrayList) DBeaverIcons(org.jkiss.dbeaver.ui.DBeaverIcons) UIUtils(org.jkiss.dbeaver.ui.UIUtils) TextTransfer(org.eclipse.swt.dnd.TextTransfer) Log(org.jkiss.dbeaver.Log) DBNProject(org.jkiss.dbeaver.model.navigator.DBNProject) UIIcon(org.jkiss.dbeaver.ui.UIIcon) GridData(org.eclipse.swt.layout.GridData) FillLayout(org.eclipse.swt.layout.FillLayout) org.jkiss.dbeaver.model(org.jkiss.dbeaver.model) SQLToolExecuteSettings(org.jkiss.dbeaver.model.sql.task.SQLToolExecuteSettings) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) CommonUtils(org.jkiss.utils.CommonUtils) NLS(org.eclipse.osgi.util.NLS) PropertyTreeViewer(org.jkiss.dbeaver.ui.properties.PropertyTreeViewer) org.eclipse.swt.widgets(org.eclipse.swt.widgets) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) ColumnLabelProvider(org.eclipse.jface.viewers.ColumnLabelProvider) PropertySourceEditable(org.jkiss.dbeaver.runtime.properties.PropertySourceEditable) List(java.util.List) SQLToolExecuteHandler(org.jkiss.dbeaver.model.sql.task.SQLToolExecuteHandler) DBException(org.jkiss.dbeaver.DBException) TasksSQLUIMessages(org.jkiss.dbeaver.tasks.ui.sql.internal.TasksSQLUIMessages) SWT(org.eclipse.swt.SWT) ISelection(org.eclipse.jface.viewers.ISelection) SelectionEvent(org.eclipse.swt.events.SelectionEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) DBException(org.jkiss.dbeaver.DBException) PropertyTreeViewer(org.jkiss.dbeaver.ui.properties.PropertyTreeViewer) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ColumnLabelProvider(org.eclipse.jface.viewers.ColumnLabelProvider) ListContentProvider(org.jkiss.dbeaver.ui.controls.ListContentProvider) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ISelection(org.eclipse.jface.viewers.ISelection) DBNProject(org.jkiss.dbeaver.model.navigator.DBNProject) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) FillLayout(org.eclipse.swt.layout.FillLayout) SashForm(org.eclipse.swt.custom.SashForm) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) GridData(org.eclipse.swt.layout.GridData) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) UIServiceSQL(org.jkiss.dbeaver.runtime.ui.UIServiceSQL) TableViewer(org.eclipse.jface.viewers.TableViewer)

Example 72 with DBSObject

use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by serge-rider.

the class DataSourceToolsContributor method fillContributionItems.

@Override
protected void fillContributionItems(List<IContributionItem> menuItems) {
    IWorkbenchPage activePage = UIUtils.getActiveWorkbenchWindow().getActivePage();
    IWorkbenchPart activePart = activePage.getActivePart();
    if (activePart == null) {
        return;
    }
    DBSObject selectedObject = null;
    INavigatorModelView navigatorModelView = GeneralUtils.adapt(activePart, INavigatorModelView.class);
    if (navigatorModelView != null) {
        final ISelectionProvider selectionProvider = activePart.getSite().getSelectionProvider();
        if (selectionProvider != null) {
            ISelection selection = selectionProvider.getSelection();
            if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
                selectedObject = RuntimeUtils.getObjectAdapter(((IStructuredSelection) selection).getFirstElement(), DBSObject.class);
                List<ToolDescriptor> tools = getAvailableTools((IStructuredSelection) selection);
                fillToolsMenu(menuItems, tools, selection);
            }
        }
    } else if (activePart instanceof IEditorPart) {
        IEditorInput editorInput = ((IEditorPart) activePart).getEditorInput();
        if (editorInput instanceof IDatabaseEditorInput) {
            selectedObject = ((IDatabaseEditorInput) editorInput).getDatabaseObject();
        } else if (activePart instanceof IDataSourceContainerProvider) {
            selectedObject = ((IDataSourceContainerProvider) activePart).getDataSourceContainer();
        }
    }
    if (selectedObject != null) {
        // Contribute standard tools like session manager
        DBPDataSource dataSource = selectedObject.getDataSource();
        if (dataSource != null) {
            DBPDataSourceContainer dataSourceContainer = dataSource.getContainer();
            DBPEditorContribution[] contributedEditors = DBWorkbench.getPlatform().getDataSourceProviderRegistry().getContributedEditors(DBPEditorContribution.MB_CONNECTION_EDITOR, dataSourceContainer);
            if (contributedEditors.length > 0) {
                menuItems.add(new Separator());
                for (DBPEditorContribution ec : contributedEditors) {
                    menuItems.add(new ActionContributionItem(new OpenToolsEditorAction(activePage, dataSource, ec)));
                }
            }
        }
    }
    // Tasks management
    {
        menuItems.add(new Separator());
        menuItems.add(ActionUtils.makeCommandContribution(activePart.getSite(), DatabaseTasksView.CREATE_TASK_CMD_ID));
    }
}
Also used : ToolDescriptor(org.jkiss.dbeaver.tools.registry.ToolDescriptor) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IDatabaseEditorInput(org.jkiss.dbeaver.ui.editors.IDatabaseEditorInput) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) DBPEditorContribution(org.jkiss.dbeaver.model.connection.DBPEditorContribution) ISelection(org.eclipse.jface.viewers.ISelection) INavigatorModelView(org.jkiss.dbeaver.ui.navigator.INavigatorModelView)

Example 73 with DBSObject

use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by serge-rider.

the class DataSourceToolsContributor method getAvailableTools.

private List<ToolDescriptor> getAvailableTools(IStructuredSelection selection) {
    List<DBSObject> objects = NavigatorUtils.getSelectedObjects(selection);
    List<ToolDescriptor> result = new ArrayList<>();
    if (!objects.isEmpty()) {
        for (ToolDescriptor descriptor : ToolsRegistry.getInstance().getTools()) {
            if (descriptor.isSingleton() && objects.size() > 1) {
                continue;
            }
            boolean applies = true;
            for (DBSObject object : objects) {
                if (!descriptor.appliesTo(object)) {
                    applies = false;
                    break;
                }
            }
            if (applies) {
                result.add(descriptor);
            }
        }
    }
    return result;
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) ToolDescriptor(org.jkiss.dbeaver.tools.registry.ToolDescriptor)

Example 74 with DBSObject

use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by serge-rider.

the class ContentPanelEditor method makeValueId.

private String makeValueId() {
    String valueId;
    DBSTypedObject valueType = valueController.getValueType();
    if (valueType instanceof DBDAttributeBinding) {
        valueType = ((DBDAttributeBinding) valueType).getAttribute();
    }
    if (valueType instanceof DBSObject) {
        DBSObject object = (DBSObject) valueType;
        valueId = DBUtils.getObjectFullName(object, DBPEvaluationContext.DDL);
        if (object.getParentObject() != null) {
            valueId = DBUtils.getObjectFullName(object.getParentObject(), DBPEvaluationContext.DDL) + ":" + valueId;
        }
    } else {
        valueId = valueController.getValueName();
    }
    String dsId = "unknown";
    if (valueController.getExecutionContext() != null) {
        dsId = valueController.getExecutionContext().getDataSource().getContainer().getId();
    }
    return dsId + ":" + valueId;
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding)

Example 75 with DBSObject

use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by serge-rider.

the class SQLEditorHandlerRunProcedureConsole method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchWindow workbenchWindow = HandlerUtil.getActiveWorkbenchWindow(event);
    SQLNavigatorContext navContext = null;
    String procName = null;
    ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
    List<DBSObject> selectedObjects = NavigatorUtils.getSelectedObjects(currentSelection);
    List<DBSProcedure> entities = new ArrayList<>();
    for (DBSObject object : selectedObjects) {
        if (object instanceof DBSProcedure) {
            DBSProcedure proc = (DBSProcedure) object;
            procName = proc.getName();
            entities.add(proc);
            if (navContext == null) {
                navContext = new SQLNavigatorContext(object);
            }
        }
    }
    if (navContext == null || navContext.getDataSourceContainer() == null) {
        log.debug("No active datasource");
        return null;
    }
    DBRRunnableWithResult<String> generator = SQLGeneratorContributor.CALL_GENERATOR(entities);
    String title = "Stored procedures call";
    if (entities.size() == 1 && !CommonUtils.isEmpty(procName)) {
        title = procName + " call";
    }
    try {
        openConsole(workbenchWindow, generator, navContext, title, false, currentSelection);
    } catch (Exception e) {
        DBWorkbench.getPlatformUI().showError("Open console", "Can open SQL editor", e);
    }
    return null;
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) ISelection(org.eclipse.jface.viewers.ISelection) ArrayList(java.util.ArrayList) DBSProcedure(org.jkiss.dbeaver.model.struct.rdb.DBSProcedure) ExecutionException(org.eclipse.core.commands.ExecutionException)

Aggregations

DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)374 DBException (org.jkiss.dbeaver.DBException)129 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)66 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)66 ArrayList (java.util.ArrayList)54 InvocationTargetException (java.lang.reflect.InvocationTargetException)52 ISelection (org.eclipse.jface.viewers.ISelection)46 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)46 AbstractObjectReference (org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference)44 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)38 GridData (org.eclipse.swt.layout.GridData)38 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)38 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)30 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)29 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)29 DBSObjectContainer (org.jkiss.dbeaver.model.struct.DBSObjectContainer)28 Composite (org.eclipse.swt.widgets.Composite)27 IEditorPart (org.eclipse.ui.IEditorPart)26 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)24 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)22