001: /*
002: * WbButton.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.components;
013:
014: import java.awt.event.MouseEvent;
015: import java.awt.event.MouseListener;
016: import javax.swing.Action;
017: import javax.swing.Icon;
018: import javax.swing.JButton;
019: import javax.swing.UIDefaults;
020: import javax.swing.UIManager;
021: import javax.swing.border.Border;
022: import javax.swing.border.CompoundBorder;
023: import javax.swing.border.EmptyBorder;
024: import javax.swing.plaf.basic.BasicBorders;
025: import workbench.resource.ResourceMgr;
026:
027: /**
028: *
029: * @author support@sql-workbench.net
030: */
031: public class WbButton extends JButton implements MouseListener {
032: protected Border rolloverBorder;
033: private Border emptyBorder;
034: protected boolean iconButton = false;
035:
036: public WbButton() {
037: super ();
038: init();
039: }
040:
041: public WbButton(Action a) {
042: super (a);
043: init();
044: }
045:
046: public WbButton(String aText) {
047: super (aText);
048: init();
049: }
050:
051: public WbButton(Icon i) {
052: super (i);
053: iconButton = true;
054: init();
055: }
056:
057: private void init() {
058: putClientProperty("jgoodies.isNarrow", Boolean.FALSE);
059: }
060:
061: public void setResourceKey(String key) {
062: this .setText(ResourceMgr.getString(key));
063: this .setToolTipText(ResourceMgr.getDescription(key));
064: }
065:
066: public void setText(String newText) {
067: if (newText == null) {
068: super .setText(null);
069: return;
070: }
071: int pos = newText.indexOf('&');
072: if (pos > -1) {
073: char mnemonic = newText.charAt(pos + 1);
074: newText = newText.substring(0, pos)
075: + newText.substring(pos + 1);
076: this .setMnemonic((int) mnemonic);
077: }
078: super .setText(newText);
079: }
080:
081: public void setBasicUI() {
082: this .setUI(new javax.swing.plaf.basic.BasicButtonUI());
083: }
084:
085: public void enableBasicRollover() {
086: this .setRolloverEnabled(true);
087: setBasicUI();
088: UIDefaults table = UIManager.getLookAndFeelDefaults();
089: Border out = new BasicBorders.RolloverButtonBorder(table
090: .getColor("controlShadow"), table
091: .getColor("controlDkShadow"), table
092: .getColor("controlHighlight"), table
093: .getColor("controlLtHighlight"));
094:
095: if (iconButton) {
096: this .rolloverBorder = out;
097: this .emptyBorder = new EmptyBorder(0, 0, 0, 0);
098: } else {
099: Border in = new EmptyBorder(3, 3, 3, 3);
100: this .rolloverBorder = new CompoundBorder(out, in);
101: this .emptyBorder = new EmptyBorder(6, 6, 6, 6);
102: }
103: this .setBorderPainted(true);
104: this .setBorder(emptyBorder);
105: this .addMouseListener(this );
106: }
107:
108: public void enableToolbarRollover() {
109: this .rolloverBorder = null;
110: this .emptyBorder = null;
111: this .setBorderPainted(false);
112: this .addMouseListener(this );
113: }
114:
115: public void mouseClicked(MouseEvent e) {
116: }
117:
118: public void mousePressed(MouseEvent e) {
119: }
120:
121: public void mouseReleased(MouseEvent e) {
122: }
123:
124: public void mouseEntered(MouseEvent e) {
125: if (this .rolloverBorder == null) {
126: this .setBorderPainted(true);
127: } else {
128: this .setBorder(this .rolloverBorder);
129: }
130: }
131:
132: public void mouseExited(MouseEvent e) {
133: if (this .rolloverBorder == null) {
134: this .setBorderPainted(false);
135: } else {
136: this.setBorder(this.emptyBorder);
137: }
138: }
139:
140: }
|