1 19 20 package org.netbeans.modules.xml.refactoring.ui.j.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 java.beans.PropertyChangeListener ; 30 import java.beans.PropertyChangeSupport ; 31 import javax.swing.Action ; 32 import javax.swing.JTree ; 33 import javax.swing.tree.DefaultTreeModel ; 34 import javax.swing.tree.TreePath ; 35 import org.netbeans.modules.xml.refactoring.ui.j.api.RefactoringElement; 36 import org.openide.nodes.Node; 37 38 47 class CheckNodeListener implements MouseListener , KeyListener { 48 private final boolean isQuery; 49 private PropertyChangeSupport propertyChangeSupport; 50 51 public CheckNodeListener(boolean isQuery) { 52 this.isQuery = isQuery; 53 propertyChangeSupport = 54 new PropertyChangeSupport (this); 55 56 } 57 58 public void mouseClicked(MouseEvent e) { 59 JTree tree = (JTree ) e.getSource(); 64 Point p = e.getPoint(); 65 int x = e.getX(); 66 int y = e.getY(); 67 int row = tree.getRowForLocation(x, y); 68 TreePath path = tree.getPathForRow(row); 69 70 if (path != null) { 72 CheckNode node = (CheckNode) path.getLastPathComponent(); 73 if (isQuery) { 74 if (e.getClickCount() == 2) { 75 if (node.getChildCount() == 0){ 76 doPreferredAction(node); 77 } else { 78 if (tree.isCollapsed(row)) 79 tree.expandRow(row); 80 else 81 tree.collapseRow(row); 82 } 83 } 84 } else { 85 Rectangle chRect = CheckRenderer.getCheckBoxRectangle(); 86 Rectangle rowRect = tree.getPathBounds(path); 87 chRect.setLocation(chRect.x + rowRect.x, chRect.y + rowRect.y); 88 if (e.getClickCount() == 1 && chRect.contains(p)) { 89 boolean isSelected = !(node.isSelected()); 90 node.setSelected(isSelected); 91 if (node.getSelectionMode() == CheckNode.DIG_IN_SELECTION) { 92 if (isSelected) 93 tree.expandPath(path); 94 else 95 tree.collapsePath(path); 96 } 97 ((DefaultTreeModel ) tree.getModel()).nodeChanged(node); 98 if (row == 0) { 99 tree.revalidate(); 100 tree.repaint(); 101 } 102 } 103 else if (e.getClickCount() == 2 && chRect.contains(p) == false) { 105 if (node.getChildCount() == 0){ 106 doPreferredAction(node); 107 } 108 else { 109 if (tree.isCollapsed(row)) 110 tree.expandRow(row); 111 else 112 tree.collapseRow(row); 113 } 114 } 115 } 116 } 117 } 118 119 private void doPreferredAction(CheckNode node) { 120 Node originalNode = node.getOrigNode(); 121 if (originalNode != null) { 124 Action preferredAction = originalNode.getPreferredAction(); 125 if (preferredAction != null) { 126 String command = (String ) 127 preferredAction.getValue(Action.ACTION_COMMAND_KEY); 128 ActionEvent ae = new ActionEvent (originalNode, 0, command); 129 preferredAction.actionPerformed(ae); 130 } 131 } 132 } 133 134 public void keyTyped(KeyEvent e) { 135 } 136 137 public void keyReleased(KeyEvent e) { 138 if (e.getKeyCode() == KeyEvent.VK_ENTER) { 140 JTree tree = (JTree ) e.getSource(); 141 TreePath path = tree.getSelectionPath(); 142 if (path != null) { 143 CheckNode node = (CheckNode) path.getLastPathComponent(); 144 findInSource(node); 145 } 146 } 147 } 148 149 public void mouseEntered(MouseEvent e) { 150 } 151 152 public void mouseExited(MouseEvent e) { 153 } 154 155 public void mousePressed(MouseEvent e) { 156 } 157 158 public void mouseReleased(MouseEvent e) { 159 } 160 161 public void keyPressed(KeyEvent e) { 162 if (e.getKeyChar() == ' ') { 163 JTree tree = (JTree ) e.getSource(); 164 TreePath path = tree.getSelectionPath(); 165 if (path != null) { 166 CheckNode node = (CheckNode) path.getLastPathComponent(); 167 node.setSelected(!node.isSelected()); 168 e.consume(); 169 } 170 } 171 } 172 173 static void findInSource(CheckNode node) { 174 Object o = node.getUserObject(); 175 if (o instanceof RefactoringElement) { 176 ((RefactoringElement) o).openInEditor(); 177 } 178 } 179 180 192 193 public void addFindInSchemaViewListener(PropertyChangeListener l){ 194 propertyChangeSupport.addPropertyChangeListener(l); 195 } 196 197 public void removeFindInSchemaViewListener(PropertyChangeListener l){ 198 propertyChangeSupport.removePropertyChangeListener(l); 199 } 200 201 } | Popular Tags |