use of org.jkiss.dbeaver.model.edit.DBEObjectMaker in project dbeaver by dbeaver.
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_SINGLE:
{
return canCreateObject(node, true);
}
case PROP_CAN_CREATE_MULTI:
{
return canCreateObject(node, false);
}
case PROP_CAN_PASTE:
{
/*
Clipboard clipboard = new Clipboard(display);
try {
if (clipboard.getContents(TreeNodeTransfer.getInstance()) == null) {
return false;
}
} finally {
clipboard.dispose();
}
*/
if (node instanceof DBNResource) {
return property.equals(PROP_CAN_PASTE);
}
return canCreateObject(node, null);
// Do not check PASTE command state. It requires clipboard contents check
// which means UI interaction which can break menu popup [RedHat]
// and also is a slow operation. So let paste be always enabled.
/*
// 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;
}
}
*/
}
case PROP_CAN_DELETE:
{
if (node instanceof DBNDataSource || node instanceof DBNLocalFolder) {
return true;
}
if (DBNUtils.isReadOnly(node)) {
return false;
}
if (node instanceof DBSWrapper) {
DBSObject object = ((DBSWrapper) node).getObject();
if (object == null || DBUtils.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) {
if (DBNUtils.isReadOnly(node)) {
return false;
}
DBSObject object = ((DBNDatabaseNode) node).getObject();
return object != null && !DBUtils.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) {
if (DBNUtils.isReadOnly(node)) {
return false;
}
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;
}
use of org.jkiss.dbeaver.model.edit.DBEObjectMaker in project dbeaver by serge-rider.
the class NavigatorHandlerObjectCreateBase method createNewObject.
protected boolean createNewObject(final IWorkbenchWindow workbenchWindow, DBNNode element, DBNDatabaseNode copyFrom) {
try {
DBNContainer container = null;
if (element instanceof DBNContainer) {
container = (DBNContainer) element;
} else {
DBNNode parentNode = element.getParentNode();
if (parentNode instanceof DBNContainer) {
container = (DBNContainer) parentNode;
}
}
if (container == null) {
throw new DBException("Can't detect container for '" + element.getNodeName() + "'");
}
Class<?> childType = container.getChildrenClass();
if (childType == null) {
throw new DBException("Can't determine child element type for container '" + container + "'");
}
if (childType == IProject.class) {
return NavigatorHandlerProjectCreate.createNewProject(workbenchWindow);
}
DBSObject sourceObject = copyFrom == null ? null : copyFrom.getObject();
// Do not check for type - manager must do it. Potentially we can copy anything into anything.
// if (sourceObject != null && !childType.isAssignableFrom(sourceObject.getClass())) {
// throw new DBException("Can't create '" + childType.getName() + "' from '" + sourceObject.getClass().getName() + "'");
// }
final EntityEditorsRegistry editorsRegistry = EntityEditorsRegistry.getInstance();
DBEObjectManager<?> objectManager = editorsRegistry.getObjectManager(childType);
if (objectManager == null) {
throw new DBException("Object manager not found for type '" + childType.getName() + "'");
}
DBEObjectMaker objectMaker = (DBEObjectMaker) objectManager;
final boolean openEditor = (objectMaker.getMakerOptions() & DBEObjectMaker.FEATURE_EDITOR_ON_CREATE) != 0;
CommandTarget commandTarget = getCommandTarget(workbenchWindow, container, childType, openEditor);
final Object parentObject = container.getValueObject();
createDatabaseObject(commandTarget, objectMaker, parentObject, sourceObject);
} catch (Throwable e) {
UIUtils.showErrorDialog(workbenchWindow.getShell(), "Create object", null, e);
return false;
}
return true;
}
use of org.jkiss.dbeaver.model.edit.DBEObjectMaker in project dbeaver by serge-rider.
the class NavigatorHandlerObjectDelete method deleteObject.
private boolean deleteObject(IWorkbenchWindow workbenchWindow, DBNDatabaseNode node) {
try {
if (!(node.getParentNode() instanceof DBNContainer)) {
throw new DBException("Node '" + node + "' doesn't have a container");
}
final DBNContainer container = (DBNContainer) node.getParentNode();
// Try to delete object using object manager
DBSObject object = node.getObject();
if (object == null) {
throw new DBException("Can't delete node with null object");
}
DBEObjectMaker objectMaker = EntityEditorsRegistry.getInstance().getObjectManager(object.getClass(), DBEObjectMaker.class);
if (objectMaker == null) {
//$NON-NLS-2$
throw new DBException("Object maker not found for type '" + object.getClass().getName() + "'");
}
Map<String, Object> deleteOptions = null;
CommandTarget commandTarget = getCommandTarget(workbenchWindow, container, object.getClass(), false);
ConfirmResult confirmResult = ConfirmResult.YES;
if (!object.isPersisted() || commandTarget.getEditor() != null) {
// There should be command context somewhere
if (deleteNewObject(workbenchWindow, node)) {
return true;
}
// No direct editor host found for this object -
// try to find corresponding command context
// and execute command within it
} else {
// Persisted object - confirm delete
// Show "View script" only if we are not in some editor (because it have its own "View script" button)
confirmResult = confirmObjectDelete(workbenchWindow, node, commandTarget.getContext() != null && commandTarget.getEditor() == null);
if (confirmResult == ConfirmResult.NO) {
return false;
}
}
objectMaker.deleteObject(commandTarget.getContext(), node.getObject(), deleteOptions);
if (confirmResult == ConfirmResult.DETAILS) {
if (!showScript(workbenchWindow, commandTarget.getContext(), CoreMessages.actions_navigator_delete_script)) {
commandTarget.getContext().resetChanges();
return false;
}
}
if (commandTarget.getEditor() == null && commandTarget.getContext() != null) {
// Persist object deletion - only if there is no host editor and we have a command context
ObjectSaver deleter = new ObjectSaver(commandTarget.getContext());
// DBeaverUI.runInProgressDialog(deleter);
tasksToExecute.add(deleter);
}
} catch (Throwable e) {
UIUtils.showErrorDialog(workbenchWindow.getShell(), CoreMessages.actions_navigator_error_dialog_delete_object_title, "Can't delete object '" + node.getNodeName() + "'", e);
return false;
}
return true;
}
use of org.jkiss.dbeaver.model.edit.DBEObjectMaker 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_SINGLE:
{
return canCreateObject(node, true);
}
case PROP_CAN_CREATE_MULTI:
{
return canCreateObject(node, false);
}
case PROP_CAN_PASTE:
{
/*
Clipboard clipboard = new Clipboard(display);
try {
if (clipboard.getContents(TreeNodeTransfer.getInstance()) == null) {
return false;
}
} finally {
clipboard.dispose();
}
*/
if (node instanceof DBNResource) {
return property.equals(PROP_CAN_PASTE);
}
return canCreateObject(node, null);
// Do not check PASTE command state. It requires clipboard contents check
// which means UI interaction which can break menu popup [RedHat]
// and also is a slow operation. So let paste be always enabled.
/*
// 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;
}
}
*/
}
case PROP_CAN_DELETE:
{
if (node instanceof DBNDataSource || node instanceof DBNLocalFolder) {
return true;
}
if (DBNUtils.isReadOnly(node)) {
return false;
}
if (node instanceof DBSWrapper) {
DBSObject object = ((DBSWrapper) node).getObject();
if (object == null || DBUtils.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) {
if (DBNUtils.isReadOnly(node)) {
return false;
}
DBSObject object = ((DBNDatabaseNode) node).getObject();
return object != null && !DBUtils.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) {
if (DBNUtils.isReadOnly(node)) {
return false;
}
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;
}
use of org.jkiss.dbeaver.model.edit.DBEObjectMaker in project dbeaver by serge-rider.
the class ObjectPropertyTester method canCreateObject.
public static boolean canCreateObject(DBNNode node, Boolean onlySingle) {
if (node instanceof DBNDatabaseNode) {
if (((DBNDatabaseNode) node).isVirtual()) {
// Can't create virtual objects
return false;
}
if (!(node instanceof DBNDataSource) && isMetadataChangeDisabled(((DBNDatabaseNode) node))) {
return false;
}
}
if (onlySingle == null) {
// Just try to find first create handler
if (node instanceof DBNDataSource) {
// We always can create datasource
return true;
}
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 (DBNUtils.isReadOnly(node)) {
return false;
}
if (node instanceof DBSWrapper && DBUtils.isReadOnly(((DBSWrapper) node).getObject())) {
return false;
}
if (objectType == null) {
return false;
}
DBEObjectMaker objectMaker = getObjectManager(objectType, DBEObjectMaker.class);
if (objectMaker == null) {
return false;
}
return objectMaker.canCreateObject(container.getValueObject());
}
if (DBNUtils.isReadOnly(node)) {
return false;
}
// Check whether only single object type can be created or multiple ones
List<IContributionItem> createItems = NavigatorHandlerObjectCreateNew.fillCreateMenuItems(null, node);
if (onlySingle) {
return createItems.size() == 1;
} else {
return createItems.size() > 1;
}
}
Aggregations