1 150 151 152 package swingwt.awt; 153 154 import java.util.Locale ; 155 import java.util.Vector ; 156 157 import org.eclipse.swt.SWT; 158 import org.eclipse.swt.widgets.Listener; 159 160 import swingwt.awt.event.ContainerEvent; 161 import swingwtx.swing.SwingUtilities; 162 import swingwtx.swing.SwingWTUtils; 163 164 168 public class Container extends Component { 169 170 171 public org.eclipse.swt.widgets.Composite composite = null; 172 173 174 protected LayoutManager layout = null; 175 176 177 protected Vector comps = new Vector (); 178 179 protected Vector containerListeners = new Vector (); 180 181 public Dimension getMinimumSize() { 182 return (layout==null) ? super.getMinimumSize() : layout.minimumLayoutSize(this); 183 } 184 185 public Dimension getPreferredSize() { 186 return (layout==null) ? super.getPreferredSize() : layout.preferredLayoutSize(this); 187 } 188 189 public Dimension getMaximumSize() { 190 return (layout instanceof LayoutManager2) ? 191 ((LayoutManager2)layout).maximumLayoutSize(this) : 192 super.getMaximumSize(); 193 } 194 195 public void addContainerListener(swingwt.awt.event.ContainerListener l) { 196 containerListeners.add(l); 197 } 198 199 public void removeContainerListener(swingwt.awt.event.ContainerListener l) { 200 containerListeners.remove(l); 201 } 202 203 public Component add(Component c) { 204 return doAdd(c); 205 } 206 207 public Component add(Component c, int index) { 208 return add(c); 209 } 210 211 212 public Component doAdd(final Component c) { 213 214 if (c == null) return null; 215 final Container me = this; 216 217 if (comps.indexOf(c) == -1) { 220 comps.add(c); 221 } 222 223 if (!SwingWTUtils.isSWTControlAvailable(composite)) return c; 224 225 addComponentToLayout(c); 227 228 SwingUtilities.invokeSync(new Runnable () { 232 public void run() { 233 234 try { 235 c.setSwingWTParent(me); 236 c.setCachedProperties(); 237 c.registerEvents(); 238 processContainerEvent(new swingwt.awt.event.ContainerEvent(me, swingwt.awt.event.ContainerEvent.COMPONENT_ADDED, c)); 239 } 240 catch (Exception e) { 241 e.printStackTrace(); 242 } 243 244 queuedValidate(); 245 246 } 247 }); 248 return c; 249 } 250 251 protected void processContainerEvent(swingwt.awt.event.ContainerEvent e) { 252 if (containerListeners.size() == 0) return; 253 for (int i = 0; i < containerListeners.size(); i++) { 254 if (e.getID() == ContainerEvent.COMPONENT_ADDED) 255 ((swingwt.awt.event.ContainerListener) containerListeners.get(i)).componentAdded(e); 256 else 257 ((swingwt.awt.event.ContainerListener) containerListeners.get(i)).componentRemoved(e); 258 } 259 } 260 261 264 public void addComponentToLayout(Component c) { 265 if (layout != null) { 266 if (layout instanceof LayoutManager2) { 267 ((LayoutManager2)layout).addLayoutComponent(c, c.layoutModifier); 268 } 269 else 270 layout.addLayoutComponent(c.getName(), c); 271 } 272 } 273 274 275 public void invalidate() { 276 final Container me = this; 277 if (SwingWTUtils.isSWTControlAvailable(composite)) 278 if (layout != null) 279 SwingUtilities.invokeSync(new Runnable () { 280 public void run() { 281 layout.layoutContainer(me); 282 } 283 }); 284 } 285 286 287 public void validate() { invalidate(); } 288 289 public void revalidate() { invalidate(); } 290 291 305 public void queuedValidate() { 306 if (queuedValidateRequest) return; queuedValidateRequest = true; 308 SwingUtilities.invokeIn(new Runnable () { 309 public void run() { 310 invalidate(); 311 queuedValidateRequest = false; 312 } 313 }, 1); 314 } 315 protected boolean queuedValidateRequest = false; 316 317 320 public void add(swingwt.awt.Component c, Object layoutModifier) { 321 if (c == null) return; 322 c.layoutModifier = layoutModifier; 323 doAdd(c); 324 } 325 326 public void add(swingwt.awt.Component c, Object layoutModifier, int index) { 327 add(c, layoutModifier); 328 } 329 330 public Component add(String name, swingwt.awt.Component c) { 331 add(c,name); 332 return c; 333 } 334 335 public void dispose() { 336 337 if (comps != null) { 339 for (int i = 0; i < comps.size(); i++) { 340 try { 341 ((Component) comps.get(i)).dispose(); 342 } 343 catch (Exception e) {} 344 } 345 comps.removeAllElements(); 347 } 348 349 if (peer != null) super.dispose(); 350 351 composite = null; 352 peer = null; 353 } 354 355 359 public void remove(swingwt.awt.Component c) { 360 comps.remove(c); 361 layout.removeLayoutComponent(c); 362 c.componentOnlyDispose(); 363 c.setComponentRemoved(); 364 processContainerEvent(new swingwt.awt.event.ContainerEvent(this, swingwt.awt.event.ContainerEvent.COMPONENT_REMOVED, c)); 365 invalidate(); 366 } 367 368 public void remove(int index) { 369 Component c = (Component) comps.get(index); 370 remove(c); 371 } 372 373 public void removeAll() { 374 for (int i = 0; i < comps.size(); i++) { 375 Component c = (Component) comps.get(i); 376 remove(c); 377 } 378 comps.removeAllElements(); 379 invalidate(); 380 } 381 382 383 public void removeComponentFromCache(Component c) { 384 comps.remove(c); 385 } 386 387 388 public LayoutManager getLayout() { 389 return layout; 390 } 391 392 404 public void setLayout(LayoutManager l) { 405 setLayoutImpl(l); 406 } 407 408 416 protected void setLayoutImpl(LayoutManager l) { 417 layout = l; 418 419 if (composite == null) 420 return; 421 422 SwingUtilities.invokeSync(new Runnable () { 423 public void run() { 424 425 composite.setLayout(null); 427 428 composite.addListener(SWT.Resize, new Listener() { 430 public void handleEvent(org.eclipse.swt.widgets.Event e) { 431 invalidate(); 432 } 433 }); 434 435 } 436 }); 437 } 438 439 440 public org.eclipse.swt.widgets.Composite getComposite() { 441 return composite; 442 } 443 444 public Container getParent() { 445 return parent; 446 } 447 448 452 public void setSwingWTParent(swingwt.awt.Container parent) throws Exception { 453 454 if (layout != null) 455 setLayoutImpl(layout); 456 457 if (comps.size() > 0) { 458 Object [] obs = comps.toArray(); 459 for (int i = 0; i < obs.length; i++) { 460 Component c = (Component) obs[i]; 461 doAdd(c); 462 } 463 } 464 } 465 466 public ComponentOrientation getComponentOrientation() { 467 return ComponentOrientation.getOrientation(Locale.getDefault()); 468 } 469 470 public Insets getInsets() { 471 return new Insets(0,0,0,0); 472 } 473 474 public int getComponentCount() { 475 if (composite != null) 476 return composite.getChildren().length; 477 else 478 return comps.size(); 479 } 480 481 public Component getComponent(int i) { 482 return (Component) comps.get(i); 483 } 484 485 public Component[] getComponents() { 486 Object [] comp = comps.toArray(); 487 Component[] cps = new Component[comp.length]; 488 for (int i = 0; i < comp.length; i++) { 489 cps[i] = (Component) comp[i]; 490 } 491 return cps; 492 } 493 494 498 public void debug_showContainmentTree() { 499 System.out.println("Containment Tree: ===================="); 500 System.out.println(getClass().getName()); 501 xdebug_showChildren(this, 1); 502 } 503 private void xdebug_showChildren(Container c, int level) { 504 final String SPACE = " "; 505 for (int i = 0; i < c.comps.size(); i++) { 506 Component d = (Component) c.comps.get(i); 507 System.out.println(SPACE.substring(0, level) + d.getClass().getName()); 508 if (d instanceof Container) 509 xdebug_showChildren((Container) d, level+1); 510 } 511 } 512 513 } 514 | Popular Tags |