How to get the levels to which the treeviewer is expanded? - java

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.

Related

JTree sometimes not expanding

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

Expanding tree nodes

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).

Purpose of Element in java Swing and its relationship to StyledDocument and paragraphs

I'm trying to understand the purpose of Element and how it can help with manipulating a StyledDocument that is to be displayed in a JEditorPane or JTextPane. I'm also trying to grasp how it relates to the concept of a "paragraph".
The javadoc for javax.swing.text.Element is almost nothing:
public interface Element
Interface to describe a structural piece of a document. It is intended to capture the spirit of an SGML element.
I am very familiar with the concept of an element in HTML and XML, and apparently this is something similar, but I just can't see its purpose as it relates to StyledDocument. At first I figured it was just something the StyledDocument used internally to manage the stop and start points of different styles, but then I saw code examples on the web where they used instances of Element.
I already have my own tree structure of the data I need to display in different fonts and colors, and traversing it will tell me where to change the font or color as needed. It looks like I'll be able to meet the immediate need with a series of calls to StyledDocument.setCharacterAttributes and setParagraphAttributes, without touching Element myself.
But I get the impression that using Element will be more efficient or cleaner. Please help me to get a proper understanding of Element and how it helps with StyledDocument and the concept of a paragrah, so even if I don't use any Elements right now I'll at least appreciate what I'm missing and know if and how to use it for the next similar situation.
In fact DefaultStyledDocument is a tree of Elements. There are BranchElements and LeafElements. Leaf represents a piece of text with text attributes like font size/style, font color attributes - bold, italic, underline etc. BrachElement contains Leaves or another branch elements. In simplest case the branches are paragraphs. But root Element is also instance of BranchElement. All Elements may have own AttributeSet. To find e.g. color of text LeafElement's set is asked. If color isn't defined the leaf's parent element is asked.
You can use this to see how Document is represented (Model) and How the model is represented in views.
http://java-sl.com/JEditorPaneStructureTool.html
The example shows HTMLDocument's structure but you can use the same code to see structure of StyledEditorKit as well

Colouring JTree's TreeNodes

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.

Richfaces:same trees in separate windows (or tabs) being in sync?

I have a newbie question in JSF, and particular in Richfaces.
I need my JSF application to have two pages showing the same tree, meaning that:
The two trees should be showing the same data
If I change something in one of the trees (i.e. open/close/add a node) the second tree should be automatically updated.
Is this possible? If yes, could you give me a brief outline?
UPDATE: Let me give some more details: two of the pages of the application will have this tree in their left pane. The first page will make the second one as a popup when a button is clicked. I want the two pages to show the same tree in sync.
Thanks!
You can use <a4j:push> to notify your trees about any changes.
Another option for others looking to be sure a part of their page is always up to date, check out the ajaxRendered attribute.

Categories

Resources