1 98 99 100 package swingwtx.swing; 101 102 import org.eclipse.swt.SWT; 103 import org.eclipse.swt.widgets.Button; 104 import org.eclipse.swt.widgets.Shell; 105 import org.eclipse.swt.widgets.ToolItem; 106 107 public class JButton extends AbstractButton implements SwingConstants, ButtonModel { 108 109 protected Icon pImage = null; 110 protected boolean pDefaultCapable = true; 111 protected swingwt.awt.Container pDefaultButtonParent = null; 112 113 protected ToolItem pSWTToolButton = null; 114 115 public JButton() {this(""); } 116 public JButton(String text) { pText = text; setModel(this); showMnemonic(); pHAlign = CENTER; } 117 public JButton(Action a) { setAction(a); pHAlign = CENTER; } 118 public JButton(Icon icon) { pImage = icon; pHAlign = CENTER; } 119 public JButton(String text, Icon icon) { pImage = icon; pText = text; pHAlign = CENTER; setModel(this); showMnemonic(); } 120 121 122 protected JButton(Action a, boolean addAsListener) { setAction(a, addAsListener); } 123 124 127 private void updateMappedAction() { 128 if (pAction instanceof JButtonMappedAction) { 129 pAction.putValue(Action.NAME, getText()); 130 pAction.putValue(Action.SHORT_DESCRIPTION, getToolTipText()); 131 pAction.putValue(Action.SMALL_ICON, getIcon()); 132 pAction.putValue(Action.DISABLED_ICON, getIcon()); 133 pAction.putValue(Action.MNEMONIC_KEY, new Integer (getMnemonic())); 134 } 135 } 136 137 141 private void updateToolButton() { 142 final JButton pthis = this; 143 if (pSWTToolButton == null) return; 144 SwingUtilities.invokeSync(new Runnable () { 145 public void run() { 146 pSWTToolButton.setText(getText()); 147 pSWTToolButton.setToolTipText(getToolTipText()); 148 pSWTToolButton.setEnabled(isEnabled()); 149 pSWTToolButton.setImage(SwingWTUtils.getSWTImageFromSwingIcon(pthis, getIcon())); 150 } 151 }); 152 } 153 154 public Icon getIcon() { return pImage; } 155 156 public void setIcon(Icon icon) { 157 pImage = icon; 158 final JButton pthis = this; 159 SwingUtilities.invokeSync(new Runnable () { 160 public void run() { 161 if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.setImage(SwingWTUtils.getSWTImageFromSwingIcon(pthis, pImage)); 162 renderText(); 163 updateToolButton(); 164 updateMappedAction(); 165 } 166 }); 167 } 168 169 170 public void setToolTipText(String text) { 171 super.setToolTipText(text); 172 updateToolButton(); 173 updateMappedAction(); 174 } 175 176 178 public String getText() { 179 return pText; 180 } 181 182 183 public void setText(String text) { 184 super.setText(text); 185 renderText(); 186 updateMappedAction(); 187 updateToolButton(); 188 } 189 190 public void setEnabled(boolean b) { 191 super.setEnabled(b); 192 updateToolButton(); 193 } 194 195 public boolean getDefaultCapable() { return pDefaultCapable; } 196 public void setDefaultCapable(boolean b) { pDefaultCapable = b; } 197 198 204 protected void renderText() { 205 206 if (pText == null) 207 return; 208 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) 209 return; 210 if (pText.length() == 2 && pText.startsWith("&")) 211 return; 212 if (pText.equals("") || pImage == null) 213 return; 214 215 final JButton pthis = this; 216 SwingUtilities.invokeSync(new Runnable () { 217 public void run() { 218 org.eclipse.swt.graphics.GC gc = new org.eclipse.swt.graphics.GC(ppeer.getImage()); 219 gc.setFont(ppeer.getFont()); 221 org.eclipse.swt.graphics.Point p = gc.textExtent(pText); 223 224 int width = ppeer.getImage().getBounds().width + 2; 227 int height = ppeer.getImage().getBounds().height; 228 org.eclipse.swt.graphics.Image im = new org.eclipse.swt.graphics.Image(SwingWTUtils.getDisplay(), width + p.x, height); 229 230 org.eclipse.swt.graphics.GC ngc = new org.eclipse.swt.graphics.GC(im); 232 ngc.setFont(ppeer.getFont()); 234 ngc.setBackground(ppeer.getShell().getBackground()); 238 ngc.setForeground(ppeer.getShell().getBackground()); 239 ngc.fillRectangle(0, 0, width + p.x, height); 240 241 ngc.setForeground(ppeer.getShell().getForeground()); 243 ngc.drawImage(SwingWTUtils.getSWTImageFromSwingIcon(pthis, pImage), 0, 0); 244 245 String text = pText; 249 int mnPos = text.indexOf("&"); 250 if (mnPos != -1) 251 text = text.substring(0, mnPos) + text.substring(mnPos + 1, text.length()); 252 253 ngc.drawText(text, width, (height - p.y) / 2, true); 255 256 ngc.dispose(); 258 gc.dispose(); 259 260 ppeer.setImage(im); 262 } 263 }); 264 } 265 266 269 protected swingwt.awt.Dimension calculatePreferredSize() { 270 swingwt.awt.Dimension size = new swingwt.awt.Dimension( 273 SwingWTUtils.getRenderStringWidth(pText) + 6, 274 SwingWTUtils.getRenderStringHeight(pText) + 12); 275 setSize(size); 276 return size; 277 } 278 279 284 public void setSwingWTParent(swingwt.awt.Container parent) throws Exception { 285 286 ppeer = new Button(parent.getComposite(), 289 (parent instanceof JToolBar ? SWT.FLAT : SWT.PUSH)); 290 peer = ppeer; 291 292 if (pText != null) 293 ppeer.setText(pText); 294 295 if (pMnemonic != ' ') showMnemonic(); 296 if (pImage != null) ppeer.setImage(SwingWTUtils.getSWTImageFromSwingIcon(this, pImage)); 297 if (pFont != null) ppeer.setFont(pFont.getSWTFont()); 298 ppeer.setAlignment(SwingWTUtils.translateSwingAlignmentConstant(pVAlign) | 299 SwingWTUtils.translateSwingAlignmentConstant(pHAlign)); 300 renderText(); 301 302 this.parent = parent; 303 304 if (pDefaultButtonParent != null) { 307 308 if (pDefaultButtonParent.getComposite() instanceof Shell) { 310 ((Shell) pDefaultButtonParent.getComposite()).setDefaultButton(ppeer); 311 } 312 else { 314 pDefaultButtonParent.getComposite().getShell().setDefaultButton(ppeer); 315 } 316 } 317 } 318 319 public void setDefaultButtonParent(swingwt.awt.Container window) { 320 pDefaultButtonParent = window; 321 } 322 323 public boolean isSelected() { return false; } 324 public void setSelected(boolean b) {} 325 326 public void addItemListener(swingwt.awt.event.ItemListener l) { 327 } 328 329 public Object [] getSelectedObjects() { 330 return null; 331 } 332 333 public void removeItemListener(swingwt.awt.event.ItemListener l) { 334 } 335 336 public boolean isArmed() { 337 return false; 338 } 339 340 public boolean isPressed() { 341 return false; 342 } 343 344 public boolean isRollover() { 345 return false; 346 } 347 348 public void setArmed(boolean b) { 349 } 350 351 public void setPressed(boolean b) { 352 } 353 354 public void setRollover(boolean b) { 355 } 356 357 } 358 | Popular Tags |