Java Code Examples for javax.swing.JTree#getSelectionPaths()

The following examples show how to use javax.swing.JTree#getSelectionPaths() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FigureUserObject.java    From openAGV with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the selected user objects in the tree.
 *
 * @param objectTree The tree to find the selected items.
 * @return All selected user objects.
 */
private Set<UserObject> getSelectedUserObjects(JTree objectTree) {
  Set<UserObject> objects = new HashSet<>();
  TreePath[] selectionPaths = objectTree.getSelectionPaths();

  if (selectionPaths != null) {
    for (TreePath path : selectionPaths) {
      if (path != null) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
        if (node.getUserObject() instanceof FigureUserObject) {
          objects.add((UserObject) node.getUserObject());
        }
      }
    }
  }

  return objects;
}
 
Example 2
Source File: VehicleUserObject.java    From openAGV with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the selected vehicle models in the tree.
 *
 * @param objectTree The tree to find the selected items.
 * @return All selected vehicle models.
 */
private Set<VehicleModel> getSelectedVehicles(JTree objectTree) {
  Set<VehicleModel> objects = new HashSet<>();
  TreePath[] selectionPaths = objectTree.getSelectionPaths();

  if (selectionPaths != null) {
    for (TreePath path : selectionPaths) {
      if (path != null) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
        //vehicles can only be selected with other vehicles
        if (node.getUserObject() instanceof VehicleUserObject) {
          objects.add((VehicleModel) ((UserObject) node.getUserObject()).getModelComponent());
        }
      }
    }
  }

  return objects;
}
 
Example 3
Source File: RepositoryTreeUtil.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Retain only the root selectedPaths
 *
 * @param tree
 *           The related tree, containing the path(s)
 */
public void retainRootSelections(JTree tree) {
	if (selectedPaths == null) {
		selectedPaths = tree.getSelectionPaths();
	}
	if (selectedPaths != null) {
		List<TreePath> parents = Arrays.asList(selectedPaths);
		List<TreePath> newSelection = new ArrayList<>(parents);
		for (TreePath entry : parents) {
			for (TreePath parent = entry.getParentPath(); parent != null; parent = parent.getParentPath()) {
				if (parents.contains(parent)) {
					newSelection.remove(entry);
					break;
				}
			}
		}
		selectedPaths = newSelection.toArray(new TreePath[0]);
	}
}
 
Example 4
Source File: MemberNodeTransferHandler.java    From binnavi with Apache License 2.0 6 votes vote down vote up
private static List<TypeMemberTreeNode> getSelectedNodesSorted(final JTree tree) {
  final List<TypeMemberTreeNode> nodes = Lists.newArrayList();
  for (final TreePath path : tree.getSelectionPaths()) {
    final DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
    if (node instanceof TypeMemberTreeNode) {
      nodes.add((TypeMemberTreeNode) node);
    } else {
      return Lists.newArrayList();
    }
  }
  Collections.sort(nodes, new Comparator<TypeMemberTreeNode>() {
    @Override
    public int compare(final TypeMemberTreeNode node0, final TypeMemberTreeNode node1) {
      return node0.getTypeMember().compareTo(node1.getTypeMember());
    }
  });

  return nodes;
}
 
Example 5
Source File: LastNodeLowerHalfDrop.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Transferable createTransferable(JComponent c) {
    JTree tree = (JTree) c;
    TreePath[] paths = tree.getSelectionPaths();
    if (paths != null) {
        // Make up a node array of copies for transfer and
        // another for/of the nodes that will be removed in
        // exportDone after a successful drop.
        List<DefaultMutableTreeNode> copies = new ArrayList<>();
        List<DefaultMutableTreeNode> toRemove = new ArrayList<>();
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)
                paths[0].getLastPathComponent();
        DefaultMutableTreeNode copy = copy(node);
        copies.add(copy);
        toRemove.add(node);
        for (int i = 1; i < paths.length; i++) {
            DefaultMutableTreeNode next = (DefaultMutableTreeNode) paths[i]
                    .getLastPathComponent();
            // Do not allow higher level nodes to be added to list.
            if (next.getLevel() < node.getLevel()) {
                break;
            } else if (next.getLevel() > node.getLevel()) {  // child node
                copy.add(copy(next));
                // node already contains child
            } else {                                        // sibling
                copies.add(copy(next));
                toRemove.add(next);
            }
        }
        DefaultMutableTreeNode[] nodes = copies
                .toArray(new DefaultMutableTreeNode[copies.size()]);
        nodesToRemove = toRemove.toArray(
                new DefaultMutableTreeNode[toRemove.size()]);
        return new NodesTransferable(nodes);
    }
    return null;
}
 
Example 6
Source File: PopupMenuItemAlert.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the number of selected alerts in the Alerts tree.
 *
 * <p>If multiple alert nodes are selected it returns the corresponding number of alerts. If
 * just a middle alert node (that is, the nodes that show the alert name) is selected it returns
 * only one selected alert when {@link #isMultiSelect() multiple selection} is not supported,
 * otherwise it returns the number of child nodes (which is one alert per node).
 *
 * @return the number of selected nodes
 */
private int getNumberOfSelectedAlerts() {
    JTree treeAlert = this.getTree();
    int count = treeAlert.getSelectionCount();
    if (count == 0) {
        return 0;
    }

    if (count == 1) {
        DefaultMutableTreeNode alertNode =
                (DefaultMutableTreeNode) treeAlert.getSelectionPath().getLastPathComponent();
        if (alertNode.getChildCount() == 0 || !isMultiSelect()) {
            return 1;
        }
        return alertNode.getChildCount();
    }

    count = 0;
    TreePath[] paths = treeAlert.getSelectionPaths();
    for (int i = 0; i < paths.length; i++) {
        TreePath nodePath = paths[i];
        int childCount =
                ((DefaultMutableTreeNode) nodePath.getLastPathComponent()).getChildCount();
        count +=
                childCount != 0
                        ? childCount
                        : (treeAlert.isPathSelected(nodePath.getParentPath()) ? 0 : 1);
    }
    return count;
}
 
Example 7
Source File: ZestPopupNodePaste.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isEnableForComponent(Component invoker) {
    if (extension.isScriptTree(invoker)) {
        try {
            JTree tree = (JTree) invoker;
            if (tree.getLastSelectedPathComponent() != null) {
                if (tree.getSelectionPaths().length != 1) {
                    // Start by just supporting one at a time..
                    return false;
                }
                ScriptNode node = extension.getSelectedZestNode();
                ZestElement elmt = ZestZapUtils.getElement(node);
                this.setEnabled(false);

                if (node == null || node.isRoot() || elmt == null) {
                    return false;

                } else if (elmt instanceof ZestContainer && extension.canPasteNodesTo(node)) {
                    this.setEnabled(true);

                } else if (elmt instanceof ZestStatement
                        && extension.canPasteNodesTo(node.getParent())) {
                    this.setEnabled(true);
                }

                return true;
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
    return false;
}
 
Example 8
Source File: ZestRecordOnOffPopupMenu.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isEnableForComponent(Component invoker) {
    if (extension.isScriptTree(invoker)) {
        try {
            JTree tree = (JTree) invoker;
            if (tree.getLastSelectedPathComponent() != null) {
                if (tree.getSelectionPaths().length != 1) {
                    // Start by just supporting one at a time..
                    return false;
                }
                ScriptNode node = extension.getSelectedZestNode();
                if (node != null && node.getUserObject() instanceof ZestScriptWrapper) {
                    ZestScriptWrapper script = (ZestScriptWrapper) node.getUserObject();
                    if (script.getType().hasCapability(ScriptType.CAPABILITY_APPEND)
                            && record != script.isRecording()) {
                        this.setEnabled(true);
                        return true;
                    } else {
                        return false;
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
    return false;
}
 
Example 9
Source File: ScriptTreeTransferHandler.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
private TransferHandler getTransferHandlerForSelection(Component c) {
    if (!(c instanceof JTree)) {
        logger.debug(
                "getTransferHandlerForSelection not jtree " + c.getClass().getCanonicalName());
        return null;
    }
    JTree tree = (JTree) c;
    TransferHandler th = null;

    if (tree.getSelectionPaths() == null) {
        return null;
    }

    for (TreePath tp : tree.getSelectionPaths()) {
        if (tp.getLastPathComponent() instanceof ScriptNode) {
            Object uo = ((ScriptNode) tp.getLastPathComponent()).getUserObject();
            if (uo == null) {
                // One of the selection doesnt have a user object
                // logger.debug("getTransferHandlerForSelection no user object for " + tp);
                return null;
            }
            TransferHandler th2 = this.htMap.get(uo.getClass());
            if (th2 == null) {
                // No transfer handler, no go
                return null;
            }
            if (th == null) {
                th = th2;
            } else if (!th.equals(th2)) {
                // Different transfer handlers, no go
                return null;
            }
        }
    }
    // logger.debug("getTransferHandlerForSelection no user objects found");
    return th;
}
 
Example 10
Source File: ScriptTreeTransferHandler.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
@Override
protected Transferable createTransferable(JComponent c) {
    JTree tree = (JTree) c;
    TreePath[] paths = tree.getSelectionPaths();
    if (paths != null) {
        // Make up a node array of copies for transfer and
        // another for/of the nodes that will be removed in
        // exportDone after a successful drop.
        List<DefaultMutableTreeNode> copies = new ArrayList<DefaultMutableTreeNode>();
        List<DefaultMutableTreeNode> toRemove = new ArrayList<DefaultMutableTreeNode>();
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) paths[0].getLastPathComponent();
        DefaultMutableTreeNode copy = copy(node);
        copies.add(copy);
        toRemove.add(node);
        for (int i = 1; i < paths.length; i++) {
            DefaultMutableTreeNode next =
                    (DefaultMutableTreeNode) paths[i].getLastPathComponent();
            // Do not allow higher level nodes to be added to list.
            if (next.getLevel() < node.getLevel()) {
                break;
            } else if (next.getLevel() > node.getLevel()) { // child node
                copy.add(copy(next));
                // node already contains child
            } else { // sibling
                copies.add(copy(next));
                toRemove.add(next);
            }
        }
        DefaultMutableTreeNode[] nodes =
                copies.toArray(new DefaultMutableTreeNode[copies.size()]);
        nodesToRemove = toRemove.toArray(new DefaultMutableTreeNode[toRemove.size()]);
        return new NodesTransferable(nodes);
    }
    return null;
}
 
Example 11
Source File: ProfilerTreeTable.java    From netbeans with Apache License 2.0 4 votes vote down vote up
static UIState getUIState(JTree tree) {
    TreePath[] selectedPaths = tree.getSelectionPaths();
    TreePath rootPath = new TreePath(tree.getModel().getRoot());
    Enumeration<TreePath> expandedPaths = tree.getExpandedDescendants(rootPath);
    return new UIState(selectedPaths, expandedPaths);
}
 
Example 12
Source File: ProfilerTreeTable.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
static UIState getUIState(JTree tree) {
    TreePath[] selectedPaths = tree.getSelectionPaths();
    TreePath rootPath = new TreePath(tree.getModel().getRoot());
    Enumeration<TreePath> expandedPaths = tree.getExpandedDescendants(rootPath);
    return new UIState(selectedPaths, expandedPaths);
}
 
Example 13
Source File: TreeTransferHandler.java    From sldeditor with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected Transferable createTransferable(JComponent c) {
    TransferableDataItem transferredDataItem = null;

    if (!(c instanceof JTree)) {
        return null;
    }

    JTree tree = (JTree) c;
    if (tree.isEditing()) {
        DefaultMutableTreeNode node =
                (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
        TreePath editPath = tree.getEditingPath();
        DefaultMutableTreeNode editNode =
                (DefaultMutableTreeNode) editPath.getLastPathComponent();
        if (node == editNode) {
            tree.stopEditing();
        }
    }

    TreePath[] paths = tree.getSelectionPaths();
    if (paths == null) {
        return null;
    } else {
        Map<NodeInterface, TreePath> map = new LinkedHashMap<>();

        for (TreePath path : paths) {
            if (path.getLastPathComponent() instanceof NodeInterface) {
                NodeInterface selection = (NodeInterface) path.getLastPathComponent();

                map.put(selection, path);
            }
        }

        if (map.isEmpty()) {
            return null;
        }
        transferredDataItem = new TransferableDataItem(map);
        setDragging(true);
        return transferredDataItem;
    }
}
 
Example 14
Source File: ZestPopupZestMove.java    From zap-extensions with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isEnableForComponent(Component invoker) {
    if (extension.isScriptTree(invoker)) {
        try {
            JTree tree = (JTree) invoker;
            if (tree.getLastSelectedPathComponent() != null) {
                if (tree.getSelectionPaths().length != 1) {
                    // Start by just supporting one at a time..
                    return false;
                }
                ScriptNode node = extension.getExtScript().getScriptUI().getSelectedNode();
                this.setEnabled(false);

                if (node == null || node.isRoot() || ZestZapUtils.getElement(node) == null) {
                    return false;
                } else if ((ZestZapUtils.getElement(node) instanceof ZestScript)) {
                    return false;
                } else if (ZestZapUtils.getShadowLevel(node) > 0) {
                    // Cant move these
                    /* TODO
                    } else if ((ZestZapUtils.getElement(node) instanceof ZestControl)) {
                    	return false;
                    } else if (ZestTreeElement.isSubclass(node.getZestElement(), ZestTreeElement.Type.COMMON_TESTS)) {
                    	// Cantmove these either
                    	 */
                } else if (up) {
                    ScriptNode prev = (ScriptNode) node.getPreviousSibling();
                    while (prev != null && ZestZapUtils.getShadowLevel(prev) > 0) {
                        prev = (ScriptNode) prev.getPreviousSibling();
                    }
                    if (prev != null) {
                        if (ZestZapUtils.getElement(node)
                                        .isSameSubclass(ZestZapUtils.getElement(prev))
                                || (ZestZapUtils.getElement(node) instanceof ZestStatement
                                        && ZestZapUtils.getElement(prev)
                                                instanceof ZestStatement)) {
                            this.setEnabled(true);
                        }
                    }
                } else {
                    // Down
                    ScriptNode next = (ScriptNode) node.getNextSibling();
                    while (next != null && ZestZapUtils.getShadowLevel(next) > 0) {
                        next = (ScriptNode) next.getNextSibling();
                    }
                    if (next != null) {
                        if (!(ZestZapUtils.getElement(next) instanceof ZestControl)
                                && (ZestZapUtils.getElement(node)
                                                .isSameSubclass(ZestZapUtils.getElement(next))
                                        || (ZestZapUtils.getElement(node)
                                                        instanceof ZestStatement
                                                && ZestZapUtils.getElement(next)
                                                        instanceof ZestStatement))) {
                            this.setEnabled(true);
                        }
                    }
                }

                return true;
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
    return false;
}
 
Example 15
Source File: PopupEnableDisableScript.java    From zap-extensions with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isEnableForComponent(Component invoker) {
    if (invoker.getName() != null && invoker.getName().equals(ScriptsListPanel.TREE)) {
        try {
            JTree tree = (JTree) invoker;
            if (tree.getLastSelectedPathComponent() != null) {
                if (tree.getSelectionPaths().length == 0) {
                    // None selected
                    return false;
                }

                this.setEnabled(false);
                Boolean enable = null; // We dont know whetehr it will be Enable or Disable yet

                for (TreePath tp : tree.getSelectionPaths()) {
                    ScriptNode node = (ScriptNode) tp.getLastPathComponent();

                    if (node == null
                            || node.isTemplate()
                            || node.getUserObject() == null
                            || !(node.getUserObject() instanceof ScriptWrapper)) {
                        return false;

                    } else {
                        ScriptWrapper script = (ScriptWrapper) node.getUserObject();
                        if (script.getEngine() == null) {
                            return false;
                        }

                        if (script.getType().isEnableable()) {
                            if (enable == null) {
                                // First one
                                enable = !script.isEnabled();
                                if (script.isEnabled()) {
                                    this.setText(
                                            Constant.messages.getString(
                                                    "scripts.disable.popup"));
                                } else {
                                    this.setText(
                                            Constant.messages.getString(
                                                    "scripts.enable.popup"));
                                }
                            } else if (enable.equals(script.isEnabled())) {
                                // Some are enabled, some disabled, cant tell which to do
                                return false;
                            }
                            this.setEnabled(true);
                        }
                    }
                }
                return true;
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
    return false;
}
 
Example 16
Source File: SelectionController.java    From nextreports-designer with Apache License 2.0 4 votes vote down vote up
public void valueChanged(TreeSelectionEvent event) {
    if (ignoreEvent) {
        return;
    }

    JTree tree = (JTree) event.getSource();
    TreePath[] paths = tree.getSelectionPaths();
    if (paths == null) {
        return;
    }

    SelectionModel selectionModel = Globals.getReportGrid().getSelectionModel();
    selectionModel.clearSelection();
    List<Cell> selectedCells = new ArrayList<Cell>();
    List<Integer> selectedRows = new ArrayList<Integer>();
    for (TreePath path : paths) {
        StructureTreeNode node = (StructureTreeNode) path.getLastPathComponent();
        Object userObject = node.getUserObject();                  
        if (userObject instanceof ReportGridCell) {
        	// cell layout properties
            boolean filter = Globals.getReportDesignerPanel().getStructurePanel().getStructureTreeModel().isActivatedFilter();
            if ((filter && node.isVisible()) || !filter) {
                selectedCells.add((Cell) userObject);
            }
        } else if (userObject instanceof Integer) { 
        	// row layout properties
        	Integer i = (Integer)userObject;
        	String bandName = ((Band)((StructureTreeNode)node.getParent()).getUserObject()).getName();
        	int gridRow = LayoutHelper.getReportLayout().getGridRow(bandName, i);
        	selectedRows.add(gridRow);            	
        } else if (node.isRoot()) {
            // report layout properties
            selectionModel.addRootSelection();
            return;   
        } else {
            // other nodes in tree (band)
            selectionModel.emptySelection(); 
            return;
        }
    }

    if (selectedCells.size() > 0) {
        ignoreEvent = true;
        selectionModel.addSelectionCells(selectedCells);
        ignoreEvent = false;
    } else if (selectedRows.size() > 0) {
    	ignoreEvent = true;
        selectionModel.addSelectionRows(selectedRows);   // used by PropertyPanel to select properties                                
        ignoreEvent = false;        
    } else {
        selectionModel.clearSelection();
    }
    Globals.getReportLayoutPanel().getReportGridPanel().repaintHeaders();
}