1 19 20 package com.sshtools.ui.swing; 21 22 import java.awt.Color ; 23 import java.awt.Insets ; 24 import java.awt.event.MouseAdapter ; 25 import java.awt.event.MouseEvent ; 26 import java.beans.PropertyChangeEvent ; 27 import java.beans.PropertyChangeListener ; 28 29 import javax.swing.Action ; 30 import javax.swing.Icon ; 31 import javax.swing.JButton ; 32 import javax.swing.KeyStroke ; 33 import javax.swing.UIManager ; 34 35 public class ActionButton extends JButton { 36 private final static Insets INSETS = new Insets (0, 0, 0, 0); 38 private boolean hideText; 40 private boolean enablePlasticWorkaround; 41 private Color hoverForeground, oldForeground; 42 43 public ActionButton(AppAction action) { 44 this(action, true); 45 } 46 47 public ActionButton(AppAction action, boolean useLargeIcon) { 48 this(action, useLargeIcon ? AppAction.LARGE_ICON : AppAction.SMALL_ICON, true); 49 } 50 51 public ActionButton(AppAction action, boolean useLargeIcon, boolean showSelectiveText) { 52 this(action, useLargeIcon ? AppAction.LARGE_ICON : AppAction.SMALL_ICON, showSelectiveText); 53 54 } 55 56 public ActionButton(AppAction action, String iconKey, boolean showSelectiveText) { 57 super(); 58 init(action, iconKey, showSelectiveText); 59 } 60 61 private void init(AppAction a, final String iconKey, boolean showText) { 62 enablePlasticWorkaround = UIManager.getLookAndFeel().getClass().getName().startsWith("com.jgoodies.looks.plastic."); 63 setAction(a); 64 addMouseListener(new MouseAdapter () { 65 public void mouseEntered(MouseEvent e) { 66 if (isEnabled()) { 67 if(hoverForeground != null) { 68 oldForeground = getForeground(); 69 setForeground(hoverForeground); 70 } 71 setBorderPainted(true); 72 if (!enablePlasticWorkaround) { 73 setContentAreaFilled(true); 74 } 75 } 76 } 77 78 public void mouseExited(MouseEvent e) { 79 setBorderPainted(false); 80 setContentAreaFilled(enablePlasticWorkaround); 81 if(oldForeground != null) { 82 setForeground(oldForeground); 83 oldForeground = null; 84 } 85 } 86 }); 87 a.addPropertyChangeListener(new PropertyChangeListener () { 88 public void propertyChange(PropertyChangeEvent evt) { 89 if (evt.getPropertyName().equals(iconKey)) { 90 Icon icon = (Icon ) evt.getNewValue(); 91 ActionButton.this.setIcon(icon); 92 ActionButton.this.invalidate(); 93 ActionButton.this.repaint(); 94 } 95 } 96 }); 97 setBorderPainted(false); 98 setContentAreaFilled(enablePlasticWorkaround); 99 if (a != null && a.getValue(Action.ACCELERATOR_KEY) != null) { 100 setMnemonic(0); 101 registerKeyboardAction(a, (KeyStroke ) a.getValue(Action.ACCELERATOR_KEY), JButton.WHEN_IN_FOCUSED_WINDOW); 102 } 103 setIcon((Icon ) a.getValue(iconKey)); 104 if (Boolean.FALSE.equals(a.getValue(AppAction.TEXT_ON_TOOLBAR)) || !showText) { 105 setHideText(true); 106 } else { 107 setHideText(false); 108 } 109 setVerticalTextPosition(JButton.BOTTOM); 110 setHorizontalTextPosition(JButton.CENTER); 111 } 112 113 public Insets getMargin() { 114 return INSETS; 115 } 116 117 public boolean isRequestFocusEnabled() { 118 return false; 119 } 120 121 public boolean isFocusTraversable() { 122 return false; 123 } 124 125 public void setHideText(boolean hideText) { 126 if (this.hideText != hideText) { 127 firePropertyChange("hideText", this.hideText, hideText); 128 } 129 this.hideText = hideText; 130 this.setHorizontalTextPosition(ActionButton.RIGHT); 131 repaint(); 132 } 133 134 public String getText() { 135 return hideText ? null : super.getText(); 136 } 137 138 public void setHoverForeground(Color hoverForeground) { 139 this.hoverForeground = hoverForeground; 140 } 141 } | Popular Tags |