@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);
    }
  }
 @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 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);
  }
  public boolean checkCanRemove(final List<? extends PackagingElementNode<?>> nodes) {
    Set<PackagingNodeSource> rootSources = new HashSet<PackagingNodeSource>();
    for (PackagingElementNode<?> node : nodes) {
      rootSources.addAll(getRootNodeSources(node.getNodeSources()));
    }

    if (!rootSources.isEmpty()) {
      final String message;
      if (rootSources.size() == 1) {
        final String name = rootSources.iterator().next().getPresentableName();
        message =
            "The selected node belongs to '"
                + name
                + "' element. Do you want to remove the whole '"
                + name
                + "' element from the artifact?";
      } else {
        message =
            "The selected node belongs to "
                + nodes.size()
                + " elements. Do you want to remove all these elements from the artifact?";
      }
      final int answer =
          Messages.showYesNoDialog(
              myArtifactsEditor.getMainComponent(), message, "Remove Elements", null);
      if (answer != Messages.YES) return false;
    }
    return true;
  }
 public boolean checkCanModifyChildren(
     @NotNull PackagingElement<?> parentElement,
     @NotNull PackagingElementNode<?> parentNode,
     @NotNull Collection<? extends PackagingElementNode<?>> children) {
   final List<PackagingNodeSource> sources =
       new ArrayList<PackagingNodeSource>(parentNode.getNodeSource(parentElement));
   for (PackagingElementNode<?> child : children) {
     sources.addAll(child.getNodeSources());
   }
   return checkCanModify(sources);
 }
示例#7
0
 @Nullable
 public PackagingElementNode<?> findCompositeNodeByPath(String parentPath) {
   PackagingElementNode<?> node = getRootPackagingNode();
   for (String name : StringUtil.split(parentPath, "/")) {
     final CompositePackagingElementNode child = node.findCompositeChild(name);
     if (child == null) {
       return null;
     }
     node = child;
   }
   return node;
 }
 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 actionPerformed(AnActionEvent e) {
    final LayoutTreeSelection selection = myArtifactEditor.getLayoutTreeComponent().getSelection();
    final PackagingElementNode<?> node = selection.getNodeIfSingle();
    if (node == null) return;

    final Collection<PackagingNodeSource> sources = node.getNodeSources();
    for (PackagingNodeSource source : sources) {
      myArtifactEditor.getSubstitutionParameters().doNotSubstitute(source.getSourceElement());
      myArtifactEditor
          .getLayoutTreeComponent()
          .getLayoutTree()
          .addSubtreeToUpdate(source.getSourceParentNode());
    }
  }
  @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 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);
  }
  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;
          }
        }
      }
    }
  }
  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();
  }
 @Override
 public void update(AnActionEvent e) {
   final LayoutTreeSelection selection = myArtifactEditor.getLayoutTreeComponent().getSelection();
   final PackagingElementNode<?> node = selection.getNodeIfSingle();
   if (node != null) {
     final Collection<PackagingNodeSource> sources = node.getNodeSources();
     if (!sources.isEmpty()) {
       String description;
       if (sources.size() == 1) {
         description = "Hide Content of '" + sources.iterator().next().getPresentableName() + "'";
       } else {
         description = "Hide Content";
       }
       e.getPresentation().setVisible(true);
       e.getPresentation().setText(description);
       return;
     }
   }
   e.getPresentation().setVisible(false);
 }
  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);
  }
 public boolean checkCanModify(
     @NotNull PackagingElement<?> element, @NotNull PackagingElementNode<?> node) {
   return checkCanModify(node.getNodeSource(element));
 }