@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);
    }
  }
  public void putIntoDefaultLocations(@NotNull final List<? extends PackagingSourceItem> items) {
    final List<PackagingElement<?>> toSelect = new ArrayList<PackagingElement<?>>();
    editLayout(
        new Runnable() {
          @Override
          public void run() {
            final CompositePackagingElement<?> rootElement = getArtifact().getRootElement();
            final ArtifactType artifactType = getArtifact().getArtifactType();
            for (PackagingSourceItem item : items) {
              final String path = artifactType.getDefaultPathFor(item);
              if (path != null) {
                final CompositePackagingElement<?> parent;
                if (path.endsWith(URLUtil.JAR_SEPARATOR)) {
                  parent =
                      PackagingElementFactory.getInstance()
                          .getOrCreateArchive(
                              rootElement, StringUtil.trimEnd(path, URLUtil.JAR_SEPARATOR));
                } else {
                  parent =
                      PackagingElementFactory.getInstance().getOrCreateDirectory(rootElement, path);
                }
                final List<? extends PackagingElement<?>> elements = item.createElements(myContext);
                toSelect.addAll(parent.addOrFindChildren(elements));
              }
            }
          }
        });

    myArtifactsEditor.getSourceItemsTree().rebuildTree();
    updateAndSelect(myTree.getRootPackagingNode(), toSelect);
  }
 @Override
 public void drop(DnDEvent aEvent) {
   final Object object = aEvent.getAttachedObject();
   if (object instanceof PackagingElementDraggingObject) {
     final PackagingElementDraggingObject draggingObject = (PackagingElementDraggingObject) object;
     final PackagingElementNode<?> targetNode = draggingObject.getTargetNode();
     final CompositePackagingElement<?> targetElement = draggingObject.getTargetElement();
     if (targetElement == null || targetNode == null || !draggingObject.checkCanDrop()) return;
     if (!checkCanAdd(targetElement, targetNode)) {
       return;
     }
     final List<PackagingElement<?>> toSelect = new ArrayList<PackagingElement<?>>();
     editLayout(
         new Runnable() {
           @Override
           public void run() {
             draggingObject.beforeDrop();
             final CompositePackagingElement<?> parent =
                 getOrCreateModifiableParent(targetElement, targetNode);
             for (PackagingElement<?> element :
                 draggingObject.createPackagingElements(myContext)) {
               toSelect.add(element);
               parent.addOrFindChild(element);
             }
           }
         });
     updateAndSelect(targetNode, toSelect);
     myArtifactsEditor.getSourceItemsTree().rebuildTree();
   }
 }
  public void addNewPackagingElement(@NotNull PackagingElementType<?> type) {
    PackagingElementNode<?> parentNode = getParentNode(myTree.getSelection());
    final PackagingElement<?> element = parentNode.getFirstElement();
    final CompositePackagingElement<?> parent;
    if (element instanceof CompositePackagingElement<?>) {
      parent = (CompositePackagingElement<?>) element;
    } else {
      parent = getArtifact().getRootElement();
      parentNode = myTree.getRootPackagingNode();
    }
    if (!checkCanAdd(parent, parentNode)) return;

    final List<? extends PackagingElement<?>> children =
        type.chooseAndCreate(myContext, getArtifact(), parent);
    final PackagingElementNode<?> finalParentNode = parentNode;
    editLayout(
        new Runnable() {
          @Override
          public void run() {
            CompositePackagingElement<?> actualParent =
                getOrCreateModifiableParent(parent, finalParentNode);
            for (PackagingElement<?> child : children) {
              actualParent.addOrFindChild(child);
            }
          }
        });
    updateAndSelect(parentNode, children);
  }
 public void putElements(
     @NotNull final String path, @NotNull final List<? extends PackagingElement<?>> elements) {
   final List<PackagingElement<?>> toSelect = new ArrayList<PackagingElement<?>>();
   editLayout(
       new Runnable() {
         @Override
         public void run() {
           final CompositePackagingElement<?> directory =
               PackagingElementFactory.getInstance()
                   .getOrCreateDirectory(getArtifact().getRootElement(), path);
           toSelect.addAll(directory.addOrFindChildren(elements));
         }
       });
   myArtifactsEditor.getSourceItemsTree().rebuildTree();
   updateAndSelect(myTree.getRootPackagingNode(), toSelect);
 }
  public void packInto(
      @NotNull final List<? extends PackagingSourceItem> items, final String pathToJar) {
    final List<PackagingElement<?>> toSelect = new ArrayList<PackagingElement<?>>();
    final CompositePackagingElement<?> rootElement = getArtifact().getRootElement();
    editLayout(
        new Runnable() {
          @Override
          public void run() {
            final CompositePackagingElement<?> archive =
                PackagingElementFactory.getInstance().getOrCreateArchive(rootElement, pathToJar);
            for (PackagingSourceItem item : items) {
              final List<? extends PackagingElement<?>> elements = item.createElements(myContext);
              archive.addOrFindChildren(elements);
            }
            toSelect.add(archive);
          }
        });

    myArtifactsEditor.getSourceItemsTree().rebuildTree();
    updateAndSelect(myTree.getRootPackagingNode(), toSelect);
  }