Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 399
  • Last Modified:

How do I make DefaultTreeSelectionModel isPathSelected() case insensitive?

Hi,
How do I make DefaultTreeSelectionModel isPathSelected() case insensitive?
Currently it is a case sensitive match.

How do I match the path of the Tree case not sensitive?

Thanks
Jamie
0
jamie_lynn
Asked:
jamie_lynn
  • 4
  • 2
1 Solution
 
Kevin CrossChief Technology OfficerCommented:
Not sure I follow as isPathSelected() method takes a TreePath object as parameter.  This is an object equality issue.  The objects must equal -- this is not a string match.  Maybe I am missing something though.

Are you referring to this class:
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/tree/DefaultTreeSelectionModel.html
0
 
jamie_lynnAuthor Commented:
Yes, it is the correct class.
In DefaultTreeCellRendererl.getTreeCellRendererComponent(JTree tree, ....)
I call
TreePath path = tree.getPathForRow(row);
then call
selectionModel.isPathSelected(path);

If I call
((javax.swing.tree.DefaultMutableTreeNode)path.getPath()[0]).getUserObject()
It will return be a String
(java.lang.String) root

Thanks
Jamie
0
 
Kevin CrossChief Technology OfficerCommented:
The isPathSelected will tell you true or false if the TreePath you pass it is the currently selected one.
boolean pathSelected = selectionModel.isPathSelected(path);
if (pathSelected) {
   // do something
}

If your intent is to set the TreePath as selected path, then you must use:
selectionModel.setSelectedPath(path);

Hope that helps.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
jamie_lynnAuthor Commented:
I understand that part. My question was that if i call
boolean pathSelected = selectionModel.isPathSelected(path);

pathSelected is true even though when I call
selectionModel.setSelectedPath(newpath);
where newpath is [root, top]
and path is  [root, TOP]

The difference in case makes the pathselected false
Is there a way to make it case insensitive?

Thanks
Jamie
0
 
Kevin CrossChief Technology OfficerCommented:
There is probably an easy way, but I don't know what it is sorry.

This is the crazy idea rattling around in my head, but not sure if you can even use customized TreePath objects, especially for your particular situation but this is what I am thinking to make the path comparison case insensitive.

Regards,
Kevin
private class MyTreePath extends TreePath {
		private static final long serialVersionUID = 1174436545852519074L;
 
		@Override
		public boolean equals(Object o) {
			if (super.equals(o)) return true;
			
			if (o instanceof TreePath) {
				Object[] oPath = ((TreePath)o).getPath();
				Object[] iPath = this.getPath();
				
				if (oPath.length == iPath.length) {
					for (int i=0; i < iPath.length; i++) {
						if (!iPath[i].toString().equalsIgnoreCase(oPath.toString())) return false;
					}
					return true;
				}
			}
			return false;
		}
		
	}

Open in new window

0
 
Kevin CrossChief Technology OfficerCommented:
Had it as private inner class, so you can remove the private modifier on the class definition.  Left over code.
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now