下面列出了javax.swing.tree.TreePath#getLastPathComponent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public int expand(JTree tree, TreePath path) {
if (myLevelsLeft == 0) return myExpansionLimit;
TreeModel model = tree.getModel();
Object node = path.getLastPathComponent();
int levelDecrement = 0;
if (!tree.isExpanded(path) && !model.isLeaf(node)) {
tree.expandPath(path);
levelDecrement = 1;
myExpansionLimit--;
}
for (int i = 0; i < model.getChildCount(node); i++) {
Object child = model.getChild(node, i);
if (model.isLeaf(child)) continue;
ExpandContext childContext = new ExpandContext(myExpansionLimit, myLevelsLeft - levelDecrement);
myExpansionLimit = childContext.expand(tree, path.pathByAddingChild(child));
if (myExpansionLimit <= 0) return 0;
}
return myExpansionLimit;
}
/**
* Called whenever the value of the selection changes.
* @param e the event that characterizes the change.
*/
public void valueChanged(TreeSelectionEvent e) {
if (!updatingSelection && tree.getSelectionCount() == 1) {
TreePath selPath = tree.getSelectionPath();
Object lastPathComponent = selPath.getLastPathComponent();
if (!(lastPathComponent instanceof DefaultMutableTreeNode)) {
Element selElement = (Element) lastPathComponent;
updatingSelection = true;
try {
getEditor().select(selElement.getStartOffset(),
selElement.getEndOffset());
} finally {
updatingSelection = false;
}
}
}
}
public void collapseChildren(TreePath tpath) {
if (tree != null) try {
markExpansionTransaction();
if (tpath == null || tree.isCollapsed(tpath)) return;
TreeModel tmodel = tree.getModel();
Object selected = tpath.getLastPathComponent();
int nchildren = tmodel.getChildCount(selected);
for (int i = 0; i < nchildren; i++) {
TreePath tp = tpath.pathByAddingChild(tmodel.getChild(selected, i));
tree.collapsePath(tp);
// tree.resetExpandedState(tp);
}
} finally {
clearExpansionTransaction();
}
}
private void handlePopupTriggerEvent(MouseEvent e) {
TreePath clickedPath = tree.getPathForLocation(e.getX(), e.getY());
if (clickedPath == null)
return;
DefaultMutableTreeNode node = (DefaultMutableTreeNode) clickedPath.getLastPathComponent();
Object clickedObject = node.getUserObject();
if (clickedObject instanceof RawDataFile)
dataFilePopupMenu.show(e.getComponent(), e.getX(), e.getY());
if (clickedObject instanceof Scan)
scanPopupMenu.show(e.getComponent(), e.getX(), e.getY());
if (clickedObject instanceof MassList)
massListPopupMenu.show(e.getComponent(), e.getX(), e.getY());
if (clickedObject instanceof PeakList)
peakListPopupMenu.show(e.getComponent(), e.getX(), e.getY());
if (clickedObject instanceof PeakListRow)
peakListRowPopupMenu.show(e.getComponent(), e.getX(), e.getY());
}
@Override
public void valueChanged(final String value) {
TreePath[] selectionPaths = operatorGroupTree.getSelectionPaths();
List<TreePath> expandedPaths = model.applyFilter(value);
for (TreePath expandedPath : expandedPaths) {
int row = this.operatorGroupTree.getRowForPath(expandedPath);
this.operatorGroupTree.expandRow(row);
}
GroupTree root = (GroupTree) this.operatorGroupTree.getModel().getRoot();
TreePath path = new TreePath(root);
showNodes(root, path, expandedPaths);
if (selectionPaths != null) {
for (TreePath selectionPath : selectionPaths) {
Object lastPathComponent = selectionPath.getLastPathComponent();
if (model.contains(lastPathComponent)) {
operatorGroupTree.addSelectionPath(selectionPath);
}
}
}
}
@Nullable
private Module[] getSelectedModules() {
final TreePath[] treePaths = myTree.getSelectionPaths();
if (treePaths != null) {
Set<Module> result = new HashSet<Module>();
for (TreePath path : treePaths) {
PackageDependenciesNode node = (PackageDependenciesNode)path.getLastPathComponent();
if (node instanceof ModuleNode) {
result.add(((ModuleNode)node).getModule());
}
else if (node instanceof ModuleGroupNode) {
final ModuleGroupNode groupNode = (ModuleGroupNode)node;
final ModuleGroup moduleGroup = groupNode.getModuleGroup();
result.addAll(moduleGroup.modulesInGroup(myProject, true));
}
}
return result.isEmpty() ? null : result.toArray(new Module[result.size()]);
}
return null;
}
public static void searchTree(JTree tree, TreePath path, String q, List<TreePath> rollOverPathLists) {
Object o = path.getLastPathComponent();
if (o instanceof TreeNode) {
TreeNode node = (TreeNode) o;
if (node.toString().startsWith(q)) {
rollOverPathLists.add(path);
tree.expandPath(path.getParentPath());
}
if (!node.isLeaf()) {
// Java 9: Collections.list(node.children())
Collections.list((Enumeration<?>) node.children())
.forEach(n -> searchTree(tree, path.pathByAddingChild(n), q, rollOverPathLists));
}
}
}
/**
* Gets the selected node.
*
* @return the selected node
*/
public Node getSelectedNode() {
TreePath path = tree.getSelectionPath();
if(path==null) {
return null;
}
return(Node) path.getLastPathComponent();
}
public static void visitAll(JTree tree, TreePath parent, boolean expand) {
TreeNode node = (TreeNode) parent.getLastPathComponent();
if (!node.isLeaf()) {
// Java 9: Collections.list(node.children())
Collections.list((Enumeration<?>) node.children())
.forEach(n -> visitAll(tree, parent.pathByAddingChild(n), expand));
}
if (expand) {
tree.expandPath(parent);
} else {
tree.collapsePath(parent);
}
}
@Override
public void mouseClicked(MouseEvent event) {
JTree tree = (JTree)event.getSource();
int x = event.getX();
int y = event.getY();
int row = tree.getRowForLocation(x, y);
TreePath path = tree.getPathForRow(row);
if (path != null) {
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)path.getLastPathComponent();
if (selectedNode != null && selectedNode.getChildCount() > 0 && event.getClickCount() > 1) {
// if (tree.isExpanded(path)) {
// tree.collapsePath(path);
// }
// else {
// tree.expandPath(path);
// }
return;
} else if (selectedNode instanceof CheckBoxTreeNode && event.getClickCount() == 1) {
CheckBoxTreeNode node = (CheckBoxTreeNode)selectedNode;
if (node != null) {
boolean isSelected = !node.isSelected();
node.setSelected(isSelected);
((DefaultTreeModel)tree.getModel()).nodeStructureChanged(node);
}
}
}
}
public void setSelectionPaths(final TreePath[] selectionPaths) {
mySelectedNodes.clear();
if (selectionPaths != null) {
for (TreePath selectionPath : selectionPaths) {
final InspectionConfigTreeNode node = (InspectionConfigTreeNode)selectionPath.getLastPathComponent();
mySelectedNodes.add(getState(node));
}
}
}
@Override
public void collapsePathRecursive( final TreePath path ) {
final TreeNode node = (TreeNode) path.getLastPathComponent();
// Going downward is faster because collapsing needs to update row indices of rows that follow
// and this way there are less rows to follow when collapsing a node
for ( int i = node.getChildCount() - 1; i >= 0; i-- )
collapsePathRecursive( path.pathByAddingChild( node.getChildAt( i ) ) );
collapsePath( path );
}
/**
* Preforms selected refactoring.
*/
private void refactorSelected() {
TreePath selectedPath = treeTable.getTree().getSelectionPath();
if (selectedPath != null && selectedPath.getPathCount() == refactorDepth) {
AbstractCandidateRefactoring computationSlice = (AbstractCandidateRefactoring) selectedPath.getLastPathComponent();
removeSelection();
doRefactor(computationSlice);
}
}
public Component getPrefferedInputVolume() {
TreePath path = findByName(new TreePath(root), new String[]{"MICROPHONE", "Volume"});
if (path == null) {
path = findByName(new TreePath(root), new String[]{"Capture source", "Capture", "Volume"});
}
if (path != null) {
if (path.getLastPathComponent() instanceof JavaMixer.ControlNode)
return ((JavaMixer.ControlNode) path.getLastPathComponent()).getComponent();
}
return null;
}
@Override
public void actionPerformed(ActionContext context) {
// This actions does double duty. When invoked from the icon, it closes all nodes.
// When invoked from the popup, it only closes selected nodes.
if (!(context instanceof DataTypesActionContext)) {
collapseAll(plugin.getProvider().getGTree()); // on the toolbar or filter field--collapse all
}
DataTypesActionContext dataTypeContext = (DataTypesActionContext) context;
if (dataTypeContext.isToolbarAction()) {
collapseAll(plugin.getProvider().getGTree()); // on the toolbar or filter field--collapse all
}
else {
DataTypeArchiveGTree gtree = (DataTypeArchiveGTree) context.getContextObject();
TreePath[] selectionPaths = gtree.getSelectionPaths();
if (selectionPaths == null || selectionPaths.length != 1) {
// no paths selected; close all paths
collapseAll(plugin.getProvider().getGTree());
}
if (selectionPaths != null) {
for (TreePath path : selectionPaths) {
GTreeNode node = (GTreeNode) path.getLastPathComponent();
if (node instanceof ArchiveRootNode) { // if the root is selected collapseAll
collapseAll(gtree);
return;
}
gtree.collapseAll(node);
}
}
}
}
@Override
protected boolean isEnabled() {
final TreePath selectionPath = myTree.getSelectionPath();
if (selectionPath != null) {
final MyNode node = (MyNode)selectionPath.getLastPathComponent();
return !node.isDisplayInBold();
}
else {
return false;
}
}
@SwingThread
boolean leavesShown() {
JTree jTree = getTree();
int rows = jTree.getRowCount();
for (int i = 0; i < rows; i++) {
TreePath treePath = jTree.getPathForRow(i);
Object lastPathComponent = treePath.getLastPathComponent();
if (lastPathComponent instanceof BugLeafNode) {
return true;
}
}
return false;
}
@Override public boolean isCellEditable(EventObject e) {
// return e instanceof MouseEvent;
Object source = e.getSource();
if (!(source instanceof JTree) || !(e instanceof MouseEvent)) {
return false;
}
JTree tree = (JTree) source;
Point p = ((MouseEvent) e).getPoint();
TreePath path = tree.getPathForLocation(p.x, p.y);
if (Objects.isNull(path)) {
return false;
}
Rectangle r = tree.getPathBounds(path);
if (Objects.isNull(r)) {
return false;
}
// r.width = panel.getButtonAreaWidth();
// return r.contains(p);
if (r.contains(p)) {
TreeNode node = (TreeNode) path.getLastPathComponent();
int row = tree.getRowForLocation(p.x, p.y);
TreeCellRenderer renderer = tree.getCellRenderer();
Component c = renderer.getTreeCellRendererComponent(tree, " ", true, true, node.isLeaf(), row, true);
c.setBounds(r);
c.setLocation(0, 0);
// tree.doLayout();
tree.revalidate();
p.translate(-r.x, -r.y);
return SwingUtilities.getDeepestComponentAt(c, p.x, p.y) instanceof JButton;
}
return false;
}
/**
* Unchecks the subtree with root path.
*
* @param path root of the tree to be unchecked
*/
public void uncheckSubTree(TreePath path) {
removeFromCheckedPathsSet(path);
removeFromGreyedPathsSet(path);
Object node = path.getLastPathComponent();
int childrenNumber = this.model.getChildCount(node);
for (int childIndex = 0; childIndex < childrenNumber; childIndex++) {
TreePath childPath = path.pathByAddingChild(this.model.getChild(node, childIndex));
uncheckSubTree(childPath);
}
}
/**
* Opens the definition of appropriate method for the selected suggestion by double-clicking or Enter key pressing.
*/
private void openMethodDefinition(InputEvent e) {
TreeTableTree treeTableTree = treeTable.getTree();
TreePath selectedPath = treeTableTree.getSelectionModel().getSelectionPath();
if (selectedPath != null) {
Object o = selectedPath.getLastPathComponent();
if (o instanceof ASTSlice) {
openDefinition(((ASTSlice) o).getSourceMethodDeclaration(), scope, (ASTSlice) o);
} else if (o instanceof ExtractMethodCandidateGroup) {
expandOrCollapsePath(e, treeTableTree, selectedPath);
}
}
}