1 7 8 package javax.swing.plaf.basic; 9 10 import sun.swing.DefaultLookup; 11 import sun.swing.UIAction; 12 import java.awt.*; 13 import java.awt.event.*; 14 import java.beans.*; 15 import javax.swing.*; 16 import javax.swing.event.*; 17 import javax.swing.plaf.ActionMapUIResource ; 18 import javax.swing.plaf.ButtonUI ; 19 import javax.swing.plaf.ComponentInputMapUIResource ; 20 21 28 29 public class BasicButtonListener implements MouseListener, MouseMotionListener, 30 FocusListener, ChangeListener, PropertyChangeListener 31 { 32 private long lastPressedTimestamp = -1; 33 private boolean shouldDiscardRelease = false; 34 35 38 static void loadActionMap(LazyActionMap map) { 39 map.put(new Actions(Actions.PRESS)); 40 map.put(new Actions(Actions.RELEASE)); 41 } 42 43 44 public BasicButtonListener(AbstractButton b) { 45 } 46 47 public void propertyChange(PropertyChangeEvent e) { 48 String prop = e.getPropertyName(); 49 if(prop == AbstractButton.MNEMONIC_CHANGED_PROPERTY) { 50 updateMnemonicBinding((AbstractButton)e.getSource()); 51 } 52 else if(prop == AbstractButton.CONTENT_AREA_FILLED_CHANGED_PROPERTY) { 53 checkOpacity((AbstractButton) e.getSource() ); 54 } 55 else if(prop == AbstractButton.TEXT_CHANGED_PROPERTY || 56 "font" == prop || "foreground" == prop) { 57 AbstractButton b = (AbstractButton) e.getSource(); 58 BasicHTML.updateRenderer(b, b.getText()); 59 } 60 } 61 62 protected void checkOpacity(AbstractButton b) { 63 b.setOpaque( b.isContentAreaFilled() ); 64 } 65 66 70 public void installKeyboardActions(JComponent c) { 71 AbstractButton b = (AbstractButton)c; 72 updateMnemonicBinding(b); 74 75 LazyActionMap.installLazyActionMap(c, BasicButtonListener .class, 76 "Button.actionMap"); 77 78 InputMap km = getInputMap(JComponent.WHEN_FOCUSED, c); 79 80 SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_FOCUSED, km); 81 } 82 83 86 public void uninstallKeyboardActions(JComponent c) { 87 SwingUtilities.replaceUIInputMap(c, JComponent. 88 WHEN_IN_FOCUSED_WINDOW, null); 89 SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_FOCUSED, null); 90 SwingUtilities.replaceUIActionMap(c, null); 91 } 92 93 97 InputMap getInputMap(int condition, JComponent c) { 98 if (condition == JComponent.WHEN_FOCUSED) { 99 BasicButtonUI ui = (BasicButtonUI )BasicLookAndFeel.getUIOfType( 100 ((AbstractButton)c).getUI(), BasicButtonUI .class); 101 if (ui != null) { 102 return (InputMap)DefaultLookup.get( 103 c, ui, ui.getPropertyPrefix() + "focusInputMap"); 104 } 105 } 106 return null; 107 } 108 109 113 void updateMnemonicBinding(AbstractButton b) { 114 int m = b.getMnemonic(); 115 if(m != 0) { 116 InputMap map = SwingUtilities.getUIInputMap( 117 b, JComponent.WHEN_IN_FOCUSED_WINDOW); 118 119 if (map == null) { 120 map = new ComponentInputMapUIResource (b); 121 SwingUtilities.replaceUIInputMap(b, 122 JComponent.WHEN_IN_FOCUSED_WINDOW, map); 123 } 124 map.clear(); 125 map.put(KeyStroke.getKeyStroke(m, InputEvent.ALT_MASK, false), 126 "pressed"); 127 map.put(KeyStroke.getKeyStroke(m, InputEvent.ALT_MASK, true), 128 "released"); 129 map.put(KeyStroke.getKeyStroke(m, 0, true), "released"); 130 } 131 else { 132 InputMap map = SwingUtilities.getUIInputMap(b, JComponent. 133 WHEN_IN_FOCUSED_WINDOW); 134 if (map != null) { 135 map.clear(); 136 } 137 } 138 } 139 140 public void stateChanged(ChangeEvent e) { 141 AbstractButton b = (AbstractButton) e.getSource(); 142 b.repaint(); 143 } 144 145 public void focusGained(FocusEvent e) { 146 AbstractButton b = (AbstractButton) e.getSource(); 147 if (b instanceof JButton && ((JButton)b).isDefaultCapable()) { 148 JRootPane root = b.getRootPane(); 149 if (root != null) { 150 BasicButtonUI ui = (BasicButtonUI )BasicLookAndFeel.getUIOfType( 151 ((AbstractButton)b).getUI(), BasicButtonUI .class); 152 if (ui != null && DefaultLookup.getBoolean(b, ui, 153 ui.getPropertyPrefix() + 154 "defaultButtonFollowsFocus", true)) { 155 root.putClientProperty("temporaryDefaultButton", b); 156 root.setDefaultButton((JButton)b); 157 root.putClientProperty("temporaryDefaultButton", null); 158 } 159 } 160 } 161 b.repaint(); 162 } 163 164 public void focusLost(FocusEvent e) { 165 AbstractButton b = (AbstractButton) e.getSource(); 166 JRootPane root = b.getRootPane(); 167 if (root != null) { 168 JButton initialDefault = (JButton)root.getClientProperty("initialDefaultButton"); 169 if (b != initialDefault) { 170 BasicButtonUI ui = (BasicButtonUI )BasicLookAndFeel.getUIOfType( 171 ((AbstractButton)b).getUI(), BasicButtonUI .class); 172 if (ui != null && DefaultLookup.getBoolean(b, ui, 173 ui.getPropertyPrefix() + 174 "defaultButtonFollowsFocus", true)) { 175 root.setDefaultButton(initialDefault); 176 } 177 } 178 } 179 180 ButtonModel model = b.getModel(); 181 model.setArmed(false); 182 model.setPressed(false); 183 184 b.repaint(); 185 } 186 187 public void mouseMoved(MouseEvent e) { 188 } 189 190 191 public void mouseDragged(MouseEvent e) { 192 } 193 194 public void mouseClicked(MouseEvent e) { 195 } 196 197 public void mousePressed(MouseEvent e) { 198 if (SwingUtilities.isLeftMouseButton(e) ) { 199 AbstractButton b = (AbstractButton) e.getSource(); 200 201 if(b.contains(e.getX(), e.getY())) { 202 long multiClickThreshhold = b.getMultiClickThreshhold(); 203 long lastTime = lastPressedTimestamp; 204 long currentTime = lastPressedTimestamp = e.getWhen(); 205 if (lastTime != -1 && currentTime - lastTime < multiClickThreshhold) { 206 shouldDiscardRelease = true; 207 return; 208 } 209 210 ButtonModel model = b.getModel(); 211 if (!model.isEnabled()) { 212 return; 214 } 215 if (!model.isArmed()) { 216 model.setArmed(true); 218 } 219 model.setPressed(true); 220 if(!b.hasFocus() && b.isRequestFocusEnabled()) { 221 b.requestFocus(); 222 } 223 } 224 } 225 }; 226 227 public void mouseReleased(MouseEvent e) { 228 if (SwingUtilities.isLeftMouseButton(e)) { 229 if (shouldDiscardRelease) { 231 shouldDiscardRelease = false; 232 return; 233 } 234 AbstractButton b = (AbstractButton) e.getSource(); 235 ButtonModel model = b.getModel(); 236 model.setPressed(false); 237 model.setArmed(false); 238 } 239 }; 240 241 public void mouseEntered(MouseEvent e) { 242 AbstractButton b = (AbstractButton) e.getSource(); 243 ButtonModel model = b.getModel(); 244 if (b.isRolloverEnabled() && !SwingUtilities.isLeftMouseButton(e)) { 245 model.setRollover(true); 246 } 247 if (model.isPressed()) 248 model.setArmed(true); 249 }; 250 251 public void mouseExited(MouseEvent e) { 252 AbstractButton b = (AbstractButton) e.getSource(); 253 ButtonModel model = b.getModel(); 254 if(b.isRolloverEnabled()) { 255 model.setRollover(false); 256 } 257 model.setArmed(false); 258 }; 259 260 261 266 private static class Actions extends UIAction { 267 private static final String PRESS = "pressed"; 268 private static final String RELEASE = "released"; 269 270 Actions(String name) { 271 super(name); 272 } 273 274 public void actionPerformed(ActionEvent e) { 275 AbstractButton b = (AbstractButton)e.getSource(); 276 String key = getName(); 277 if (key == PRESS) { 278 ButtonModel model = b.getModel(); 279 model.setArmed(true); 280 model.setPressed(true); 281 if(!b.hasFocus()) { 282 b.requestFocus(); 283 } 284 } 285 else if (key == RELEASE) { 286 ButtonModel model = b.getModel(); 287 model.setPressed(false); 288 model.setArmed(false); 289 } 290 } 291 292 public boolean isEnabled(Object sender) { 293 if(sender != null && (sender instanceof AbstractButton) && 294 !((AbstractButton)sender).getModel().isEnabled()) { 295 return false; 296 } else { 297 return true; 298 } 299 } 300 } 301 } 302 | Popular Tags |