Search in sources :

Example 1 with DBEObjectReorderer

use of org.jkiss.dbeaver.model.edit.DBEObjectReorderer 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)

Aggregations

Display (org.eclipse.swt.widgets.Display)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 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)1 DBSObjectFilter (org.jkiss.dbeaver.model.struct.DBSObjectFilter)1 DBSWrapper (org.jkiss.dbeaver.model.struct.DBSWrapper)1