Search in sources :

Example 11 with DBPObject

use of org.jkiss.dbeaver.model.DBPObject in project dbeaver by dbeaver.

the class SQLGeneratorConfigurationRegistry method getApplicableGenerators.

public List<SQLGeneratorDescriptor> getApplicableGenerators(Collection<?> objects, Object context) {
    List<SQLGeneratorDescriptor> result = new ArrayList<>();
    for (SQLGeneratorDescriptor gen : generators) {
        for (Object object : objects) {
            if (object instanceof DBPObject && gen.appliesTo((DBPObject) object, context)) {
                if (gen.isMultiObject() && objects.size() < 2) {
                    continue;
                }
                result.add(gen);
                break;
            }
        }
    }
    result.sort(Comparator.comparingInt(SQLGeneratorDescriptor::getOrder));
    return result;
}
Also used : DBPObject(org.jkiss.dbeaver.model.DBPObject) ArrayList(java.util.ArrayList) DBPObject(org.jkiss.dbeaver.model.DBPObject)

Example 12 with DBPObject

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

the class SQLGeneratorContributor method getContributionItems.

// ////////////////////////////////////////////////////////
// Contributors
@Override
protected IContributionItem[] getContributionItems() {
    IWorkbenchPart part = UIUtils.getActiveWorkbenchWindow().getActivePage().getActivePart();
    IStructuredSelection structuredSelection = NavigatorUtils.getSelectionFromPart(part);
    if (structuredSelection == null || structuredSelection.isEmpty()) {
        return new IContributionItem[0];
    }
    List<IContributionItem> menu = new ArrayList<>();
    if (structuredSelection instanceof IResultSetSelection) {
        // Results
        makeResultSetContributions(menu, (IResultSetSelection) structuredSelection);
    } else {
        List<DBPObject> objects = new ArrayList<>();
        for (Object obj : structuredSelection.toList()) {
            DBSObject adaptedObject = GeneralUtils.adapt(obj, DBSObject.class);
            if (adaptedObject != null) {
                objects.add(adaptedObject);
            } else if (obj instanceof DBSWrapper) {
                objects.add(((DBSWrapper) obj).getObject());
            } else if (obj instanceof DBPObject) {
                objects.add((DBPObject) obj);
            }
        }
        List<SQLGeneratorDescriptor> generators = SQLGeneratorConfigurationRegistry.getInstance().getApplicableGenerators(objects, structuredSelection);
        int lastGrand = 0;
        for (SQLGeneratorDescriptor gen : generators) {
            int order = gen.getOrder();
            if (order > 0 && order / 1000 > lastGrand) {
                menu.add(new Separator());
            }
            lastGrand = order / 1000;
            menu.add(makeAction(gen.getLabel(), gen, objects));
        }
    }
    return menu.toArray(new IContributionItem[0]);
}
Also used : DBPObject(org.jkiss.dbeaver.model.DBPObject) DBSWrapper(org.jkiss.dbeaver.model.struct.DBSWrapper) ArrayList(java.util.ArrayList) IResultSetSelection(org.jkiss.dbeaver.ui.controls.resultset.IResultSetSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) SQLGeneratorDescriptor(org.jkiss.dbeaver.model.sql.registry.SQLGeneratorDescriptor) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) DBPScriptObject(org.jkiss.dbeaver.model.DBPScriptObject) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPObject(org.jkiss.dbeaver.model.DBPObject)

Example 13 with DBPObject

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

the class ObjectListDialog method createObjectSelector.

@NotNull
protected static <T extends DBPObject> DatabaseObjectListControl<T> createObjectSelector(Composite group, boolean singleSelection, String listId, List<T> selectedObjects, DBRRunnableWithResult<List<T>> objectReader) {
    return new DatabaseObjectListControl<T>(group, (singleSelection ? SWT.SINGLE : SWT.MULTI), null, new ListContentProvider()) {

        private Font boldFont;

        private ISearchExecutor searcher = new SearcherFilter();

        @NotNull
        @Override
        protected String getListConfigId(List<Class<?>> classList) {
            return listId;
        }

        @Override
        protected LoadingJob<Collection<T>> createLoadService() {
            return LoadingJob.createService(new AbstractLoadService<Collection<T>>() {

                @Override
                public Collection<T> evaluate(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    objectReader.run(monitor);
                    return objectReader.getResult();
                }

                @Override
                public Object getFamily() {
                    return ObjectListDialog.class;
                }
            }, new ObjectsLoadVisualizer() {

                @Override
                public void completeLoading(Collection<T> items) {
                    super.completeLoading(items);
                    performSearch(SearchType.NONE);
                    getItemsViewer().getControl().setFocus();
                }
            });
        }

        protected CellLabelProvider getColumnLabelProvider(ObjectColumn objectColumn) {
            return new ObjectLabelProvider(objectColumn);
        }

        @Override
        protected Object getObjectValue(T item) {
            if (item instanceof DBSWrapper) {
                return ((DBSWrapper) item).getObject();
            }
            return super.getObjectValue(item);
        }

        @Override
        protected DBPImage getObjectImage(T item) {
            if (item instanceof DBNDatabaseNode) {
                return ((DBNDatabaseNode) item).getNodeIcon();
            }
            return null;
        }

        @Override
        protected void setListData(Collection<T> items, boolean append) {
            super.setListData(items, append);
            if (selectedObjects != null) {
                getItemsViewer().setSelection(new StructuredSelection(selectedObjects), true);
            }
        }

        @Override
        public void fillCustomActions(IContributionManager contributionManager) {
            super.fillCustomActions(contributionManager);
            addColumnConfigAction(contributionManager);
        }

        protected void addSearchAction(IContributionManager contributionManager) {
            contributionManager.add(new Action("Filter objects", DBeaverIcons.getImageDescriptor(UIIcon.SEARCH)) {

                @Override
                public void run() {
                    performSearch(SearchType.NONE);
                }
            });
        }

        @Override
        protected ISearchExecutor getSearchRunner() {
            return searcher;
        }

        class ObjectLabelProvider extends ObjectColumnLabelProvider implements IFontProvider {

            ObjectLabelProvider(ObjectColumn objectColumn) {
                super(objectColumn);
            }

            @Override
            public Font getFont(Object element) {
                if (selectedObjects.contains(element)) {
                    if (boldFont == null) {
                        boldFont = UIUtils.makeBoldFont(group.getFont());
                        group.addDisposeListener(e -> boldFont.dispose());
                    }
                    return boldFont;
                }
                return null;
            }
        }
    };
}
Also used : ListContentProvider(org.jkiss.dbeaver.ui.controls.ListContentProvider) org.jkiss.dbeaver.ui(org.jkiss.dbeaver.ui) IDialogConstants(org.eclipse.jface.dialogs.IDialogConstants) IDialogSettings(org.eclipse.jface.dialogs.IDialogSettings) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) DBPImage(org.jkiss.dbeaver.model.DBPImage) NotNull(org.jkiss.code.NotNull) ArrayList(java.util.ArrayList) Composite(org.eclipse.swt.widgets.Composite) DatabaseObjectListControl(org.jkiss.dbeaver.ui.navigator.itemlist.DatabaseObjectListControl) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) Font(org.eclipse.swt.graphics.Font) GridData(org.eclipse.swt.layout.GridData) AbstractLoadService(org.jkiss.dbeaver.model.runtime.load.AbstractLoadService) CommonUtils(org.jkiss.utils.CommonUtils) Shell(org.eclipse.swt.widgets.Shell) DBRRunnableWithResult(org.jkiss.dbeaver.model.runtime.DBRRunnableWithResult) Collection(java.util.Collection) IContributionManager(org.eclipse.jface.action.IContributionManager) Action(org.eclipse.jface.action.Action) IFontProvider(org.eclipse.jface.viewers.IFontProvider) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) DBSWrapper(org.jkiss.dbeaver.model.struct.DBSWrapper) CellLabelProvider(org.eclipse.jface.viewers.CellLabelProvider) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) DBException(org.jkiss.dbeaver.DBException) SWT(org.eclipse.swt.SWT) DBPObject(org.jkiss.dbeaver.model.DBPObject) AbstractPopupPanel(org.jkiss.dbeaver.ui.dialogs.AbstractPopupPanel) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Control(org.eclipse.swt.widgets.Control) Action(org.eclipse.jface.action.Action) DBSWrapper(org.jkiss.dbeaver.model.struct.DBSWrapper) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DatabaseObjectListControl(org.jkiss.dbeaver.ui.navigator.itemlist.DatabaseObjectListControl) Font(org.eclipse.swt.graphics.Font) InvocationTargetException(java.lang.reflect.InvocationTargetException) ListContentProvider(org.jkiss.dbeaver.ui.controls.ListContentProvider) SWT(org.eclipse.swt.SWT) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) DBPObject(org.jkiss.dbeaver.model.DBPObject) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) IContributionManager(org.eclipse.jface.action.IContributionManager) NotNull(org.jkiss.code.NotNull)

Example 14 with DBPObject

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

the class NavigatorHandlerObjectCreateBase method createNewObject.

protected boolean createNewObject(final IWorkbenchWindow workbenchWindow, DBNNode element, @Nullable Class<?> newObjectType, DBNDatabaseNode copyFrom, boolean isFolder) {
    try {
        DBNNode container = null;
        if (isFolder || (element instanceof DBNContainer && !(element instanceof DBNDataSource))) {
            container = element;
        } else {
            DBNNode parentNode = element.getParentNode();
            if (parentNode instanceof DBNContainer) {
                container = parentNode;
            }
        }
        if (container == null) {
            throw new DBException("Can't detect container for '" + element.getNodeName() + "'");
        }
        if (container instanceof DBNDatabaseNode && ObjectPropertyTester.isMetadataChangeDisabled((DBNDatabaseNode) container)) {
            throw new DBException("Object create not available in simple view mode");
        }
        if (newObjectType == null) {
            Class<?> childType = container instanceof DBNContainer ? ((DBNContainer) container).getChildrenClass() : null;
            if (childType == null) {
                throw new DBException("Can't determine child element type for container '" + container + "'");
            }
            newObjectType = childType;
        }
        if (newObjectType == IProject.class) {
            return NavigatorHandlerProjectCreate.createNewProject(workbenchWindow);
        }
        DBSObject sourceObject = copyFrom == null ? null : copyFrom.getObject();
        final Object parentObject;
        if (container instanceof DBNDatabaseNode) {
            parentObject = ((DBNDatabaseNode) container).getValueObject();
        } else if (container instanceof DBNProject) {
            parentObject = ((DBNProject) container).getProject();
        } else if (container instanceof DBNProjectDatabases) {
            parentObject = container.getOwnerProject();
        } else if (container instanceof DBNLocalFolder) {
            parentObject = ((DBNLocalFolder) container).getFolder();
        } else {
            parentObject = null;
        }
        // Check that child nodes are read an cached
        if (container.hasChildren(false) || parentObject instanceof DBSInstanceLazy) {
            try {
                DBNNode finalContainer = container;
                UIUtils.runInProgressService(monitor -> {
                    try {
                        if (finalContainer.hasChildren(false)) {
                            finalContainer.getChildren(monitor);
                        }
                        if (parentObject instanceof DBSInstanceLazy) {
                            ((DBSInstanceLazy) parentObject).checkInstanceConnection(monitor);
                        }
                    } catch (DBException e) {
                        throw new InvocationTargetException(e);
                    }
                });
            } catch (InvocationTargetException e) {
                DBWorkbench.getPlatformUI().showError("New object", "Error creating new object", e);
            } catch (InterruptedException e) {
            // ignore
            }
        }
        DBEObjectManager<?> objectManager = DBWorkbench.getPlatform().getEditorsRegistry().getObjectManager(newObjectType);
        if (objectManager == null) {
            throw new DBException("Object manager not found for type '" + newObjectType.getName() + "'");
        }
        DBEObjectMaker objectMaker = (DBEObjectMaker) objectManager;
        DBPDataSource dataSource = container instanceof DBNDatabaseNode ? ((DBNDatabaseNode) container).getDataSource() : null;
        final boolean openEditor = dataSource != null && (objectMaker.getMakerOptions(dataSource) & DBEObjectMaker.FEATURE_EDITOR_ON_CREATE) != 0;
        CommandTarget commandTarget = getCommandTarget(workbenchWindow, container, newObjectType, openEditor);
        // Parent is model object - not node
        Map<String, Object> options = new HashMap<>();
        options.put(DBEObjectMaker.OPTION_CONTAINER, container);
        options.put(DBEObjectMaker.OPTION_OBJECT_TYPE, newObjectType);
        createDatabaseObject(commandTarget, objectMaker, parentObject instanceof DBPObject ? (DBPObject) parentObject : null, sourceObject, options);
    } catch (Throwable e) {
        DBWorkbench.getPlatformUI().showError("Create object", null, e);
        return false;
    }
    return true;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBPObject(org.jkiss.dbeaver.model.DBPObject) HashMap(java.util.HashMap) DBEObjectMaker(org.jkiss.dbeaver.model.edit.DBEObjectMaker) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPScriptObject(org.jkiss.dbeaver.model.DBPScriptObject) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPRefreshableObject(org.jkiss.dbeaver.model.DBPRefreshableObject) DBPObject(org.jkiss.dbeaver.model.DBPObject) DBSInstanceLazy(org.jkiss.dbeaver.model.struct.DBSInstanceLazy)

Example 15 with DBPObject

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

the class SQLGeneratorConfigurationRegistry method getApplicableGenerators.

public List<SQLGeneratorDescriptor> getApplicableGenerators(Collection<?> objects, Object context) {
    List<SQLGeneratorDescriptor> result = new ArrayList<>();
    for (SQLGeneratorDescriptor gen : generators) {
        for (Object object : objects) {
            if (object instanceof DBPObject && gen.appliesTo((DBPObject) object, context)) {
                if (gen.isMultiObject() && objects.size() < 2) {
                    continue;
                }
                result.add(gen);
                break;
            }
        }
    }
    result.sort(Comparator.comparingInt(SQLGeneratorDescriptor::getOrder));
    return result;
}
Also used : DBPObject(org.jkiss.dbeaver.model.DBPObject) ArrayList(java.util.ArrayList) DBPObject(org.jkiss.dbeaver.model.DBPObject)

Aggregations

DBPObject (org.jkiss.dbeaver.model.DBPObject)21 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)7 ArrayList (java.util.ArrayList)6 DBException (org.jkiss.dbeaver.DBException)5 DBPScriptObject (org.jkiss.dbeaver.model.DBPScriptObject)5 DBEObjectMaker (org.jkiss.dbeaver.model.edit.DBEObjectMaker)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)4 DBSWrapper (org.jkiss.dbeaver.model.struct.DBSWrapper)4 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)3 DBPRefreshableObject (org.jkiss.dbeaver.model.DBPRefreshableObject)3 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)3 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 List (java.util.List)2 IFile (org.eclipse.core.resources.IFile)2 Action (org.eclipse.jface.action.Action)2 IContributionManager (org.eclipse.jface.action.IContributionManager)2 IDialogConstants (org.eclipse.jface.dialogs.IDialogConstants)2 IDialogSettings (org.eclipse.jface.dialogs.IDialogSettings)2