Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Section
borderfalse
Column

Column

JContextTreePopup

Like the JContextTree, the JContextTreePopup is a frame class. It's not a JPopupMenu (well.. yes at the end it will be a JPopupMenu, but you cannot access it directly). It's not just a popup menu, this one is dynamic, what he shows depends on what is selected in the tree.

How to use it ?

Code Block

//Manipulating popup menu-----------------------------------------------
        JContextTreePopup popup = tree.getPopupMenu();

        TreePopupItem myTreePopupItem = buildPopupItem();
        
        popup.addItem(myTreePopupItem);
//      popup.addItem( index, myTreePopupItem);
//      popup.addAllItem( index, Collection <? extends TreePopupItem>);
//      popup.addAllItem(Collection <? extends TreePopupItem>);
//      popup.removeItem( myTreePopupItem );
//      popup.removeItem( index );
//      TreePopupItem[]  tpi = popup.getControls();


public TreePopupItem buildPopupItem() {

        class ActionItem implements TreePopupItem {

            private JMenu menu = new JMenu("Grid Menu");

            public ActionItem() {

                menu.add(new JMenuItem("Action1"));
                menu.add(new JMenuItem("Action2"));
                menu.add(new JCheckBoxMenuItem("check this"));

            }

            public boolean isValid(TreePath[] selection) {
                //We said it could be visible only with rasters
                // and only if there is one selected 
                if (selection.length == 1) {
                    ContextTreeNode node = (ContextTreeNode) selection[0].getLastPathComponent();
                    
                    if (node.getUserObject() instanceof MapLayer) {
                            return true;
                        
                    }
                }
                return false;
            }

            public Component getComponent(TreePath[] selection) {
                // We can return whatever we want, a classic JMenuItem
                // or much more complicate components. we could imagine
                // a JTable or an animated component. there's no limit.
                return menu;
            }
        }
        
        return new ActionItem();

    }

The JContextTreePopup works with TreePopupItem.
Each TreePopupItem is a component that might be visible in the final JPopupMenu but only if the condition described in the TreePopupItem are met.