Search in sources :

Example 1 with DBSWrapper

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

the class BrowseObjectDialog method createDialogArea.

@Override
protected Control createDialogArea(Composite parent) {
    getShell().setText(title);
    Composite group = (Composite) super.createDialogArea(parent);
    GridData gd = new GridData(GridData.FILL_BOTH);
    group.setLayoutData(gd);
    navigatorTree = new DatabaseNavigatorTree(group, rootNode, (singleSelection ? SWT.SINGLE : SWT.MULTI) | SWT.BORDER);
    gd = new GridData(GridData.FILL_BOTH);
    gd.widthHint = 500;
    gd.heightHint = 500;
    navigatorTree.setLayoutData(gd);
    navigatorTree.getViewer().addFilter(new ViewerFilter() {

        @Override
        public boolean select(Viewer viewer, Object parentElement, Object element) {
            if (element instanceof TreeLoadNode || element instanceof DBNLocalFolder) {
                return true;
            }
            if (element instanceof DBNNode) {
                if (element instanceof DBNDatabaseFolder) {
                    DBNDatabaseFolder folder = (DBNDatabaseFolder) element;
                    Class<? extends DBSObject> folderItemsClass = folder.getChildrenClass();
                    return folderItemsClass != null && matchesType(folderItemsClass, false);
                }
                if (element instanceof DBNProject || element instanceof DBNProjectDatabases || element instanceof DBNDataSource || (element instanceof DBSWrapper && matchesType(((DBSWrapper) element).getObject().getClass(), false))) {
                    return true;
                }
            }
            return false;
        }
    });
    if (selectedNode != null) {
        navigatorTree.getViewer().setSelection(new StructuredSelection(selectedNode));
        selectedObjects.add(selectedNode);
    }
    navigatorTree.getViewer().addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            selectedObjects.clear();
            IStructuredSelection selection = (IStructuredSelection) navigatorTree.getViewer().getSelection();
            for (Iterator iter = selection.iterator(); iter.hasNext(); ) {
                DBNNode node = (DBNNode) iter.next();
                if (node instanceof DBSWrapper) {
                    DBSObject object = DBUtils.getAdapter(DBSObject.class, ((DBSWrapper) node).getObject());
                    if (object != null && matchesType(object.getClass(), true)) {
                        selectedObjects.add(node);
                    }
                }
            }
            getButton(IDialogConstants.OK_ID).setEnabled(!selectedObjects.isEmpty());
        }
    });
    return group;
}
Also used : Composite(org.eclipse.swt.widgets.Composite) DBSWrapper(org.jkiss.dbeaver.model.struct.DBSWrapper) DatabaseNavigatorTree(org.jkiss.dbeaver.ui.navigator.database.DatabaseNavigatorTree) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) TreeLoadNode(org.jkiss.dbeaver.ui.navigator.database.load.TreeLoadNode) GridData(org.eclipse.swt.layout.GridData) Iterator(java.util.Iterator) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject)

Example 2 with DBSWrapper

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

the class NavigatorHandlerObjectGoto method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    DBCExecutionContext context = null;
    DBSObject container = null;
    IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
    if (activePart instanceof DBPContextProvider) {
        context = ((DBPContextProvider) activePart).getExecutionContext();
    } else if (activePart instanceof INavigatorModelView) {
        final ISelection selection = HandlerUtil.getCurrentSelection(event);
        if (selection instanceof IStructuredSelection) {
            Object element = ((IStructuredSelection) selection).getFirstElement();
            if (element instanceof DBSWrapper) {
                DBSObject object = ((DBSWrapper) element).getObject();
                if (object != null) {
                    container = object;
                    while (container instanceof DBSFolder) {
                        container = container.getParentObject();
                    }
                    DBPDataSource dataSource = object.getDataSource();
                    if (dataSource != null) {
                        context = dataSource.getDefaultContext(true);
                    }
                }
            }
        }
    }
    if (context == null) {
        DBUserInterface.getInstance().showError("Go to object", "No active datasource");
        return null;
    }
    IWorkbenchWindow workbenchWindow = HandlerUtil.getActiveWorkbenchWindow(event);
    GotoObjectDialog dialog = new GotoObjectDialog(HandlerUtil.getActiveShell(event), context, container);
    dialog.open();
    Object[] objectsToOpen = dialog.getResult();
    if (!ArrayUtils.isEmpty(objectsToOpen)) {
        Collection<DBNDatabaseNode> nodes = NavigatorHandlerObjectBase.getNodesByObjects(Arrays.asList(objectsToOpen));
        for (DBNDatabaseNode node : nodes) {
            NavigatorUtils.openNavigatorNode(node, workbenchWindow);
        }
    }
    return null;
}
Also used : DBSFolder(org.jkiss.dbeaver.model.struct.DBSFolder) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBSWrapper(org.jkiss.dbeaver.model.struct.DBSWrapper) DBPContextProvider(org.jkiss.dbeaver.model.DBPContextProvider) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) ISelection(org.eclipse.jface.viewers.ISelection) GotoObjectDialog(org.jkiss.dbeaver.ui.dialogs.GotoObjectDialog) INavigatorModelView(org.jkiss.dbeaver.ui.navigator.INavigatorModelView) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 3 with DBSWrapper

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

the class ObjectPropertyTester method test.

@SuppressWarnings("unchecked")
@Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
    if (!(receiver instanceof DBNNode)) {
        return false;
    }
    Display display = Display.getCurrent();
    if (display == null) {
        return false;
    }
    DBNNode node = (DBNNode) receiver;
    switch(property) {
        case PROP_CAN_OPEN:
            return node.isPersisted();
        case PROP_CAN_CREATE:
        case PROP_CAN_PASTE:
            {
                if (node instanceof DBNResource) {
                    return property.equals(PROP_CAN_PASTE);
                }
                Class objectType;
                if (!(node instanceof DBNContainer)) {
                    if (node.getParentNode() instanceof DBNContainer) {
                        node = node.getParentNode();
                    }
                }
                DBNContainer container;
                if (node instanceof DBNContainer) {
                    // Try to detect child type
                    objectType = ((DBNContainer) node).getChildrenClass();
                    container = (DBNContainer) node;
                } else {
                    return false;
                }
                if (node instanceof DBSWrapper && isReadOnly(((DBSWrapper) node).getObject())) {
                    return false;
                }
                if (objectType == null) {
                    return false;
                }
                DBEObjectMaker objectMaker = getObjectManager(objectType, DBEObjectMaker.class);
                if (objectMaker == null) {
                    return false;
                }
                if (!objectMaker.canCreateObject(container.getValueObject())) {
                    return false;
                }
                /*
            if (property.equals(PROP_CAN_CREATE)) {
                return true;
            }
            // Check objects in clipboard
            Collection<DBNNode> cbNodes = TreeNodeTransfer.getFromClipboard();
            if (cbNodes == null) {
                return false;
            }
            for (DBNNode nodeObject : cbNodes) {
                if (nodeObject.isManagable() && nodeObject instanceof DBSWrapper) {
                    DBSObject pasteObject = ((DBSWrapper)nodeObject).getObject();
                    if (pasteObject == null || objectType != pasteObject.getClass()) {
                        return false;
                    }
                } else {
                    return false;
                }
            }
*/
                return true;
            }
        case PROP_CAN_DELETE:
            {
                if (node instanceof DBNDataSource || node instanceof DBNLocalFolder) {
                    return true;
                }
                if (node instanceof DBSWrapper) {
                    DBSObject object = ((DBSWrapper) node).getObject();
                    if (object == null || isReadOnly(object) || !(node.getParentNode() instanceof DBNContainer)) {
                        return false;
                    }
                    DBEObjectMaker objectMaker = getObjectManager(object.getClass(), DBEObjectMaker.class);
                    return objectMaker != null && objectMaker.canDeleteObject(object);
                } else if (node instanceof DBNResource) {
                    if ((((DBNResource) node).getFeatures() & DBPResourceHandler.FEATURE_DELETE) != 0) {
                        return true;
                    }
                }
                break;
            }
        case PROP_CAN_RENAME:
            {
                if (node.supportsRename()) {
                    return true;
                }
                if (node instanceof DBNDatabaseNode) {
                    DBSObject object = ((DBNDatabaseNode) node).getObject();
                    return object != null && !isReadOnly(object) && object.isPersisted() && node.getParentNode() instanceof DBNContainer && getObjectManager(object.getClass(), DBEObjectRenamer.class) != null;
                }
                break;
            }
        case PROP_CAN_MOVE_UP:
        case PROP_CAN_MOVE_DOWN:
            {
                if (node instanceof DBNDatabaseNode) {
                    DBSObject object = ((DBNDatabaseNode) node).getObject();
                    if (object instanceof DBPOrderedObject) {
                        DBEObjectReorderer objectReorderer = getObjectManager(object.getClass(), DBEObjectReorderer.class);
                        if (objectReorderer != null) {
                            final int position = ((DBPOrderedObject) object).getOrdinalPosition();
                            if (property.equals(PROP_CAN_MOVE_UP)) {
                                return position > objectReorderer.getMinimumOrdinalPosition(object);
                            }
                            return position < objectReorderer.getMaximumOrdinalPosition(object);
                        }
                    }
                }
                break;
            }
        case PROP_CAN_FILTER:
            {
                if (node instanceof DBNDatabaseItem) {
                    node = node.getParentNode();
                }
                if (node instanceof DBNDatabaseFolder && ((DBNDatabaseFolder) node).getItemsMeta() != null) {
                    return true;
                }
                break;
            }
        case PROP_CAN_FILTER_OBJECT:
            {
                if (node.getParentNode() instanceof DBNDatabaseFolder && ((DBNDatabaseFolder) node.getParentNode()).getItemsMeta() != null) {
                    return true;
                }
                break;
            }
        case PROP_HAS_FILTER:
            {
                if (node instanceof DBNDatabaseItem) {
                    node = node.getParentNode();
                }
                if (node instanceof DBNDatabaseFolder && ((DBNDatabaseFolder) node).getItemsMeta() != null) {
                    DBSObjectFilter filter = ((DBNDatabaseFolder) node).getNodeFilter(((DBNDatabaseFolder) node).getItemsMeta(), true);
                    if ("defined".equals(expectedValue)) {
                        return filter != null && !filter.isEmpty();
                    } else {
                        return filter != null && !filter.isNotApplicable();
                    }
                }
                break;
            }
    }
    return false;
}
Also used : DBSObjectFilter(org.jkiss.dbeaver.model.struct.DBSObjectFilter) DBSWrapper(org.jkiss.dbeaver.model.struct.DBSWrapper) DBPOrderedObject(org.jkiss.dbeaver.model.DBPOrderedObject) DBEObjectRenamer(org.jkiss.dbeaver.model.edit.DBEObjectRenamer) DBEObjectMaker(org.jkiss.dbeaver.model.edit.DBEObjectMaker) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBEObjectReorderer(org.jkiss.dbeaver.model.edit.DBEObjectReorderer) Display(org.eclipse.swt.widgets.Display)

Example 4 with DBSWrapper

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

the class NavigatorAdapterFactory method getAdapter.

@Override
public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
    if (adapterType == DBPDataSourceContainer.class) {
        if (adaptableObject instanceof DBNDataSource) {
            return adapterType.cast(((DBNDataSource) adaptableObject).getDataSourceContainer());
        }
        DBSObject object = DBUtils.getFromObject(adaptableObject);
        if (object == null) {
            return null;
        }
        if (object instanceof DBPDataSourceContainer) {
            return adapterType.cast(object);
        }
        DBPDataSource dataSource = object.getDataSource();
        return dataSource == null ? null : adapterType.cast(dataSource.getContainer());
    } else if (DBPObject.class.isAssignableFrom(adapterType)) {
        DBPObject object = null;
        if (adaptableObject instanceof DBSWrapper) {
            object = ((DBSWrapper) adaptableObject).getObject();
        } else if (adaptableObject instanceof DBPObject) {
            object = (DBPObject) adaptableObject;
        }
        if (object != null && adapterType.isAssignableFrom(object.getClass())) {
            return adapterType.cast(object);
        }
    } else if (IResource.class.isAssignableFrom(adapterType)) {
        if (adaptableObject instanceof DBNResource) {
            return ((DBNResource) adaptableObject).getAdapter(adapterType);
        }
    } else if (adapterType == IPropertySource.class) {
        DBPObject dbObject = null;
        if (adaptableObject instanceof DBSWrapper) {
            dbObject = ((DBSWrapper) adaptableObject).getObject();
        } else if (adaptableObject instanceof DBPObject) {
            dbObject = (DBPObject) adaptableObject;
        }
        if (dbObject instanceof IPropertySource) {
            return adapterType.cast(dbObject);
        }
        if (dbObject instanceof DBPPropertySource) {
            return adapterType.cast(new PropertySourceDelegate((DBPPropertySource) dbObject));
        }
        if (dbObject instanceof IAdaptable) {
            Object adapter = ((IAdaptable) dbObject).getAdapter(IPropertySource.class);
            if (adapter != null) {
                return adapterType.cast(adapter);
            }
            adapter = ((IAdaptable) dbObject).getAdapter(DBPPropertySource.class);
            if (adapter != null) {
                return adapterType.cast(new PropertySourceDelegate((DBPPropertySource) adapter));
            }
        }
        if (dbObject != null) {
            PropertyCollector props = new PropertyCollector(adaptableObject, dbObject, true);
            props.collectProperties();
            if (props.isEmpty() && adaptableObject instanceof DBSObject) {
                // Add default properties
                DBSObject meta = (DBSObject) adaptableObject;
                //$NON-NLS-1$
                props.addProperty(null, DBConstants.PROP_ID_NAME, CoreMessages.model_navigator_Name, meta.getName());
                //$NON-NLS-1$
                props.addProperty(null, "desc", CoreMessages.model_navigator_Description, meta.getDescription());
            }
            return adapterType.cast(new PropertySourceDelegate(props));
        }
    } else if (adapterType == IWorkbenchAdapter.class) {
        // Workbench adapter
        if (adaptableObject instanceof DBNNode) {
            final DBNNode node = (DBNNode) adaptableObject;
            WorkbenchAdapter workbenchAdapter = new WorkbenchAdapter() {

                @Override
                public ImageDescriptor getImageDescriptor(Object object) {
                    return DBeaverIcons.getImageDescriptor(node.getNodeIconDefault());
                }

                @Override
                public String getLabel(Object o) {
                    return node.getNodeName();
                }

                @Override
                public Object getParent(Object o) {
                    return node.getParentNode();
                }
            };
            return adapterType.cast(workbenchAdapter);
        } else {
            return null;
        }
    }
    return null;
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) PropertySourceDelegate(org.jkiss.dbeaver.ui.properties.PropertySourceDelegate) DBSWrapper(org.jkiss.dbeaver.model.struct.DBSWrapper) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DBNDataSource(org.jkiss.dbeaver.model.navigator.DBNDataSource) DBPPropertySource(org.jkiss.dbeaver.model.preferences.DBPPropertySource) PropertyCollector(org.jkiss.dbeaver.runtime.properties.PropertyCollector) IPropertySource(org.eclipse.ui.views.properties.IPropertySource) WorkbenchAdapter(org.eclipse.ui.model.WorkbenchAdapter) IWorkbenchAdapter(org.eclipse.ui.model.IWorkbenchAdapter) DBNResource(org.jkiss.dbeaver.model.navigator.DBNResource) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject)

Example 5 with DBSWrapper

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

the class NavigatorUtils method isDefaultElement.

public static boolean isDefaultElement(Object element) {
    if (element instanceof DBSWrapper) {
        DBSObject object = ((DBSWrapper) element).getObject();
        DBSObjectSelector activeContainer = DBUtils.getParentAdapter(DBSObjectSelector.class, object);
        if (activeContainer != null) {
            return activeContainer.getDefaultObject() == object;
        }
    } else if (element instanceof DBNProject) {
        if (((DBNProject) element).getProject() == DBeaverCore.getInstance().getProjectRegistry().getActiveProject()) {
            return true;
        }
    }
    return false;
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBSWrapper(org.jkiss.dbeaver.model.struct.DBSWrapper) DBSObjectSelector(org.jkiss.dbeaver.model.struct.DBSObjectSelector)

Aggregations

DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)5 DBSWrapper (org.jkiss.dbeaver.model.struct.DBSWrapper)5 Iterator (java.util.Iterator)1 IAdaptable (org.eclipse.core.runtime.IAdaptable)1 ISelection (org.eclipse.jface.viewers.ISelection)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 GridData (org.eclipse.swt.layout.GridData)1 Composite (org.eclipse.swt.widgets.Composite)1 Display (org.eclipse.swt.widgets.Display)1 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)1 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)1 IWorkbenchAdapter (org.eclipse.ui.model.IWorkbenchAdapter)1 WorkbenchAdapter (org.eclipse.ui.model.WorkbenchAdapter)1 IPropertySource (org.eclipse.ui.views.properties.IPropertySource)1 DBPContextProvider (org.jkiss.dbeaver.model.DBPContextProvider)1 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)1 DBPOrderedObject (org.jkiss.dbeaver.model.DBPOrderedObject)1 DBEObjectMaker (org.jkiss.dbeaver.model.edit.DBEObjectMaker)1 DBEObjectRenamer (org.jkiss.dbeaver.model.edit.DBEObjectRenamer)1 DBEObjectReorderer (org.jkiss.dbeaver.model.edit.DBEObjectReorderer)1