I am new to eclipse RCP, well as a matter of fact to java also. Have a very basic question.
I have a JFace TreeViewer. I want to expand a particular node in that. The catch is I only have the name of the node. and no information apart from that.
I tried using treeItem, compared its string with that of the node name that I have, thus I got the node. I tried expanding it in the contentprovider of the tree. But i am not getting the desired output. When I check it in the log i get that it is expanded but it doesnt show in the viewer. I am performing this in display.asyncExec method in the contentprovider.
I hope the question is clear.
JFace viewers were created so that developers wouldn't have to mess around with SWT widgets and could use higher level API instead. When using JFace's viewer/content provider/label provider you should be in control of what nodes are in your tree (thus when you say you only know the name, I assume you are using SWT Tree directly). You can read about JFace viewers from Eclipse help.
To expand a tree node use expandToLevel(Object elementOrTreePath, int level) method of TreeViewer (the level is relative to the node that is expanded, not the root of the tree).
Related
lets consider below Treeviewer data,
Project
->package1
->package2
->->class1
The Viewer has been expanded to level-2. But, how to know that to what level the viewer has been expanded.
I am aware of setting the viewer to expand to a particular level by expandToLevel() method.
Is there a way to get the maximum level to which the viewer has been expanded?
I don't think there is a simple way to get this information.
You could use the getExpandedTreePaths method and find the longest TreePath that is returned. But note that the JavaDoc for this says:
Returns a list of tree paths corresponding to expanded nodes in this
viewer's tree, including currently hidden ones that are marked as
expanded but are under a collapsed ancestor.
which might not be what you want.
I am making a program in Java and I have a SWT tree widget that the user is supposed to toy with via drag and drop.
Now the problem is that when the user drags and drops a branch, I want to change the branch item's parent to whichever treeItem it was dropped on, but there doesn't appear to be any way to do so. I could simply create a new treeItem, but there is no easy way to transfer the children, so I need to redefine all the children, and their grandchildren and so on recursively. It seems pretty klunky and inefficient to me that I need to remake the entire branch just to change the parent.
Is there any clean way to do this?
Well, the clean way is to separate view/widgets from model (as in MVC). In case of SWT, you should use TreeViewer and implement ITreeContentProvider. jFace will take care of creating tree nodes for you and you only need to update your model and refresh the viewer.
I have this pretty big project that I'm working on, graphic editor, and I've had some big problems working with swing components. However, now I have a programmers worst nightmare, a bug that is happening only some of the times.
If I try to select the elements in my JTree, whether when adding elements, or when selecting them when they are selected in diagram (JInternalFrame), some of them don't get expanded.
My structure is something like this, I have a workspace, containing projects and projects that contain diagrams. Diagrams hold all the elements I have, let's say circles, rectangles and so on, in folders (if they are circles, they are put in circles folder...).
Another thing to know is that I select my elements via setSelectedPath/Paths method of my JTree.
Some of the things that may help understanding what I do and what I tried:
I made sure my nodes know how to get to root. (getTreeModel.getPathToRoot returns good path)
I tried adding paths to trees selection model and to tree directly
I have set the trees expandsSelectedPaths to true
Nodes are selected when I expand my tree manually (they even expand afterwards), until I add new elements of type that caused problems
This happens about once when switching through 5 types of elements, and some stranger things happen when I try to add other type of element after I added one that made problems
I hope someone will know what to do, although I think this is very complicated problem. Please ask anything that may help you help me.
OK, thanks everyone for answering, but I have found a very simple workaround for this problem.
The thing was expanding has no effect if the last path component is leaf, for some reason.
What I did was simply making my leaf nodes return false for isLeaf method, and all my problems went away.
if addWhatever() to JTree firing the correct TreeModelEvent (fireChildAdded(), firePathChanged(), fireChildrenLoaded(), treeStructureChanged())
all changes for GUI would be moved to the BackGround Task(s), please look at SwingWorker or Runnable#Thread (most clear and easiest way), but Runnable#Thread required wrapping all output (Swing methods) to the invokeLater
I have created two trees in gwt-ext one contains the values of nodes that can be dragged and other the nodes which can be dropped on. The problem being i am not able to set only a particular set of leaves mentioned in the array, from a tree to be dragged on other tree while the leaves which are not in the array cannot be dragged. I have enabled drag drop on the tree and then manually allow a leaf to disable drag but its not working. Tried it using the setallowdrag() method and also using setTreeAttribute("allowDrag",false) but don't know why it just doesn't work. Please help.
TreeNode n22=treePanel.getNodeById("ynode-121");
n22.setTreeAttribute("allowDrag","false");
n22.setAllowDrag(false);
I have a JTree which I give objects that implement the TreeNode interface, and a custom TreeModel to display them (not using DefaultMutableTreeNode). I would like to change the text colour of some nodes. I can't find anything in the docs, except javax.swing.tree.DefaultTreeCellRenderer.setTextNonSelectionColor(Color newColor), but it will change everything indiscriminately, and I only need it for some nodes (specifically, broken links, i.e. nodes whose corresponding files can't be found on the disk, should be greyed out, the rest should be default). Can it be done, and how?
You are close to your answer. What you need to do is Sub Class the DefaultTreeCellRenderer and override a few of the DefaultTreeCellRenderer's methods. Then make sure you tell the tree to use your custom cell renderer.
What you will need to do is have some state variables that indicate whether or not a link is broken, and set the color of the node based on that.
You might also look at org.netbeans.swing.outline, mentioned in this answer. Ordinary extensions of TableCellRenderer and the RenderDataProvider interface make it especially easy to customize the appearance of rows in the tree.