use of org.jkiss.dbeaver.model.DBPOrderedObject 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:
case PROP_CAN_PASTE:
{
if (node instanceof DBNResource) {
return property.equals(PROP_CAN_PASTE);
}
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 (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;
}
use of org.jkiss.dbeaver.model.DBPOrderedObject in project dbeaver by serge-rider.
the class NavigatorHandlerObjectMove method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
final ISelection selection = HandlerUtil.getCurrentSelection(event);
DBNNode node = NavigatorUtils.getSelectedNode(selection);
if (node == null || !(node.getParentNode() instanceof DBNContainer)) {
return null;
}
DBSObject object = ((DBNDatabaseNode) node).getObject();
if (!(object instanceof DBPOrderedObject)) {
return null;
}
@SuppressWarnings("unchecked") DBEObjectReorderer<DBSObject> objectReorderer = DBWorkbench.getPlatform().getEditorsRegistry().getObjectManager(object.getClass(), DBEObjectReorderer.class);
if (objectReorderer == null) {
return null;
}
DBPOrderedObject orderedObject = (DBPOrderedObject) object;
try {
// Sibling objects - they are involved in reordering process
List<DBSObject> siblingObjects = new ArrayList<>();
for (DBNNode siblingNode : node.getParentNode().getChildren(new VoidProgressMonitor())) {
if (siblingNode instanceof DBNDatabaseNode) {
DBSObject siblingObject = ((DBNDatabaseNode) siblingNode).getObject();
if (siblingObject.getClass() != object.getClass()) {
log.warn("Sibling object class " + siblingObject.getClass() + " differs from moving object class " + object.getClass().getName());
} else {
siblingObjects.add(siblingObject);
}
} else {
log.warn("Wrong sibling node type: " + siblingNode);
}
}
CommandTarget commandTarget = getCommandTarget(HandlerUtil.getActiveWorkbenchWindow(event), node.getParentNode(), object.getClass(), false);
String actionId = event.getCommand().getId();
switch(actionId) {
case NavigatorCommands.CMD_OBJECT_MOVE_UP:
objectReorderer.setObjectOrdinalPosition(commandTarget.getContext(), object, siblingObjects, orderedObject.getOrdinalPosition() - 1);
break;
case NavigatorCommands.CMD_OBJECT_MOVE_DOWN:
objectReorderer.setObjectOrdinalPosition(commandTarget.getContext(), object, siblingObjects, orderedObject.getOrdinalPosition() + 1);
break;
}
if (object.isPersisted() && commandTarget.getEditor() == null) {
Map<String, Object> options = DBPScriptObject.EMPTY_OPTIONS;
if (!showScript(HandlerUtil.getActiveWorkbenchWindow(event), commandTarget.getContext(), options, "Reorder script")) {
commandTarget.getContext().resetChanges(true);
return false;
} else {
ObjectSaver orderer = new ObjectSaver(commandTarget.getContext(), options);
TasksJob.runTask("Change object '" + object.getName() + "' position", orderer);
}
}
} catch (DBException e) {
DBWorkbench.getPlatformUI().showError("Object move", "Error during object reposition", e);
}
return null;
}
use of org.jkiss.dbeaver.model.DBPOrderedObject in project dbeaver by serge-rider.
the class NavigatorHandlerObjectMove method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
final ISelection selection = HandlerUtil.getCurrentSelection(event);
DBNNode node = NavigatorUtils.getSelectedNode(selection);
if (node == null || !(node.getParentNode() instanceof DBNContainer)) {
return null;
}
DBNContainer containerNode = (DBNContainer) node.getParentNode();
DBSObject object = ((DBNDatabaseNode) node).getObject();
if (!(object instanceof DBPOrderedObject)) {
return null;
}
@SuppressWarnings("unchecked") DBEObjectReorderer<DBSObject> objectReorderer = EntityEditorsRegistry.getInstance().getObjectManager(object.getClass(), DBEObjectReorderer.class);
if (objectReorderer == null) {
return null;
}
DBPOrderedObject orderedObject = (DBPOrderedObject) object;
try {
// Sibling objects - they are involved in reordering process
List<DBSObject> siblingObjects = new ArrayList<>();
for (DBNNode siblingNode : node.getParentNode().getChildren(VoidProgressMonitor.INSTANCE)) {
if (siblingNode instanceof DBNDatabaseNode) {
DBSObject siblingObject = ((DBNDatabaseNode) siblingNode).getObject();
if (siblingObject.getClass() != object.getClass()) {
log.warn("Sibling object class " + siblingObject.getClass() + " differs from moving object class " + object.getClass().getName());
} else {
siblingObjects.add(siblingObject);
}
} else {
log.warn("Wrong sibling node type: " + siblingNode);
}
}
CommandTarget commandTarget = getCommandTarget(HandlerUtil.getActiveWorkbenchWindow(event), containerNode, object.getClass(), false);
String actionId = event.getCommand().getId();
switch(actionId) {
case CoreCommands.CMD_OBJECT_MOVE_UP:
objectReorderer.setObjectOrdinalPosition(commandTarget.getContext(), object, siblingObjects, orderedObject.getOrdinalPosition() - 1);
break;
case CoreCommands.CMD_OBJECT_MOVE_DOWN:
objectReorderer.setObjectOrdinalPosition(commandTarget.getContext(), object, siblingObjects, orderedObject.getOrdinalPosition() + 1);
break;
}
if (object.isPersisted() && commandTarget.getEditor() == null) {
if (!showScript(HandlerUtil.getActiveWorkbenchWindow(event), commandTarget.getContext(), "Reorder script")) {
commandTarget.getContext().resetChanges();
return false;
} else {
ObjectSaver orderer = new ObjectSaver(commandTarget.getContext());
TasksJob.runTask("Change object '" + object.getName() + "' position", orderer);
}
}
} catch (DBException e) {
UIUtils.showErrorDialog(HandlerUtil.getActiveShell(event), "Object move", "Error during object reposition", e);
}
return null;
}
use of org.jkiss.dbeaver.model.DBPOrderedObject in project dbeaver by dbeaver.
the class NavigatorHandlerObjectMove method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
final ISelection selection = HandlerUtil.getCurrentSelection(event);
DBNNode node = NavigatorUtils.getSelectedNode(selection);
if (node == null || !(node.getParentNode() instanceof DBNContainer)) {
return null;
}
DBNContainer containerNode = (DBNContainer) node.getParentNode();
DBSObject object = ((DBNDatabaseNode) node).getObject();
if (!(object instanceof DBPOrderedObject)) {
return null;
}
@SuppressWarnings("unchecked") DBEObjectReorderer<DBSObject> objectReorderer = EntityEditorsRegistry.getInstance().getObjectManager(object.getClass(), DBEObjectReorderer.class);
if (objectReorderer == null) {
return null;
}
DBPOrderedObject orderedObject = (DBPOrderedObject) object;
try {
// Sibling objects - they are involved in reordering process
List<DBSObject> siblingObjects = new ArrayList<>();
for (DBNNode siblingNode : node.getParentNode().getChildren(new VoidProgressMonitor())) {
if (siblingNode instanceof DBNDatabaseNode) {
DBSObject siblingObject = ((DBNDatabaseNode) siblingNode).getObject();
if (siblingObject.getClass() != object.getClass()) {
log.warn("Sibling object class " + siblingObject.getClass() + " differs from moving object class " + object.getClass().getName());
} else {
siblingObjects.add(siblingObject);
}
} else {
log.warn("Wrong sibling node type: " + siblingNode);
}
}
CommandTarget commandTarget = getCommandTarget(HandlerUtil.getActiveWorkbenchWindow(event), containerNode, object.getClass(), false);
String actionId = event.getCommand().getId();
switch(actionId) {
case CoreCommands.CMD_OBJECT_MOVE_UP:
objectReorderer.setObjectOrdinalPosition(commandTarget.getContext(), object, siblingObjects, orderedObject.getOrdinalPosition() - 1);
break;
case CoreCommands.CMD_OBJECT_MOVE_DOWN:
objectReorderer.setObjectOrdinalPosition(commandTarget.getContext(), object, siblingObjects, orderedObject.getOrdinalPosition() + 1);
break;
}
if (object.isPersisted() && commandTarget.getEditor() == null) {
Map<String, Object> options = DBPScriptObject.EMPTY_OPTIONS;
if (!showScript(HandlerUtil.getActiveWorkbenchWindow(event), commandTarget.getContext(), options, "Reorder script")) {
commandTarget.getContext().resetChanges();
return false;
} else {
ObjectSaver orderer = new ObjectSaver(commandTarget.getContext(), options);
TasksJob.runTask("Change object '" + object.getName() + "' position", orderer);
}
}
} catch (DBException e) {
DBUserInterface.getInstance().showError("Object move", "Error during object reposition", e);
}
return null;
}
use of org.jkiss.dbeaver.model.DBPOrderedObject 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;
}
Aggregations