Search in sources :

Example 76 with DBNDatabaseNode

use of org.jkiss.dbeaver.model.navigator.DBNDatabaseNode in project dbeaver by serge-rider.

the class DatabaseEditorInputFactory method saveState.

public static void saveState(IMemento memento, DatabaseEditorInput input) {
    if (!DBWorkbench.getPlatform().getPreferenceStore().getBoolean(DatabaseEditorPreferences.PROP_SAVE_EDITORS_STATE)) {
        return;
    }
    final DBCExecutionContext context = input.getExecutionContext();
    if (context == null) {
        // Detached - nothing to save
        return;
    }
    if (input.getDatabaseObject() != null && !input.getDatabaseObject().isPersisted()) {
        return;
    }
    final DBNDatabaseNode node = input.getNavigatorNode();
    memento.putString(TAG_CLASS, input.getClass().getName());
    memento.putString(TAG_PROJECT, context.getDataSource().getContainer().getProject().getName());
    memento.putString(TAG_DATA_SOURCE, context.getDataSource().getContainer().getId());
    memento.putString(TAG_NODE, node.getNodeItemPath());
    memento.putString(TAG_NODE_NAME, node.getNodeName());
    if (!CommonUtils.isEmpty(input.getDefaultPageId())) {
        memento.putString(TAG_ACTIVE_PAGE, input.getDefaultPageId());
    }
    if (!CommonUtils.isEmpty(input.getDefaultFolderId())) {
        memento.putString(TAG_ACTIVE_FOLDER, input.getDefaultFolderId());
    }
}
Also used : DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 77 with DBNDatabaseNode

use of org.jkiss.dbeaver.model.navigator.DBNDatabaseNode in project dbeaver by dbeaver.

the class EditForeignKeyPage method createPageContents.

@Override
protected Composite createPageContents(Composite parent) {
    final Composite panel = UIUtils.createComposite(parent, 1);
    panel.setLayoutData(new GridData(GridData.FILL_BOTH));
    {
        final Composite tableGroup = UIUtils.createComposite(panel, 2);
        tableGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        UIUtils.createLabelText(tableGroup, EditorsMessages.dialog_struct_edit_fk_label_table, DBUtils.getObjectFullName(foreignKey.getParentObject(), DBPEvaluationContext.UI), SWT.READ_ONLY | SWT.BORDER);
        if (allowedKeyTypes.length > 1) {
            UIUtils.createControlLabel(tableGroup, "Key type");
            Composite ktPanel = UIUtils.createFormPlaceholder(tableGroup, allowedKeyTypes.length, 1);
            // keyTypeCombo.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
            for (FKType type : allowedKeyTypes) {
                Button keyTypeButton = UIUtils.createRadioButton(ktPanel, type.getName(), type, new SelectionAdapter() {

                    @Override
                    public void widgetSelected(SelectionEvent e) {
                        selectedKeyType = (FKType) e.widget.getData();
                        updateControlsVisibility();
                    }
                });
                if (type == preferredKeyType) {
                    keyTypeButton.setSelection(true);
                }
            }
        }
        if (curRefTable == null) {
            try {
                if (foreignKey instanceof DBVEntityForeignKey) {
                    // Virtual key - add container selector
                    createContainerSelector(tableGroup);
                } else if (ownerTableNode != null) {
                    createSchemaSelector(tableGroup);
                }
            } catch (Throwable e) {
                log.debug("Can't create schema selector", e);
            }
        } else {
            UIUtils.createLabelText(tableGroup, EditorsMessages.dialog_struct_edit_fk_label_ref_table, DBUtils.getObjectFullName(curRefTable, DBPEvaluationContext.UI), SWT.READ_ONLY | SWT.BORDER);
        }
    }
    if (curRefTable == null) {
        DBNNode containerNode = ownerTableNode == null ? null : ownerTableNode.getParentNode();
        while (containerNode instanceof DBNDatabaseFolder) {
            containerNode = containerNode.getParentNode();
        }
        DBNNode rootNode = containerNode == null ? DBWorkbench.getPlatform().getNavigatorModel().getRoot() : containerNode;
        UIUtils.createControlLabel(panel, EditorsMessages.dialog_struct_edit_fk_label_ref_table);
        tableList = new Table(panel, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);
        tableList.setLinesVisible(true);
        final GridData gd = new GridData(GridData.FILL_BOTH);
        gd.widthHint = 500;
        gd.heightHint = 150;
        tableList.setLayoutData(gd);
        tableList.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                handleRefTableSelect((DBNDatabaseNode) e.item.getData());
            }
        });
        if (rootNode instanceof DBNDatabaseNode) {
            loadTableList((DBNDatabaseNode) rootNode);
        }
    }
    final Composite pkGroup = UIUtils.createComposite(panel, enableCustomKeys ? 3 : 2);
    {
        pkGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        uniqueKeyCombo = UIUtils.createLabelCombo(pkGroup, EditorsMessages.dialog_struct_edit_fk_combo_unik, SWT.DROP_DOWN | SWT.READ_ONLY);
        // uniqueKeyCombo.setEnabled(false);
        uniqueKeyCombo.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                handleUniqueKeySelect();
                updatePageState();
            }
        });
        if (enableCustomKeys) {
            customUKButton = UIUtils.createDialogButton(pkGroup, "Create", new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    defineRefTableConstraint();
                }
            });
            customUKButton.setEnabled(false);
        }
        if (supportsCustomName()) {
            fkNameText = UIUtils.createLabelText(pkGroup, EditorsMessages.dialog_struct_edit_fk_name, "");
            fkNameText.addModifyListener(e -> fkName = fkNameText.getText());
        }
    }
    {
        UIUtils.createControlLabel(panel, EditorsMessages.dialog_struct_edit_fk_label_columns);
        columnsTable = new Table(panel, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER);
        columnsTable.setHeaderVisible(true);
        columnsTable.setLinesVisible(true);
        final GridData gd = new GridData(GridData.FILL_BOTH);
        gd.widthHint = 500;
        gd.heightHint = 100;
        columnsTable.setLayoutData(gd);
        UIUtils.createTableColumn(columnsTable, SWT.LEFT, EditorsMessages.dialog_struct_edit_fk_column_column);
        UIUtils.createTableColumn(columnsTable, SWT.LEFT, EditorsMessages.dialog_struct_edit_fk_column_col_type);
        UIUtils.createTableColumn(columnsTable, SWT.LEFT, EditorsMessages.dialog_struct_edit_fk_column_ref_col);
        UIUtils.createTableColumn(columnsTable, SWT.LEFT, EditorsMessages.dialog_struct_edit_fk_column_ref_col_type);
        final TableEditor tableEditor = new TableEditor(columnsTable);
        tableEditor.horizontalAlignment = SWT.CENTER;
        tableEditor.verticalAlignment = SWT.TOP;
        tableEditor.grabHorizontal = true;
        tableEditor.minimumWidth = 50;
        columnsTable.addMouseListener(new ColumnsMouseListener(tableEditor, columnsTable));
    }
    if (!ArrayUtils.isEmpty(supportedModifyRules)) {
        final Composite cascadeGroup = UIUtils.createPlaceholder(panel, 4, 5);
        {
            // Cascades
            cascadeGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
            final Combo onDeleteCombo = UIUtils.createLabelCombo(cascadeGroup, EditorsMessages.dialog_struct_edit_fk_combo_on_delete, SWT.DROP_DOWN | SWT.READ_ONLY);
            onDeleteCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
            final Combo onUpdateCombo = UIUtils.createLabelCombo(cascadeGroup, EditorsMessages.dialog_struct_edit_fk_combo_on_update, SWT.DROP_DOWN | SWT.READ_ONLY);
            onUpdateCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
            for (DBSForeignKeyModifyRule modifyRule : supportedModifyRules) {
                onDeleteCombo.add(modifyRule.getName());
                onUpdateCombo.add(modifyRule.getName());
            }
            onDeleteCombo.select(0);
            onUpdateCombo.select(0);
            onDeleteRule = onUpdateRule = supportedModifyRules[0];
            onDeleteCombo.addSelectionListener(new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    onDeleteRule = supportedModifyRules[onDeleteCombo.getSelectionIndex()];
                }
            });
            onUpdateCombo.addSelectionListener(new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    onUpdateRule = supportedModifyRules[onUpdateCombo.getSelectionIndex()];
                }
            });
        }
        addPhysicalKeyComponent(cascadeGroup);
    }
    if (tableList != null) {
        tableList.setFocus();
    }
    return panel;
}
Also used : TableEditor(org.eclipse.swt.custom.TableEditor) DBWorkbench(org.jkiss.dbeaver.runtime.DBWorkbench) org.jkiss.dbeaver.model.struct.rdb(org.jkiss.dbeaver.model.struct.rdb) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) ObjectContainerSelectorPanel(org.jkiss.dbeaver.ui.controls.ObjectContainerSelectorPanel) org.jkiss.dbeaver.model.virtual(org.jkiss.dbeaver.model.virtual) Image(org.eclipse.swt.graphics.Image) EditorsMessages(org.jkiss.dbeaver.ui.editors.internal.EditorsMessages) Nullable(org.jkiss.code.Nullable) Point(org.eclipse.swt.graphics.Point) NotNull(org.jkiss.code.NotNull) ArrayList(java.util.ArrayList) DBeaverIcons(org.jkiss.dbeaver.ui.DBeaverIcons) UIUtils(org.jkiss.dbeaver.ui.UIUtils) Log(org.jkiss.dbeaver.Log) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) GridData(org.eclipse.swt.layout.GridData) CCombo(org.eclipse.swt.custom.CCombo) org.jkiss.dbeaver.model(org.jkiss.dbeaver.model) CommonUtils(org.jkiss.utils.CommonUtils) org.eclipse.swt.events(org.eclipse.swt.events) org.jkiss.dbeaver.model.struct(org.jkiss.dbeaver.model.struct) NLS(org.eclipse.osgi.util.NLS) Collection(java.util.Collection) org.eclipse.swt.widgets(org.eclipse.swt.widgets) DBXTreeItem(org.jkiss.dbeaver.model.navigator.meta.DBXTreeItem) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) ArrayUtils(org.jkiss.utils.ArrayUtils) InvocationTargetException(java.lang.reflect.InvocationTargetException) ILabelProvider(org.eclipse.jface.viewers.ILabelProvider) List(java.util.List) DBException(org.jkiss.dbeaver.DBException) CSmartCombo(org.jkiss.dbeaver.ui.controls.CSmartCombo) SWT(org.eclipse.swt.SWT) DBXTreeNode(org.jkiss.dbeaver.model.navigator.meta.DBXTreeNode) Collections(java.util.Collections) LabelProvider(org.eclipse.jface.viewers.LabelProvider) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) DBNDatabaseFolder(org.jkiss.dbeaver.model.navigator.DBNDatabaseFolder) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) CCombo(org.eclipse.swt.custom.CCombo) CSmartCombo(org.jkiss.dbeaver.ui.controls.CSmartCombo) DBNDatabaseFolder(org.jkiss.dbeaver.model.navigator.DBNDatabaseFolder) TableEditor(org.eclipse.swt.custom.TableEditor) GridData(org.eclipse.swt.layout.GridData) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 78 with DBNDatabaseNode

use of org.jkiss.dbeaver.model.navigator.DBNDatabaseNode in project dbeaver by dbeaver.

the class EditForeignKeyPage method loadTableList.

private void loadTableList(DBNDatabaseNode newContainerNode) {
    tableList.removeAll();
    final List<DBNDatabaseNode> entities = new ArrayList<>();
    try {
        UIUtils.runInProgressService(monitor -> {
            try {
                loadEntities(monitor, entities, newContainerNode);
            } catch (DBException e) {
                throw new InvocationTargetException(e);
            }
        });
    } catch (InvocationTargetException e) {
        DBWorkbench.getPlatformUI().showError("Error loading tables", "Error during table load", e);
    } catch (InterruptedException e) {
    // Ignore
    }
    for (DBNDatabaseNode entityNode : entities) {
        TableItem tableItem = new TableItem(tableList, SWT.LEFT);
        tableItem.setText(entityNode.getNodeName());
        tableItem.setImage(DBeaverIcons.getImage(entityNode.getNodeIconDefault()));
        tableItem.setData(entityNode);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) ArrayList(java.util.ArrayList) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 79 with DBNDatabaseNode

use of org.jkiss.dbeaver.model.navigator.DBNDatabaseNode in project dbeaver by dbeaver.

the class StatisticsNavigatorNodeRenderer method renderObjectStatistics.

// /////////////////////////////////////////////////////////////////
// Statistics renderer
private void renderObjectStatistics(DBNDatabaseNode element, Tree tree, GC gc, Event event) {
    DBSObject object = element.getObject();
    if (object instanceof DBPObjectStatistics) {
        String sizeText;
        int percentFull;
        boolean statsWasRead = false;
        DBNNode parentNode = getParentItem(element);
        DBSObject parentObject = parentNode instanceof DBNDatabaseNode ? DBUtils.getPublicObject(((DBNDatabaseNode) parentNode).getObject()) : null;
        if (parentObject instanceof DBPObjectStatisticsCollector) {
            // && !((DBPObjectStatisticsCollector) parentObject).isStatisticsCollected()
            statsWasRead = ((DBPObjectStatisticsCollector) parentObject).isStatisticsCollected();
        } else {
            // If there is no stats collector then do not check for stats presence
            // Because it will trigger stats read job which won't read any statistics (as there is no way to load it for individual object).
            // ((DBPObjectStatistics) object).hasStatistics();
            statsWasRead = true;
        }
        long maxObjectSize = statsWasRead ? getMaxObjectSize((TreeItem) event.item) : -1;
        if (statsWasRead && maxObjectSize >= 0) {
            long statObjectSize = ((DBPObjectStatistics) object).getStatObjectSize();
            if (statObjectSize <= 0) {
                // Empty or no size - nothing to show
                return;
            }
            percentFull = maxObjectSize == 0 ? 0 : (int) (statObjectSize * 100 / maxObjectSize);
            if (percentFull < 0 || percentFull > 100) {
                log.debug("Object stat > 100%!");
                percentFull = 100;
            }
            Format format;
            synchronized (classFormatMap) {
                format = classFormatMap.get(object.getClass().getName());
                if (format == null) {
                    try {
                        Method getStatObjectSizeMethod = object.getClass().getMethod("getStatObjectSize");
                        Property propAnnotation = getStatObjectSizeMethod.getAnnotation(Property.class);
                        if (propAnnotation != null) {
                            Class<? extends Format> formatterClass = propAnnotation.formatter();
                            if (formatterClass != Format.class) {
                                format = formatterClass.getConstructor().newInstance();
                            }
                        }
                    } catch (Exception e) {
                        log.debug(e);
                    }
                    if (format == null) {
                        format = numberFormat;
                    }
                    classFormatMap.put(object.getClass().getName(), format);
                }
            }
            sizeText = format.format(statObjectSize);
        } else {
            sizeText = "...";
            percentFull = 0;
            if (parentNode instanceof DBNDatabaseNode) {
                DBSObject realParentObject = DBUtils.getPublicObject(((DBNDatabaseNode) parentNode).getObject());
                if (!readObjectStatistics(element.getParentNode(), realParentObject, ((TreeItem) event.item).getParentItem())) {
                    return;
                }
            }
        }
        Point textSize = gc.stringExtent(sizeText);
        textSize.x += 4;
        // int caWidth = tree.getClientArea().width;
        int occupiedWidth = event.x + event.width + 4;
        int xWidth = getTreeWidth(tree);
        if (xWidth - occupiedWidth > Math.max(PERCENT_FILL_WIDTH, textSize.x)) {
            {
                CTabFolder tabFolder = UIUtils.getParentOfType(tree, CTabFolder.class);
                Color fillColor = tabFolder == null ? UIStyles.getDefaultWidgetBackground() : tabFolder.getBackground();
                gc.setBackground(fillColor);
                int fillWidth = PERCENT_FILL_WIDTH * percentFull / 100 + 1;
                int x = xWidth - fillWidth - 2;
                gc.fillRectangle(x, event.y + 2, fillWidth, event.height - 4);
            }
            gc.setForeground(tree.getForeground());
            int x = xWidth - textSize.x - 2;
            Font oldFont = gc.getFont();
            gc.setFont(tree.getFont());
            gc.drawText(sizeText, x + 2, event.y + (event.height - textSize.y) / 2, true);
            gc.setFont(oldFont);
        }
    }
}
Also used : CTabFolder(org.eclipse.swt.custom.CTabFolder) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) TreeItem(org.eclipse.swt.widgets.TreeItem) Method(java.lang.reflect.Method) DBException(org.jkiss.dbeaver.DBException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) Format(java.text.Format) ByteNumberFormat(org.jkiss.utils.ByteNumberFormat) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) Property(org.jkiss.dbeaver.model.meta.Property)

Example 80 with DBNDatabaseNode

use of org.jkiss.dbeaver.model.navigator.DBNDatabaseNode in project dbeaver by dbeaver.

the class StatisticsNavigatorNodeRenderer method paintNodeDetails.

public void paintNodeDetails(DBNNode node, Tree tree, GC gc, Event event) {
    super.paintNodeDetails(node, tree, gc, event);
    ScrollBar hSB = tree.getHorizontalBar();
    boolean scrollEnabled = (hSB != null && hSB.isVisible());
    Object element = event.item.getData();
    if (element instanceof DBNDatabaseNode) {
        if (element instanceof DBNDataSource) {
            int widthOccupied = 0;
            if (!scrollEnabled && DBWorkbench.getPlatform().getPreferenceStore().getBoolean(NavigatorPreferences.NAVIGATOR_SHOW_NODE_ACTIONS)) {
                widthOccupied += renderDataSourceNodeActions((DBNDatabaseNode) element, tree, gc, event);
            }
            if (DBWorkbench.getPlatform().getPreferenceStore().getBoolean(NavigatorPreferences.NAVIGATOR_SHOW_CONNECTION_HOST_NAME)) {
                renderDataSourceHostName((DBNDataSource) element, tree, gc, event, widthOccupied);
            }
        }
        if (!scrollEnabled && DBWorkbench.getPlatform().getPreferenceStore().getBoolean(NavigatorPreferences.NAVIGATOR_SHOW_STATISTICS_INFO)) {
            renderObjectStatistics((DBNDatabaseNode) element, tree, gc, event);
        }
    }
}
Also used : DBNDataSource(org.jkiss.dbeaver.model.navigator.DBNDataSource) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) ScrollBar(org.eclipse.swt.widgets.ScrollBar)

Aggregations

DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)222 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)85 DBException (org.jkiss.dbeaver.DBException)83 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)81 InvocationTargetException (java.lang.reflect.InvocationTargetException)53 ArrayList (java.util.ArrayList)44 GridData (org.eclipse.swt.layout.GridData)36 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)29 VoidProgressMonitor (org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)28 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)27 DBNModel (org.jkiss.dbeaver.model.navigator.DBNModel)26 ISelection (org.eclipse.jface.viewers.ISelection)21 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)21 DBNDatabaseFolder (org.jkiss.dbeaver.model.navigator.DBNDatabaseFolder)19 Collection (java.util.Collection)18 List (java.util.List)17 Composite (org.eclipse.swt.widgets.Composite)17 DBNDataSource (org.jkiss.dbeaver.model.navigator.DBNDataSource)17 DBXTreeNode (org.jkiss.dbeaver.model.navigator.meta.DBXTreeNode)17 SWT (org.eclipse.swt.SWT)15