1 package com.sshtools.ui.awt; 2 3 import java.awt.Color ; 4 import java.awt.Image ; 5 import java.awt.Insets ; 6 import java.awt.ItemSelectable ; 7 import java.awt.SystemColor ; 8 import java.awt.event.ActionEvent ; 9 import java.awt.event.ActionListener ; 10 import java.awt.event.ItemEvent ; 11 import java.awt.event.ItemListener ; 12 import java.awt.event.MouseEvent ; 13 import java.awt.event.MouseListener ; 14 import java.util.Vector ; 15 16 import com.sshtools.ui.awt.tooltips.ToolTipManager; 17 18 23 public class ImageButton extends ImageTextLabel implements MouseListener , ItemSelectable { 24 25 private boolean mouseIsDown = false; 26 private Vector listeners, itemListeners; 27 private int darkness = -5263441; 28 private Image grayImage = null; 29 private String actionCommand; 30 private Color borderColor; 31 private boolean hoverButton; 32 private boolean mouseInComponent; 33 private int hoverBorder, depressedBorder, normalBorder; 34 private String toolTipText; 35 private Color hoverBackground = SystemColor.controlLtHighlight; 36 private Color hoverForeground = SystemColor.controlText; 37 private Color normalBackground = SystemColor.control; 38 private Color normalForeground = SystemColor.controlText; 39 private Color depressedBackground = SystemColor.controlShadow; 40 private Color depressedForeground = SystemColor.controlHighlight; 41 private boolean pressed; 42 43 public ImageButton() { 44 this(null, null, null); 45 } 46 47 public ImageButton(Image image, String text, String actionCommand) { 48 this(image, text, actionCommand, FLAT, FLAT, NONE); 49 } 50 51 public ImageButton(Image image, String text, String actionCommand, int hoverBorder, int depressedBorder, int normalBorder) { 52 super(image, text); 53 this.actionCommand = actionCommand; 54 this.hoverBorder = hoverBorder; 55 this.depressedBorder = depressedBorder; 56 this.normalBorder = normalBorder; 57 setMargin(new Insets (4, 2, 4, 2)); 58 setBorderType(hoverBorder); 59 addMouseListener(this); 60 normalBackground = getBackground(); 61 normalForeground = getForeground(); 62 } 63 64 public void setEnabled(boolean enabled) { 65 if(!enabled) { 66 setBorderType(normalBorder); 67 super.setBackground(normalBackground); 68 super.setForeground(normalForeground); 69 } 70 super.setEnabled(enabled); 71 repaint(); 72 } 73 74 public void setBaseBackground(Color base) { 75 if(base == null) { 76 setBackground(SystemColor.control); 77 hoverBackground = SystemColor.controlHighlight; 78 depressedBackground = SystemColor.controlShadow; 79 } 80 else { 81 setBackground(base); 82 hoverBackground = base.brighter(); 83 depressedBackground = base.darker(); 84 } 85 } 86 87 public void setBaseForeground(Color base) { 88 if(base == null) { 89 setForeground(SystemColor.controlText); 90 hoverForeground = SystemColor.controlText; 91 depressedForeground = SystemColor.controlHighlight; 92 } 93 else { 94 hoverForeground = base.darker(); 95 depressedForeground = base.brighter(); 96 setForeground(base); 97 } 98 } 99 100 public void setBackground(Color background) { 101 this.normalBackground = background; 102 super.setBackground(background); 103 } 104 105 public void setForeground(Color foreground) { 106 this.normalForeground = foreground; 107 super.setForeground(foreground); 108 } 109 110 115 public void setHoverBorder(int hoverBorder) { 116 this.hoverBorder = hoverBorder; 117 } 118 119 125 public void setHoverButton(boolean hoverButton) { 126 this.hoverButton = hoverButton; 127 setBorderType(hoverButton ? (mouseInComponent ? hoverBorder : normalBorder) : hoverBorder); 128 129 } 130 131 137 public void setHoverBackground(Color hoverBackground) { 138 this.hoverBackground = hoverBackground; 139 } 140 141 147 public void setHoverForeground(Color hoverForeground) { 148 this.hoverForeground = hoverForeground; 149 } 150 151 154 public String getActionCommand() { 155 return actionCommand; 156 } 157 158 164 public void setActionCommand(String actionCommand) { 165 this.actionCommand = actionCommand; 166 } 167 168 public void setToolTipText(String toolTipText) { 169 this.toolTipText = toolTipText; 170 } 171 172 public String getToolTipText() { 173 return toolTipText; 174 } 175 176 184 197 211 public void addActionListener(ActionListener l) { 212 if (listeners == null) { 213 listeners = new Vector (); 214 } 215 listeners.addElement(l); 216 } 217 218 public void addItemListener(ItemListener l) { 219 if (itemListeners == null) { 220 itemListeners = new Vector (); 221 } 222 itemListeners.addElement(l); 223 } 224 225 public void removeActionListener(ActionListener l) { 226 if (listeners != null) { 227 listeners.removeElement(l); 228 } 229 } 230 231 public void removeItemListener(ItemListener l) { 232 if (itemListeners != null) { 233 itemListeners.removeElement(l); 234 } 235 } 236 237 boolean itemEvent(boolean selected) { 238 ItemEvent evt = null; 239 for (int i = itemListeners == null ? -1 : itemListeners.size() - 1; i >= 0; i--) { 240 if (evt == null) { 241 evt = new ItemEvent (this, selected ? ItemEvent.SELECTED : 242 ItemEvent.DESELECTED, this, 0); 243 } 244 ((ItemListener ) itemListeners.elementAt(i)).itemStateChanged(evt); 245 } 246 return false; 247 248 } 249 250 boolean actionEvent(int modifiers) { 251 ActionEvent evt = null; 252 for (int i = listeners == null ? -1 : listeners.size() - 1; i >= 0; i--) { 253 if (evt == null) { 254 evt = new ActionEvent (this, 1001, actionCommand, modifiers); 255 } 256 ((ActionListener ) listeners.elementAt(i)).actionPerformed(evt); 257 } 258 return false; 259 } 260 261 276 public int getDarkness() { 277 return darkness; 278 } 279 280 public void setDarkness(int darkness) { 281 this.darkness = darkness; 282 } 283 284 public Image getGrayImage() { 285 return grayImage; 286 } 287 288 public void setGrayImage(Image grayImage) { 289 this.grayImage = grayImage; 290 } 291 292 301 302 307 public void mouseClicked(MouseEvent e) { 308 310 } 311 312 317 public void mousePressed(MouseEvent e) { 318 if (isEnabled()) { 319 mouseIsDown = true; 320 setBorderType(depressedBorder); 321 super.setBackground(depressedBackground); 322 super.setForeground(depressedForeground); 323 } 324 } 325 326 331 public void mouseReleased(MouseEvent event) { 332 mouseIsDown = false; 333 if (isEnabled()) { 334 if (this.inside(event.getX(), event.getY())) { 335 setBorderType(normalBorder); 336 paint(this.getGraphics()); 337 actionEvent(event.getModifiers()); 338 } 339 } 340 341 } 342 343 348 public void mouseEntered(MouseEvent e) { 349 if (isEnabled()) { 350 if (hoverButton && !pressed) { 351 setBorderType(hoverBorder); 352 if(hoverForeground != null) { 353 super.setForeground(hoverForeground); 354 } 355 if(hoverBackground != null) { 356 super.setBackground(hoverBackground); 357 } 358 itemEvent(true); 359 } 360 } 361 if (toolTipText != null) { 362 ToolTipManager.getInstance().requestToolTip(this, toolTipText); 363 } 364 mouseInComponent = true; 365 366 } 367 368 373 public void mouseExited(MouseEvent e) { 374 mouseInComponent = false; 375 if (hoverButton) { 376 if(!pressed) { 377 setBorderType(normalBorder); 378 if(hoverForeground != null) { 379 super.setForeground(normalForeground); 380 } 381 if(hoverBackground != null) { 382 super.setBackground(normalBackground); 383 } 384 itemEvent(true); 385 } 386 } else { 387 if (mouseIsDown) { 388 paint(this.getGraphics()); 389 } 390 } 391 if (toolTipText != null) { 392 ToolTipManager.getInstance().requestToolTip(null, null); 393 } 394 } 395 396 399 public void setPressed(boolean pressed) { 400 this.pressed = pressed; 401 if(pressed) { 402 setBorderType(depressedBorder); 403 super.setBackground(depressedBackground); 404 super.setForeground(depressedForeground); 405 } 406 else { 407 setBorderType(normalBorder); 408 super.setBackground(normalBackground); 409 super.setForeground(normalForeground); 410 }; 411 repaint(); 412 413 } 414 415 418 public Object [] getSelectedObjects() { 419 return new Object [] { this }; 420 } 421 } | Popular Tags |