public static void main(String[] args) {

    Display display = new Display();
    Shell shell = new Shell(display, SWT.DIALOG_TRIM | SWT.RESIZE);
    shell.setLayout(new FillLayout());

    SwtMetawidget metawidget = new SwtMetawidget(shell, SWT.NONE);
    metawidget.setMetawidgetLayout(
        new SeparatorLayoutDecorator(
            new SeparatorLayoutDecoratorConfig()
                .setLayout(
                    new TabFolderLayoutDecorator(
                        new TabFolderLayoutDecoratorConfig().setLayout(new GridLayout())))));
    metawidget.setToInspect(new Baz());

    shell.setVisible(true);
    shell.open();

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }

    display.dispose();
  }
  public void testFlatSectionAroundNestedSectionLayoutDecorator() {

    SwtMetawidget metawidget =
        new SwtMetawidget(new Shell(SwtMetawidgetTests.TEST_DISPLAY, SWT.NONE), SWT.NONE);
    metawidget.setMetawidgetLayout(
        new SeparatorLayoutDecorator(
            new SeparatorLayoutDecoratorConfig()
                .setLayout(
                    new TabFolderLayoutDecorator(
                        new TabFolderLayoutDecoratorConfig().setLayout(new GridLayout())))));
    metawidget.setToInspect(new Baz());

    Composite composite = (Composite) metawidget.getChildren()[0];
    assertEquals(((org.eclipse.swt.layout.GridLayout) composite.getLayout()).marginWidth, 0);
    assertEquals("Foo", ((Label) composite.getChildren()[0]).getText());
    assertEquals((composite.getChildren()[1].getStyle() & SWT.SEPARATOR), SWT.SEPARATOR);

    TabFolder innerTabFolder = (TabFolder) metawidget.getChildren()[1];
    assertEquals("Bar", innerTabFolder.getItem(0).getText());
    Composite innerPanel = (Composite) innerTabFolder.getChildren()[0];
    assertEquals("Abc:", ((Label) innerPanel.getChildren()[0]).getText());
    assertTrue(innerPanel.getChildren()[1] instanceof Text);
    assertEquals(2, innerPanel.getChildren().length);

    composite = (Composite) metawidget.getChildren()[2];
    assertEquals(((org.eclipse.swt.layout.GridLayout) composite.getLayout()).marginWidth, 0);
    assertEquals("Baz", ((Label) composite.getChildren()[0]).getText());
    assertEquals((composite.getChildren()[1].getStyle() & SWT.SEPARATOR), SWT.SEPARATOR);

    assertEquals("Def:", ((Label) metawidget.getChildren()[3]).getText());
    assertTrue(metawidget.getChildren()[4] instanceof Button);
    assertEquals((metawidget.getChildren()[4].getStyle() & SWT.CHECK), SWT.CHECK);
    assertEquals(5, metawidget.getChildren().length);
  }
Beispiel #3
0
  protected String layoutBeforeChild(
      Control control,
      String labelText,
      String elementName,
      Map<String, String> attributes,
      Composite composite,
      SwtMetawidget metawidget) {

    // Add label

    if (SimpleLayoutUtils.needsLabel(labelText, elementName)) {
      Label label = new Label(composite, SWT.None);
      label.setData(NAME, attributes.get(NAME) + LABEL_NAME_SUFFIX);

      if (mLabelFont != null) {
        label.setFont(mLabelFont);
      }

      if (mLabelForeground != null) {
        label.setForeground(mLabelForeground);
      }

      label.setAlignment(mLabelAlignment);

      // Required

      String labelTextToUse = labelText;

      if (mRequiredText != null
          && TRUE.equals(attributes.get(REQUIRED))
          && !WidgetBuilderUtils.isReadOnly(attributes)
          && !metawidget.isReadOnly()) {
        if (mRequiredAlignment == SWT.CENTER) {
          labelTextToUse += mRequiredText;
        } else if (mRequiredAlignment == SWT.LEFT) {
          labelTextToUse = mRequiredText + labelTextToUse;
        }
      }

      if (mLabelSuffix != null) {
        labelTextToUse += mLabelSuffix;
      }

      label.setText(labelTextToUse);

      GridData labelLayoutData = new GridData();
      labelLayoutData.horizontalAlignment = SWT.FILL;
      labelLayoutData.verticalAlignment = SWT.FILL;

      label.setLayoutData(labelLayoutData);
      label.moveAbove(control);
    }

    return labelText;
  }
Beispiel #4
0
  public void onEndBuild(SwtMetawidget metawidget) {

    // Buttons

    Facet buttonsFacet = metawidget.getFacet("buttons");

    if (buttonsFacet != null) {
      GridData buttonLayoutData = new GridData();
      buttonLayoutData.horizontalSpan = 2;
      buttonLayoutData.horizontalAlignment = SWT.FILL;
      buttonLayoutData.grabExcessHorizontalSpace = true;
      buttonsFacet.setLayoutData(buttonLayoutData);

      buttonsFacet.moveBelow(null);
    }
  }
  public void testRealWorld() {

    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    String json =
        "{ \"firstname\": \"Richard\", \"surname\": \"Kennard\", \"notes\": \"Software developer\" }";
    String jsonSchema =
        "{ properties: { \"firstname\": { \"required\": true }, \"notes\": { \"large\": true }}}";

    SwtMetawidget metawidget = new SwtMetawidget(shell, SWT.None);
    metawidget.setInspector(
        new CompositeInspector(
            new CompositeInspectorConfig()
                .setInspectors(
                    new JsonInspector(
                        new JsonInspectorConfig()
                            .setInputStream(new ByteArrayInputStream(json.getBytes()))),
                    new JsonSchemaInspector(
                        new JsonInspectorConfig()
                            .setInputStream(new ByteArrayInputStream(jsonSchema.getBytes()))))));
    metawidget.setInspectionPath("fooObject");

    assertEquals("Firstname*:", ((Label) metawidget.getChildren()[0]).getText());
    assertTrue(metawidget.getChildren()[1] instanceof Text);
    assertEquals("Surname:", ((Label) metawidget.getChildren()[2]).getText());
    assertTrue(metawidget.getChildren()[3] instanceof Text);
    assertEquals("Notes:", ((Label) metawidget.getChildren()[4]).getText());
    assertTrue(metawidget.getChildren()[5] instanceof Text);
    assertEquals((metawidget.getChildren()[5].getStyle() & SWT.MULTI), SWT.MULTI);
    assertEquals(6, metawidget.getChildren().length);
  }
Beispiel #6
0
  public void layoutWidget(
      Control control,
      String elementName,
      Map<String, String> attributes,
      Composite composite,
      SwtMetawidget metawidget) {

    // Do not layout space for empty stubs

    if (control instanceof Stub && ((Stub) control).getChildren().length == 0) {
      GridData stubData = new GridData();
      stubData.exclude = true;
      control.setLayoutData(stubData);
      return;
    }

    // Special support for large controls (make the previous control span all and kick us to the
    // next row)

    boolean spanAllColumns = willFillHorizontally(control, attributes);

    if (spanAllColumns) {
      int totalHorizontalSpan = getTotalHorizontalSpan(composite);
      int numberColumnsRemainingOnRow =
          totalHorizontalSpan % (mNumberOfColumns * LABEL_AND_CONTROL);

      if (numberColumnsRemainingOnRow != 0) {
        Control lastControl = getLastNonExcludedControl(composite);
        ((GridData) lastControl.getLayoutData()).horizontalSpan = numberColumnsRemainingOnRow + 1;
      }
    }

    // Layout a label...

    String labelText = null;

    if (attributes != null) {
      labelText = metawidget.getLabelString(attributes);
    }

    layoutBeforeChild(control, labelText, elementName, attributes, composite, metawidget);

    // ...and layout the control

    GridData controlLayoutData = new GridData();
    controlLayoutData.grabExcessHorizontalSpace = true;

    if (!(control instanceof Button)) {
      controlLayoutData.horizontalAlignment = SWT.FILL;
      controlLayoutData.verticalAlignment = SWT.FILL;
    }

    if (spanAllColumns) {
      controlLayoutData.horizontalSpan = mNumberOfColumns * LABEL_AND_CONTROL;

      if (SimpleLayoutUtils.needsLabel(labelText, elementName)) {
        controlLayoutData.horizontalSpan--;
      }
    } else if (!SimpleLayoutUtils.needsLabel(labelText, elementName)) {
      controlLayoutData.horizontalSpan = LABEL_AND_CONTROL;
    }

    if (willFillVertically(control, attributes)) {
      controlLayoutData.grabExcessVerticalSpace = true;
    }

    // Add it

    control.setLayoutData(controlLayoutData);
  }
  public void testNestedTabs() {

    SwtMetawidget metawidget =
        new SwtMetawidget(new Shell(SwtMetawidgetTests.TEST_DISPLAY, SWT.NONE), SWT.NONE);
    metawidget.setMetawidgetLayout(
        new TabFolderLayoutDecorator(
            new TabFolderLayoutDecoratorConfig()
                .setLayout(
                    new TabFolderLayoutDecorator(
                        new TabFolderLayoutDecoratorConfig().setLayout(new GridLayout())))));
    metawidget.setToInspect(new Bar());

    assertEquals("Abc:", ((Label) metawidget.getChildren()[0]).getText());
    assertTrue(metawidget.getChildren()[1] instanceof Text);

    TabFolder outerTabFolder = (TabFolder) metawidget.getChildren()[2];
    assertEquals("Foo", outerTabFolder.getItem(0).getText());
    Composite outerPanel = (Composite) outerTabFolder.getChildren()[0];
    assertEquals(4, outerPanel.getChildren().length);

    TabFolder innerTabFolder = (TabFolder) outerPanel.getChildren()[0];
    assertEquals("Bar", innerTabFolder.getItem(0).getText());
    Composite barComposite = (Composite) innerTabFolder.getChildren()[0];
    assertEquals("Def:", ((Label) barComposite.getChildren()[0]).getText());
    assertTrue(barComposite.getChildren()[1] instanceof Button);
    assertEquals((barComposite.getChildren()[1].getStyle() & SWT.CHECK), SWT.CHECK);
    assertEquals("Ghi:", ((Label) barComposite.getChildren()[2]).getText());
    assertEquals((barComposite.getChildren()[3].getStyle() & SWT.MULTI), SWT.MULTI);
    assertEquals((barComposite.getChildren()[3].getStyle() & SWT.BORDER), SWT.BORDER);
    assertEquals((barComposite.getChildren()[3].getStyle() & SWT.V_SCROLL), SWT.V_SCROLL);
    assertEquals((barComposite.getChildren()[3].getStyle() & SWT.WRAP), SWT.WRAP);

    assertEquals(4, barComposite.getChildren().length);

    assertEquals("Baz", innerTabFolder.getItem(1).getText());
    Composite bazComposite = (Composite) innerTabFolder.getChildren()[1];
    assertEquals("Jkl:", ((Label) bazComposite.getChildren()[0]).getText());
    assertTrue(bazComposite.getChildren()[1] instanceof Text);
    assertEquals(2, bazComposite.getChildren().length);

    assertEquals("Mno:", ((Label) outerPanel.getChildren()[1]).getText());
    assertTrue(outerPanel.getChildren()[2] instanceof Button);
    assertEquals((outerPanel.getChildren()[2].getStyle() & SWT.CHECK), SWT.CHECK);

    innerTabFolder = (TabFolder) outerPanel.getChildren()[3];
    assertEquals("Moo", innerTabFolder.getItem(0).getText());
    Composite mooComposite = (Composite) innerTabFolder.getChildren()[0];
    assertEquals("Pqr:", ((Label) mooComposite.getChildren()[0]).getText());
    assertTrue(mooComposite.getChildren()[1] instanceof Text);
    assertEquals(2, mooComposite.getChildren().length);

    assertEquals("Stu:", ((Label) metawidget.getChildren()[3]).getText());
    assertTrue(metawidget.getChildren()[4] instanceof Text);
    assertEquals(5, metawidget.getChildren().length);

    // Test components within nested tabs still accessible by name

    assertEquals(metawidget.getChildren()[1], metawidget.getControl("abc"));
    assertEquals(barComposite.getChildren()[1], metawidget.getControl("def"));
    assertEquals(barComposite.getChildren()[3], metawidget.getControl("ghi"));
    assertEquals(bazComposite.getChildren()[1], metawidget.getControl("jkl"));
    assertEquals(outerPanel.getChildren()[2], metawidget.getControl("mno"));
    assertEquals(mooComposite.getChildren()[1], metawidget.getControl("pqr"));
    assertEquals(metawidget.getChildren()[4], metawidget.getControl("stu"));
  }