Search in sources :

Example 1 with DBRRunnableWithResult

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

the class NodeDropTargetListener method createTargetRequest.

@Override
protected Request createTargetRequest() {
    CreateRequest request = new CreateRequest();
    request.setFactory(new CreationFactory() {

        @Override
        public Object getNewObject() {
            Collection<DBPNamedObject> objects = DatabaseObjectTransfer.getInstance().getObject();
            if (objects == null) {
                return null;
            }
            DBRRunnableWithResult<List<ERDEntity>> collector = new DBRRunnableWithResult<List<ERDEntity>>() {

                @Override
                public void run(DBRProgressMonitor monitor) {
                    result = DiagramObjectCollector.generateEntityList(monitor, ((DiagramPart) getViewer().getRootEditPart().getContents()).getDiagram(), objects, new DiagramCollectSettingsDefault(), true);
                }
            };
            try {
                UIUtils.runInProgressService(collector);
            } catch (InvocationTargetException e) {
                DBWorkbench.getPlatformUI().showError("Entity collect error", "Error during diagram entities collect", e);
            } catch (InterruptedException e) {
            // ignore
            }
            return collector.getResult();
        }

        @Override
        public Object getObjectType() {
            return RequestConstants.REQ_CREATE;
        }
    });
    request.setLocation(getDropLocation());
    return request;
}
Also used : CreateRequest(org.eclipse.gef.requests.CreateRequest) ERDEntity(org.jkiss.dbeaver.erd.model.ERDEntity) CreationFactory(org.eclipse.gef.requests.CreationFactory) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBRRunnableWithResult(org.jkiss.dbeaver.model.runtime.DBRRunnableWithResult) DiagramCollectSettingsDefault(org.jkiss.dbeaver.erd.ui.model.DiagramCollectSettingsDefault) Collection(java.util.Collection) DBPNamedObject(org.jkiss.dbeaver.model.DBPNamedObject) List(java.util.List) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 2 with DBRRunnableWithResult

use of org.jkiss.dbeaver.model.runtime.DBRRunnableWithResult in project dbeaver by dbeaver.

the class DBUtils method createNewAttributeValue.

public static <T> T createNewAttributeValue(DBCExecutionContext context, DBDValueHandler valueHandler, DBSTypedObject valueType, Class<T> targetType) throws DBCException {
    DBRRunnableWithResult<Object> runnable = new DBRRunnableWithResult<Object>() {

        @Override
        public void run(DBRProgressMonitor monitor) throws InvocationTargetException {
            try (DBCSession session = context.openSession(monitor, DBCExecutionPurpose.UTIL, "Create new object")) {
                result = valueHandler.createNewValueObject(session, valueType);
            } catch (DBCException e) {
                throw new InvocationTargetException(e);
            }
        }
    };
    try {
        DBWorkbench.getPlatformUI().executeWithProgress(runnable);
    // UIUtils.runInProgressService(runnable);
    } catch (InvocationTargetException e) {
        throw new DBCException(e.getTargetException(), context);
    } catch (InterruptedException e) {
        throw new DBCException(e, context);
    }
    Object result = runnable.getResult();
    if (result == null) {
        throw new DBCException("Internal error - null object created");
    }
    if (!targetType.isInstance(result)) {
        throw new DBCException("Internal error - wrong object type '" + result.getClass().getName() + "' while '" + targetType.getName() + "' was expected");
    }
    return targetType.cast(result);
}
Also used : DBRRunnableWithResult(org.jkiss.dbeaver.model.runtime.DBRRunnableWithResult) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with DBRRunnableWithResult

use of org.jkiss.dbeaver.model.runtime.DBRRunnableWithResult in project dbeaver by dbeaver.

the class ObjectListDialog method createDialogArea.

@Override
protected Control createDialogArea(Composite parent) {
    Composite group = (Composite) super.createDialogArea(parent);
    GridData gd = new GridData(GridData.FILL_BOTH);
    group.setLayoutData(gd);
    createUpperControls(group);
    objectList = createObjectSelector(group, singleSelection, listId, selectedObjects, new DBRRunnableWithResult<List<T>>() {

        @Override
        public void run(DBRProgressMonitor monitor) throws InvocationTargetException {
            try {
                result = getObjects(monitor);
            } catch (DBException e) {
                throw new InvocationTargetException(e);
            }
        }
    });
    objectList.createProgressPanel();
    gd = new GridData(GridData.FILL_BOTH);
    gd.heightHint = 300;
    gd.minimumWidth = 300;
    objectList.setLayoutData(gd);
    objectList.getSelectionProvider().addSelectionChangedListener(event -> {
        IStructuredSelection selection = (IStructuredSelection) event.getSelection();
        selectedObjects.clear();
        selectedObjects.addAll(selection.toList());
        if (!isModeless()) {
            getButton(IDialogConstants.OK_ID).setEnabled(!selectedObjects.isEmpty());
        }
    });
    objectList.setDoubleClickHandler(event -> {
        if (isModeless() || getButton(IDialogConstants.OK_ID).isEnabled()) {
            okPressed();
        }
    });
    objectList.loadData();
    closeOnFocusLost(objectList.getItemsViewer().getControl(), objectList.getSearchTextControl());
    return group;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBRRunnableWithResult(org.jkiss.dbeaver.model.runtime.DBRRunnableWithResult) Composite(org.eclipse.swt.widgets.Composite) SWT(org.eclipse.swt.SWT) GridData(org.eclipse.swt.layout.GridData) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 4 with DBRRunnableWithResult

use of org.jkiss.dbeaver.model.runtime.DBRRunnableWithResult in project dbeaver by dbeaver.

the class SelectDatabaseDialog method createInstanceSelector.

private void createInstanceSelector(Composite group, DBSObjectContainer instanceContainer) {
    ((GridLayout) group.getLayout()).numColumns++;
    instanceList = createObjectSelector(group, true, "DatabaseInstanceSelector", selectedInstances, new DBRRunnableWithResult<List<DBNDatabaseNode>>() {

        @Override
        public void run(DBRProgressMonitor monitor) throws InvocationTargetException {
            try {
                if (!CommonUtils.isEmpty(currentInstanceName) && selectedInstances.isEmpty()) {
                    DBSObject activeInstance = instanceContainer.getChild(monitor, currentInstanceName);
                    if (activeInstance != null) {
                        DBNDatabaseNode activeInstanceNode = DBNUtils.getNodeByObject(monitor, activeInstance, false);
                        if (activeInstanceNode != null) {
                            selectedInstances.add(activeInstanceNode);
                        }
                    }
                }
                Collection<? extends DBSObject> instances = instanceContainer.getChildren(new VoidProgressMonitor());
                List<DBNDatabaseNode> instanceNodes = new ArrayList<>();
                if (!CommonUtils.isEmpty(instances)) {
                    for (DBSObject instance : instances) {
                        DBNDatabaseNode instanceNode = DBNUtils.getNodeByObject(monitor, instance, false);
                        if (instanceNode != null) {
                            instanceNodes.add(instanceNode);
                        }
                    }
                }
                result = instanceNodes;
                objectList.loadData();
            } catch (DBException e) {
                throw new InvocationTargetException(e);
            }
        }
    });
    instanceList.createProgressPanel();
    GridData gd = new GridData(GridData.FILL_BOTH);
    gd.heightHint = 300;
    gd.minimumWidth = 300;
    instanceList.setLayoutData(gd);
    instanceList.getSelectionProvider().addSelectionChangedListener(event -> {
        IStructuredSelection selection = (IStructuredSelection) event.getSelection();
        selectedInstances.clear();
        selectedInstances.addAll(selection.toList());
        DBNDatabaseNode instance = selectedInstances.isEmpty() ? null : selectedInstances.get(0);
        if (instance != null && !CommonUtils.equalObjects(instance.getNodeName(), currentInstanceName)) {
            currentInstanceName = instance.getNodeName();
            objectList.loadData();
        }
    });
    instanceList.loadData();
    closeOnFocusLost(instanceList);
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBException(org.jkiss.dbeaver.DBException) DBRRunnableWithResult(org.jkiss.dbeaver.model.runtime.DBRRunnableWithResult) ArrayList(java.util.ArrayList) GridData(org.eclipse.swt.layout.GridData) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 5 with DBRRunnableWithResult

use of org.jkiss.dbeaver.model.runtime.DBRRunnableWithResult in project dbeaver by dbeaver.

the class DatabaseNavigatorTree method expandNodeOnLoad.

private void expandNodeOnLoad(final DBNNode node) {
    if (node instanceof DBNDataSource && DBWorkbench.getPlatform().getPreferenceStore().getBoolean(NavigatorPreferences.NAVIGATOR_EXPAND_ON_CONNECT)) {
        try {
            DBRRunnableWithResult<DBNNode> runnable = new DBRRunnableWithResult<DBNNode>() {

                @Override
                public void run(DBRProgressMonitor monitor) throws InvocationTargetException {
                    try {
                        result = findActiveNode(monitor, node);
                    } catch (DBException e) {
                        throw new InvocationTargetException(e);
                    }
                }
            };
            UIUtils.runInProgressService(runnable);
            if (runnable.getResult() != null && !treeViewer.getTree().isDisposed()) {
                showNode(runnable.getResult());
                treeViewer.expandToLevel(runnable.getResult(), 1);
            /*
                    // TODO: it is a bug in Eclipse Photon.
                    try {
                        treeViewer.expandToLevel(runnable.getResult(), 1, true);
                    } catch (Throwable e) {
                        treeViewer.expandToLevel(runnable.getResult(), 1);
                    }
*/
            }
        } catch (InvocationTargetException e) {
            log.error("Can't expand node", e.getTargetException());
        } catch (InterruptedException e) {
        // skip it
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBRRunnableWithResult(org.jkiss.dbeaver.model.runtime.DBRRunnableWithResult) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)12 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)12 DBRRunnableWithResult (org.jkiss.dbeaver.model.runtime.DBRRunnableWithResult)12 DBException (org.jkiss.dbeaver.DBException)8 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)6 GridData (org.eclipse.swt.layout.GridData)6 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 List (java.util.List)4 SWT (org.eclipse.swt.SWT)4 Composite (org.eclipse.swt.widgets.Composite)4 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)4 CreateRequest (org.eclipse.gef.requests.CreateRequest)2 CreationFactory (org.eclipse.gef.requests.CreationFactory)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 CellLabelProvider (org.eclipse.jface.viewers.CellLabelProvider)2 IFontProvider (org.eclipse.jface.viewers.IFontProvider)2