下面列出了javax.swing.tree.TreePath#equals ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
*
*
* @param path1
* @param path2
*
* @return
*/
public static boolean isDescendant(TreePath path1, TreePath path2) {
int count1 = path1.getPathCount();
int count2 = path2.getPathCount();
if (count1 <= count2) {
return false;
}
while (count1 != count2) {
path1 = path1.getParentPath();
count1--;
}
return path1.equals(path2);
}
/** Verifies the actual is within one of the expected */
private void assertCloseEnough(TreePath expected, TreePath actual) {
if (actual == null) {
fail("No actual TreePath found");
}
if (expected.equals(actual)) {
return; // good
}
// Unusual Code: we add some 'fudge' to the viewable tree rows (1 in the top and
// bottom direction). So, the actual path visible may be off by that
// much.
int fudge = 2; // 1 in both directions
int expectedRow = gTree.getRowForPath(expected);
if (rowNumberIsWithin(expected, actual, fudge)) {
return; // good enough
}
int actualRow = gTree.getRowForPath(actual);
fail("Tree paths do not match - expected row " + expectedRow + "; actual row " + actualRow);
}
String[] getSelectedCategoryPath() {
TreePath currentPath = scriptCategoryTree.getSelectionPath();
String[] currentCategory = null;
if (currentPath != null) {
if (currentPath.equals(previousPath)) {
return previousCategory;
}
if (currentPath.getPathCount() > 1) {
GTreeNode node = (GTreeNode) currentPath.getLastPathComponent();
currentCategory = getCategoryPath(node);
}
}
previousPath = currentPath;
previousCategory = currentCategory;
return currentCategory;
}
/**
* For a multi-selection case, verifies that a selection
* from the node's level is valid.
* Returns true if (1) either the paths of all children of node
* are selected, or if (2) none of the paths of all children of node
* are selected.
* Returns false if not all of the children of node are selected
*/
private boolean validPathSelection(ProgramNode node, TreePath[] selectedPaths) {
int nchild = node.getChildCount();
int numberSelected = 0;
for (int i = 0; i < nchild; i++) {
ProgramNode child = (ProgramNode) node.getChildAt(i);
TreePath childPath = child.getTreePath();
// see if childPath is in selected list
for (TreePath element : selectedPaths) {
if (childPath.equals(element)) {
++numberSelected;
break;
}
}
}
if (numberSelected == 0 || numberSelected == nchild) {
return true;
}
return false;
}
/** Configures a tree cell renderer and sets up sizing and the
* backing image from it */
public boolean configure (Object nd, JScrollPane tv, JTree tree, TreePath path, int row) {
boolean sameVn = setLastRendereredObject(nd);
boolean sameComp = setLastRenderedScrollPane (tv);
Component renderer = null;
bg = tree.getBackground();
boolean sel = tree.isSelectionEmpty() ? false :
tree.getSelectionModel().isPathSelected(path);
boolean exp = tree.isExpanded(path);
boolean leaf = !exp && tree.getModel().isLeaf(nd);
boolean lead = path.equals(tree.getSelectionModel().getLeadSelectionPath());
renderer = tree.getCellRenderer().getTreeCellRendererComponent(tree, nd, sel, exp, leaf, row, lead);
if (renderer != null) {
setComponent (renderer, tree);
}
return true;
}
@Override
public void actionPerformed( final ActionEvent event ) {
// Max expand depth for game events and tracker events
final int maxDepth = Env.APP_SETTINGS.get( Settings.RAW_TREE_EXPAND_DEPTH ) - 1;
tree.expandRow( 0 );
for ( int i = 0; i < root.getChildCount(); i++ ) {
final TreePath tp = new TreePath(
( (DefaultMutableTreeNode) root.getChildAt( i ) ).getPath() );
if ( tp.equals( gameEventsTreePath ) || tp.equals( trackerEventsTreePath ) ) {
if ( maxDepth >= 0 )
tree.expandPathRecursive( tp, maxDepth );
} else
tree.expandPathRecursive( tp );
}
}
private void checkSelectAndPopUp(MouseEvent evt)
{
if (evt.isPopupTrigger())
{
// If the user wants to select for Right mouse clicks then change the selection before popup
// appears
if (_session.getApplication().getSquirrelPreferences().getSelectOnRightMouseClick()) {
TreePath path = ObjectTree.this.getPathForLocation(evt.getX(), evt.getY());
boolean alreadySelected = false;
TreePath[] selectedPaths = ObjectTree.this.getSelectionPaths();
if (selectedPaths != null) {
for (TreePath selectedPath : selectedPaths) {
if (path != null && path.equals(selectedPath)) {
alreadySelected = true;
break;
}
}
}
if (!alreadySelected) {
ObjectTree.this.setSelectionPath(path);
}
}
showPopup(evt.getX(), evt.getY());
}
}
public void updateCheckBoxStates() {
final TreePath[] selectionPaths = checkBoxTree.getCheckBoxTreeSelectionModel().getSelectionPaths();
if (selectionPaths == null || selectionPaths.length == 0) {
selectAllCheckBox.setSelected(false);
selectAllCheckBox.setEnabled(true);
selectAllCheckBox.updateUI();
selectNoneCheckBox.setSelected(true);
selectNoneCheckBox.setEnabled(false);
selectNoneCheckBox.updateUI();
} else {
final TreePath rootPath = new TreePath(checkBoxTree.getModel().getRoot());
boolean allSelected = false;
for (TreePath selectionPath : selectionPaths) {
if (selectionPath.equals(rootPath)) {
allSelected = true;
}
selectAllCheckBox.setSelected(allSelected);
selectAllCheckBox.setEnabled(!allSelected);
selectAllCheckBox.updateUI();
selectNoneCheckBox.setSelected(false);
selectNoneCheckBox.setEnabled(true);
selectNoneCheckBox.updateUI();
}
}
}
@Override
public boolean onDoubleClick(MouseEvent e) {
final TreePath clickPath = myTree.getUI() instanceof WideSelectionTreeUI ? myTree.getClosestPathForLocation(e.getX(), e.getY())
: myTree.getPathForLocation(e.getX(), e.getY());
if (clickPath == null) return false;
final DataContext dataContext = DataManager.getInstance().getDataContext(myTree);
final Project project = dataContext.getData(CommonDataKeys.PROJECT);
if (project == null) return false;
final TreePath selectionPath = myTree.getSelectionPath();
if (selectionPath == null || !clickPath.equals(selectionPath)) return false;
final Object lastPathComponent = selectionPath.getLastPathComponent();
if (((TreeNode)lastPathComponent).isLeaf() || !expandOnDoubleClick(((TreeNode)lastPathComponent))) {
//Node expansion for non-leafs has a higher priority
processDoubleClick(e, dataContext, (TreeNode) lastPathComponent);
return true;
}
return false;
}
/**
* Validate the selection for a case of the multi-selection
* for either the popup menu or the plugin action.
* @return true if the root node is not selected, or
* a node and all of its children are selected or only
* a parent node is selected; return false if this is not
* the case.
*/
private boolean validMultiSelection() {
TreePath[] paths = tree.getSelectionPaths();
if (paths == null) {
return false;
}
TreePath rootPath = root.getTreePath();
for (TreePath element : paths) {
if (element.equals(rootPath)) {
return false;
}
}
for (TreePath path : paths) {
ProgramNode node = (ProgramNode) path.getLastPathComponent();
// if the node allows children, then verify that
// either (1) only the parent is selected, or
// (2) all children are selected
if (node.getAllowsChildren() && !validPathSelection(node, paths)) {
return false;
}
}
return true;
}
public static boolean findString(ProfilerTreeTable treeTable, String text, boolean matchCase, boolean next, TreeHelper helper) {
treeTable.putClientProperty(LAST_FIND_TEXT, text);
treeTable.putClientProperty(LAST_FIND_MATCH_CASE, matchCase);
if (!matchCase) text = text.toLowerCase();
int mainColumn = treeTable.convertColumnIndexToView(treeTable.getMainColumn());
TreePath selectedPath = treeTable.getSelectionPath();
if (selectedPath == null) selectedPath = treeTable.getRootPath();
boolean firstPath = true;
TreePath startPath = null;
int nodeType = helper == null ? TreeHelper.NODE_SEARCH_DOWN : helper.getNodeType(selectedPath);
do {
selectedPath = next ? treeTable.getNextPath(selectedPath, TreeHelper.isDown(nodeType)) :
treeTable.getPreviousPath(selectedPath, TreeHelper.isDown(nodeType));
TreeNode node = (TreeNode)selectedPath.getLastPathComponent();
if (helper != null) nodeType = helper.getNodeType(node);
if (TreeHelper.isSearch(nodeType)) {
String nodeValue = treeTable.getStringValue(node, mainColumn);
if (!matchCase) nodeValue = nodeValue.toLowerCase();
if (nodeValue.contains(text)) {
treeTable.selectPath(selectedPath, true);
return true;
}
}
if (startPath == null) startPath = selectedPath;
else if (firstPath) firstPath = false;
} while (firstPath || !selectedPath.equals(startPath));
ProfilerDialogs.displayInfo(MSG_NOTFOUND, ACTION_FIND, null);
return false;
}
private boolean isSelectionKept (TreePath selPath) {
if (curSelPath != null) {
TreePath oldSel = curSelPath.get();
if (oldSel != null && oldSel.equals(selPath)) {
return true;
}
}
return false;
}
private boolean isValidDoubleClickPath(MouseEvent event) {
TreePath locationPath = tree.getPathForLocation(event.getX(), event.getY());
TreePath selectionPath = tree.getSelectionPath();
if (selectionPath == null || locationPath == null || !selectionPath.equals(locationPath)) {
return false;
}
ClassTreeNode lastPathComponent = (ClassTreeNode)selectionPath.getLastPathComponent();
return !lastPathComponent.isPackageNode();
}
@Override
public void actionPerformed( final ActionEvent event ) {
final int maxDepth = Env.APP_SETTINGS.get( Settings.RAW_TREE_EXPAND_DEPTH ) - 1;
final TreePath[] selectionPaths = tree.getSelectionPaths();
for ( final TreePath tp : selectionPaths ) {
if ( tp.equals( gameEventsTreePath ) || tp.equals( trackerEventsTreePath ) ) {
if ( maxDepth >= 0 )
tree.expandPathRecursive( tp, maxDepth );
} else
tree.expandPathRecursive( tp );
}
}
public static boolean findString(ProfilerTreeTable treeTable, String text, boolean matchCase, boolean next, TreeHelper helper) {
treeTable.putClientProperty(LAST_FIND_TEXT, text);
treeTable.putClientProperty(LAST_FIND_MATCH_CASE, matchCase);
if (!matchCase) text = text.toLowerCase();
int mainColumn = treeTable.convertColumnIndexToView(treeTable.getMainColumn());
TreePath selectedPath = treeTable.getSelectionPath();
if (selectedPath == null) selectedPath = treeTable.getRootPath();
boolean firstPath = true;
TreePath startPath = null;
int nodeType = helper == null ? TreeHelper.NODE_SEARCH_DOWN : helper.getNodeType(selectedPath);
do {
selectedPath = next ? treeTable.getNextPath(selectedPath, TreeHelper.isDown(nodeType)) :
treeTable.getPreviousPath(selectedPath, TreeHelper.isDown(nodeType));
TreeNode node = (TreeNode)selectedPath.getLastPathComponent();
if (helper != null) nodeType = helper.getNodeType(node);
if (TreeHelper.isSearch(nodeType)) {
String nodeValue = treeTable.getStringValue(node, mainColumn);
if (!matchCase) nodeValue = nodeValue.toLowerCase();
if (nodeValue.contains(text)) {
treeTable.selectPath(selectedPath, true);
return true;
}
}
if (startPath == null) startPath = selectedPath;
else if (firstPath) firstPath = false;
} while (firstPath || !selectedPath.equals(startPath));
ProfilerDialogs.displayInfo(MSG_NOTFOUND, ACTION_FIND, null);
return false;
}
private void selectTargetInTree(Wrapper dbgElm) {
TestElement wrpElm = (TestElement) dbgElm.getWrappedElement();
TreePath treePath = getTreePathFor(wrpElm);
if (treePath == null) {
// case for wrapped controllers
treePath = getTreePathFor(dbgElm);
}
if (treePath == null) {
log.debug("Did not find tree path for element");
} else {
if (treePath.equals(tree.getSelectionPath())) {
tree.setSelectionPath(treePath.getParentPath());
}
tree.setSelectionPath(treePath);
}
Sampler sampler = debugger.getCurrentSampler();
if (sampler != null) {
TreePath samplerPath = getTreePathFor(sampler);
if (samplerPath != null) {
log.debug("Expanding: " + samplerPath);
tree.expandPath(samplerPath.getParentPath());
}
}
tree.repaint();
}
private static boolean isDescendant(TreePath path1, final TreePath path2) {
int count1 = path1.getPathCount();
final int count2 = path2.getPathCount();
if (count1 <= count2) {
return false;
}
while (count1 != count2) {
path1 = path1.getParentPath();
count1--;
}
return path1.equals(path2);
}
@Override
@SuppressWarnings("unchecked")
public int compare(RowMapping rm1, RowMapping rm2) {
int index1 = rm1.getModelRowIndex();
int index2 = rm2.getModelRowIndex();
if (index1 == index2) {
return 0;
}
TreePath tp1 = getLayoutCache().getPathForRow(index1);
TreePath tp2 = getLayoutCache().getPathForRow(index2);
if (tp1 == null) {
if (tp2 == null) {
return 0;
} else {
return -1;
}
} else if (tp2 == null) {
return 1;
}
if (tp1.isDescendant(tp2)) {
return -1;
}
if (tp2.isDescendant(tp1)) {
return 1;
}
boolean tp1Changed = false;
boolean tp2Changed = false;
TreePath parent1 = tp1.getParentPath();
TreePath parent2 = tp2.getParentPath();
if (parent1 != null && parent2 != null && parent1.equals(parent2) &&
getOutlineModel().isLeaf(tp1.getLastPathComponent()) &&
getOutlineModel().isLeaf(tp2.getLastPathComponent())) {
return ascending ? super.compare(rm1, rm2) : - super.compare(rm1, rm2);
}
while (tp1.getPathCount() < tp2.getPathCount()) {
tp2 = tp2.getParentPath();
tp2Changed = true;
}
while (tp1.getPathCount() > tp2.getPathCount()) {
tp1 = tp1.getParentPath();
tp1Changed = true;
}
parent1 = tp1.getParentPath();
parent2 = tp2.getParentPath();
while (parent1 != null && parent2 != null && !parent1.equals(parent2)) {
tp1 = parent1;
tp2 = parent2;
parent1 = tp1.getParentPath();
parent2 = tp2.getParentPath();
tp1Changed = true;
tp2Changed = true;
}
if (tp1Changed || tp2Changed) {
return compare(tempSortMap.get(tp1), tempSortMap.get(tp2));
} else {
return ascending ? super.compare(rm1, rm2) : - super.compare(rm1, rm2);
}
}
/**
* Convenience method to test whether drop location is valid
*
* @param destination
* The destination path
* @param dropper
* The path for the node to be dropped
* @param type
* object data type
* @return null if no problems, otherwise an explanation
*/
private String testDropTarget(TreePath destination, TreePath dropper, byte type) {
boolean destinationPathIsNull = destination == null;
if (destinationPathIsNull) {
return "Invalid drop location.";
}
DBBrowserNode node = (DBBrowserNode) destination.getLastPathComponent();
if (!node.getAllowsChildren()) {
return "This node does not allow children";
}
if (destination.equals(dropper)) {
return "Destination cannot be same as source";
}
if (dropper.isDescendant(destination)) {
return "Destination node cannot be a descendant.";
}
if (dropper.getParentPath().equals(destination)) {
return "Destination node cannot be a parent.";
}
if ((type == DBObject.QUERIES) || (type == DBObject.FOLDER_QUERY)) {
if ((node.getDBObject().getType() != DBObject.FOLDER_QUERY)
&& (node.getDBObject().getType() != DBObject.QUERIES_GROUP)) {
return "No query folder";
}
} else if ((type == DBObject.REPORTS) || (type == DBObject.FOLDER_REPORT)) {
if ((node.getDBObject().getType() != DBObject.FOLDER_REPORT)
&& (node.getDBObject().getType() != DBObject.REPORTS_GROUP)) {
return "No report folder";
}
} else if ((type == DBObject.CHARTS) || (type == DBObject.FOLDER_CHART)) {
if ((node.getDBObject().getType() != DBObject.FOLDER_CHART)
&& (node.getDBObject().getType() != DBObject.CHARTS_GROUP)) {
return "No chart folder";
}
} else {
return "No folder";
}
return null;
}
@Override
public boolean isDropRedundant(@Nonnull TreePath source, @Nonnull TreePath target) {
return target.equals(source.getParentPath()) || MoveHandler.isMoveRedundant(getPsiElement(source), getPsiElement(target));
}