1 19 20 package org.netbeans.swing.tabcontrol; 21 22 import java.awt.AWTEvent ; 23 import java.awt.Component ; 24 import java.awt.Point ; 25 import java.awt.Toolkit ; 26 import java.awt.event.AWTEventListener ; 27 import java.awt.event.KeyEvent ; 28 import java.awt.event.MouseEvent ; 29 import javax.swing.JComponent ; 30 import javax.swing.Popup ; 31 import javax.swing.PopupFactory ; 32 import javax.swing.SwingUtilities ; 33 import javax.swing.event.MouseInputListener ; 34 import org.netbeans.swing.popupswitcher.SwitcherTable; 35 import org.netbeans.swing.popupswitcher.SwitcherTableItem; 36 37 43 final class ButtonPopupSwitcher 44 implements MouseInputListener , AWTEventListener { 45 46 50 private static Popup popup; 51 52 57 private Component invokingComponent = null; 58 59 65 private long invocationTime = -1; 66 67 68 private static boolean shown; 69 70 private SwitcherTable pTable; 71 72 private int x; 73 private int y; 74 75 82 public static void selectItem(JComponent owner, SwitcherTableItem[] items, 83 int x, int y) { 84 ButtonPopupSwitcher switcher 85 = new ButtonPopupSwitcher(items, x, y); 86 switcher.doSelect(owner); 87 } 88 89 90 private ButtonPopupSwitcher(SwitcherTableItem items[], 91 int x, 92 int y) { 93 this.pTable = new SwitcherTable(items, y); 94 this.x = x - (int) pTable.getPreferredSize().getWidth(); 95 this.y = y + 1; 96 } 97 98 private void doSelect(JComponent owner) { 99 invokingComponent = owner; 100 invokingComponent.addMouseListener(this); 101 invokingComponent.addMouseMotionListener(this); 102 pTable.addMouseListener(this); 103 pTable.addMouseMotionListener(this); 104 105 Toolkit.getDefaultToolkit().addAWTEventListener(this, 106 AWTEvent.MOUSE_EVENT_MASK 107 | AWTEvent.KEY_EVENT_MASK); 108 popup = PopupFactory.getSharedInstance().getPopup( 109 invokingComponent, pTable, x, y); 110 popup.show(); 111 shown = true; 112 invocationTime = System.currentTimeMillis(); 113 } 114 115 120 public static boolean isShown() { 121 return shown; 122 } 123 124 127 private synchronized void hideCurrentPopup() { 128 pTable.removeMouseListener(this); 129 pTable.removeMouseMotionListener(this); 130 Toolkit.getDefaultToolkit().removeAWTEventListener(this); 131 if (invokingComponent != null) { 132 invokingComponent.removeMouseListener(this); 133 invokingComponent.removeMouseMotionListener(this); 134 invokingComponent = null; 135 } 136 if (popup != null) { 137 SwingUtilities.invokeLater(new PopupHider(popup)); 141 popup = null; 142 shown = false; 143 } 144 } 145 146 153 private class PopupHider implements Runnable { 154 private Popup toHide; 155 public PopupHider(Popup popup) { 156 toHide = popup; 157 } 158 159 public void run() { 160 toHide.hide(); 161 toHide = null; 162 } 163 } 164 165 public void mouseMoved(MouseEvent e) { 166 Point p = e.getPoint(); 167 if (e.getSource() != this) { 169 p = SwingUtilities.convertPoint((Component ) e.getSource(), p, pTable); 170 } 171 if (pTable.contains(p)) { 172 int row = pTable.rowAtPoint(p); 173 int col = pTable.columnAtPoint(p); 174 pTable.changeSelection(row, col, false, false); 175 } else { 176 pTable.clearSelection(); 177 } 178 e.consume(); 179 } 180 181 public void mousePressed(MouseEvent e) { 182 Point p = e.getPoint(); 183 p = SwingUtilities.convertPoint((Component ) e.getSource(), p, pTable); 184 if (pTable.contains(p)) { 185 final SwitcherTableItem item = pTable.getSelectedItem(); 186 if (item != null) { 187 item.activate(); 188 hideCurrentPopup(); 189 e.consume(); 190 } 191 } 192 } 193 194 public void mouseReleased(MouseEvent e) { 195 if (e.getSource() == invokingComponent) { 196 long time = System.currentTimeMillis(); 197 if (time - invocationTime > 500) { 198 mousePressed(e); 199 } 200 } 201 e.consume(); 202 } 203 204 public void mouseClicked(MouseEvent e) { 205 e.consume(); 206 } 207 208 public void mouseEntered(MouseEvent e) { 209 mouseDragged(e); 210 e.consume(); 211 } 212 213 public void mouseExited(MouseEvent e) { 214 pTable.clearSelection(); 215 e.consume(); 216 } 217 218 public void mouseDragged(MouseEvent e) { 220 mouseMoved(e); 221 e.consume(); 222 } 223 224 227 private boolean onSwitcherTable(MouseEvent e) { 228 Point p = e.getPoint(); 229 p = SwingUtilities.convertPoint((Component ) e.getSource(), p, pTable); 230 return pTable.contains(p); 231 } 232 233 238 public void eventDispatched(AWTEvent event) { 239 if (event.getSource() == this) { 240 return; 241 } 242 if (event instanceof MouseEvent ) { 243 if (event.getID() == MouseEvent.MOUSE_RELEASED) { 244 long time = System.currentTimeMillis(); 245 if (time - invocationTime > 500) { 247 if (!onSwitcherTable((MouseEvent ) event)) { 248 hideCurrentPopup(); 250 } 251 } 252 } else if (event.getID() == MouseEvent.MOUSE_PRESSED) { 253 if (!onSwitcherTable((MouseEvent ) event)) { 254 if (event.getSource() != invokingComponent) { 256 hideCurrentPopup(); 261 } 262 } 263 } 264 } else if (event instanceof KeyEvent ) { 265 if (event.getID() == KeyEvent.KEY_PRESSED) { 266 Toolkit.getDefaultToolkit().removeAWTEventListener(this); 267 hideCurrentPopup(); 268 } 269 } 270 } 271 } 272 | Popular Tags |