Skip to content

Commit

Permalink
fix bugs, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan-nikitko committed Mar 3, 2023
1 parent 163895b commit 4f85827
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.cayenne.modeler.action.dbimport.AddIncludeTableAction;
import org.apache.cayenne.modeler.action.dbimport.AddSchemaAction;
import org.apache.cayenne.modeler.action.dbimport.DeleteNodeAction;
import org.apache.cayenne.modeler.action.dbimport.DragAndDropNodeAction;
import org.apache.cayenne.modeler.action.dbimport.EditNodeAction;
import org.apache.cayenne.modeler.action.dbimport.MoveImportNodeAction;
import org.apache.cayenne.modeler.action.dbimport.MoveInvertNodeAction;
Expand Down Expand Up @@ -124,6 +125,7 @@ public DefaultActionManager(@Inject Application application, @Inject Configurati
registerAction(new EditNodeAction(application)).setAlwaysOn(true);
registerAction(new DeleteNodeAction(application)).setAlwaysOn(true);
registerAction(new MoveImportNodeAction(application)).setAlwaysOn(true);
registerAction(new DragAndDropNodeAction(application)).setAlwaysOn(true);
registerAction(new LoadDbSchemaAction(application)).setAlwaysOn(true);
registerAction(new SortNodesAction(application)).setAlwaysOn(true);
registerAction(new MoveInvertNodeAction(application)).setAlwaysOn(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
****************************************************************/

package org.apache.cayenne.modeler.action.dbimport;

import org.apache.cayenne.dbsync.reverse.dbimport.ReverseEngineering;
import org.apache.cayenne.modeler.Application;
import org.apache.cayenne.modeler.dialog.db.load.DbImportTreeNode;
import org.apache.cayenne.modeler.editor.dbimport.DbImportModel;
import org.apache.cayenne.modeler.editor.dbimport.DbImportSorter;

import javax.swing.JTree;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Collections;

/**
* @since 5.0
*/
public class DragAndDropNodeAction extends TreeManipulationAction {

private static final String ACTION_NAME = "DragAndDrop";
private DbImportTreeNode[] nodes;
private DbImportTreeNode dropLocationParentNode;
private DbImportTreeNode sourceParentNode;
private JTree.DropLocation dropLocation;

public DragAndDropNodeAction(Application application) {
super(ACTION_NAME, application);
}

@Override
public void performAction(ActionEvent e) {
DbImportModel model = (DbImportModel) tree.getModel();
ReverseEngineering reverseEngineeringOldCopy = new ReverseEngineering(tree.getReverseEngineering());

for (DbImportTreeNode node : nodes) {
if (checkDropPossibility(node)) {
int index = calculateDropIndex();
model.insertNodeInto(node, dropLocationParentNode, index);
}
}
getProjectController().setDirty(true);
DbImportSorter.syncUserObjectItems(dropLocationParentNode);
DbImportSorter.syncUserObjectItems(sourceParentNode);
putReverseEngineeringToUndoManager(reverseEngineeringOldCopy);
model.reload(dropLocationParentNode);
tree.expandTree(new ArrayList<>(Collections.singletonList(dropLocationParentNode)));
}

private int calculateDropIndex() {
int index = dropLocation.getChildIndex();
//check is our node moving inside a one node
if (sourceParentNode == dropLocationParentNode) {

int childCount = dropLocationParentNode.getChildCount();
int childIndex = dropLocation.getChildIndex();
if (childIndex == childCount) {
index = childCount - 1;
}
}
//If target node is collapsed
if (index == -1) {
index = dropLocationParentNode.getChildCount();
}
return index;
}

private boolean checkDropPossibility(DbImportTreeNode node) {
// Don't allow a node to be dropped onto itself
if (node == dropLocationParentNode) {
return false;
}
// Don't allow a node to be dropped onto one of its descendants
for (DbImportTreeNode childNode : node.getChildNodes()) {
if (isNodeAncestor(childNode, dropLocationParentNode)) {
return false;
}
}
return true;
}

private boolean isNodeAncestor(DbImportTreeNode node1, DbImportTreeNode node2) {
if (node2 == null) {
return false;
}
if (node2.getParent() == node1) {
return true;
}
return isNodeAncestor(node1, node2.getParent());
}

public void setNodes(DbImportTreeNode[] nodes) {
this.nodes = nodes;
}

public void setDropLocationParentNode(DbImportTreeNode dropLocationParentNode) {
this.dropLocationParentNode = dropLocationParentNode;
}

public void setSourceParentNode(DbImportTreeNode sourceParentNode) {
this.sourceParentNode = sourceParentNode;
}

public void setDropLocation(JTree.DropLocation dropLocation) {
this.dropLocation = dropLocation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.cayenne.modeler.action.dbimport.AddIncludeProcedureAction;
import org.apache.cayenne.modeler.action.dbimport.AddIncludeTableAction;
import org.apache.cayenne.modeler.action.dbimport.AddSchemaAction;
import org.apache.cayenne.modeler.action.dbimport.DragAndDropNodeAction;
import org.apache.cayenne.modeler.action.dbimport.MoveImportNodeAction;
import org.apache.cayenne.modeler.action.dbimport.MoveInvertNodeAction;
import org.apache.cayenne.modeler.action.dbimport.TreeManipulationAction;
Expand Down Expand Up @@ -360,7 +361,6 @@ public void valueChanged(TreeSelectionEvent e) {

private class TargetTreeTransferHandler extends TransferHandler {
private DbImportTreeNode sourceParentNode;
DbImportModel model = (DbImportModel) targetTree.getModel();

@Override
protected Transferable createTransferable(JComponent c) {
Expand Down Expand Up @@ -398,6 +398,7 @@ public Object getTransferData(DataFlavor flavor) {
@Override
protected void exportDone(JComponent source, Transferable data, int action) {
if (importSourceTree == ImportSourceTree.TARGET_TREE && sourceParentNode != null) {
DbImportModel model = (DbImportModel) targetTree.getModel();
model.reload(sourceParentNode);
}
}
Expand All @@ -409,6 +410,18 @@ public int getSourceActions(JComponent c) {

@Override
public boolean canImport(TransferSupport support) {
JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
DbImportTreeNode dropLocationParentNode = (DbImportTreeNode) dropLocation.getPath().getLastPathComponent();

List<Class<?>> allowedItemsList = insertableLevels.get(dropLocationParentNode.getUserObject().getClass());
DbImportTreeNode[] nodes = getNodesFromSupport(support);
if (nodes != null && allowedItemsList != null) {
for (DbImportTreeNode node : nodes) {
if (!allowedItemsList.contains(node.getUserObject().getClass())) {
return false;
}
}
}
return support.isDrop();
}

Expand All @@ -430,16 +443,8 @@ private boolean importDataFromSourceTree(TransferSupport support) {
if (!canBeMoved()) {
return false;
}
Transferable transferable = support.getTransferable();
DbImportTreeNode[] transferData = null;
try {
for (DataFlavor dataFlavor : transferable.getTransferDataFlavors()) {
transferData = (DbImportTreeNode[]) transferable.getTransferData(dataFlavor);
}
} catch (IOException | UnsupportedFlavorException e) {
return false;
}
if (transferData != null) {
DbImportTreeNode[] nodes = getNodesFromSupport(support);
if (nodes != null) {
MoveImportNodeAction action = projectController.getApplication().getActionManager()
.getAction(MoveImportNodeAction.class);
action.setSourceTree(sourceTree);
Expand All @@ -453,65 +458,37 @@ private boolean importDataFromSourceTree(TransferSupport support) {

private boolean importDataFromTargetTree(TransferSupport support) {
JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
TreePath dropLocationPath = dropLocation.getPath();

DbImportTreeNode dropLocationParentNode = (DbImportTreeNode) dropLocationPath.getLastPathComponent();
Transferable transferable = support.getTransferable();

int dropIndex = dropLocation.getChildIndex();
if (dropIndex == -1) {
dropIndex = dropLocationParentNode.getChildCount();
DbImportTreeNode dropLocationParentNode = (DbImportTreeNode) dropLocation.getPath().getLastPathComponent();

DbImportTreeNode[] nodes = getNodesFromSupport(support);
if (nodes != null) {
DragAndDropNodeAction action = projectController.getApplication().getActionManager()
.getAction(DragAndDropNodeAction.class);
action.setDropLocationParentNode(dropLocationParentNode);
action.setSourceParentNode(sourceParentNode);
action.setDropLocation(dropLocation);
action.setNodes(nodes);
action.setTree(targetTree);
action.performAction(null);
return true;
}
return false;
}

private DbImportTreeNode[] getNodesFromSupport(TransferSupport support) {
Transferable transferable = support.getTransferable();
DbImportTreeNode[] nodes = null;
try {
for (DataFlavor dataFlavor : transferable.getTransferDataFlavors()) {
nodes = (DbImportTreeNode[]) transferable.getTransferData(dataFlavor);
}
if (nodes != null) {
for (DbImportTreeNode node : nodes) {
if (checkDropPossibility(dropLocationParentNode, node)) return false;
model.insertNodeInto(node, dropLocationParentNode, dropIndex++);
}
targetTree.expandPath(dropLocationPath);
DbImportSorter.syncUserObjectItems(dropLocationParentNode);
model.reload(dropLocationParentNode);
projectController.setDirty(true);
return true;
}
} catch (IOException | UnsupportedFlavorException e) {
return false;
}
return false;
}

private boolean checkDropPossibility(DbImportTreeNode dropLocationParentNode, DbImportTreeNode node) {
// Don't allow a node to be dropped onto itself
if (node == dropLocationParentNode) {
return true;
}
// Don't allow a node to be dropped onto one of its descendants
for (DbImportTreeNode childNode : node.getChildNodes()) {
if (isNodeAncestor(childNode, dropLocationParentNode)) {
return true;
}
}
return false;
}


private boolean isNodeAncestor(DbImportTreeNode node1, DbImportTreeNode node2) {
if (node2 == null) {
return false;
return null;
}
if (node2.getParent() == node1) {
return true;
}
return isNodeAncestor(node1, node2.getParent());
return nodes;
}
}


private class SourceTreeSelectionListener implements TreeSelectionListener {
@Override
public void valueChanged(TreeSelectionEvent e) {
Expand Down

0 comments on commit 4f85827

Please sign in to comment.