1 19 20 package org.netbeans.modules.java.editor.codegen.ui; 21 22 import java.awt.Point ; 23 import java.awt.Rectangle ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.KeyEvent ; 26 import java.awt.event.KeyListener ; 27 import java.awt.event.MouseEvent ; 28 import java.awt.event.MouseListener ; 29 import javax.swing.Action ; 30 import javax.swing.JScrollPane ; 31 import javax.swing.JTree ; 32 import javax.swing.tree.TreePath ; 33 import javax.swing.tree.TreeSelectionModel ; 34 import org.openide.explorer.view.BeanTreeView; 35 import org.openide.explorer.view.Visualizer; 36 import org.openide.nodes.Node; 37 38 42 public class CheckTreeView extends BeanTreeView { 43 44 private static final JScrollPane FOR_BORDER = new JScrollPane (); 45 46 47 public CheckTreeView() { 48 49 setFocusable( false ); 50 51 setBorder(FOR_BORDER.getBorder()); 52 setViewportBorder(FOR_BORDER.getViewportBorder()); 53 54 CheckListener l = new CheckListener(); 55 tree.addMouseListener( l ); 56 tree.addKeyListener( l ); 57 58 CheckRenderer check = new CheckRenderer(); 59 tree.setCellRenderer( check ); 60 tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); 61 62 tree.setShowsRootHandles(false); 63 } 64 65 class CheckListener implements MouseListener , KeyListener { 66 67 69 public void mouseClicked(MouseEvent e) { 70 Point p = e.getPoint(); 71 TreePath path = tree.getPathForLocation(e.getPoint().x, e.getPoint().y); 72 toggle( path ); 73 } 74 75 public void keyTyped(KeyEvent e) {} 76 77 public void keyReleased(KeyEvent e) {} 78 79 public void mouseEntered(MouseEvent e) {} 80 81 public void mouseExited(MouseEvent e) {} 82 83 public void mousePressed(MouseEvent e) {} 84 85 public void mouseReleased(MouseEvent e) {} 86 87 89 public void keyPressed(KeyEvent e) { 90 if (e.getKeyCode() == KeyEvent.VK_SPACE || e.getKeyCode() == KeyEvent.VK_ENTER ) { 91 92 JTree tree = (JTree ) e.getSource(); 93 TreePath path = tree.getSelectionPath(); 94 95 if ( toggle( path )) { 96 e.consume(); 97 } 98 } 99 } 100 101 103 private boolean toggle( TreePath treePath ) { 104 105 if( treePath == null ) 106 return false; 107 108 Node node = Visualizer.findNode( treePath.getLastPathComponent() ); 109 if( node == null ) 110 return false ; 111 112 ElementNode.Description description = node.getLookup().lookup(ElementNode.Description.class); 113 if (description != null && description.isSelectable() ) { 114 description.setSelected( !description.isSelected() ); 115 return true; 116 } 117 118 return false; 119 } 120 121 } 122 123 } 124 | Popular Tags |