@Override
  public void actionPerformed(AnActionEvent e) {
    final PackagingElementNode<?> node = myLayoutTreeComponent.getSelection().getNodeIfSingle();
    if (node == null) return;
    final CompositePackagingElementNode parent = node.getParentNode();
    if (parent == null) return;

    final PackagingElement<?> element = node.getElementIfSingle();
    final CompositePackagingElement<?> parentElement = parent.getElementIfSingle();
    if (parentElement == null || element == null) return;

    if (!myLayoutTreeComponent.checkCanModifyChildren(parentElement, parent, Arrays.asList(node)))
      return;

    final List<PackagingElement<?>> toSelect = new ArrayList<PackagingElement<?>>();
    myLayoutTreeComponent.editLayout(
        new Runnable() {
          public void run() {
            final int index = parentElement.getChildren().indexOf(element);
            final PackagingElement<?> moved = parentElement.moveChild(index, myDirection);
            if (moved != null) {
              toSelect.add(moved);
            }
          }
        });
    if (!toSelect.isEmpty()) {
      myLayoutTreeComponent.updateAndSelect(parent, toSelect);
    }
  }
 private PackagingElementNode<?> getParentNode(final LayoutTreeSelection selection) {
   final PackagingElementNode<?> node = selection.getNodeIfSingle();
   if (node != null) {
     if (node.getFirstElement() instanceof CompositePackagingElement) {
       return node;
     }
     final PackagingElementNode<?> parent = node.getParentNode();
     if (parent != null) {
       return parent;
     }
   }
   return myTree.getRootPackagingNode();
 }
  public boolean checkCanAdd(
      CompositePackagingElement<?> parentElement, PackagingElementNode<?> parentNode) {
    boolean allParentsAreDirectories = true;
    PackagingElementNode<?> current = parentNode;
    while (current != null && !(current instanceof ArtifactRootNode)) {
      final PackagingElement<?> element = current.getFirstElement();
      if (!(element instanceof DirectoryPackagingElement)) {
        allParentsAreDirectories = false;
        break;
      }
      current = current.getParentNode();
    }

    return allParentsAreDirectories || checkCanModify(parentElement, parentNode);
  }
  private boolean isEnabled() {
    if (myLayoutTreeComponent.isSortElements()) {
      return false;
    }
    final PackagingElementNode<?> node = myLayoutTreeComponent.getSelection().getNodeIfSingle();
    if (node == null) {
      return false;
    }
    final CompositePackagingElementNode parent = node.getParentNode();
    if (parent == null) return false;

    final PackagingElement<?> element = node.getElementIfSingle();
    final CompositePackagingElement<?> parentElement = parent.getElementIfSingle();
    if (parentElement == null || element == null) return false;
    final List<PackagingElement<?>> children = parentElement.getChildren();
    final int index = children.indexOf(element);
    return index != -1 && 0 <= index + myDirection && index + myDirection < children.size();
  }
  private static CompositePackagingElement<?> getOrCreateModifiableParent(
      CompositePackagingElement<?> parentElement, PackagingElementNode<?> node) {
    PackagingElementNode<?> current = node;
    List<String> dirNames = new ArrayList<String>();
    while (current != null && !(current instanceof ArtifactRootNode)) {
      final PackagingElement<?> packagingElement = current.getFirstElement();
      if (!(packagingElement instanceof DirectoryPackagingElement)) {
        return parentElement;
      }
      dirNames.add(((DirectoryPackagingElement) packagingElement).getDirectoryName());
      current = current.getParentNode();
    }

    if (current == null) return parentElement;
    final PackagingElement<?> rootElement = current.getElementIfSingle();
    if (!(rootElement instanceof CompositePackagingElement<?>)) return parentElement;

    Collections.reverse(dirNames);
    String path = StringUtil.join(dirNames, "/");
    return PackagingElementFactory.getInstance()
        .getOrCreateDirectory((CompositePackagingElement<?>) rootElement, path);
  }