1 50 51 52 package swingwtx.swing; 53 54 import java.util.Vector ; 55 56 import org.eclipse.swt.SWT; 57 import org.eclipse.swt.events.SelectionAdapter; 58 import org.eclipse.swt.events.SelectionEvent; 59 import org.eclipse.swt.layout.RowLayout; 60 import org.eclipse.swt.widgets.Listener; 61 import org.eclipse.swt.widgets.Menu; 62 import org.eclipse.swt.widgets.ToolBar; 63 import org.eclipse.swt.widgets.ToolItem; 64 65 import swingwt.awt.Component; 66 import swingwt.awt.Container; 67 import swingwt.awt.Insets; 68 import swingwt.awt.event.ActionEvent; 69 70 105 public class JToolBar extends JComponent implements SwingConstants { 106 107 109 protected org.eclipse.swt.widgets.Composite lastAdd; 110 111 protected int orientation = HORIZONTAL; 112 protected String titleString = ""; 113 protected boolean coolbar = false; 114 protected Container parent = null; 115 protected org.eclipse.swt.widgets.Composite ppeer = null; 116 117 private boolean cached = false; 118 private Vector cache = new Vector (); 119 120 121 private Object threadSafeObjectRetValue = null; 122 private boolean threadSafeBoolean = false; 123 124 public JToolBar() { 125 this("", SwingConstants.HORIZONTAL); 126 } 127 128 public JToolBar(String titleString) { 129 this(titleString, SwingConstants.HORIZONTAL); 130 } 131 132 public JToolBar(int orientation) { 133 this("", orientation); 134 } 135 136 public JToolBar(String titleString, int orientation) { 137 this.titleString = titleString; 138 this.orientation = orientation; 139 } 140 141 public int getOrientation() { 142 return orientation; 143 } 144 145 149 public void setOrientation(int orientation) { 150 this.orientation = orientation; 151 } 152 153 157 public Component getComponentAtIndex(int index) { 158 return (Component) comps.get(index); 159 } 160 161 165 public int getComponentIndex(Component c) { 166 167 for (int i = 0; i < comps.size(); i++) { 168 if (comps.get(i).equals(c)) return i; 169 } 170 171 return 0; 172 } 173 174 177 public Insets getMargin() { 178 org.eclipse.swt.graphics.Rectangle bounds = ppeer.getBounds(); 179 org.eclipse.swt.graphics.Rectangle client = ppeer.getClientArea(); 180 Insets insets = new Insets(); 181 182 insets.left = client.x; 183 insets.top = client.y; 184 insets.right = bounds.width - (client.width + client.x); 185 insets.bottom = bounds.height - (client.height + client.y); 186 187 return insets; 188 } 189 190 195 public void setMargin(Insets insets) {} 196 197 201 public void invalidate() { 202 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) return; 203 SwingUtilities.invokeSync(new Runnable () { 204 public void run() { 205 ppeer.layout(); 206 parent.invalidate(); 207 } 208 }); 209 } 210 211 216 public ToolBarUI getUI() { 217 return null; 218 } 219 220 public void setUI(ToolBarUI ui) {} 221 222 228 public void setBorderPainted(boolean b) {} 229 230 236 public boolean isBorderPainted() { 237 return false; 238 } 239 240 247 public boolean isRollover() { 248 if (SWT.getPlatform().equals("carbon")) return false; 249 return true; 250 } 251 252 258 public void setRollover(boolean b) { } 259 260 public boolean isFloatable() { 261 return false; 262 } 263 264 274 public void setFloatable(final boolean b) { 275 } 276 277 public void addSeparator() { 278 SwingUtilities.invokeSync(new Runnable () { 279 public void run() { 280 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) { 281 cache.add(new JSeparator()); 282 } 283 else { 284 ToolBar peer; 285 if (lastAdd instanceof ToolBar) 286 peer = (ToolBar) lastAdd; 287 else 288 peer = new ToolBar(ppeer, orientation); 289 new ToolItem(peer, SWT.SEPARATOR); 290 lastAdd = peer; 292 } 293 } 294 }); 295 } 296 297 public void setSwingWTParent(swingwt.awt.Container parent) throws Exception { 298 descendantHasPeer = true; 299 300 this.parent = parent; 301 302 ppeer = new org.eclipse.swt.widgets.Composite(parent.getComposite(), SWT.NONE); 303 lastAdd = ppeer; 304 peer = ppeer; 305 composite = ppeer; 306 307 RowLayout lay = null; 308 if (orientation == SwingConstants.HORIZONTAL) 309 lay = new RowLayout(SWT.HORIZONTAL); 310 else 311 lay = new RowLayout(SWT.VERTICAL); 312 lay.fill = true; 313 lay.wrap = false; 314 ppeer.setLayout(lay); 315 316 if (cached) { 317 for (int i = 0; i < cache.size(); i++) { 318 if (cache.get(i) instanceof Action) 319 add((Action) cache.get(i)); 320 else if (cache.get(i) instanceof JSeparator) 321 addSeparator(); 322 else 323 add((Component) cache.get(i)); 324 } 325 cached = false; 326 } 327 } 328 329 333 public Component add(JButton b) { 334 335 if (b.pText != null && (b.pText.length() ==2) && b.pText.startsWith("&")) { 342 add((Component) b); 343 return b; 344 } 345 346 JButtonMappedAction x = new JButtonMappedAction(b); 347 x.putValue(Action.NAME, b.getText()); 348 x.putValue(Action.SHORT_DESCRIPTION, b.getToolTipText()); 349 x.putValue(Action.SMALL_ICON, b.getIcon()); 350 x.putValue(Action.DISABLED_ICON, b.getIcon()); 351 x.putValue(Action.MNEMONIC_KEY, new Integer (b.getMnemonic())); 352 b.setAction(x, false); add(x); 354 355 return b; 356 } 357 358 363 public Component add(JCheckBox checkBox) { 364 return checkBox; 365 } 366 367 372 public Component add(JRadioButton radioButton) { 373 return radioButton; 374 } 375 376 382 public Component add(final Component c) { 383 384 comps.add(c); 385 386 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) { 387 cached = true; 388 cache.add(c); 389 return c; 390 } 391 392 final Container me = this; 393 SwingUtilities.invokeSync(new Runnable () { 394 public void run() { 395 396 400 403 try { 404 c.setSwingWTParent(me); 407 c.setCachedProperties(); 408 c.registerEvents(); 409 } 410 catch (Exception e) { 411 e.printStackTrace(); 412 } 413 414 lastAdd = ppeer; 417 } 418 }); 419 420 return c; 421 } 422 423 435 public JButton add(final Action a) { 436 437 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) { 438 cached = true; 439 cache.add(a); 440 return null; 441 } 442 443 SwingUtilities.invokeSync(new Runnable () { 444 public void run() { 445 446 ToolBar toolbar; final ToolItem peer; 448 449 JButton btnWrapper; 452 if (a instanceof JButtonMappedAction) 453 btnWrapper = ((JButtonMappedAction) a).getJButton(); 454 else 455 btnWrapper = new JButton(a, false); 457 460 if (lastAdd instanceof ToolBar) 461 toolbar = (ToolBar) lastAdd; else 463 toolbar = new ToolBar(ppeer, 464 ((orientation == SwingConstants.HORIZONTAL ? SWT.HORIZONTAL : SWT.VERTICAL) | SWT.FLAT)); 466 if (a.getValue(Action.DROP_MENU) != null) { peer = new ToolItem(toolbar, SWT.DROP_DOWN); 468 } else { 469 peer = new ToolItem(toolbar, SWT.PUSH); 470 } 471 472 btnWrapper.pSWTToolButton = peer; 474 475 if (a.getValue(Action.SMALL_ICON) != null) 476 peer.setHotImage(SwingWTUtils.getSWTImageFromSwingIcon(btnWrapper, ((Icon) a.getValue(Action.SMALL_ICON)))); 477 if (a.getValue(Action.NAME) != null) 478 peer.setText((String ) a.getValue(Action.NAME)); 479 if (a.getValue(Action.SHORT_DESCRIPTION) != null) 480 peer.setToolTipText((String ) a.getValue(Action.SHORT_DESCRIPTION)); 481 482 if (!SWT.getPlatform().equals("carbon")) if (a.getValue(Action.DISABLED_ICON) != null) 484 peer.setImage(SwingWTUtils.getSWTImageFromSwingIcon(btnWrapper, ((Icon) a.getValue(Action.DISABLED_ICON)))); 485 486 if (a.getValue(Action.DROP_MENU) != null) { 487 final JPopupMenu jmenu = (JPopupMenu) a.getValue(Action.DROP_MENU); 488 489 try { 491 jmenu.setSwingWTParent(ppeer.getShell()); 492 493 495 } catch (Exception e) { 496 e.printStackTrace(); 497 } 498 499 peer.addListener(SWT.Selection, new Listener() { 501 public void handleEvent(org.eclipse.swt.widgets.Event e) { 502 if (e.detail == SWT.ARROW) { 503 Menu menu = jmenu.menu; 504 org.eclipse.swt.graphics.Rectangle rect = peer.getBounds(); 505 org.eclipse.swt.graphics.Point pt = new org.eclipse.swt.graphics.Point(rect.x, rect.y + rect.height); 506 pt = ppeer.toDisplay(pt); 507 menu.setLocation(pt); 508 menu.setVisible(true); 509 } 510 } 511 }); 512 } 513 514 final JButton btnWrapper2 = btnWrapper; 515 peer.addSelectionListener(new SelectionAdapter() { 516 public void widgetSelected(SelectionEvent e) { 517 a.actionPerformed(new ActionEvent(btnWrapper2, 0)); 518 } 519 }); 520 521 threadSafeObjectRetValue = btnWrapper; 522 523 lastAdd = toolbar; 524 } 525 }); 526 527 return (JButton) threadSafeObjectRetValue; 528 } 529 530 536 public class ToolBarUI { 537 538 } 539 } 540 541 | Popular Tags |