1 19 package org.openide.awt; 20 21 import java.awt.event.MouseAdapter ; 22 import java.awt.event.MouseEvent ; 23 24 import javax.swing.SwingUtilities ; 25 26 27 32 public class MouseUtils extends Object { 33 private static int DOUBLE_CLICK_DELTA = 300; 34 35 36 private static int tempx = 0; 37 private static int tempy = 0; 38 private static long temph = 0; 39 private static int tempm = 0; 40 private static MouseEvent tempe; 41 42 47 public static boolean isRightMouseButton(MouseEvent e) { 48 return SwingUtilities.isRightMouseButton(e); 49 } 50 51 56 public static boolean isLeftMouseButton(MouseEvent e) { 57 return javax.swing.SwingUtilities.isLeftMouseButton(e); 58 } 59 60 64 public static boolean isDoubleClick(MouseEvent e) { 65 if ((e.getID() != MouseEvent.MOUSE_CLICKED) || (e.getClickCount() == 0)) { 73 return false; 74 } 75 76 return ((e.getClickCount() % 2) == 0) || isDoubleClickImpl(e); 77 } 78 79 81 private static boolean isDoubleClickImpl(MouseEvent e) { 82 int x = e.getX(); 83 int y = e.getY(); 84 long h = e.getWhen(); 85 int m = e.getModifiers(); 86 87 if ((tempx == x) && (tempy == y) && ((h - temph) < DOUBLE_CLICK_DELTA) && ( 90 e != tempe) && (m == tempm)) { 93 tempx = 0; 95 tempy = 0; 96 temph = 0; 97 tempm = 0; 98 tempe = null; 99 100 return true; 101 } else { 102 tempx = x; 104 tempy = y; 105 temph = h; 106 tempm = m; 107 tempe = e; 108 109 return false; 110 } 111 } 112 113 116 127 public static abstract class PopupMouseAdapter extends MouseAdapter { 128 131 public static final int DEFAULT_THRESHOLD = 5; 132 133 139 public PopupMouseAdapter(int threshold) { 140 this(); 141 } 142 143 144 public PopupMouseAdapter() { 145 super(); 146 } 147 148 public void mousePressed(MouseEvent e) { 149 maybePopup(e); 150 } 151 152 public void mouseReleased(MouseEvent e) { 153 maybePopup(e); 154 } 155 156 private void maybePopup(MouseEvent e) { 157 if (e.isPopupTrigger()) { 158 showPopup(e); 159 } 160 } 161 162 168 abstract protected void showPopup(MouseEvent evt); 169 } 170 } 171 | Popular Tags |