|
Lines 14-20
Link Here
|
| 14 |
|
14 |
|
| 15 |
package org.eclipse.wst.dtd.ui.internal.dnd; |
15 |
package org.eclipse.wst.dtd.ui.internal.dnd; |
| 16 |
|
16 |
|
|
|
17 |
import java.util.ArrayList; |
| 17 |
import java.util.Collection; |
18 |
import java.util.Collection; |
|
|
19 |
import java.util.Iterator; |
| 20 |
import java.util.List; |
| 18 |
|
21 |
|
| 19 |
import org.eclipse.wst.common.ui.internal.dnd.DragAndDropCommand; |
22 |
import org.eclipse.wst.common.ui.internal.dnd.DragAndDropCommand; |
| 20 |
import org.eclipse.wst.common.ui.internal.dnd.DragAndDropManager; |
23 |
import org.eclipse.wst.common.ui.internal.dnd.DragAndDropManager; |
|
Lines 22-27
Link Here
|
| 22 |
import org.eclipse.wst.dtd.core.internal.CMNode; |
25 |
import org.eclipse.wst.dtd.core.internal.CMNode; |
| 23 |
import org.eclipse.wst.dtd.core.internal.DTDNode; |
26 |
import org.eclipse.wst.dtd.core.internal.DTDNode; |
| 24 |
import org.eclipse.wst.dtd.core.internal.TopLevelNode; |
27 |
import org.eclipse.wst.dtd.core.internal.TopLevelNode; |
|
|
28 |
import org.w3c.dom.Node; |
| 25 |
|
29 |
|
| 26 |
public class DTDDragAndDropManager implements DragAndDropManager { |
30 |
public class DTDDragAndDropManager implements DragAndDropManager { |
| 27 |
|
31 |
|
|
Lines 29-34
Link Here
|
| 29 |
if (target instanceof DTDNode) { |
33 |
if (target instanceof DTDNode) { |
| 30 |
DTDNode node = (DTDNode) target; |
34 |
DTDNode node = (DTDNode) target; |
| 31 |
|
35 |
|
|
|
36 |
source = mergeSource(source); |
| 32 |
if (node instanceof TopLevelNode) { |
37 |
if (node instanceof TopLevelNode) { |
| 33 |
return new DragTopLevelNodesCommand(target, location, operations, operation, source); |
38 |
return new DragTopLevelNodesCommand(target, location, operations, operation, source); |
| 34 |
} |
39 |
} |
|
Lines 43-46
Link Here
|
| 43 |
return null; |
48 |
return null; |
| 44 |
} |
49 |
} |
| 45 |
|
50 |
|
|
|
51 |
/** |
| 52 |
* The source is merged only with the ancestor node. |
| 53 |
*/ |
| 54 |
private Collection mergeSource(Collection collection) { |
| 55 |
List result = new ArrayList(); |
| 56 |
for(Iterator it = collection.iterator(); it.hasNext();) { |
| 57 |
Node node = (Node) it.next(); |
| 58 |
if(result.contains(node)) { |
| 59 |
continue ; |
| 60 |
} |
| 61 |
|
| 62 |
boolean isAdd = true; |
| 63 |
for(int i = result.size() - 1; i >= 0; i--) { |
| 64 |
Node addedNode = (Node)result.get(i); |
| 65 |
if(isAncestor(node, addedNode)) { |
| 66 |
if(isAdd) { |
| 67 |
// Replace with the ancestor node |
| 68 |
result.set(i, node); |
| 69 |
isAdd = false; |
| 70 |
} |
| 71 |
else { |
| 72 |
// Delete the child node |
| 73 |
result.remove(i); |
| 74 |
} |
| 75 |
} |
| 76 |
else if(!isAdd && isAncestor(addedNode, node)) { |
| 77 |
// Do not add child nodes |
| 78 |
isAdd = false; |
| 79 |
break ; |
| 80 |
} |
| 81 |
} |
| 82 |
if(isAdd) { |
| 83 |
result.add(node); |
| 84 |
} |
| 85 |
} |
| 86 |
return result; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Returns with true if node1 is an ancestor of node2. |
| 91 |
*/ |
| 92 |
private boolean isAncestor(Node node1, Node node2) { |
| 93 |
boolean result = false; |
| 94 |
for (Node parent = node2; parent != null; parent = parent.getParentNode()) { |
| 95 |
if (parent == node1) { |
| 96 |
result = true; |
| 97 |
break; |
| 98 |
} |
| 99 |
} |
| 100 |
return result; |
| 101 |
} |
| 46 |
} |
102 |
} |