Eclipse Yoxos Services Downloads Blogs About
Home > Blogs >

on May 8th, 2009UI.toString() in a snippet

Ever wanted to get a print out of all components that make up a piece of UI?

This little recursive snippet does that for you.

private void printChildren(Composite composite, int count) {
  StringBuilder spaces = new StringBuilder(count * 2);
  for (int i = 0; i < count * 2; i++) {
    spaces.append(' ');
  }
  for (Control c : composite.getChildren()) {
    System.out.println(String.format("%s%s (%s)", spaces.toString(), c.toString(), c.getLayoutData()));
    if (c instanceof Composite) {
      printChildren((Composite) c, count + 1);
    }
  }
}

Here’s an example of the output:

ui to string UI.toString() in a snippet

Composite {} (null)
  Composite {} (null)
    Composite {} (null)
      Composite {} (GridData {horizontalAlignment=Undefined 256 grabExcessHorizontalSpace=true verticalAlignment=GridData.CENTER})
        Label {Subject:} (GridData {horizontalAlignment=SWT.BEGINNING verticalAlignment=GridData.CENTER})
        Label {This is a message about the cool Eclipse RCP!} (GridData {horizontalAlignment=SWT.BEGINNING verticalAlignment=GridData.CENTER})
        Label {From:} (GridData {horizontalAlignment=SWT.BEGINNING verticalAlignment=GridData.CENTER})
        Link {<a>nicole@mail.org</a>} (GridData {horizontalAlignment=SWT.BEGINNING verticalAlignment=GridData.CENTER})
        Label {Date:} (GridData {horizontalAlignment=SWT.BEGINNING verticalAlignment=GridData.CENTER})
        Label {10:34 am} (GridData {horizontalAlignment=SWT.BEGINNING verticalAlignment=GridData.CENTER})
      Text {} (GridData {horizontalAlignment=SWT.FILL grabExcessHorizontalSpace=true verticalAlignment=SWT.FILL grabExcessVerticalSpace=true})

Share and Enjoy:

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Twitter
  • FriendFeed
  • LinkedIn
  • Reddit
  • Slashdot

Related posts:

2 Responses to “UI.toString() in a snippet”

  1. Ben says:

    Hi Elias,

    do you know about PDE Picasso? It helps you to find layout issues by coloring all widgets and in addition it provides tooltips on every widgets with layout issues and other informations of the widget.

    See http://wiki.eclipse.org/PDE/Incubator/Picasso

    And also ping Chris regarding https://bugs.eclipse.org/bugs/show_bug.cgi?id=267975 ;-)

  2. Hi Ben,

    thanks, I know about picasso :-) .

    Greetings,
    Elias.

Leave a Reply

Get Adobe Flash playerPlugin by wpburn.com wordpress themes
© EclipseSource 2008 - 2009