提交 f14a786a 编写于 作者: S Serge Rider 提交者: GitHub

Merge pull request #12212 from dbeaver/metadata-move-multiple-columns-consecutive-fix#11331

#11331 Non-consecutive objects move fix

Former-commit-id: 176df775
......@@ -3965,7 +3965,7 @@ public class ResultSetViewer extends Viewer
// No rows selected, use zero as the only row number
partitionedSelectedRows = new int[][]{new int[]{0, 0}};
} else {
partitionedSelectedRows = groupContiguousRows(
partitionedSelectedRows = groupConsecutiveRows(
selectedRows.stream()
.mapToInt(ResultSetRow::getVisualNumber)
.toArray()
......@@ -4090,7 +4090,7 @@ public class ResultSetViewer extends Viewer
* @return grouped indexes
*/
@NotNull
private static int[][] groupContiguousRows(@NotNull int[] indexes) {
private static int[][] groupConsecutiveRows(@NotNull int[] indexes) {
final List<int[]> ranges = new ArrayList<>();
for (int index = 1, start = 0, length = indexes.length; index <= length; index++) {
if (index == length || indexes[index - 1] != indexes[index] - 1) {
......
......@@ -20,6 +20,7 @@ import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.handlers.HandlerUtil;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBPOrderedObject;
......@@ -46,80 +47,115 @@ public class NavigatorHandlerObjectMove extends NavigatorHandlerObjectBase {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
final ISelection selection = HandlerUtil.getCurrentSelection(event);
final List<DBNNode> nodes = NavigatorUtils.getSelectedNodes(selection);
final DBNNode[][] nodes = groupConsecutiveNodes(NavigatorUtils.getSelectedNodes(selection));
for (DBNNode node : nodes) {
if (!(node.getParentNode() instanceof DBNContainer)) {
return null;
}
DBSObject object = ((DBNDatabaseNode) node).getObject();
if (!(object instanceof DBPOrderedObject)) {
return null;
}
for (DBNNode[] partition : nodes) {
for (DBNNode node : partition) {
if (!(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());
@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 {
siblingObjects.add(siblingObject);
log.warn("Wrong sibling node type: " + siblingNode);
}
} else {
log.warn("Wrong sibling node type: " + siblingNode);
}
}
CommandTarget commandTarget = getCommandTarget(
HandlerUtil.getActiveWorkbenchWindow(event),
node.getParentNode(),
object.getClass(),
false);
CommandTarget commandTarget = getCommandTarget(
HandlerUtil.getActiveWorkbenchWindow(event),
node.getParentNode(),
object.getClass(),
false);
switch (event.getCommand().getId()) {
case NavigatorCommands.CMD_OBJECT_MOVE_UP:
objectReorderer.setObjectOrdinalPosition(
commandTarget.getContext(),
object,
siblingObjects,
orderedObject.getOrdinalPosition() - 1);
break;
case NavigatorCommands.CMD_OBJECT_MOVE_DOWN:
// Need to take in account total amount of moved objects to avoid overlapping
objectReorderer.setObjectOrdinalPosition(
commandTarget.getContext(),
object,
siblingObjects,
orderedObject.getOrdinalPosition() + nodes.size());
break;
default:
break;
}
switch (event.getCommand().getId()) {
case NavigatorCommands.CMD_OBJECT_MOVE_UP:
objectReorderer.setObjectOrdinalPosition(
commandTarget.getContext(),
object,
siblingObjects,
orderedObject.getOrdinalPosition() - 1);
break;
case NavigatorCommands.CMD_OBJECT_MOVE_DOWN:
// Need to take in account total amount of moved objects to avoid overlapping
objectReorderer.setObjectOrdinalPosition(
commandTarget.getContext(),
object,
siblingObjects,
orderedObject.getOrdinalPosition() + partition.length);
break;
default:
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);
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;
}
} catch (DBException e) {
DBWorkbench.getPlatformUI().showError("Object move", "Error during object reposition", e);
return null;
}
}
return null;
}
/**
* Performs grouping of a consecutive nodes.
*
* @param nodes nodes to group
* @return an array of arrays containing consecutive nodes
*/
@NotNull
private static DBNNode[][] groupConsecutiveNodes(@NotNull List<DBNNode> nodes) {
final List<DBNNode[]> ranges = new ArrayList<>();
final List<DBNNode> range = new ArrayList<>();
for (int index = 1, length = nodes.size(); index <= length; index++) {
range.add(nodes.get(index - 1));
if (index == length || getNodePosition(nodes.get(index - 1)) != getNodePosition(nodes.get(index)) - 1) {
ranges.add(range.toArray(new DBNNode[0]));
range.clear();
}
}
return ranges.toArray(new DBNNode[0][]);
}
private static int getNodePosition(@NotNull DBNNode node) {
if (!(node instanceof DBNDatabaseNode)) {
return -1;
}
final DBSObject object = ((DBNDatabaseNode) node).getObject();
if (!(object instanceof DBPOrderedObject)) {
return -1;
}
return ((DBPOrderedObject) object).getOrdinalPosition();
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册