private static boolean isMoveSupported(JTree tree, int dir) {
   final TreePath[] selectionPaths = tree.getSelectionPaths();
   if (selectionPaths != null) {
     DefaultMutableTreeNode parent = null;
     for (TreePath treePath : selectionPaths)
       if (treePath.getLastPathComponent() != null) {
         final DefaultMutableTreeNode node =
             (DefaultMutableTreeNode) treePath.getLastPathComponent();
         if (parent == null) {
           parent = (DefaultMutableTreeNode) node.getParent();
         }
         if (parent != node.getParent()) {
           return false;
         }
         if (dir > 0) {
           if (parent.getIndex(node) == parent.getChildCount() - 1) {
             return false;
           }
         } else {
           if (parent.getIndex(node) == 0) {
             return false;
           }
         }
       }
     return true;
   }
   return false;
 }
Example #2
0
 protected Transferable createTransferable(JComponent c) {
   JTree tree = (JTree) c;
   TreePath[] paths = tree.getSelectionPaths();
   if (paths == null || paths.length != 1) return null;
   TreePath path = tree.getSelectionPath();
   GroovyTreeNode groovyTreeNode = (GroovyTreeNode) path.getLastPathComponent();
   return new StringTransferable((String) (groovyTreeNode.node.getNodeValue()));
 }
  public UpdaterTreeState(AbstractTreeUi ui, boolean isEmpty) {
    myUi = ui;

    if (!isEmpty) {
      final JTree tree = myUi.getTree();
      putAll(addPaths(tree.getSelectionPaths()), myToSelect);
      putAll(
          addPaths(tree.getExpandedDescendants(new TreePath(tree.getModel().getRoot()))),
          myToExpand);
    }
  }
Example #4
0
 public static void unselect(JTree tree, final DefaultMutableTreeNode node) {
   final TreePath rootPath = new TreePath(node.getPath());
   final TreePath[] selectionPaths = tree.getSelectionPaths();
   if (selectionPaths != null) {
     for (TreePath selectionPath : selectionPaths) {
       if (selectionPath.getPathCount() > rootPath.getPathCount()
           && rootPath.isDescendant(selectionPath)) {
         tree.removeSelectionPath(selectionPath);
       }
     }
   }
 }
Example #5
0
 public static List<TreePath> collectSelectedPaths(final JTree tree, final TreePath treePath) {
   final ArrayList<TreePath> result = new ArrayList<TreePath>();
   final TreePath[] selections = tree.getSelectionPaths();
   if (selections != null) {
     for (TreePath selection : selections) {
       if (treePath.isDescendant(selection)) {
         result.add(selection);
       }
     }
   }
   return result;
 }
  @Nullable
  private Node[] getSelectedNodes() {
    TreePath[] leadSelectionPath = myTree.getSelectionPaths();
    if (leadSelectionPath == null || leadSelectionPath.length == 0) return null;

    final List<Node> result = new ArrayList<Node>();
    for (TreePath comp : leadSelectionPath) {
      final Object lastPathComponent = comp.getLastPathComponent();
      if (lastPathComponent instanceof Node) {
        final Node node = (Node) lastPathComponent;
        result.add(node);
      }
    }
    return result.isEmpty() ? null : result.toArray(new Node[result.size()]);
  }
    @Nullable
    public Set<Object> getTreeSelectedActionIds() {
      TreePath[] paths = myTree.getSelectionPaths();
      if (paths == null) return null;

      Set<Object> actions = new HashSet<Object>();
      for (TreePath path : paths) {
        Object node = path.getLastPathComponent();
        if (node instanceof DefaultMutableTreeNode) {
          DefaultMutableTreeNode defNode = (DefaultMutableTreeNode) node;
          Object userObject = defNode.getUserObject();
          actions.add(userObject);
        }
      }
      return actions;
    }
  @Override
  @Nullable
  public Set<Usage> getSelectedUsages() {
    TreePath[] selectionPaths = myTree.getSelectionPaths();
    if (selectionPaths == null) {
      return null;
    }

    Set<Usage> usages = new THashSet<Usage>();
    for (TreePath selectionPath : selectionPaths) {
      DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectionPath.getLastPathComponent();
      collectUsages(node, usages);
    }

    return usages;
  }
Example #9
0
 @NotNull
 public static <T> List<T> collectSelectedObjectsOfType(JTree tree, Class<T> clazz) {
   final TreePath[] selections = tree.getSelectionPaths();
   if (selections != null) {
     final ArrayList<T> result = new ArrayList<T>();
     for (TreePath selection : selections) {
       final DefaultMutableTreeNode node =
           (DefaultMutableTreeNode) selection.getLastPathComponent();
       final Object userObject = node.getUserObject();
       if (clazz.isInstance(userObject)) {
         //noinspection unchecked
         result.add((T) userObject);
       }
     }
     return result;
   }
   return Collections.emptyList();
 }
Example #10
0
  public static void ensureSelection(JTree tree) {
    final TreePath[] paths = tree.getSelectionPaths();

    if (paths != null) {
      for (TreePath each : paths) {
        if (tree.getRowForPath(each) >= 0 && tree.isVisible(each)) {
          return;
        }
      }
    }

    for (int eachRow = 0; eachRow < tree.getRowCount(); eachRow++) {
      TreePath eachPath = tree.getPathForRow(eachRow);
      if (eachPath != null && tree.isVisible(eachPath)) {
        tree.setSelectionPath(eachPath);
        break;
      }
    }
  }
  @Nullable
  private UsageTarget[] getSelectedUsageTargets() {
    TreePath[] selectionPaths = myTree.getSelectionPaths();
    if (selectionPaths == null) return null;

    Set<UsageTarget> targets = new THashSet<UsageTarget>();
    for (TreePath selectionPath : selectionPaths) {
      Object lastPathComponent = selectionPath.getLastPathComponent();
      if (lastPathComponent instanceof UsageTargetNode) {
        UsageTargetNode usageTargetNode = (UsageTargetNode) lastPathComponent;
        UsageTarget target = usageTargetNode.getTarget();
        if (target != null && target.isValid()) {
          targets.add(target);
        }
      }
    }

    return targets.isEmpty() ? null : targets.toArray(new UsageTarget[targets.size()]);
  }
 private List<ActionUrl> findActionsUnderSelection() {
   final ArrayList<ActionUrl> actions = new ArrayList<ActionUrl>();
   final TreePath[] selectionPaths = myActionsTree.getSelectionPaths();
   if (selectionPaths != null) {
     for (TreePath path : selectionPaths) {
       final ActionUrl selectedUrl = CustomizationUtil.getActionUrl(path, ActionUrl.MOVE);
       final ArrayList<String> selectedGroupPath =
           new ArrayList<String>(selectedUrl.getGroupPath());
       final Object component = selectedUrl.getComponent();
       if (component instanceof Group) {
         selectedGroupPath.add(((Group) component).getName());
         for (ActionUrl action : mySelectedSchema.getActions()) {
           final ArrayList<String> groupPath = action.getGroupPath();
           final int idx = Collections.indexOfSubList(groupPath, selectedGroupPath);
           if (idx > -1) {
             actions.add(action);
           }
         }
       }
     }
   }
   return actions;
 }