1 7 package com.ca.commons.cbutil; 8 9 import javax.swing.*; 10 import java.awt.*; 11 import java.awt.event.MouseAdapter ; 12 import java.awt.event.MouseEvent ; 13 import java.util.logging.Logger ; 14 15 26 public class CBToolBarButton extends JButton 27 { 28 29 static Logger logger = Logger.getLogger(CBToolBarButton.class.getPackage().getName()); 30 31 private boolean alwaysPaintBorder = true; 32 private int width = 75; 33 private int height = 40; 34 35 43 public CBToolBarButton(String label, String tooltip) 44 { 45 super(); 46 init(label, tooltip); 47 } 48 49 58 public CBToolBarButton(String label, String iconFileName, String tooltip) 59 { 60 this(label, iconFileName, tooltip, CENTER, BOTTOM); 61 } 62 63 74 public CBToolBarButton(String label, String iconFileName, String tooltip, 75 int horizPos, int vertPos) 76 { 77 super(); 78 init(label, tooltip); 79 80 ImageIcon buttonIcon = new ImageIcon(iconFileName); 81 setIcon(buttonIcon); 82 setHorizontalTextPosition(horizPos); 83 setVerticalTextPosition(vertPos); 84 } 85 86 89 public Insets getInsets() 90 { 91 return new Insets(1, 1, 1, 1); 92 } 93 94 97 public Dimension getPreferredSize() 98 { 99 return new Dimension(width, height); 100 } 101 102 105 public void init(String label, String tooltip) 106 { 107 setForeground(Color.black); 108 if (!alwaysPaintBorder) setBorderPainted(false); 109 110 setText(label); 111 112 setToolTipText(tooltip); 113 114 addMouseListener(new MouseAdapter () 115 { 116 public void mouseEntered(MouseEvent e) 117 { 118 setForeground(Color.blue); 119 if (!alwaysPaintBorder && isEnabled()) 120 setBorderPainted(true); 121 } 122 123 public void mouseExited(MouseEvent e) 124 { 125 setForeground(Color.black); 126 if (!alwaysPaintBorder && isEnabled()) 127 setBorderPainted(false); 128 } 129 }); 130 131 setMinimumSize(getPreferredSize()); 132 setMaximumSize(getPreferredSize()); 133 } 134 135 142 143 public void setText(String label) 144 { 145 if (label != null) 146 { 147 int pos = label.indexOf('&'); 148 if (pos >= 0 && pos <= label.length() - 2) 149 { 150 super.setText(label.substring(0, pos) + label.substring(pos + 1)); 151 setMnemonic(getText().charAt(pos)); 152 } 153 else 154 { 155 super.setText(label); 156 } 157 } 158 } 159 160 163 public void setAlwaysPaintBorder(boolean alwaysPaintBorder) 164 { 165 this.alwaysPaintBorder = alwaysPaintBorder; 166 if (alwaysPaintBorder) 167 setBorderPainted(true); 168 else 169 setBorderPainted(false); 170 } 171 172 178 public void setWidthHeight(int width, int height) 179 { 180 this.width = width; 181 this.height = height; 182 setMinimumSize(getPreferredSize()); 183 setMaximumSize(getPreferredSize()); 184 } 185 } 186 | Popular Tags |