Java Code Examples for javax.swing.tree.TreePath#getPathComponent()

The following examples show how to use javax.swing.tree.TreePath#getPathComponent() . 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: ShelvedChangesViewManager.java    From consulo with Apache License 2.0 6 votes vote down vote up
private Set<ShelvedChangeList> getSelectedLists(final boolean recycled) {
  final TreePath[] selections = getSelectionPaths();
  final Set<ShelvedChangeList> changeLists = new HashSet<ShelvedChangeList>();
  if (selections != null) {
    for(TreePath path: selections) {
      if (path.getPathCount() >= 2) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getPathComponent(1);
        if (node.getUserObject() instanceof ShelvedChangeList) {
          final ShelvedChangeList list = (ShelvedChangeList)node.getUserObject();
          if (((! recycled) && (! list.isRecycled())) ||
            (recycled && list.isRecycled())) {
            changeLists.add(list);
          }
        }
      }
    }
  }
  return changeLists;
}
 
Example 2
Source File: GltfBrowserPanel.java    From JglTF with MIT License 6 votes vote down vote up
/**
 * Translates one TreePath to a new TreeModel. This methods assumes 
 * DefaultMutableTreeNodes, and identifies the path based on the
 * user objects.
 * 
 * This method is similar to 
 * {@link de.javagl.common.ui.JTrees#translatePath(TreeModel, TreePath)},
 * but returns a "partial" path if the new tree model only contains a
 * subset of the given path.
 * 
 * @param newTreeModel The new tree model
 * @param oldPath The old tree path
 * @return The new tree path
 */
private static TreePath translatePathPartial(
    TreeModel newTreeModel, TreePath oldPath)
{
    Object newRoot = newTreeModel.getRoot();
    List<Object> newPath = new ArrayList<Object>();
    newPath.add(newRoot);
    Object newPreviousElement = newRoot;
    for (int i=1; i<oldPath.getPathCount(); i++)
    {
        Object oldElement = oldPath.getPathComponent(i);
        DefaultMutableTreeNode oldElementNode = 
            (DefaultMutableTreeNode)oldElement;
        Object oldUserObject = oldElementNode.getUserObject();
        
        Object newElement = getChildWith(newPreviousElement, oldUserObject);
        if (newElement == null)
        {
            break;
        }
        newPath.add(newElement);
        newPreviousElement = newElement;
    }
    return new TreePath(newPath.toArray());
}
 
Example 3
Source File: InstructionSequenceTreePathFilter.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Create an InstSeqTreePathFilter based on a {@link Treepath} and an {@link InstructionType}.
 * @param path {@link InstructionSequence}s must be consistent with this path in order to pass the filter.
 * @param type the type of instructions to filter.
 */
public InstructionSequenceTreePathFilter(TreePath path, PatternType type) {
	//start at 1 instead of 0 because we don't care about the root node
	instructions = new ArrayList<String>();
	lengths = new ArrayList<Integer>();
	this.type = type;
	for (int i = 1; i < path.getPathCount(); ++i) {
		if (!(path.getPathComponent(i) instanceof FunctionBitPatternsGTreeNode)) {
			throw new IllegalArgumentException(
				"non-root nodes must be members of class FunctionStartPatternsGTreeNode");
		}
		FunctionBitPatternsGTreeNode currentNode =
			(FunctionBitPatternsGTreeNode) path.getPathComponent(i);
		String currentInstruction = currentNode.getInstruction();
		Integer currentNumBytes = currentNode.getNumBytes();
		if ((currentInstruction == null) || (currentNumBytes == null)) {
			throw new IllegalArgumentException(
				"Can't have null instructions or lengths in the path!");
		}
		instructions.add(currentInstruction);
		lengths.add(currentNumBytes);
	}

}
 
Example 4
Source File: CPUTreeTableView.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private long getMaxValue(int row, int val) {
    TreePath path = treeTable.getPathForRow(row);
    if (path == null) return Long.MIN_VALUE; // TODO: prevents NPE from export but doesn't provide the actual value!
    if (path.getPathCount() < 2) return 1;
    
    PrestimeCPUCCTNode node = (PrestimeCPUCCTNode)path.getPathComponent(1);
    if (val == 0) return Math.abs(node.getTotalTime0());
    else if (val == 1) return Math.abs(node.getTotalTime1());
    else return Math.abs(node.getNCalls());
}
 
Example 5
Source File: ButtonTreePathView.java    From pdfxtk with Apache License 2.0 5 votes vote down vote up
private TreePath shortestCommonPath(TreePath p1, TreePath p2) {
   TreePath result = new TreePath(p1.getPathComponent(0));
   for(int i = 1; i < Math.min(p1.getPathCount(), p2.getPathCount()); i++)
     if(p1.getPathComponent(i) != p2.getPathComponent(i))
break;
     else
result = result.pathByAddingChild(p1.getPathComponent(i));
   return result;
 }
 
Example 6
Source File: JTreeView.java    From pdfxtk with Apache License 2.0 5 votes vote down vote up
private TreePath shortestCommonPath(TreePath p1, TreePath p2) {
   TreePath result = new TreePath(p1.getPathComponent(0));
   for(int i = 0; i < Math.min(p1.getPathCount(), p2.getPathCount()); i++)
     if(!p1.getPathComponent(i).equals(p2.getPathComponent(i)))
break;
     else
result = result.pathByAddingChild(p1.getPathComponent(i));
   return result;
 }
 
Example 7
Source File: IndexTreePathState.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static int[] pathToChildIndecies(TreePath path) {
  int[] result = new int[path.getPathCount()];
  for (int i = 0; i < path.getPathCount(); i++) {
    TreeNode node = (TreeNode) path.getPathComponent(i);
    TreeNode parent = node.getParent();
    result[i] = parent != null ? parent.getIndex(node) : 0;
  }
  return result;
}
 
Example 8
Source File: ProvisionDataSourcePanel.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public DataFactory getSelectedDataSource() {
  final TreePath selectionPath = availableDataSources.getSelectionPath();
  if ( selectionPath == null ) {
    return null;
  }

  final int size = selectionPath.getPathCount();
  if ( size >= 2 ) {
    final DataFactoryWrapper dataFactoryWrapper =
      (DataFactoryWrapper) selectionPath.getPathComponent( 1 );
    return dataFactoryWrapper.getEditedDataFactory();
  }
  return null;
}
 
Example 9
Source File: SubTreeModel.java    From pdfxtk with Apache License 2.0 5 votes vote down vote up
public TreePath absolute(TreePath relPath) {
  if(relPath == null) return null;
  int rpl = rootPath.getPathCount() - 1;
  Object[] result = new Object[rpl + relPath.getPathCount()];
  
  for(int i = 0; i < rpl; i++)
    result[i] = rootPath.getPathComponent(i);
  
  for(int i = 0; i < relPath.getPathCount(); i++)
    result[i + rpl] = relPath.getPathComponent(i);
  
  return new TreePath(result);
}
 
Example 10
Source File: BrowserComponent.java    From ApkToolPlus with Apache License 2.0 5 votes vote down vote up
private void addClassMemberPathComponent(ClassMember classMember, BrowserPath browserPath, TreePath selectionPath) {

        try {
            browserPath.addPathComponent(new ReferenceHolder(classMember.getName(), classMember.getDescriptor()));
            if (selectionPath.getPathCount() > 3) {
                for (int i = 3; i < selectionPath.getPathCount(); i++) {
                    BrowserTreeNode attributeNode = (BrowserTreeNode)selectionPath.getPathComponent(i);
                    browserPath.addPathComponent(new IndexHolder(attributeNode.getIndex()));
                }
            }
        } catch (InvalidByteCodeException ex) {
        }
    }
 
Example 11
Source File: BrowserComponent.java    From ApkToolPlus with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a <tt>BrowserPath</tt> object for the curently selected path in the tree.
 *
 * @return the browser path
 */
public BrowserPath getBrowserPath(String oldCatName) {

    TreePath selectionPath = treePane.getTree().getSelectionPath();
    if (selectionPath == null || selectionPath.getPathCount() < 3) {
        return null;
    }

    BrowserTreeNode categoryNode = (BrowserTreeNode)selectionPath.getPathComponent(2);
    if (!oldCatName.equals(categoryNode.getUserObject().toString())) {
    	return null;
    }
    String category = categoryNode.getType();
    if (category.equals(BrowserTreeNode.NODE_NO_CONTENT)) {
        return null;
    }

    BrowserPath browserPath = new BrowserPath();
    browserPath.addPathComponent(new CategoryHolder(category));
    int categoryNodeIndex = categoryNode.getIndex();
    if (category.equals(BrowserTreeNode.NODE_CONSTANT_POOL)) {
        --categoryNodeIndex;
    }
    if (category.equals(BrowserTreeNode.NODE_METHOD)) {
        MethodInfo methodInfo = services.getClassFile().getMethods()[categoryNodeIndex];
        addClassMemberPathComponent(methodInfo, browserPath, selectionPath);
    } else if (category.equals(BrowserTreeNode.NODE_FIELD)) {
        FieldInfo fieldInfo = services.getClassFile().getFields()[categoryNodeIndex];
        addClassMemberPathComponent(fieldInfo, browserPath, selectionPath);
    } else {
        browserPath.addPathComponent(new IndexHolder(categoryNodeIndex));
    }

    return browserPath;
}
 
Example 12
Source File: ObjectsUI.java    From whyline with MIT License 5 votes vote down vote up
public long getSelectedObjectID() {
	
	TreePath path = getSelectionPath();
	if(path== null || path.getPathCount() <= 1) return 0;
	Object selection = path.getPathComponent(1);
	return selection instanceof ReferenceState ? ((ReferenceState)selection).getObjectIDForChildren() : 0;
	
}
 
Example 13
Source File: DifficultyDialog.java    From freecol with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void valueChanged(TreeSelectionEvent event) {
    TreePath path = event.getPath();
    if (path.getPathCount() >= 2) {
        DefaultMutableTreeNode node
            = (DefaultMutableTreeNode)path.getPathComponent(1);
        this.selected = (OptionGroup)node.getUserObject();
    }
}
 
Example 14
Source File: CPUTreeTableView.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private long getMaxValue(int row, int val) {
    TreePath path = treeTable.getPathForRow(row);
    if (path == null) return Long.MIN_VALUE; // TODO: prevents NPE from export but doesn't provide the actual value!
    if (path.getPathCount() < 2) return 1;
    
    PrestimeCPUCCTNode node = (PrestimeCPUCCTNode)path.getPathComponent(1);
    if (val == 0) return Math.abs(node.getTotalTime0());
    else if (val == 1) return Math.abs(node.getTotalTime1());
    else return Math.abs(node.getNCalls());
}
 
Example 15
Source File: ProgramListener.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private String[] getNames(TreePath path) {

		int count = path.getPathCount();
		String[] names = new String[count];

		for (int i = 0; i < count; i++) {
			ProgramNode node = (ProgramNode) path.getPathComponent(i);
			names[i] = node.getName();
		}
		return names;
	}
 
Example 16
Source File: InspectorTreeUI.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void paintHorizontalPartOfLeg(final Graphics g,
                                        final Rectangle clipBounds,
                                        final Insets insets,
                                        final Rectangle bounds,
                                        final TreePath path,
                                        final int row,
                                        final boolean isExpanded,
                                        final boolean hasBeenExpanded,
                                        final boolean isLeaf) {
  if (path.getPathCount() < 2) {
    // We could draw lines for nodes with a single child but we omit them
    // to more of an emphasis of lines for nodes with multiple children.
    return;
  }
  if (path.getPathCount() >= 2) {
    final Object treeNode = path.getPathComponent(path.getPathCount() - 2);
    if (treeNode instanceof DefaultMutableTreeNode) {
      DefaultMutableTreeNode node = (DefaultMutableTreeNode)treeNode;
      if (node.getChildCount() < 2) {
        return;
      }
    }
  }
  boolean dashed = false;
  final DiagnosticsNode diagnosticsNode = maybeGetDiagnostic((DefaultMutableTreeNode)path.getLastPathComponent());
  if (diagnosticsNode != null) {
    if (diagnosticsNode.isProperty()) {
      // Intentionally avoid ever drawing lines for properties as we need to
      // distinguish them from other nodes. See the DiagnosticsNode class in
      // Flutter which applies the same concept rendering properties inline
      // as part of ascii art tree display.
      return;
    }
    // This also consistent with the ascii art tree display where offstage
    // nodes are rendered using dashed lines.
    if (diagnosticsNode.getStyle() == DiagnosticsTreeStyle.offstage) {
      dashed = true;
    }
  }

  final int depth = path.getPathCount() - 1;
  if ((depth == 0 || (depth == 1 && !isRootVisible())) &&
      !getShowsRootHandles()) {
    return;
  }

  final int lineY = bounds.y + bounds.height / 2;
  final int leafChildLineInset = 4;

  if (leftToRight) {
    int leftX = bounds.x - getRightChildIndent();
    int nodeX = bounds.x - getHorizontalLegBuffer();

    leftX = getRowX(row, depth - 1) - getRightChildIndent() + insets.left;
    nodeX = isLeaf ? getRowX(row, depth) - leafChildLineInset :
            getRowX(row, depth - 1);
    nodeX += insets.left;
    if (clipBounds.intersects(leftX, lineY, nodeX - leftX, 1)) {
      g.setColor(JBColor.GRAY);
      if (dashed) {
        drawDashedHorizontalLine(g, lineY, leftX, nodeX - 1);
      }
      else {
        paintHorizontalLine(g, tree, lineY, leftX, nodeX - 1);
      }
    }
  }
  // TODO(jacobr): implement RTL case.
}
 
Example 17
Source File: InspectorTreeUI.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void paintHorizontalPartOfLeg(final Graphics g,
                                        final Rectangle clipBounds,
                                        final Insets insets,
                                        final Rectangle bounds,
                                        final TreePath path,
                                        final int row,
                                        final boolean isExpanded,
                                        final boolean hasBeenExpanded,
                                        final boolean isLeaf) {
  if (path.getPathCount() < 2) {
    // We could draw lines for nodes with a single child but we omit them
    // to more of an emphasis of lines for nodes with multiple children.
    return;
  }
  if (path.getPathCount() >= 2) {
    final Object treeNode = path.getPathComponent(path.getPathCount() - 2);
    if (treeNode instanceof DefaultMutableTreeNode) {
      DefaultMutableTreeNode node = (DefaultMutableTreeNode)treeNode;
      if (node.getChildCount() < 2) {
        return;
      }
    }
  }
  boolean dashed = false;
  final DiagnosticsNode diagnosticsNode = maybeGetDiagnostic((DefaultMutableTreeNode)path.getLastPathComponent());
  if (diagnosticsNode != null) {
    if (diagnosticsNode.isProperty()) {
      // Intentionally avoid ever drawing lines for properties as we need to
      // distinguish them from other nodes. See the DiagnosticsNode class in
      // Flutter which applies the same concept rendering properties inline
      // as part of ascii art tree display.
      return;
    }
    // This also consistent with the ascii art tree display where offstage
    // nodes are rendered using dashed lines.
    if (diagnosticsNode.getStyle() == DiagnosticsTreeStyle.offstage) {
      dashed = true;
    }
  }

  final int depth = path.getPathCount() - 1;
  if ((depth == 0 || (depth == 1 && !isRootVisible())) &&
      !getShowsRootHandles()) {
    return;
  }

  final int lineY = bounds.y + bounds.height / 2;
  final int leafChildLineInset = 4;

  if (leftToRight) {
    int leftX = bounds.x - getRightChildIndent();
    int nodeX = bounds.x - getHorizontalLegBuffer();

    leftX = getRowX(row, depth - 1) - getRightChildIndent() + insets.left;
    nodeX = isLeaf ? getRowX(row, depth) - leafChildLineInset :
            getRowX(row, depth - 1);
    nodeX += insets.left;
    if (clipBounds.intersects(leftX, lineY, nodeX - leftX, 1)) {
      g.setColor(JBColor.GRAY);
      if (dashed) {
        drawDashedHorizontalLine(g, lineY, leftX, nodeX - 1);
      }
      else {
        paintHorizontalLine(g, tree, lineY, leftX, nodeX - 1);
      }
    }
  }
  // TODO(jacobr): implement RTL case.
}
 
Example 18
Source File: TDA.java    From tda with GNU Lesser General Public License v2.1 4 votes vote down vote up
private DefaultMutableTreeNode fetchTop(TreePath pathToRoot) {
    return((DefaultMutableTreeNode) pathToRoot.getPathComponent(getRootNodeLevel()));
}
 
Example 19
Source File: ChangesListView.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void calcData(Key<?> key, DataSink sink) {
  if (key == VcsDataKeys.CHANGES) {
    sink.put(VcsDataKeys.CHANGES, getSelectedChanges().toArray(Change[]::new));
  }
  else if (key == VcsDataKeys.CHANGE_LEAD_SELECTION) {
    sink.put(VcsDataKeys.CHANGE_LEAD_SELECTION, getLeadSelection().toArray(Change[]::new));
  }
  else if (key == VcsDataKeys.CHANGE_LISTS) {
    sink.put(VcsDataKeys.CHANGE_LISTS, getSelectedChangeLists().toArray(ChangeList[]::new));
  }
  else if (key == CommonDataKeys.VIRTUAL_FILE_ARRAY) {
    sink.put(CommonDataKeys.VIRTUAL_FILE_ARRAY, getSelectedFiles().toArray(VirtualFile[]::new));
  }
  else if (key == VcsDataKeys.VIRTUAL_FILE_STREAM) {
    sink.put(VcsDataKeys.VIRTUAL_FILE_STREAM, getSelectedFiles());
  }
  else if (key == CommonDataKeys.NAVIGATABLE) {
    VirtualFile file = getIfSingle(getSelectedFiles());
    if (file != null && !file.isDirectory()) {
      sink.put(CommonDataKeys.NAVIGATABLE, new OpenFileDescriptor(myProject, file, 0));
    }
  }
  else if (key == CommonDataKeys.NAVIGATABLE_ARRAY) {
    sink.put(CommonDataKeys.NAVIGATABLE_ARRAY, ChangesUtil.getNavigatableArray(myProject, getSelectedFiles()));
  }
  else if (key == PlatformDataKeys.DELETE_ELEMENT_PROVIDER) {
    if (getSelectionObjectsStream().anyMatch(userObject -> !(userObject instanceof ChangeList))) {
      sink.put(PlatformDataKeys.DELETE_ELEMENT_PROVIDER, new VirtualFileDeleteProvider());
    }
  }
  else if (key == PlatformDataKeys.COPY_PROVIDER) {
    sink.put(PlatformDataKeys.COPY_PROVIDER, myCopyProvider);
  }
  else if (key == UNVERSIONED_FILES_DATA_KEY) {
    sink.put(UNVERSIONED_FILES_DATA_KEY, getSelectedUnversionedFiles());
  }
  else if (key == IGNORED_FILES_DATA_KEY) {
    sink.put(IGNORED_FILES_DATA_KEY, getSelectedIgnoredFiles());
  }
  else if (key == VcsDataKeys.MODIFIED_WITHOUT_EDITING_DATA_KEY) {
    sink.put(VcsDataKeys.MODIFIED_WITHOUT_EDITING_DATA_KEY, getSelectedModifiedWithoutEditing().collect(toList()));
  }
  else if (key == LOCALLY_DELETED_CHANGES) {
    sink.put(LOCALLY_DELETED_CHANGES, getSelectedLocallyDeletedChanges().collect(toList()));
  }
  else if (key == MISSING_FILES_DATA_KEY) {
    sink.put(MISSING_FILES_DATA_KEY, getSelectedMissingFiles().collect(toList()));
  }
  else if (VcsDataKeys.HAVE_LOCALLY_DELETED == key) {
    sink.put(VcsDataKeys.HAVE_LOCALLY_DELETED, getSelectedMissingFiles().findAny().isPresent());
  }
  else if (VcsDataKeys.HAVE_MODIFIED_WITHOUT_EDITING == key) {
    sink.put(VcsDataKeys.HAVE_MODIFIED_WITHOUT_EDITING, getSelectedModifiedWithoutEditing().findAny().isPresent());
  }
  else if (VcsDataKeys.HAVE_SELECTED_CHANGES == key) {
    sink.put(VcsDataKeys.HAVE_SELECTED_CHANGES, haveSelectedChanges());
  }
  else if (key == PlatformDataKeys.HELP_ID) {
    sink.put(PlatformDataKeys.HELP_ID, HELP_ID);
  }
  else if (key == VcsDataKeys.CHANGES_IN_LIST_KEY) {
    final TreePath selectionPath = getSelectionPath();
    if (selectionPath != null && selectionPath.getPathCount() > 1) {
      ChangesBrowserNode<?> firstNode = (ChangesBrowserNode)selectionPath.getPathComponent(1);
      if (firstNode instanceof ChangesBrowserChangeListNode) {
        sink.put(VcsDataKeys.CHANGES_IN_LIST_KEY, firstNode.getAllChangesUnder());
      }
    }
  }
}
 
Example 20
Source File: RefactoringTestCase.java    From netbeans with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the file name form the selected path in the preview tree. Supposed
 * is that the name of file in the second element in the path
 *
 * @param tree Preview tree
 * @return File name related to selected node
 */
public String getFileForSelectedNode(JTreeOperator tree) {
    TreePath selectionPath = tree.getSelectionPath();
    Object pathComponent = selectionPath.getPathComponent(2);
    return getPreviewItemLabel(pathComponent);
}