public void removeNodes(final List<PackagingElementNode<?>> nodes) {
   Set<PackagingElement<?>> parents = new HashSet<PackagingElement<?>>();
   for (PackagingElementNode<?> node : nodes) {
     final List<? extends PackagingElement<?>> toDelete = node.getPackagingElements();
     for (PackagingElement<?> element : toDelete) {
       final Collection<PackagingNodeSource> nodeSources = node.getNodeSource(element);
       if (nodeSources.isEmpty()) {
         final CompositePackagingElement<?> parent = node.getParentElement(element);
         if (parent != null) {
           parents.add(parent);
           parent.removeChild(element);
         }
       } else {
         Collection<PackagingNodeSource> rootSources = getRootNodeSources(nodeSources);
         for (PackagingNodeSource source : rootSources) {
           parents.add(source.getSourceParentElement());
           source.getSourceParentElement().removeChild(source.getSourceElement());
         }
       }
     }
   }
   final List<PackagingElementNode<?>> parentNodes = myTree.findNodes(parents);
   for (PackagingElementNode<?> parentNode : parentNodes) {
     myTree.addSubtreeToUpdate(parentNode);
   }
 }
  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);
  }
 @Nullable
 private DefaultMutableTreeNode findParentCompositeElementNode(Point point) {
   TreePath path = myTree.getPathForLocation(point.x, point.y);
   while (path != null) {
     final PackagingElement<?> element = myTree.getElementByPath(path);
     if (element instanceof CompositePackagingElement) {
       return (DefaultMutableTreeNode) path.getLastPathComponent();
     }
     path = path.getParentPath();
   }
   return null;
 }
  public LayoutTreeComponent(
      ArtifactEditorImpl artifactsEditor,
      ComplexElementSubstitutionParameters substitutionParameters,
      ArtifactEditorContext context,
      Artifact originalArtifact,
      boolean sortElements) {
    myArtifactsEditor = artifactsEditor;
    mySubstitutionParameters = substitutionParameters;
    myContext = context;
    myOriginalArtifact = originalArtifact;
    mySortElements = sortElements;
    myTree = new LayoutTree(myArtifactsEditor);
    myTreeStructure = new LayoutTreeStructure();
    myBuilder = new LayoutTreeBuilder();
    Disposer.register(this, myTree);
    Disposer.register(this, myBuilder);

    myTree.addTreeSelectionListener(
        new TreeSelectionListener() {
          @Override
          public void valueChanged(TreeSelectionEvent e) {
            updatePropertiesPanel(false);
          }
        });
    createPropertiesPanel();
    myTreePanel = new JPanel(new BorderLayout());
    myTreePanel.add(ScrollPaneFactory.createScrollPane(myTree), BorderLayout.CENTER);
    myTreePanel.add(myPropertiesPanelWrapper, BorderLayout.SOUTH);
    if (!ApplicationManager.getApplication().isUnitTestMode()) {
      DnDManager.getInstance().registerTarget(this, myTree);
    }
  }
  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 boolean update(DnDEvent aEvent) {
   aEvent.setDropPossible(false);
   aEvent.hideHighlighter();
   final Object object = aEvent.getAttachedObject();
   if (object instanceof PackagingElementDraggingObject) {
     final DefaultMutableTreeNode parent =
         findParentCompositeElementNode(aEvent.getRelativePoint().getPoint(myTree));
     if (parent != null) {
       final PackagingElementDraggingObject draggingObject =
           (PackagingElementDraggingObject) object;
       final PackagingElementNode node = getNode(parent);
       if (node != null && draggingObject.canDropInto(node)) {
         final PackagingElement element = node.getFirstElement();
         if (element instanceof CompositePackagingElement) {
           draggingObject.setTargetNode(node);
           draggingObject.setTargetElement((CompositePackagingElement<?>) element);
           final Rectangle bounds = myTree.getPathBounds(TreeUtil.getPathFromRoot(parent));
           aEvent.setHighlighting(
               new RelativeRectangle(myTree, bounds),
               DnDEvent.DropTargetHighlightingType.RECTANGLE);
           aEvent.setDropPossible(true);
         }
       }
     }
   }
   return false;
 }
 public void setRootElement(CompositePackagingElement<?> rootElement) {
   myContext
       .getOrCreateModifiableArtifactModel()
       .getOrCreateModifiableArtifact(myOriginalArtifact)
       .setRootElement(rootElement);
   myTreeStructure.updateRootElement();
   final DefaultMutableTreeNode node = myTree.getRootNode();
   node.setUserObject(myTreeStructure.getRootElement());
   myBuilder.updateNode(node);
   rebuildTree();
   myArtifactsEditor.getSourceItemsTree().rebuildTree();
 }
 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 void removeSelectedElements() {
    final LayoutTreeSelection selection = myTree.getSelection();
    if (!checkCanRemove(selection.getNodes())) return;

    editLayout(
        new Runnable() {
          @Override
          public void run() {
            removeNodes(selection.getNodes());
          }
        });

    myArtifactsEditor.rebuildTries();
  }
  @TestOnly
  public void selectNode(@NotNull String parentPath, @NotNull String nodeName) {
    final PackagingElementNode<?> parent = myTree.findCompositeNodeByPath(parentPath);
    if (parent == null) return;

    for (SimpleNode node : parent.getChildren()) {
      if (node instanceof PackagingElementNode) {
        if (nodeName.equals(
            ((PackagingElementNode) node).getElementPresentation().getSearchName())) {
          myBuilder.select(node);
          return;
        }
      }
    }
  }
 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 selectNode(@NotNull String parentPath, @NotNull PackagingElement<?> element) {
    final PackagingElementNode<?> parent = myTree.findCompositeNodeByPath(parentPath);
    if (parent == null) return;

    for (SimpleNode node : parent.getChildren()) {
      if (node instanceof PackagingElementNode) {
        final List<? extends PackagingElement<?>> elements =
            ((PackagingElementNode<?>) node).getPackagingElements();
        for (PackagingElement<?> packagingElement : elements) {
          if (packagingElement.isEqualTo(element)) {
            myBuilder.select(node);
            return;
          }
        }
      }
    }
  }
  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);
  }
 public LayoutTreeSelection getSelection() {
   return myTree.getSelection();
 }
 public void updateRootNode() {
   myBuilder.updateNode(myTree.getRootNode());
 }
 public boolean isEditing() {
   return myTree.isEditing();
 }
 public void startRenaming(TreePath path) {
   myTree.startEditingAtPath(path);
 }