eclipseswt

How to create the default interface of the CTabFolder?


How to create the default interface of the CTabFolder, i.e. what is displayed in the center area when all CTabItems are closed, like the Eclipse edit area.

CTabFolder doesn't seem to provide a public method for this.

enter image description here

,/.;;',/.;';/,..,/.;;',/.[;][/,;.';,/.;';/,..,/.;;',/.[;][/,;


Solution

  • This isn't really part of CTabFolder, it is done by creating a Composite child of CTabFolder that CTabFolder doesn't manage.

    For the Eclipse implementation see the createOnboardingControls method in org.eclipse.e4.ui.workbench.renderers.swt.StackRenderer

    To make this work you have to listen to the tab folder resize event and the item count change event so that you can set the composite size and make it visible/invisible.

    A basic implementation:

    final CTabFolder tabFolder = new CTabFolder(parent, SWT.TOP);
    
    // Composite as child of CTabFolder
    final Composite empty = new Composite(tabFolder, SWT.NONE);
    
    empty.setLayout(new FillLayout()); // Whatever fill you want
    
    // Just set background here to show when the composite is visible
    // Replace with your contents
    empty.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_DARK_CYAN));
    
    // Set location below tab bar
    empty.setLocation(2, 30);
    
    // Listen for resize and tab item count events
    
    tabFolder.addListener(SWT.Resize, _ -> setEmptyFolderSize(tabFolder, empty));
    
    tabFolder.addCTabFolder2Listener(CTabFolder2Listener.itemsCountAdapter(_ -> setEmptyFolderSize(tabFolder, empty)));
    
    private static void setEmptyFolderSize(final CTabFolder tabFolder, final Composite empty)
     {
       if (tabFolder.getItemCount() != 0) {
         empty.setVisible(false);
         empty.setSize(0, 0);
       }
       else { // No visible tabs
         empty.setVisible(true);
    
         final Point location = empty.getLocation();
    
         final Rectangle folderBounds = tabFolder.getBounds();
    
         final int width = folderBounds.width - (2 * location.x);
         final int height = folderBounds.height - location.y;
    
         empty.setSize(width, height);
       }
    }