Author Topic: JTree node context menus  (Read 70702 times)

aevett

  • Customer
  • Sr. Member
  • *
  • Posts: 482
    • View Profile
JTree node context menus
« on: September 21, 2007, 10:37:04 AM »
The nodes of my native JTree have JPopupMenus.  In v5.3 of WebCream, I modified DefaultJTreeRenderer to allow a left-click on the already selected node to display a menu.  To do this, I generated a wcPulldownMenu for the selected node in my custom JTree renderer and modified the renderNodeLink() method to have a different onclick value.  To get this to work in v5.3, I had to change the class of the tree node from tree/treeSel to menuButton.

I've ported this to v6.1.  I am avoiding changing the tree node's class because this always caused positional and display problems with the node that I had to try to compensate for, so I'm keeping the class=tree/treeSel.  I have onclick="return buttonClick(this, 'mymenuname');".  While this does display the menu, the menu is positioned on top of the tree node.  How do I position the menu below the tree node?

I would much rather have context menu support via right mouse clicks.  Preferably I'd like to have a right-click select the tree node if it's not already selected and then display it's context menu.  Is there a way that I can do this?

Support Team

  • Administrator
  • Hero Member
  • *****
  • Posts: 1074
    • View Profile
Re: JTree node context menus
« Reply #1 on: September 21, 2007, 11:01:02 AM »
I think the right solution is to enhance JTree rendering to support right mouse clicks on a JTree node. I think it shouldn't be too hard given that we already support right mouse clicks. The only thing that I think needs to be actually done is to make sure that the node is selected before the popup menu is displayed. The node selection is handled by ClIt() that delegates to  setSelectedItemId(). So I think we need to add a JavaScript function CClIt() that would call ClIt() and then buttonClick(), and add it as oncontextmenu="CClit(....)" to all rendered nodes, it should work.

We can do this enhancement as part of our migration deal - how soon do you need it?

aevett

  • Customer
  • Sr. Member
  • *
  • Posts: 482
    • View Profile
Re: JTree node context menus
« Reply #2 on: September 21, 2007, 12:01:10 PM »
I can wait until for the enhancement until we purchase the next version if I can figure out a way to get my current code to display the menu under the node rather than on top of it.  Do I need to create some custom javascript function?

Support Team

  • Administrator
  • Hero Member
  • *****
  • Posts: 1074
    • View Profile
Re: JTree node context menus
« Reply #3 on: September 21, 2007, 07:25:20 PM »
We've added JPopupMenu support to JTree. I've uploaded a new build to the internal FTP site. It includes fixed to the problems you've mentioned in the previous post. Please treat it as a Beta quality because it required a few changes in common rendering classes.

You can look at TreeDialog.java and run WindowsThemeDemo to see how right mouse click can be added to tree nodes.

aevett

  • Customer
  • Sr. Member
  • *
  • Posts: 482
    • View Profile
Re: JTree node context menus
« Reply #4 on: September 24, 2007, 09:54:47 AM »
Thank you!  I still will need to have a custom jtree renderer as I also override the default node images.  Can you please make the new DefaultJTreeRenderer.java source available.

Support Team

  • Administrator
  • Hero Member
  • *****
  • Posts: 1074
    • View Profile
Re: JTree node context menus
« Reply #5 on: September 24, 2007, 05:47:42 PM »
Source file is attached.

aevett

  • Customer
  • Sr. Member
  • *
  • Posts: 482
    • View Profile
Re: JTree node context menus
« Reply #6 on: September 25, 2007, 11:04:39 AM »
I'm having some trouble making this work.  I have the following questions:

- I'm not sure I understand the implementation in DefaultJTreeRenderer.  Each node in the tree appears to be appending the same context menu over and over, which won't work for me.  For me, the menu changes depending on what node or nodes are selected and I don't actually generate the menu until a node is selected.  Could the implementation be changed as follows:

Only generate one content menu for the tree (which is for the currently selected node or nodes).  When right-click a selected node, display the generated content menu (no submit is necessary).  When right-click an unselected node, submit a selection of the node and display the (newly) generated content menu in the returned page.

- For my v5.3 custom JTree renderer, I had already created an interface to my JTree that returned the currently selected row(s) JPopupMenu.  Since I need to support JRE 1.4 and therefore can't rely on set/getComponentPopupMenu() methods, I need to modify the new appendContentMenu() in DefaultJTreeRenderer.  I was trying to use RenderUtils.appendContextMenu(HTMLPage page, Component component, JPopupMenu contextMenu), but it looks like that method no longer exists or at least is no longer public.  Can it be made available again?  Also, will the new RenderUtils.appendContextMenuEventHandler() work for me since I'm not using set/getComponentPopupMenu(), or do I need a version where I can pass in the JPopupMenu?

Support Team

  • Administrator
  • Hero Member
  • *****
  • Posts: 1074
    • View Profile
Re: JTree node context menus
« Reply #7 on: September 25, 2007, 04:22:20 PM »
First and foremost. I think it would be better to include your changes that support icons in tree nodes into the DefaultJTreeRenderer because they are generic. This way you won't be stuck changing your code again when default renderer is refactored (and it is already refactored for AjaxSwing).

Second. Since you have an Open Source License, would you like to have SVN repository access? This way you wouldn't have to ask for source files every time.

Third.

Yes, the current implementation supports the context menu at the tree level. To support menus at the node level we need a way to tell what the menu should look like for each node. Do you know if Swing has standard support for menus at the node level? If not, we'll have to define and interface with getContextMenu(TreeNode node) method and have your node implement it. Would that work?

I think the method in RenderUtils has been broken down into 2 methods

RenderUtils.appendContextMenuEventHandler(page, tree) - appends "onContextMenu=..." code to HTML element
renderContextMenu(HTMLPage page, Component component)  - writes the contents of the menu to the HTML page



aevett

  • Customer
  • Sr. Member
  • *
  • Posts: 482
    • View Profile
Re: JTree node context menus
« Reply #8 on: September 25, 2007, 05:09:13 PM »
First) I agree in principal with getting my changes for supporting different images into the DefaultJTreeRenderer, but how do you propose doing that?  In my custom JTree renderer, I've got changes to renderItemHTML_IE(), renderNodeLink(), appendImage(), getImageForNode(), and a bunch of additional static ints and static Strings for my 20 images.  I actually have two custom JTree renderers because I have two different trees.  And I've got corresponding changes to WebCream_tree.js.

Second) Okay, but I haven't used Subversion before.

Third) As far as I know, Swing doesn't have a standard method for supporting menus at the node level.  But I don't think a getContextMenu(TreeNode node) method will work for me.  I support multiple node selections, and the menu for a node can change depending if and what other nodes are also selected.  This is one of the reasons why I don't create the menu for a node until the node is selected.  I also can have a large number of nodes, so I don't like the idea of adding more overhead with a menu for every node.  I really think the best way is to define an interface that returns the menu for the selected node(s) (I guess you could use set/getComponentPopupMenu(), but I need to support JDK1.4) and generate only that menu.  If an unselected node is right-clicked, I think you should submit the selection (it has to be submitted anyway) and generate (and display) a new menu.

Support Team

  • Administrator
  • Hero Member
  • *****
  • Posts: 1074
    • View Profile
Re: JTree node context menus
« Reply #9 on: September 25, 2007, 07:14:52 PM »
1. I think what we need to do is define an interface (actually, we can use the same interface that we plan for the context menu) that would have a method getIcon(TreeNode node). You can implement it to return the icon and if it's not null, DefaultJTreeRenderer will use it. The things is that DefaultJTreeRenderer has already changed quite a bit in AjaxSwing so the issue would be do we want to keep making those changes in 6.1 or just move to 7.0. Maintaining 2 branches is a pain but it depends on your timeframe. 7.0 Beta will be available in a couple of weeks.

2. You can read up on Subversion, it's very easy. Install TortoiseSVN and all you'll need from us is URL and user name/password.

3. You may be right about generating the menu for selected node only. We force selection anyway so there is no point in pregenerating the tree menus. Only issue with that is right mouse click menu would take a while to pop up. And for those apps where the menu is the same for all nodes, this would be a disadvantage.

Sounds to me that the right thing to do this would require further changes to DefaultJTreeRenderer. Can you live with the way it is right now and wait for 7.0 where we'll provide the functionality the way we describe here?

We can still extend 6.1 but the tradeoff is that you'll have to do migration again once 7.0 is out.

aevett

  • Customer
  • Sr. Member
  • *
  • Posts: 482
    • View Profile
Re: JTree node context menus
« Reply #10 on: September 26, 2007, 12:02:52 PM »
1. I can wait for 7.0.  I'll email you my custom JTree renderer that you can compare to the v6.1.3 DefaultJTreeRenderer so you can see what I'm doing.  I'll also email you my changes to WebCream_tree.js.

2. Okay, I'll get Subversion set up and try it.

3. I can wait for 7.0.

aevett

  • Customer
  • Sr. Member
  • *
  • Posts: 482
    • View Profile
Re: JTree node context menus
« Reply #11 on: May 27, 2008, 04:44:24 PM »
Is the enhancement for JTreeNode context menus and icons included in v2.1.1?  Is so, what is the interface class I need to use and do you have any examples showing use of this interface?

Support Team

  • Administrator
  • Hero Member
  • *****
  • Posts: 1074
    • View Profile
Re: JTree node context menus
« Reply #12 on: May 27, 2008, 05:54:44 PM »

aevett

  • Customer
  • Sr. Member
  • *
  • Posts: 482
    • View Profile
Re: JTree node context menus
« Reply #13 on: May 28, 2008, 03:39:27 PM »
I'm starting with the context menus in our trees.  I implemented the TreeSupport interface in my JTree, but I'm not getting the right-click menus in the browser.  I looked in my copy of the AjaxSwing source code and I don't see where TreeSupport.getNodeContextMenu() is ever called in the DefaultJTreeRenderer?  (I do see where TableSupport.getContextMenu() is called in DefaultJTableRenderer.)

aevett

  • Customer
  • Sr. Member
  • *
  • Posts: 482
    • View Profile
Re: JTree node context menus
« Reply #14 on: May 28, 2008, 04:11:14 PM »
And in regards to the image bundle for custom images, would it be possible to extend the concept of the image bundle to allow for different "folder" and "item" icons depending on the node.  We use different icons for folders and leafs depending on node type.  I've attached a screen shot of our native tree.

I think to support multiple sets of folder and item icons, you'd have to add something like a

public String getNodeImageName(TreeNode node)

method to the TreeSupport interface which if it returns a non-null String is the base name of the set.  So if getNodeImageName() returned "host" for a branch node, then there would be hostClosed.gif, hostLastClosed.gif, hostLastOpen.gif, and hostOpen.gif files in the custom javascript image bundle and custom folder.  And if getNodeImageName() returned "service" for a leaf node, then there would be service.gif and serviceLast.gif files in the custom javascript image bundle and custom folder.