1 19 20 package org.netbeans.modules.palette.ui; 21 import java.awt.Point ; 22 import java.awt.Rectangle ; 23 import java.awt.event.KeyEvent ; 24 import java.awt.event.KeyListener ; 25 import java.awt.event.MouseEvent ; 26 import java.awt.event.MouseListener ; 27 import javax.swing.JTree ; 28 import javax.swing.tree.DefaultTreeModel ; 29 import javax.swing.tree.TreePath ; 30 import org.netbeans.modules.palette.DefaultSettings; 31 import org.openide.explorer.view.Visualizer; 32 import org.openide.nodes.Node; 33 34 43 class CheckListener implements MouseListener , KeyListener { 44 45 DefaultSettings settings; 46 47 public CheckListener( DefaultSettings settings ) { 48 this.settings = settings; 49 } 50 51 public void mouseClicked(MouseEvent e) { 52 JTree tree = (JTree ) e.getSource(); 57 Point p = e.getPoint(); 58 int x = e.getX(); 59 int y = e.getY(); 60 int row = tree.getRowForLocation(x, y); 61 TreePath path = tree.getPathForRow(row); 62 63 if( null == path ) 65 return; 66 67 Node node = Visualizer.findNode( path.getLastPathComponent() ); 68 if( null == node ) 69 return; 70 71 Rectangle chRect = CheckRenderer.getCheckBoxRectangle(); 72 Rectangle rowRect = tree.getPathBounds(path); 73 chRect.setLocation(chRect.x + rowRect.x, chRect.y + rowRect.y); 74 if (e.getClickCount() == 1 && chRect.contains(p)) { 75 boolean isSelected = settings.isNodeVisible( node ); 76 settings.setNodeVisible( node, !isSelected ); 77 tree.repaint(); 78 } 79 } 80 81 public void keyTyped(KeyEvent e) { 82 } 83 84 public void keyReleased(KeyEvent e) { 85 } 86 87 public void mouseEntered(MouseEvent e) { 88 } 89 90 public void mouseExited(MouseEvent e) { 91 } 92 93 public void mousePressed(MouseEvent e) { 94 } 95 96 public void mouseReleased(MouseEvent e) { 97 } 98 99 public void keyPressed(KeyEvent e) { 100 if (e.getKeyChar() == ' ') { 101 JTree tree = (JTree ) e.getSource(); 102 TreePath path = tree.getSelectionPath(); 103 if( null == path ) 104 return; 105 106 Node node = Visualizer.findNode( path.getLastPathComponent() ); 107 if( null == node ) 108 return; 109 110 boolean isSelected = settings.isNodeVisible( node ); 111 settings.setNodeVisible( node, !isSelected ); 112 tree.repaint(); 113 114 e.consume(); 115 } 116 } 117 } | Popular Tags |