1 18 19 package org.apache.jmeter.gui.tree; 20 21 import java.awt.Container ; 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.awt.event.KeyEvent ; 25 import java.awt.event.KeyListener ; 26 import java.awt.event.MouseEvent ; 27 import java.awt.event.MouseListener ; 28 import java.awt.event.MouseMotionListener ; 29 30 import javax.swing.JLabel ; 31 import javax.swing.JMenuItem ; 32 import javax.swing.JPopupMenu ; 33 import javax.swing.JTree ; 34 import javax.swing.event.TreeSelectionEvent ; 35 import javax.swing.event.TreeSelectionListener ; 36 import javax.swing.tree.TreeNode ; 37 import javax.swing.tree.TreePath ; 38 39 import org.apache.jmeter.control.gui.TestPlanGui; 40 import org.apache.jmeter.control.gui.WorkBenchGui; 41 import org.apache.jmeter.gui.GuiPackage; 42 import org.apache.jmeter.gui.MainFrame; 43 import org.apache.jmeter.gui.action.DragNDrop; 44 import org.apache.jmeter.util.JMeterUtils; 45 import org.apache.jorphan.logging.LoggingManager; 46 import org.apache.log.Logger; 47 48 53 public class JMeterTreeListener 54 implements TreeSelectionListener , MouseListener , KeyListener , 55 MouseMotionListener 56 { 57 transient private static Logger log = LoggingManager.getLoggerForClass(); 58 private TreePath currentPath; 61 private ActionListener actionHandler; 62 63 private JMeterTreeModel model; 64 private JTree tree; 65 private boolean dragging = false; 66 private JMeterTreeNode[] draggedNodes; 67 private JLabel dragIcon = new JLabel (JMeterUtils.getImage("leafnode.gif")); 68 69 72 public JMeterTreeListener(JMeterTreeModel model) 73 { 74 this.model = model; 75 dragIcon.validate(); 76 dragIcon.setVisible(true); 77 } 78 79 public JMeterTreeListener() 80 { 81 dragIcon.validate(); 82 dragIcon.setVisible(true); 83 } 84 85 public void setModel(JMeterTreeModel m) 86 { 87 model = m; 88 } 89 90 95 public void setActionHandler(ActionListener ah) 96 { 97 actionHandler = ah; 98 } 99 100 105 public void setJTree(JTree tree) 106 { 107 this.tree = tree; 108 } 109 110 115 public void setEndWindow(Container window) 116 { 117 } 119 120 125 public JTree getJTree() 126 { 127 return tree; 128 } 129 130 135 public JMeterTreeNode getCurrentNode() 136 { 137 if (currentPath != null) 138 { 139 if (currentPath.getLastPathComponent() != null) 140 { 141 return (JMeterTreeNode) currentPath.getLastPathComponent(); 142 } 143 else 144 { 145 return (JMeterTreeNode) currentPath 146 .getParentPath() 147 .getLastPathComponent(); 148 } 149 } 150 else 151 { 152 return (JMeterTreeNode) model.getRoot(); 153 } 154 } 155 156 public JMeterTreeNode[] getSelectedNodes() 157 { 158 TreePath [] paths = tree.getSelectionPaths(); 159 if (paths == null) 160 { 161 return new JMeterTreeNode[] { getCurrentNode()}; 162 } 163 JMeterTreeNode[] nodes = new JMeterTreeNode[paths.length]; 164 for (int i = 0; i < paths.length; i++) 165 { 166 nodes[i] = (JMeterTreeNode) paths[i].getLastPathComponent(); 167 } 168 169 return nodes; 170 } 171 172 public TreePath removedSelectedNode() 173 { 174 currentPath = currentPath.getParentPath(); 175 return currentPath; 176 } 177 178 public void valueChanged(TreeSelectionEvent e) 179 { 180 currentPath = e.getNewLeadSelectionPath(); 181 actionHandler.actionPerformed(new ActionEvent (this, 3333, "edit")); 182 } 183 184 public void mouseClicked(MouseEvent ev) 185 { 186 } 187 188 public void mouseReleased(MouseEvent e) 189 { 190 if (dragging && isValidDragAction(draggedNodes, getCurrentNode())) 191 { 192 dragging = false; 193 JPopupMenu dragNdrop = new JPopupMenu (); 194 JMenuItem item = 195 new JMenuItem (JMeterUtils.getResString("Insert Before")); 196 item.addActionListener(actionHandler); 197 item.setActionCommand(DragNDrop.INSERT_BEFORE); 198 dragNdrop.add(item); 199 item = new JMenuItem (JMeterUtils.getResString("Insert After")); 200 item.addActionListener(actionHandler); 201 item.setActionCommand(DragNDrop.INSERT_AFTER); 202 dragNdrop.add(item); 203 item = new JMenuItem (JMeterUtils.getResString("Add as Child")); 204 item.addActionListener(actionHandler); 205 item.setActionCommand(DragNDrop.ADD); 206 dragNdrop.add(item); 207 dragNdrop.addSeparator(); 208 item = new JMenuItem (JMeterUtils.getResString("Cancel")); 209 dragNdrop.add(item); 210 displayPopUp(e, dragNdrop); 211 } 212 else 213 { 214 GuiPackage.getInstance().getMainFrame().repaint(); 215 } 216 dragging = false; 217 } 218 219 public JMeterTreeNode[] getDraggedNodes() 220 { 221 return draggedNodes; 222 } 223 224 228 private boolean isValidDragAction( 229 JMeterTreeNode[] source, 230 JMeterTreeNode dest) 231 { 232 boolean isValid = true; 233 TreeNode [] path = dest.getPath(); 234 for (int i = 0; i < path.length; i++) 235 { 236 if (contains(source,path[i])) 237 { 238 isValid = false; 239 } 240 } 241 return isValid; 242 } 243 244 public void mouseEntered(MouseEvent e) 245 { 246 } 247 248 private void changeSelectionIfDragging(MouseEvent e) 249 { 250 if (dragging) 251 { 252 GuiPackage.getInstance().getMainFrame().drawDraggedComponent( 253 dragIcon, 254 e.getX(), 255 e.getY()); 256 if (tree.getPathForLocation(e.getX(), e.getY()) != null) 257 { 258 currentPath = tree.getPathForLocation(e.getX(), e.getY()); 259 if (!contains(draggedNodes,getCurrentNode())) 260 { 261 tree.setSelectionPath(currentPath); 262 } 263 } 264 } 265 } 266 267 private boolean contains(Object [] container,Object item) 268 { 269 for (int i = 0; i < container.length; i++) 270 { 271 if(container[i] == item) 272 { 273 return true; 274 } 275 } 276 return false; 277 } 278 279 public void mousePressed(MouseEvent e) 280 { 281 MainFrame mainFrame = GuiPackage.getInstance().getMainFrame(); 283 284 mainFrame.closeMenu(); 286 int selRow = tree.getRowForLocation(e.getX(), e.getY()); 287 if (tree.getPathForLocation(e.getX(), e.getY()) != null) 288 { 289 currentPath = tree.getPathForLocation(e.getX(), e.getY()); 290 } 291 if (selRow != -1) 292 { 293 if (isRightClick(e)) 296 { 297 if (tree.getSelectionCount() < 2) 298 { 299 tree.setSelectionPath(currentPath); 300 } 301 if (getCurrentNode() instanceof JMeterTreeNode) 302 { 303 log.debug("About to display pop-up"); 304 displayPopUp(e); 305 } 306 } 307 } 308 } 309 310 public void mouseDragged(MouseEvent e) 311 { 312 if (!dragging) 313 { 314 dragging = true; 315 draggedNodes = getSelectedNodes(); 316 if (draggedNodes[0].getUserObject() instanceof TestPlanGui 317 || draggedNodes[0].getUserObject() instanceof WorkBenchGui) 318 { 319 dragging = false; 320 } 321 322 } 323 changeSelectionIfDragging(e); 324 } 325 326 public void mouseMoved(MouseEvent e) 327 { 328 } 329 330 public void mouseExited(MouseEvent ev) 331 { 332 } 333 334 public void keyPressed(KeyEvent e) 335 { 336 } 337 338 public void keyReleased(KeyEvent e) 339 { 340 } 341 342 public void keyTyped(KeyEvent e) 343 { 344 } 345 346 private boolean isRightClick(MouseEvent e) 347 { 348 return (MouseEvent.BUTTON2_MASK & e.getModifiers()) > 0 349 || (MouseEvent.BUTTON3_MASK == e.getModifiers()); 350 } 351 352 367 368 private void displayPopUp(MouseEvent e) 369 { 370 JPopupMenu pop = getCurrentNode().createPopupMenu(); 371 GuiPackage.getInstance().displayPopUp(e, pop); 372 } 373 374 private void displayPopUp(MouseEvent e, JPopupMenu popup) 375 { 376 log.warn("Shouldn't be here"); 377 if (popup != null) 378 { 379 popup.pack(); 380 popup.show(tree, e.getX(), e.getY()); 381 popup.setVisible(true); 382 popup.requestFocus(); 383 } 384 } 385 } 386 | Popular Tags |