1 78 79 80 package swingwtx.swing; 81 82 import org.eclipse.swt.widgets.*; 83 import org.eclipse.swt.*; 84 85 import swingwt.awt.Component; 86 import swingwtx.swing.event.*; 87 88 import java.util.*; 89 90 public class JTabbedPane extends JComponent implements SwingConstants { 91 92 protected TabFolder ppeer = null; 93 protected int pTabPlacement = TOP; 94 protected Vector changeListeners = new Vector(); 95 96 97 protected Vector tabs = new Vector(); 98 99 100 private int iRetVal = 0; 101 private String sRetVal = ""; 102 private Object retval; 103 104 public JTabbedPane() { } 105 106 public void addChangeListener(ChangeListener l) { 107 changeListeners.add(l); 108 } 109 110 public void removeChangeListener(ChangeListener l) { 111 changeListeners.remove(l); 112 } 113 114 public void addTab(String title, swingwt.awt.Component component) { 115 addTab(title, null, component); 116 } 117 118 public void addTab(String title, Icon icon, swingwt.awt.Component component) { 119 addTab(title, icon, component, null, true); 120 } 121 122 public void addTab(final String title, final Icon icon, final swingwt.awt.Component component, final String tip) { 123 addTab(title, icon, component, tip, true); 124 } 125 126 public void addTab(final String title, final Icon icon, final swingwt.awt.Component component, final String tip, final boolean enabled) { 127 addTab(title, icon, component, tip, enabled, false); 128 } 129 130 public void add(swingwt.awt.Component component, Object modifiers) { 131 addTab("", component); 132 } 133 public swingwt.awt.Component add(swingwt.awt.Component component) { 134 addTab("", component); 135 return component; 136 } 137 public swingwt.awt.Component add(String title, swingwt.awt.Component component) { 138 addTab(title, component); 139 return component; 140 } 141 142 public void addTab(final String title, final Icon icon, final swingwt.awt.Component component, final String tip, final boolean enabled, boolean fromcache) { 143 144 if (!fromcache) { 146 TabItemCache t = new TabItemCache(); 147 t.title = title; 148 t.icon = icon; 149 t.component = component; 150 t.tip = tip; 151 t.enabled = enabled; 152 tabs.add(t); 153 t = null; 154 return; 155 } 156 157 final JTabbedPane me = this; 158 SwingUtilities.invokeSync(new Runnable () { 159 public void run() { 160 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) return; 161 162 TabItem t = new TabItem(ppeer, SWT.NONE); 163 164 try { 167 component.setSwingWTParent(me); 168 } 169 catch (Exception e) { 170 e.printStackTrace(); 171 } 172 173 if (title != null) t.setText(SwingWTUtils.removeHTML(title)); 174 if (icon != null) t.setImage(SwingWTUtils.getSWTImageFromSwingIcon(me, icon)); 175 176 if (component != null && enabled) t.setControl(component.getPeer()); 178 179 if (tip != null) t.setToolTipText(tip); 180 t.setData("enabled", (enabled ? "true" : "false")); 181 182 postponeLayout(); 184 } 185 }); 186 187 } 188 189 public Icon getIconAt(final int index) { 190 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) { 191 TabItemCache t = (TabItemCache) tabs.get(index); 192 return t.icon; 193 } 194 else { 195 SwingUtilities.invokeSync(new Runnable () { 196 public void run() { 197 swingwt.awt.Image image = new swingwt.awt.Image(); 198 image.image = ppeer.getItem(index).getImage(); 199 retval = new ImageIcon(image); 200 } 201 }); 202 return (Icon) retval; 203 } 204 } 205 public void setIconAt(final int index, final Icon icon) { 206 final JTabbedPane pthis = this; 207 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) { 208 TabItemCache t = (TabItemCache) tabs.get(index); 209 t.icon = icon; 210 } 211 else 212 SwingUtilities.invokeSync(new Runnable () { 213 public void run() { 214 ppeer.getItem(index).setImage( 215 SwingWTUtils.getSWTImageFromSwingIcon(pthis, icon)); 216 } 217 }); 218 } 219 220 public void setEnabledAt(final int index, final boolean b) { 221 if (SwingWTUtils.isSWTControlAvailable(ppeer)) { 222 SwingUtilities.invokeSync(new Runnable () { 223 public void run() { 224 ppeer.getItem(index).setData("enabled", (b ? "true" : "false")); 225 if (!b) 228 ppeer.getItem(index).setControl(null); 229 else 230 ppeer.getItem(index).setControl( ((TabItemCache) tabs.get(index)).component.getPeer() ); 231 } 232 }); 233 } 234 else { 235 TabItemCache t = (TabItemCache) tabs.get(index); 236 t.enabled = b; 237 } 238 } 239 240 public int getTabCount() { 241 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) 242 return tabs.size(); 243 else { 244 SwingUtilities.invokeSync(new Runnable () { 245 public void run() { 246 iRetVal = ppeer.getItemCount(); 247 } 248 }); 249 return iRetVal; 250 } 251 } 252 public int getSelectedIndex() { 253 if (SwingWTUtils.isSWTControlAvailable(ppeer)) { 254 SwingUtilities.invokeSync(new Runnable () { 255 public void run() { 256 iRetVal = ppeer.getSelectionIndex(); 257 } 258 }); 259 } 260 else 261 iRetVal = -1; 262 return iRetVal; 263 } 264 265 public swingwt.awt.Component getSelectedComponent() { 266 int i = getSelectedIndex(); 267 if (i != -1) 268 return ((TabItemCache) tabs.get(i)).component; 269 else 270 return null; 271 } 272 273 public void setSelectedIndex(final int index) { 274 if (SwingWTUtils.isSWTControlAvailable(ppeer)) 275 SwingUtilities.invokeSync(new Runnable () { 276 public void run() { 277 ppeer.setSelection(index); 278 } 279 }); 280 } 281 282 public void removeTabAt(final int index) { 283 if (SwingWTUtils.isSWTControlAvailable(ppeer)) { 284 SwingUtilities.invokeSync(new Runnable () { 285 public void run() { 286 tabs.removeElementAt(index); 287 ppeer.getItem(index).dispose(); 288 } 289 }); 290 } 291 else 292 tabs.removeElementAt(index); 293 } 294 295 public String getTitleAt(final int index) { 296 if (SwingWTUtils.isSWTControlAvailable(ppeer)) { 297 SwingUtilities.invokeSync(new Runnable () { 298 public void run() { 299 sRetVal = ppeer.getItem(index).getText(); 300 } 301 }); 302 return sRetVal; 303 } 304 else { 305 TabItemCache t = (TabItemCache) tabs.get(index); 306 return t.title; 307 } 308 } 309 310 public void setTitleAt(final int index, final String title) { 311 if (SwingWTUtils.isSWTControlAvailable(ppeer)) { 312 SwingUtilities.invokeSync(new Runnable () { 313 public void run() { 314 ppeer.getItem(index).setText(SwingWTUtils.removeHTML(title)); 315 } 316 }); 317 } 318 else { 319 TabItemCache t = (TabItemCache) tabs.get(index); 320 t.title = title; 321 } 322 } 323 324 public String getToolTipTextAt(final int index) { 325 if (SwingWTUtils.isSWTControlAvailable(ppeer)) { 326 SwingUtilities.invokeSync(new Runnable () { 327 public void run() { 328 sRetVal = ppeer.getItem(index).getToolTipText(); 329 } 330 }); 331 return sRetVal; 332 } 333 else { 334 TabItemCache t = (TabItemCache) tabs.get(index); 335 return t.tooltip; 336 } 337 } 338 339 public void setToolTipTextAt(final int index, final String text) { 340 if (SwingWTUtils.isSWTControlAvailable(ppeer)) { 341 SwingUtilities.invokeSync(new Runnable () { 342 public void run() { 343 ppeer.getItem(index).setToolTipText(text); 344 } 345 }); 346 } 347 else { 348 TabItemCache t = (TabItemCache) tabs.get(index); 349 t.tooltip = text; 350 } 351 } 352 353 public int indexOfComponent(final Component c) { 354 final int[] ret = new int[1]; 355 SwingUtilities.invokeSync(new Runnable () { 356 public void run() { 357 if (SwingWTUtils.isSWTControlAvailable(ppeer)) { 358 org.eclipse.swt.widgets.TabItem[] tabs = ppeer.getItems(); 360 for (int i = 0; i < tabs.length; i++) { 361 if (tabs[i].getControl().equals(c.getPeer())) { 362 ret[0] = i; 363 break; 364 } 365 } 366 } 367 else { 368 for (int i = 0; i < tabs.size(); i++) { 370 if (((TabItemCache) tabs.get(i)).component.equals(c)) { 371 ret[0] = i; 372 break; 373 } 374 } 375 } 376 } 377 }); 378 return ret[0]; 379 } 380 381 public int indexOfTab(final String name) { 382 final int[] ret = new int[1]; 383 SwingUtilities.invokeSync(new Runnable () { 384 public void run() { 385 if (SwingWTUtils.isSWTControlAvailable(ppeer)) { 386 org.eclipse.swt.widgets.TabItem[] tabs = ppeer.getItems(); 388 for (int i = 0; i < tabs.length; i++) { 389 if (tabs[i].getText().equals(name)) { 390 ret[0] = i; 391 break; 392 } 393 } 394 } 395 else { 396 for (int i = 0; i < tabs.size(); i++) { 398 if (((TabItemCache) tabs.get(i)).title.equals(name)) { 399 ret[0] = i; 400 break; 401 } 402 } 403 } 404 } 405 }); 406 return ret[0]; 407 } 408 409 public int indexOfTab(final Icon icon) { 410 final int[] ret = new int[1]; 411 SwingUtilities.invokeSync(new Runnable () { 412 public void run() { 413 for (int i = 0; i < tabs.size(); i++) { 417 if (((TabItemCache) tabs.get(i)).icon.equals(icon)) { 418 ret[0] = i; 419 break; 420 } 421 } 422 } 423 }); 424 return ret[0]; 425 } 426 427 public Component getComponentAt(int index) { 428 return ((TabItemCache) tabs.get(index)).component; 429 } 430 431 public int getTabPlacement() { return pTabPlacement; } 432 public void setTabPlacement(int place) { pTabPlacement = place; } 433 434 public Control getPeer() { return ppeer; } 435 436 protected void postponeLayout() { 437 SwingUtilities.invokeLater(new Runnable () { 438 public void run() { 439 layoutTab(); 440 } 441 }); 442 } 443 444 protected void layoutTab() { 445 Control p = ppeer.getItem(ppeer.getSelectionIndex()).getControl(); 446 if (p instanceof Composite) 447 ((Composite) p).layout(true); 448 else 449 parent.invalidate(); 450 } 451 452 455 protected void processChangeEvent(ChangeEvent e) { 456 Iterator i = changeListeners.iterator(); 457 while (i.hasNext()) { 458 ChangeListener l = (ChangeListener) i.next(); 459 l.stateChanged(e); 460 } 461 } 462 463 public void setSwingWTParent(swingwt.awt.Container parent) throws Exception { 464 descendantHasPeer = true; 465 ppeer = new TabFolder(parent.getComposite(), SwingWTUtils.translateSwingAlignmentConstant(pTabPlacement)); 466 peer = ppeer; 467 composite = ppeer; 468 this.parent = parent; 469 470 if (tabs.size() > 0) { 472 Object [] tabso = tabs.toArray(); 473 for (int i = 0; i < tabso.length; i++) { 474 TabItemCache t = (TabItemCache) tabso[i]; 475 addTab(t.title, t.icon, t.component, t.tip, t.enabled, true); 476 } 477 } 478 479 final JTabbedPane me = this; 482 ppeer.addSelectionListener(new org.eclipse.swt.events.SelectionListener() { 483 public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) { 484 if (checkTabEnabled(e)) { 486 postponeLayout(); ChangeEvent ce = new ChangeEvent(me); 489 processChangeEvent(ce); 490 } 491 } 492 public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {} 493 }); 494 495 if (ppeer.getItemCount() > 0) 497 postponeLayout(); 498 499 } 500 501 505 protected boolean checkTabEnabled(org.eclipse.swt.events.SelectionEvent e) { 506 e.doit = (e.item.getData("enabled").toString().equals("true")); 507 return e.doit; 508 } 509 510 } 511 | Popular Tags |