package com.vci.client.ui.swing; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.event.KeyListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionAdapter; import java.awt.event.MouseMotionListener; import javax.swing.BorderFactory; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JList; import javax.swing.JPopupMenu; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.MenuSelectionManager; import javax.swing.SwingUtilities; import javax.swing.plaf.basic.ComboPopup; import javax.swing.tree.TreePath; public class TreePopup extends JPopupMenu implements ComboPopup{ /** * */ private static final long serialVersionUID = 6071571114509174652L; protected JTreeComboBox comboBox; protected JScrollPane scrollPane; protected MouseMotionListener mouseMotionListener; protected MouseListener mouseListener; private MouseListener treeSelectListener = new MouseAdapter(){ public void mouseReleased(MouseEvent e){ JTree tree = (JTree)e.getSource(); TreePath tp = tree.getPathForLocation(e.getPoint().x, e.getPoint().y); if(tp == null){ return; } comboBox.setSelectedItem(tp); togglePopup(); MenuSelectionManager.defaultManager().clearSelectedPath(); } }; public TreePopup(JComboBox comboBox){ this.comboBox = (JTreeComboBox)comboBox; setBorder(BorderFactory.createLineBorder(Color.black)); setLayout(new BorderLayout()); setLightWeightPopupEnabled(comboBox.isLightWeightPopupEnabled()); JTree tree = this.comboBox.getTree(); if(tree != null){ scrollPane = new JScrollPane(tree); scrollPane.setBorder(null); add(scrollPane, BorderLayout.CENTER); tree.addMouseListener(treeSelectListener); } } public void show(){ updatePopup(); show(comboBox, 0, comboBox.getHeight()); comboBox.getTree().requestFocus(); } public void hide(){ setVisible(false); comboBox.firePropertyChange("popupVisible", true, false); } protected JList list = new JList(); public JList getList(){ return list; } public MouseMotionListener getMouseMotionListener(){ if(mouseMotionListener == null){ mouseMotionListener = new MouseMotionAdapter(){}; } return mouseMotionListener; } public KeyListener getKeyListener(){ return null; } public void uninstallingUI(){} /** * Implementation of ComboPopup.getMouseListener(). * * @return a MouseListener or null * @see ComboPopup#getMouseListener */ public MouseListener getMouseListener(){ if(mouseListener == null){ mouseListener = new InvocationMouseHandler(); } return mouseListener; } protected void togglePopup(){ if(isVisible()){ hide(); } else{ show(); } } protected void updatePopup(){ setPreferredSize(new Dimension(comboBox.getSize().width, 200)); Object selectedObj = comboBox.getSelectedItem(); if(selectedObj != null){ TreePath tp = (TreePath)selectedObj; ((JTreeComboBox)comboBox).getTree().setSelectionPath(tp); } } protected class InvocationMouseHandler extends MouseAdapter{ public void mousePressed(MouseEvent e){ if(!SwingUtilities.isLeftMouseButton(e) || !comboBox.isEnabled()){ return; } if(comboBox.isEditable()){ Component comp = comboBox.getEditor().getEditorComponent(); if((!(comp instanceof JComponent)) || ((JComponent)comp).isRequestFocusEnabled()){ comp.requestFocus(); } } else if(comboBox.isRequestFocusEnabled()){ comboBox.requestFocus(); } togglePopup(); } } }